Skip to content

Commit a2f0245

Browse files
authored
fix: remove gotos, use newer functions, fix types (#1732)
1 parent ee1de03 commit a2f0245

File tree

19 files changed

+189
-138
lines changed

19 files changed

+189
-138
lines changed

.github/workflows/.luarc-5.1.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
3-
"diagnostics.libraryFiles": "Disable",
3+
"diagnostics": {
4+
"libraryFiles": "Disable"
5+
},
46
"runtime": {
5-
"version": "Lua 5.1",
7+
"version": "LuaJIT",
68
"path": [
79
"lua/?.lua",
810
"lua/?/init.lua",
@@ -23,7 +25,8 @@
2325
],
2426
"ignoreDir": [
2527
".dependencies",
26-
".luarocks"
28+
".luarocks",
29+
".lua"
2730
]
2831
}
2932
}

.github/workflows/.luarc-luajit-master.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
3-
"diagnostics.libraryFiles": "Disable",
3+
"diagnostics": {
4+
"libraryFiles": "Disable"
5+
},
46
"runtime": {
57
"version": "LuaJIT",
68
"path": [
@@ -23,7 +25,8 @@
2325
],
2426
"ignoreDir": [
2527
".dependencies",
26-
".luarocks"
28+
".luarocks",
29+
".lua"
2730
]
2831
}
2932
}

.github/workflows/luals-check.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ jobs:
2727
mise_toml: |
2828
[tools]
2929
neovim = "${{ matrix.neovim }}"
30-
lua-language-server = "3.13.6"
30+
cargo-binstall = "latest"
31+
"cargo:emmylua_check" = "latest"
32+
"cargo:emmylua_ls" = "latest"
33+
lua-language-server = "3.13.9"
3134
3235
- name: Run lua-language-server check
33-
continue-on-error: true
3436
run: |
3537
LUARC=".github/workflows/.luarc-${{ matrix.lua }}.json"
3638
make luals-check CONFIGURATION="$LUARC"
39+
40+
- name: Run emmylua_check
41+
continue-on-error: true # Doesn't type-check well enough to be worth erroring on, but this runs so fast we might as well help test this out.
42+
run: |
43+
LUARC=".github/workflows/.luarc-${{ matrix.lua }}.json"
44+
make emmylua-check CONFIGURATION="$LUARC"

.luarc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
],
2626
"ignoreDir": [
2727
".dependencies",
28-
".luarocks"
28+
".luarocks",
29+
".lua"
2930
]
3031
}
3132
}

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ clean:
3737
CONFIGURATION = .luarc.json
3838
luals-check: setup
3939
VIMRUNTIME="`nvim --clean --headless --cmd 'lua io.write(vim.env.VIMRUNTIME)' --cmd 'quit'`" lua-language-server --configpath=$(CONFIGURATION) --check=.
40+
41+
emmylua-check: setup
42+
VIMRUNTIME="`nvim --clean --headless --cmd 'lua io.write(vim.env.VIMRUNTIME)' --cmd 'quit'`" emmylua_check -c $(CONFIGURATION) .

lua/neo-tree/events/init.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ end
6262

6363
---@param event_name neotree.Event|string
6464
---@param autocmds string[]
65-
---@param debounce_frequency integer
66-
---@param seed_fn function
67-
---@param nested boolean
65+
---@param debounce_frequency integer?
66+
---@param seed_fn function?
67+
---@param nested boolean?
6868
M.define_autocmd_event = function(event_name, autocmds, debounce_frequency, seed_fn, nested)
6969
log.debug("Defining autocmd event: %s", event_name)
7070
local augroup_name = "NeoTreeEvent_" .. event_name
@@ -79,9 +79,9 @@ M.define_autocmd_event = function(event_name, autocmds, debounce_frequency, seed
7979
group = augroup,
8080
nested = nested,
8181
callback = function(args)
82-
---@class neotree.Event.Autocmd.CallbackArgs : vim.api.keyset.create_autocmd.callback_args
82+
---@class neotree.Event.Autocmd.CallbackArgs : neotree._vim.api.keyset.create_autocmd.callback_args
8383
---@field afile string
84-
local event_args = args
84+
local event_args = args --[[@as neotree._vim.api.keyset.create_autocmd.callback_args]]
8585
event_args.afile = args.file or ""
8686
M.fire_event(event_name, event_args)
8787
end,

lua/neo-tree/events/queue.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ local fire_event_internal = function(event, args)
102102
end
103103

104104
---@param event string
105-
---@param args table?
105+
---@param args any?
106106
M.fire_event = function(event, args)
107107
local freq = utils.get_value(event_definitions, event .. ".debounce_frequency", 0, true)
108108
local strategy = utils.get_value(event_definitions, event .. ".debounce_strategy", 0, true)

lua/neo-tree/sources/common/container.lua

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,23 @@ local render_content = function(config, node, state, context)
7171
local rendered_width = 0
7272

7373
for _, item in ipairs(items) do
74-
if item.enabled == false then
75-
goto continue
76-
end
77-
local required_width = item.required_width or 0
78-
if required_width > window_width then
79-
goto continue
80-
end
81-
local rendered_item = renderer.render_component(item, node, state, context.available_width)
82-
if rendered_item then
83-
local align = item.align or "left"
84-
should_pad[align] = add_padding(rendered_item, should_pad[align])
74+
repeat
75+
if item.enabled == false then
76+
break
77+
end
78+
local required_width = item.required_width or 0
79+
if required_width > window_width then
80+
break
81+
end
82+
local rendered_item = renderer.render_component(item, node, state, context.available_width)
83+
if rendered_item then
84+
local align = item.align or "left"
85+
should_pad[align] = add_padding(rendered_item, should_pad[align])
8586

86-
vim.list_extend(zindex_rendered[align], rendered_item)
87-
rendered_width = rendered_width + calc_rendered_width(rendered_item)
88-
end
89-
::continue::
87+
vim.list_extend(zindex_rendered[align], rendered_item)
88+
rendered_width = rendered_width + calc_rendered_width(rendered_item)
89+
end
90+
until true
9091
end
9192

9293
max_width = math.max(max_width, rendered_width)

lua/neo-tree/sources/common/file-nesting.lua

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,25 @@ pattern_matcher.get_children = function(item, siblings, rule)
150150

151151
for type, type_functions in pairs(pattern_matcher_types) do
152152
for _, pattern in pairs(rule[type] or {}) do
153-
---@cast rule neotree.FileNesting.Rule.Pattern
154-
local item_name = rule.ignore_case and item.name:lower() or item.name
155-
156-
local success, replaced_pattern = pcall(string.gsub, item_name, rule.pattern, pattern)
157-
if not success then
158-
log.error("Error using file glob '" .. pattern .. "'; Error: " .. replaced_pattern)
159-
goto continue
160-
end
161-
for _, sibling in pairs(siblings) do
162-
if sibling.id ~= item.id then
163-
local sibling_name = rule.ignore_case and sibling.name:lower() or sibling.name
164-
local glob_or_file = type_functions.get_pattern(replaced_pattern)
165-
if type_functions.match(sibling_name, glob_or_file) then
166-
table.insert(matching_files, sibling)
153+
repeat
154+
---@cast rule neotree.FileNesting.Rule.Pattern
155+
local item_name = rule.ignore_case and item.name:lower() or item.name
156+
157+
local success, replaced_pattern = pcall(string.gsub, item_name, rule.pattern, pattern)
158+
if not success then
159+
log.error("Error using file glob '" .. pattern .. "'; Error: " .. replaced_pattern)
160+
break
161+
end
162+
for _, sibling in pairs(siblings) do
163+
if sibling.id ~= item.id then
164+
local sibling_name = rule.ignore_case and sibling.name:lower() or sibling.name
165+
local glob_or_file = type_functions.get_pattern(replaced_pattern)
166+
if type_functions.match(sibling_name, glob_or_file) then
167+
table.insert(matching_files, sibling)
168+
end
167169
end
168170
end
169-
end
170-
::continue::
171+
until true
171172
end
172173
end
173174
return matching_files

lua/neo-tree/sources/common/preview.lua

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -314,46 +314,48 @@ function Preview:setBuffer(bufnr)
314314
local eventignore = vim.opt.eventignore
315315
vim.opt.eventignore:append("BufEnter,BufWinEnter")
316316

317-
if self.config.use_image_nvim and try_load_image_nvim_buf(self.winid, bufnr) then
318-
-- calling the try method twice should be okay here, image.nvim should cache the image and displaying the image takes
319-
-- really long anyways
320-
vim.api.nvim_win_set_buf(self.winid, bufnr)
321-
try_load_image_nvim_buf(self.winid, bufnr)
322-
goto finally
323-
end
324-
325-
if self.config.use_float then
326-
-- Workaround until https://github.com/neovim/neovim/issues/24973 is resolved or maybe 'previewpopup' comes in?
327-
vim.fn.bufload(bufnr)
328-
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
329-
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
330-
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
331-
-- I'm not sure why float windows won't show numbers without this
332-
vim.wo[self.winid].number = true
333-
334-
-- code below is from mini.pick
335-
-- only starts treesitter parser if the filetype is matching
336-
local ft = vim.bo[bufnr].filetype
337-
local bufsize = get_bufsize(bufnr)
338-
if bufsize > 1024 * 1024 or bufsize > 1000 * #lines then
339-
goto finally
340-
end
341-
local has_lang, lang = pcall(vim.treesitter.language.get_lang, ft)
342-
lang = has_lang and lang or ft
343-
local has_parser, parser = pcall(vim.treesitter.get_parser, self.bufnr, lang, { error = false })
344-
has_parser = has_parser and parser ~= nil
345-
if has_parser then
346-
has_parser = pcall(vim.treesitter.start, self.bufnr, lang)
317+
repeat
318+
if self.config.use_image_nvim and try_load_image_nvim_buf(self.winid, bufnr) then
319+
-- calling the try method twice should be okay here, image.nvim should cache the image and displaying the image takes
320+
-- really long anyways
321+
vim.api.nvim_win_set_buf(self.winid, bufnr)
322+
try_load_image_nvim_buf(self.winid, bufnr)
323+
break -- goto end
347324
end
348-
if not has_parser then
349-
vim.bo[self.bufnr].syntax = ft
325+
326+
if self.config.use_float then
327+
-- Workaround until https://github.com/neovim/neovim/issues/24973 is resolved or maybe 'previewpopup' comes in?
328+
vim.fn.bufload(bufnr)
329+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
330+
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
331+
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
332+
-- I'm not sure why float windows won't show numbers without this
333+
vim.wo[self.winid].number = true
334+
335+
-- code below is from mini.pick
336+
-- only starts treesitter parser if the filetype is matching
337+
local ft = vim.bo[bufnr].filetype
338+
local bufsize = get_bufsize(bufnr)
339+
if bufsize > 1024 * 1024 or bufsize > 1000 * #lines then
340+
break -- goto end
341+
end
342+
local has_lang, lang = pcall(vim.treesitter.language.get_lang, ft)
343+
lang = has_lang and lang or ft
344+
local has_parser, parser =
345+
pcall(vim.treesitter.get_parser, self.bufnr, lang, { error = false })
346+
has_parser = has_parser and parser ~= nil
347+
if has_parser then
348+
has_parser = pcall(vim.treesitter.start, self.bufnr, lang)
349+
end
350+
if not has_parser then
351+
vim.bo[self.bufnr].syntax = ft
352+
end
353+
else
354+
vim.api.nvim_win_set_buf(self.winid, bufnr)
355+
self.bufnr = bufnr
350356
end
351-
else
352-
vim.api.nvim_win_set_buf(self.winid, bufnr)
353-
self.bufnr = bufnr
354-
end
355357

356-
::finally::
358+
until true
357359
vim.opt.eventignore = eventignore
358360
end
359361

@@ -387,28 +389,15 @@ function Preview:highlight_preview_range()
387389
end_pos = start_pos
388390
end
389391

390-
local highlight = function(line, col_start, col_end)
391-
vim.api.nvim_buf_add_highlight(
392-
self.bufnr,
393-
neo_tree_preview_namespace,
394-
highlights.PREVIEW,
395-
line,
396-
col_start,
397-
col_end
398-
)
399-
end
400-
401392
local start_line, end_line = start_pos[1], end_pos[1]
402393
local start_col, end_col = start_pos[2], end_pos[2]
403-
if start_line == end_line then
404-
highlight(start_line, start_col, end_col)
405-
else
406-
highlight(start_line, start_col, -1)
407-
for line = start_line + 1, end_line - 1 do
408-
highlight(line, 0, -1)
409-
end
410-
highlight(end_line, 0, end_col)
411-
end
394+
vim.api.nvim_buf_set_extmark(self.bufnr, neo_tree_preview_namespace, start_line, start_col, {
395+
hl_group = highlights.PREVIEW,
396+
end_row = end_line,
397+
end_col = end_col,
398+
-- priority = priority,
399+
strict = false,
400+
})
412401
end
413402

414403
---Clear the preview highlight in the buffer currently in the preview window.

0 commit comments

Comments
 (0)