Skip to content

Commit ba75e9b

Browse files
committed
ref: make workspace diagnostics more accurate
1 parent fb06f15 commit ba75e9b

File tree

15 files changed

+198
-227
lines changed

15 files changed

+198
-227
lines changed

lua/libmodal/src/Layer.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
--- @type libmodal.globals
12
local globals = require 'libmodal/src/globals'
3+
4+
--- @type libmodal.utils
25
local utils = require 'libmodal/src/utils'
36

47
--- Normalizes a `buffer = true|false|0` argument into a number.
@@ -143,6 +146,12 @@ function Layer:map(mode, lhs, rhs, options)
143146
end
144147
end
145148

149+
--- @param keymaps_by_mode table the keymaps (e.g. `{n = {gg = {rhs = 'G', silent = true}}}`)
150+
--- @return libmodal.Layer
151+
function Layer.new(keymaps_by_mode)
152+
return setmetatable({existing_keymaps_by_mode = {}, layer_keymaps_by_mode = keymaps_by_mode, active = false}, Layer)
153+
end
154+
146155
--- restore one keymapping to its original state.
147156
--- @param buffer? number the buffer to unmap from (`nil` if it is not buffer-local)
148157
--- @param mode string the mode of the keymap.
@@ -175,11 +184,4 @@ function Layer:unmap(buffer, mode, lhs)
175184
self.existing_keymaps_by_mode[mode][lhs] = nil
176185
end
177186

178-
return
179-
{
180-
--- @param keymaps_by_mode table the keymaps (e.g. `{n = {gg = {rhs = 'G', silent = true}}}`)
181-
--- @return libmodal.Layer
182-
new = function(keymaps_by_mode)
183-
return setmetatable({existing_keymaps_by_mode = {}, layer_keymaps_by_mode = keymaps_by_mode, active = false}, Layer)
184-
end
185-
}
187+
return Layer

lua/libmodal/src/Mode.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
local globals = require 'libmodal/src/globals'
1+
--- @type libmodal.globals
2+
local globals = require 'libmodal/src/globals'
3+
4+
--- @type libmodal.collections.ParseTable
25
local ParseTable = require 'libmodal/src/collections/ParseTable'
3-
local utils = require 'libmodal/src/utils'
6+
7+
--- @type libmodal.utils
8+
local utils = require 'libmodal/src/utils'
49

510
--- @class libmodal.Mode
611
--- @field private exit libmodal.utils.Vars
@@ -111,6 +116,7 @@ function Mode:enter()
111116

112117
-- if there were errors, handle them.
113118
if not ok then
119+
--- @diagnostic disable-next-line:param-type-mismatch if `not ok` then `mode_result` is a string
114120
utils.notify_error('Error during nvim-libmodal mode', mode_result)
115121
continue_mode = false
116122
else
@@ -195,7 +201,7 @@ return
195201
local self = setmetatable(
196202
{
197203
exit = utils.Vars.new('exit', name),
198-
indicator = utils.Indicator.mode(name),
204+
indicator = utils.Indicator.new('LibmodalPrompt', '-- ' .. name .. ' --'),
199205
input = utils.Vars.new('input', name),
200206
instruction = instruction,
201207
name = name,
@@ -213,6 +219,7 @@ return
213219

214220
-- determine if a default `Help` should be created.
215221
if not self.instruction[HELP] then
222+
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
216223
self.help = utils.Help.new(self.instruction, 'KEY MAP')
217224
end
218225

lua/libmodal/src/Prompt.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
--- @type libmodal.globals
12
local globals = require 'libmodal/src/globals'
3+
4+
--- @type libmodal.utils
25
local utils = require 'libmodal/src/utils'
36

47
--- @class libmodal.Prompt
@@ -95,6 +98,7 @@ function Prompt:enter()
9598

9699
-- if there were errors.
97100
if not ok then
101+
--- @diagnostic disable-next-line:param-type-mismatch if `not ok` then `mode_result` is a string
98102
utils.notify_error('Error during nvim-libmodal mode', prompt_result)
99103
continue_mode = false
100104
else
@@ -116,7 +120,7 @@ return
116120
local self = setmetatable(
117121
{
118122
exit = utils.Vars.new('exit', name),
119-
indicator = utils.Indicator.prompt(name),
123+
indicator = utils.Indicator.new('LibmodalStar', '* ' .. name .. ' > '),
120124
input = utils.Vars.new('input', name),
121125
instruction = instruction,
122126
name = name
@@ -140,6 +144,7 @@ return
140144

141145
if not contained_help then -- assign it.
142146
completions[#completions + 1] = HELP
147+
--- @diagnostic disable-next-line:param-type-mismatch we checked that `instruction` is a table above
143148
self.help = utils.Help.new(instruction, 'COMMAND')
144149
end
145150

lua/libmodal/src/collections/ParseTable.lua

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
--- the number corresponding to <CR> in vim.
2-
local CR = 13
1+
--- @type libmodal.globals
32
local globals = require 'libmodal/src/globals'
43

4+
--- the number corresponding to <CR> in vim.
5+
local CR = string.byte(vim.api.nvim_replace_termcodes('<CR>', true, true, true))
6+
57
--- @class libmodal.collections.ParseTable
6-
local ParseTable = require('libmodal/src/utils/classes').new(nil)
8+
--- @field CR number the byte representation of `<CR>`
9+
local ParseTable = require('libmodal/src/utils/classes').new {CR = CR}
710

811
--- reverse the order of elements in some `tbl`
912
--- @param tbl table the table to reverse
@@ -93,6 +96,19 @@ function ParseTable:get(key_dict)
9396
return get(self, table_reverse(key_dict))
9497
end
9598

99+
--- create a new `libmodal.collections.ParseTable` from a user-provided table.
100+
--- @param user_table table keymaps (e.g. `{zfo = 'tabnew'}`)
101+
--- @return libmodal.collections.ParseTable
102+
function ParseTable.new(user_table)
103+
local self = setmetatable({}, ParseTable)
104+
105+
-- parse the passed in table.
106+
self:parse_put_all(user_table)
107+
108+
-- return the new `ParseTable`.
109+
return self
110+
end
111+
96112
--- parse `key` and retrieve its value
97113
--- @param key string the left-hand-side of the mapping to retrieve
98114
--- @return false|fun()|nil|string|table match a string/func when fully found; a table when partially found; false when not found.
@@ -123,20 +139,4 @@ function ParseTable:parse_put_all(keys_and_values)
123139
end
124140
end
125141

126-
return
127-
{
128-
CR = CR,
129-
130-
--- create a new `libmodal.collections.ParseTable` from a user-provided table.
131-
--- @param user_table table keymaps (e.g. `{zfo = 'tabnew'}`)
132-
--- @return libmodal.collections.ParseTable
133-
new = function(user_table)
134-
local self = setmetatable({}, ParseTable)
135-
136-
-- parse the passed in table.
137-
self:parse_put_all(user_table)
138-
139-
-- return the new `ParseTable`.
140-
return self
141-
end,
142-
}
142+
return ParseTable

lua/libmodal/src/collections/Stack.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
--- @class libmodal.collections.Stack
22
local Stack = require('libmodal/src/utils/classes').new(nil)
33

4+
--- @return libmodal.collections.Stack
5+
function Stack.new()
6+
return setmetatable({}, Stack)
7+
end
8+
49
--- @return unknown top the foremost value of the stack
510
function Stack:peek()
611
return self[#self]
@@ -19,9 +24,4 @@ function Stack:push(value)
1924
self[#self + 1] = value
2025
end
2126

22-
return
23-
{
24-
new = function()
25-
return setmetatable({}, Stack)
26-
end,
27-
}
27+
return Stack

lua/libmodal/src/collections/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
return
55
{
66
ParseTable = require 'libmodal/src/collections/ParseTable',
7-
Stack = require 'libmodal/src/collections/Stack'
7+
Stack = require 'libmodal/src/collections/Stack'
88
}

lua/libmodal/src/globals.lua

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
1-
local VIM_FALSE = 0
2-
local VIM_TRUE = 1
3-
41
--- @class libmodal.globals
5-
--- @field private ESC_NR number
6-
--- @field private TYPE_FUNC string
7-
--- @field private TYPE_NUM string
8-
--- @field private TYPE_STR string
9-
--- @field private TYPE_TBL string
10-
--- @field private VIM_FALSE number
11-
--- @field private VIM_TRUE number
12-
return
2+
--- @field ESC_NR number the key-code for the escape character.
3+
--- @field TYPE_FUNC string the string which is returned by `type(function() end)` (or any function)
4+
--- @field TYPE_NUM string the string which is returned by `type(0)` (or any number)
5+
--- @field TYPE_STR string the string which is returned by `type ''` (or any string)
6+
--- @field TYPE_TBL string the string which is returned by `type {}` (or any table)
7+
--- @field VIM_FALSE number the value of Vimscript's `v:false`
8+
--- @field VIM_TRUE number the value of Vimscript's `v:true`
9+
local globals =
1310
{
14-
--- the key-code for the escape character.
1511
ESC_NR = 27,
16-
17-
--- the string which is returned by `type(function() end)` (or any function)
1812
TYPE_FUNC = type(function() end),
19-
20-
--- the string which is returned by `type(0)` (or any number)
2113
TYPE_NUM = type(0),
22-
23-
--- the string which is returned by `type ''` (or any string)
2414
TYPE_STR = type '',
25-
26-
--- the string which is returned by `type {}` (or any table)
2715
TYPE_TBL = type {},
28-
29-
--- the value of Vimscript's `v:false`
30-
VIM_FALSE = VIM_FALSE,
31-
32-
--- the value of Vimscript's `v:true`
33-
VIM_TRUE = VIM_TRUE,
34-
35-
--- assert some value is either `false` or `v:false`.
36-
--- @param val boolean|number
37-
--- @return boolean
38-
is_false = function(val)
39-
return val == false or val == VIM_FALSE
40-
end,
41-
42-
--- assert some value is either `true` or `v:true`.
43-
--- @param val boolean|number
44-
--- @return boolean
45-
is_true = function(val)
46-
return val == true or val == VIM_TRUE
47-
end
16+
VIM_FALSE = 0,
17+
VIM_TRUE = 1,
4818
}
19+
20+
--- assert some value is either `false` or `v:false`.
21+
--- @param val boolean|number
22+
--- @return boolean
23+
function globals.is_false(val)
24+
return val == false or val == globals.VIM_FALSE
25+
end
26+
27+
--- assert some value is either `true` or `v:true`.
28+
--- @param val boolean|number
29+
--- @return boolean
30+
function globals.is_true(val)
31+
return val == true or val == globals.VIM_TRUE
32+
end
33+
34+
return globals

lua/libmodal/src/init.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
--- @class libmodal
2-
--- @field private collections libmodal.collections
3-
--- @field private globals libmodal.globals
4-
--- @field private Layer libmodal.Layer
5-
--- @field private Mode libmodal.Mode
6-
--- @field private Prompt libmodal.Prompt
7-
--- @field private utils libmodal.utils
8-
return
2+
--- @field collections libmodal.collections
3+
--- @field globals libmodal.globals
4+
--- @field Layer libmodal.Layer
5+
--- @field Mode libmodal.Mode
6+
--- @field Prompt libmodal.Prompt
7+
--- @field utils libmodal.utils
8+
local libmodal =
99
{
1010
collections = require 'libmodal/src/collections',
11-
globals = require 'libmodal/src/globals',
12-
Layer = require 'libmodal/src/Layer',
13-
Mode = require 'libmodal/src/Mode',
14-
Prompt = require 'libmodal/src/Prompt',
15-
utils = require 'libmodal/src/utils',
11+
globals = require 'libmodal/src/globals',
12+
Layer = require 'libmodal/src/Layer',
13+
Mode = require 'libmodal/src/Mode',
14+
Prompt = require 'libmodal/src/Prompt',
15+
utils = require 'libmodal/src/utils',
1616
}
17+
18+
return libmodal

lua/libmodal/src/utils/Help.lua

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--- @type libmodal.globals
12
local globals = require 'libmodal/src/globals'
23

34
--- @class libmodal.utils.Help
@@ -27,6 +28,32 @@ local function align_columns(tbl, longest_key_len)
2728
return to_print
2829
end
2930

31+
--- create a default help table with `commands_or_maps` and vim expressions.
32+
--- @param commands_or_maps {[string]: fun()|string} commands or mappings to vim expressions.
33+
--- @param title string
34+
--- @return libmodal.utils.Help
35+
function Help.new(commands_or_maps, title)
36+
-- find the longest key in the table, or at least the length of the title
37+
local longest_key_maps = string.len(title)
38+
for key, _ in pairs(commands_or_maps) do
39+
local key_len = string.len(key)
40+
if key_len > longest_key_maps then
41+
longest_key_maps = key_len
42+
end
43+
end
44+
45+
-- create a new `Help`.
46+
return setmetatable(
47+
{
48+
[1] = ' ',
49+
[2] = table.concat(align_columns({[title] = 'VIM EXPRESSION'}, longest_key_maps)),
50+
[3] = table.concat(align_columns({[string.rep('-', string.len(title))] = '--------------'}, longest_key_maps)),
51+
[4] = table.concat(align_columns(commands_or_maps, longest_key_maps)),
52+
},
53+
Help
54+
)
55+
end
56+
3057
--- show the contents of this `Help`.
3158
function Help:show()
3259
for _, help_text in ipairs(self) do
@@ -35,33 +62,4 @@ function Help:show()
3562
vim.fn.getchar()
3663
end
3764

38-
--[[/* CLASS `Help` */]]
39-
40-
return
41-
{
42-
--- create a default help table with `commands_or_maps` and vim expressions.
43-
--- @param commands_or_maps {[string]: fun()|string} commands or mappings to vim expressions.
44-
--- @param title string
45-
--- @return libmodal.utils.Help
46-
new = function(commands_or_maps, title)
47-
-- find the longest key in the table, or at least the length of the title
48-
local longest_key_maps = string.len(title)
49-
for key, _ in pairs(commands_or_maps) do
50-
local key_len = string.len(key)
51-
if key_len > longest_key_maps then
52-
longest_key_maps = key_len
53-
end
54-
end
55-
56-
-- create a new `Help`.
57-
return setmetatable(
58-
{
59-
[1] = ' ',
60-
[2] = table.concat(align_columns({[title] = 'VIM EXPRESSION'}, longest_key_maps)),
61-
[3] = table.concat(align_columns({[string.rep('-', string.len(title))] = '--------------'}, longest_key_maps)),
62-
[4] = table.concat(align_columns(commands_or_maps, longest_key_maps)),
63-
},
64-
Help
65-
)
66-
end
67-
}
65+
return Help

0 commit comments

Comments
 (0)