@@ -4,26 +4,33 @@ local Config = require('boolean-toggle.config')
44
55local delim = vim .split ([[ .,'"()[]{}$#?!:;%%^%*+=\\|/<>~` ]] , ' ' , { trimempty = false })
66
7+ --- @class BooleanToggle.ConvertSpec
8+ --- @field [ 1] string
9+ --- @field ft ? string[] | nil
10+
11+ --- @type table<string , BooleanToggle.ConvertSpec>
712local convert_to_false = {
8- [' true' ] = { ' false' , ft = { ' *' } },
9- yes = { ' no' , ft = { ' *' } },
10- True = { ' False' , ft = { ' *' } },
11- Yes = { ' No' , ft = { ' *' } },
1213 TRUE = { ' FALSE' , ft = { ' *' } },
14+ True = { ' False' , ft = { ' *' } },
1315 YES = { ' NO' , ft = { ' *' } },
16+ Yes = { ' No' , ft = { ' *' } },
1417 [' nil' ] = { ' t' , ft = { ' lisp' } },
18+ [' true' ] = { ' false' , ft = { ' *' } },
19+ yes = { ' no' , ft = { ' *' } },
1520}
1621
22+ --- @type table<string , BooleanToggle.ConvertSpec>
1723local convert_to_true = {
18- t = { ' nil' , ft = { ' lisp' } },
19- [' false' ] = { ' true' , ft = { ' *' } },
20- no = { ' yes' , ft = { ' *' } },
21- False = { ' True' , ft = { ' *' } },
22- No = { ' Yes' , ft = { ' *' } },
2324 FALSE = { ' TRUE' , ft = { ' *' } },
25+ False = { ' True' , ft = { ' *' } },
2426 NO = { ' NO' , ft = { ' *' } },
27+ No = { ' Yes' , ft = { ' *' } },
28+ [' false' ] = { ' true' , ft = { ' *' } },
29+ no = { ' yes' , ft = { ' *' } },
30+ t = { ' nil' , ft = { ' lisp' } },
2531}
2632
33+ --- @type table<string , BooleanToggle.ConvertSpec>
2734local convert = {
2835 FALSE = { ' TRUE' , ft = { ' *' } },
2936 False = { ' True' , ft = { ' *' } },
@@ -35,7 +42,6 @@ local convert = {
3542 Yes = { ' No' , ft = { ' *' } },
3643 [' false' ] = { ' true' , ft = { ' *' } },
3744 [' nil' ] = { ' t' , ft = { ' lisp' } },
38- [' not' ] = { ' ' , ft = { ' *' } },
3945 [' true' ] = { ' false' , ft = { ' *' } },
4046 no = { ' yes' , ft = { ' *' } },
4147 t = { ' nil' , ft = { ' lisp' } },
@@ -79,6 +85,12 @@ function M.get_spec_values(ft, bool)
7985 end
8086
8187 for _ , spec in ipairs (Config .config .custom_spec ) do
88+ Util .validate ({
89+ [' spec.yes' ] = { spec .yes , { ' string' } },
90+ [' spec.no' ] = { spec .no , { ' string' } },
91+ [' spec.ft' ] = { spec .ft , { ' table' , ' nil' }, true },
92+ })
93+
8294 local ft_spec = (not spec .ft or vim .tbl_isempty (spec .ft )) and { ' *' } or spec .ft
8395 if not bool then
8496 conv [spec .no ] = { spec .yes , ft = ft_spec }
@@ -137,7 +149,11 @@ function M.setup(opts)
137149 end
138150 end
139151
140- vim .api .nvim_create_user_command (' Bool' , M .cursor_toggle_boolean , { desc = ' Invert Boolean Value on Cursor' })
152+ vim .api .nvim_create_user_command (
153+ ' Bool' ,
154+ M .cursor_toggle_boolean ,
155+ { desc = ' Invert Boolean Value on Cursor' , nargs = 0 }
156+ )
141157end
142158
143159--- @param bool ? ' true' | ' false'
147163--- @return table<string , { [1] : string , ft : string[] } >| nil convert
148164function M .boolean_under_cursor (bool )
149165 Util .validate ({ bool = { bool , { ' string' , ' nil' }, true } })
166+ if bool and not vim .list_contains ({ ' true' , ' false' }, bool ) then
167+ error ((' Invalid value `%s`' ):format (bool ), ERROR )
168+ end
150169
151170 local pos = vim .api .nvim_win_get_cursor (vim .api .nvim_get_current_win ())
152171 local line = vim .api .nvim_get_current_line ()
@@ -183,11 +202,13 @@ function M.boolean_under_cursor(bool)
183202 elseif bool == ' false' then
184203 conv = M .get_spec_values (' ft' , ' false' )
185204 end
186- if conv [word ] then
187- local conv_ft = conv [word ].ft or {}
188- if vim .list_contains (conv_ft , ft ) or vim .tbl_isempty (conv_ft ) or vim .list_contains (conv_ft , ' *' ) then
189- return true , start_col , col , conv
190- end
205+ if not conv [word ] then
206+ return false
207+ end
208+
209+ local conv_ft = conv [word ].ft or { ' *' }
210+ if vim .list_contains (conv_ft , ft ) or vim .tbl_isempty (conv_ft ) or vim .list_contains (conv_ft , ' *' ) then
211+ return true , start_col , col , conv
191212 end
192213 return false
193214end
@@ -240,15 +261,11 @@ function M.cursor_set_to_false()
240261 and end_col
241262 and conv
242263 and vim .list_contains ({ ' acwrite' , ' ' }, vim .api .nvim_get_option_value (' buftype' , { buf = bufnr }))
243- )
264+ ) or vim . list_contains ( Config . config . ignore_ft , vim . api . nvim_get_option_value ( ' filetype ' , { buf = bufnr }))
244265 then
245266 return
246267 end
247268
248- if vim .list_contains (Config .config .ignore_ft , vim .api .nvim_get_option_value (' filetype' , { buf = bufnr })) then
249- return
250- end
251-
252269 local line = vim .api .nvim_get_current_line ()
253270 local current_bool = line :sub (start_col , end_col )
254271 if not conv [current_bool ] then
0 commit comments