Skip to content

Commit 8d94858

Browse files
committed
feat: add support for Lisp nil <==> t
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
1 parent ca1fc45 commit 8d94858

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

lua/boolean-toggle.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
local Util = require('boolean-toggle.util')
22
local Config = require('boolean-toggle.config')
33

4-
local valid_chars = Util.dedup(vim.split('aeflrstuAEFLRSTU', '', { trimempty = false }))
4+
local valid_chars = Util.dedup(vim.split('aeflnrstuAEFLRSTU', '', { trimempty = false }))
55
local delim = vim.split([[.,'"()[]{}$#?!:;%%^%*+=\\|/<>~` ]], '', { trimempty = false })
66

77
---@enum BooleanToggle.ConvertToFalse
88
local convert_to_false = {
99
['true'] = 'false',
1010
True = 'False',
1111
TRUE = 'FALSE',
12+
t = 'nil',
1213
}
1314

1415
---@enum BooleanToggle.ConvertToTrue
1516
local convert_to_true = {
1617
['false'] = 'true',
1718
False = 'True',
1819
FALSE = 'TRUE',
20+
['nil'] = 't',
1921
}
2022

2123
---@enum BooleanToggle.Convert
@@ -26,6 +28,8 @@ local convert = {
2628
False = 'True',
2729
TRUE = 'FALSE',
2830
FALSE = 'TRUE',
31+
t = 'nil',
32+
['nil'] = 't',
2933
}
3034

3135
---@param line string
@@ -122,6 +126,10 @@ function M.boolean_under_cursor()
122126
col = col + 1
123127
end
124128

129+
local bufnr = vim.api.nvim_get_current_buf()
130+
if vim.list_contains({ 't', 'nil' }, word) and Util.optget('filetype', 'buf', bufnr) == 'lisp' then
131+
return true, start_col, col
132+
end
125133
if vim.list_contains({ 'false', 'true', 'False', 'True', 'FALSE', 'TRUE' }, word) then
126134
return true, start_col, col
127135
end
@@ -155,7 +163,7 @@ function M.cursor_toggle_boolean()
155163
local win = vim.api.nvim_get_current_win()
156164
local pos = vim.api.nvim_win_get_cursor(win)
157165
local before, after = get_boolean_surround(line, start_col, end_col)
158-
if not vim.list_contains({ 'f', 'F', 't', 'T' }, line:sub(pos[2] + 1, pos[2] + 1)) then
166+
if not vim.list_contains({ 'f', 'F', 't', 'T', 'n' }, line:sub(pos[2] + 1, pos[2] + 1)) then
159167
pos[2] = pos[2] + (line:len() > (before .. convert[current_bool] .. after):len() and -1 or 1)
160168
end
161169

lua/boolean-toggle/util.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,83 @@
55
---@field [3]? boolean
66
---@field [4]? string
77

8+
local ERROR = vim.log.levels.ERROR
9+
810
---@class BooleanToggle.Util
911
local M = {}
1012

13+
---@overload fun(option: string|vim.wo|vim.bo): value: any
14+
---@overload fun(option: string|vim.wo|vim.bo, param: 'scope', param_value: 'local'|'global'): value: any
15+
---@overload fun(option: string|vim.wo|vim.bo, param: 'ft', param_value: string): value: any
16+
---@overload fun(option: string|vim.wo|vim.bo, param: 'buf'|'win', param_value: integer): value: any
17+
---@nodiscard
18+
function M.optget(option, param, param_value)
19+
M.validate({
20+
option = { option, { 'string' } },
21+
param = { param, { 'string', 'nil' }, true },
22+
param_value = { param_value, { 'string', 'number', 'nil' }, true },
23+
})
24+
param = param or 'buf'
25+
if not vim.list_contains({ 'scope', 'ft', 'buf', 'win' }, param) then
26+
error(('Bad parameter: `%s`\nCan only accept `scope`, `ft`, `buf` or `win`!'):format(vim.inspect(param)), ERROR)
27+
end
28+
if param == 'scope' then
29+
param_value = param_value or 'local'
30+
if not vim.list_contains({ 'global', 'local' }, param_value) then
31+
error(('Bad param value `%s`\nCan only accept `global` or `local`!'):format(vim.inspect(param_value)), ERROR)
32+
end
33+
end
34+
if param == 'ft' and (not param_value or type(param_value) ~= 'string') then
35+
error('Missing/bad value for `ft` parameter!', ERROR)
36+
end
37+
if
38+
vim.list_contains({ 'win', 'buf' }, param)
39+
and not (param_value and type(param_value) == 'number' and M.is_int(param_value, param_value >= 0))
40+
then
41+
error('Missing/bad value for `win`/`buf` parameter!', ERROR)
42+
end
43+
44+
return vim.api.nvim_get_option_value(option, { [param] = param_value })
45+
end
46+
47+
---@overload fun(option: string|vim.wo|vim.bo, value: any)
48+
---@overload fun(option: string|vim.wo|vim.bo, value: any, param: 'scope', param_value: 'local'|'global')
49+
---@overload fun(option: string|vim.wo|vim.bo, value: any, param: 'ft', param_value: string)
50+
---@overload fun(option: string|vim.wo|vim.bo, value: any, param: 'buf'|'win', param_value: integer)
51+
function M.optset(option, value, param, param_value)
52+
M.validate({
53+
option = { option, { 'string' } },
54+
param = { param, { 'string', 'nil' }, true },
55+
param_value = { param_value, { 'string', 'number', 'nil' }, true },
56+
})
57+
if value == nil then
58+
error('Empty option value is unacceptable!', ERROR)
59+
end
60+
param = param or 'buf'
61+
62+
if not vim.list_contains({ 'scope', 'ft', 'buf', 'win' }, param) then
63+
error(('Bad parameter: `%s`\nCan only accept `scope`, `ft`, `buf` or `win`!'):format(vim.inspect(param)), ERROR)
64+
end
65+
66+
if param == 'scope' then
67+
param_value = param_value or 'local'
68+
if not vim.list_contains({ 'global', 'local' }, param_value) then
69+
error(('Bad param value `%s`\nCan only accept `global` or `local`!'):format(vim.inspect(param_value)), ERROR)
70+
end
71+
end
72+
if param == 'ft' and (not param_value or type(param_value) ~= 'string') then
73+
error('Missing/bad value for `ft` parameter!', ERROR)
74+
end
75+
if
76+
vim.list_contains({ 'win', 'buf' }, param)
77+
and not (param_value and type(param_value) == 'number' and M.is_int(param_value, param_value >= 0))
78+
then
79+
error('Missing/bad value for `win`/`buf` parameter!', ERROR)
80+
end
81+
82+
vim.api.nvim_set_option_value(option, value, { [param] = param_value })
83+
end
84+
1185
---Checks whether nvim is running on Windows.
1286
--- ---
1387
---@return boolean win32

0 commit comments

Comments
 (0)