Skip to content

Commit c9995e2

Browse files
committed
fix: update documentation and include missing statements in code
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
1 parent f378b9c commit c9995e2

2 files changed

Lines changed: 68 additions & 23 deletions

File tree

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,15 @@ You can find these in [`config.lua`](https://github.com/DrKJeff16/boolean-toggle
132132
By default, `setup()` loads with the following options:
133133

134134
```lua
135-
{
135+
require('boolean-toggle').setup({
136136
-- Whether to automatically save the file when a boolean is changed
137137
auto_write = true,
138138

139139
-- A list of strings with the filetypes for which this plugin will be deactivated
140140
ignore_ft = {},
141141

142+
custom_spec = {}, -- Read the Custom Spec section below
143+
142144
-- Normal mode keymaps
143145
--
144146
-- Each option can be either 'nil' or a keymap of your liking.
@@ -154,7 +156,33 @@ By default, `setup()` loads with the following options:
154156
-- to_false = '<KEYMAP>',
155157
-- to_true = '<KEYMAP>',
156158
},
157-
}
159+
})
160+
```
161+
162+
### Custom Spec
163+
164+
You can set your own custom boolean pairs with the `custom_spec` option. For example:
165+
166+
```lua
167+
require('boolean-toggle').setup({
168+
custom_spec = {
169+
{
170+
yes = 'foo',
171+
no = 'bar',
172+
ft = { '*' }, -- Setting a `*` means all filetypes will be able to be operated on
173+
},
174+
{
175+
yes = 'Foo',
176+
no = 'Bar',
177+
ft = nil, -- Setting a `nil` is the same as above
178+
},
179+
{
180+
yes = '1',
181+
no = '0',
182+
ft = { 'c', 'cpp' }, -- Only works for C/C++ filetypes
183+
},
184+
},
185+
})
158186
```
159187

160188
---

lua/boolean-toggle.lua

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@ local Config = require('boolean-toggle.config')
44

55
local 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>
712
local 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>
1723
local 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>
2734
local 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+
)
141157
end
142158

143159
---@param bool? 'true'|'false'
@@ -147,6 +163,9 @@ end
147163
---@return table<string, { [1]: string, ft: string[] }>|nil convert
148164
function 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
193214
end
@@ -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

Comments
 (0)