Skip to content

fix: remove gotos, use newer functions, fix types #1732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/.luarc-5.1.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"diagnostics.libraryFiles": "Disable",
"diagnostics": {
"libraryFiles": "Disable"
},
"runtime": {
"version": "Lua 5.1",
"version": "LuaJIT",
"path": [
"lua/?.lua",
"lua/?/init.lua",
Expand All @@ -23,7 +25,8 @@
],
"ignoreDir": [
".dependencies",
".luarocks"
".luarocks",
".lua"
]
}
}
7 changes: 5 additions & 2 deletions .github/workflows/.luarc-luajit-master.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"diagnostics.libraryFiles": "Disable",
"diagnostics": {
"libraryFiles": "Disable"
},
"runtime": {
"version": "LuaJIT",
"path": [
Expand All @@ -23,7 +25,8 @@
],
"ignoreDir": [
".dependencies",
".luarocks"
".luarocks",
".lua"
]
}
}
12 changes: 10 additions & 2 deletions .github/workflows/luals-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ jobs:
mise_toml: |
[tools]
neovim = "${{ matrix.neovim }}"
lua-language-server = "3.13.6"
cargo-binstall = "latest"
"cargo:emmylua_check" = "latest"
"cargo:emmylua_ls" = "latest"
lua-language-server = "3.13.9"

- name: Run lua-language-server check
continue-on-error: true
run: |
LUARC=".github/workflows/.luarc-${{ matrix.lua }}.json"
make luals-check CONFIGURATION="$LUARC"

- name: Run emmylua_check
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.
run: |
LUARC=".github/workflows/.luarc-${{ matrix.lua }}.json"
make emmylua-check CONFIGURATION="$LUARC"
3 changes: 2 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"ignoreDir": [
".dependencies",
".luarocks"
".luarocks",
".lua"
]
}
}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ clean:
CONFIGURATION = .luarc.json
luals-check: setup
VIMRUNTIME="`nvim --clean --headless --cmd 'lua io.write(vim.env.VIMRUNTIME)' --cmd 'quit'`" lua-language-server --configpath=$(CONFIGURATION) --check=.

emmylua-check: setup
VIMRUNTIME="`nvim --clean --headless --cmd 'lua io.write(vim.env.VIMRUNTIME)' --cmd 'quit'`" emmylua_check -c $(CONFIGURATION) .
10 changes: 5 additions & 5 deletions lua/neo-tree/events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ end

---@param event_name neotree.Event|string
---@param autocmds string[]
---@param debounce_frequency integer
---@param seed_fn function
---@param nested boolean
---@param debounce_frequency integer?
---@param seed_fn function?
---@param nested boolean?
M.define_autocmd_event = function(event_name, autocmds, debounce_frequency, seed_fn, nested)
log.debug("Defining autocmd event: %s", event_name)
local augroup_name = "NeoTreeEvent_" .. event_name
Expand All @@ -79,9 +79,9 @@ M.define_autocmd_event = function(event_name, autocmds, debounce_frequency, seed
group = augroup,
nested = nested,
callback = function(args)
---@class neotree.Event.Autocmd.CallbackArgs : vim.api.keyset.create_autocmd.callback_args
---@class neotree.Event.Autocmd.CallbackArgs : neotree._vim.api.keyset.create_autocmd.callback_args
---@field afile string
local event_args = args
local event_args = args --[[@as neotree._vim.api.keyset.create_autocmd.callback_args]]
event_args.afile = args.file or ""
M.fire_event(event_name, event_args)
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/neo-tree/events/queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ local fire_event_internal = function(event, args)
end

---@param event string
---@param args table?
---@param args any?
M.fire_event = function(event, args)
local freq = utils.get_value(event_definitions, event .. ".debounce_frequency", 0, true)
local strategy = utils.get_value(event_definitions, event .. ".debounce_strategy", 0, true)
Expand Down
31 changes: 16 additions & 15 deletions lua/neo-tree/sources/common/container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,23 @@ local render_content = function(config, node, state, context)
local rendered_width = 0

for _, item in ipairs(items) do
if item.enabled == false then
goto continue
end
local required_width = item.required_width or 0
if required_width > window_width then
goto continue
end
local rendered_item = renderer.render_component(item, node, state, context.available_width)
if rendered_item then
local align = item.align or "left"
should_pad[align] = add_padding(rendered_item, should_pad[align])
repeat
if item.enabled == false then
break
end
local required_width = item.required_width or 0
if required_width > window_width then
break
end
local rendered_item = renderer.render_component(item, node, state, context.available_width)
if rendered_item then
local align = item.align or "left"
should_pad[align] = add_padding(rendered_item, should_pad[align])

vim.list_extend(zindex_rendered[align], rendered_item)
rendered_width = rendered_width + calc_rendered_width(rendered_item)
end
::continue::
vim.list_extend(zindex_rendered[align], rendered_item)
rendered_width = rendered_width + calc_rendered_width(rendered_item)
end
until true
end

max_width = math.max(max_width, rendered_width)
Expand Down
33 changes: 17 additions & 16 deletions lua/neo-tree/sources/common/file-nesting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,25 @@ pattern_matcher.get_children = function(item, siblings, rule)

for type, type_functions in pairs(pattern_matcher_types) do
for _, pattern in pairs(rule[type] or {}) do
---@cast rule neotree.FileNesting.Rule.Pattern
local item_name = rule.ignore_case and item.name:lower() or item.name

local success, replaced_pattern = pcall(string.gsub, item_name, rule.pattern, pattern)
if not success then
log.error("Error using file glob '" .. pattern .. "'; Error: " .. replaced_pattern)
goto continue
end
for _, sibling in pairs(siblings) do
if sibling.id ~= item.id then
local sibling_name = rule.ignore_case and sibling.name:lower() or sibling.name
local glob_or_file = type_functions.get_pattern(replaced_pattern)
if type_functions.match(sibling_name, glob_or_file) then
table.insert(matching_files, sibling)
repeat
---@cast rule neotree.FileNesting.Rule.Pattern
local item_name = rule.ignore_case and item.name:lower() or item.name

local success, replaced_pattern = pcall(string.gsub, item_name, rule.pattern, pattern)
if not success then
log.error("Error using file glob '" .. pattern .. "'; Error: " .. replaced_pattern)
break
end
for _, sibling in pairs(siblings) do
if sibling.id ~= item.id then
local sibling_name = rule.ignore_case and sibling.name:lower() or sibling.name
local glob_or_file = type_functions.get_pattern(replaced_pattern)
if type_functions.match(sibling_name, glob_or_file) then
table.insert(matching_files, sibling)
end
end
end
end
::continue::
until true
end
end
return matching_files
Expand Down
103 changes: 46 additions & 57 deletions lua/neo-tree/sources/common/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,46 +314,48 @@ function Preview:setBuffer(bufnr)
local eventignore = vim.opt.eventignore
vim.opt.eventignore:append("BufEnter,BufWinEnter")

if self.config.use_image_nvim and try_load_image_nvim_buf(self.winid, bufnr) then
-- calling the try method twice should be okay here, image.nvim should cache the image and displaying the image takes
-- really long anyways
vim.api.nvim_win_set_buf(self.winid, bufnr)
try_load_image_nvim_buf(self.winid, bufnr)
goto finally
end

if self.config.use_float then
-- Workaround until https://github.com/neovim/neovim/issues/24973 is resolved or maybe 'previewpopup' comes in?
vim.fn.bufload(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
-- I'm not sure why float windows won't show numbers without this
vim.wo[self.winid].number = true

-- code below is from mini.pick
-- only starts treesitter parser if the filetype is matching
local ft = vim.bo[bufnr].filetype
local bufsize = get_bufsize(bufnr)
if bufsize > 1024 * 1024 or bufsize > 1000 * #lines then
goto finally
end
local has_lang, lang = pcall(vim.treesitter.language.get_lang, ft)
lang = has_lang and lang or ft
local has_parser, parser = pcall(vim.treesitter.get_parser, self.bufnr, lang, { error = false })
has_parser = has_parser and parser ~= nil
if has_parser then
has_parser = pcall(vim.treesitter.start, self.bufnr, lang)
repeat
if self.config.use_image_nvim and try_load_image_nvim_buf(self.winid, bufnr) then
-- calling the try method twice should be okay here, image.nvim should cache the image and displaying the image takes
-- really long anyways
vim.api.nvim_win_set_buf(self.winid, bufnr)
try_load_image_nvim_buf(self.winid, bufnr)
break -- goto end
end
if not has_parser then
vim.bo[self.bufnr].syntax = ft

if self.config.use_float then
-- Workaround until https://github.com/neovim/neovim/issues/24973 is resolved or maybe 'previewpopup' comes in?
vim.fn.bufload(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
-- I'm not sure why float windows won't show numbers without this
vim.wo[self.winid].number = true

-- code below is from mini.pick
-- only starts treesitter parser if the filetype is matching
local ft = vim.bo[bufnr].filetype
local bufsize = get_bufsize(bufnr)
if bufsize > 1024 * 1024 or bufsize > 1000 * #lines then
break -- goto end
end
local has_lang, lang = pcall(vim.treesitter.language.get_lang, ft)
lang = has_lang and lang or ft
local has_parser, parser =
pcall(vim.treesitter.get_parser, self.bufnr, lang, { error = false })
has_parser = has_parser and parser ~= nil
if has_parser then
has_parser = pcall(vim.treesitter.start, self.bufnr, lang)
end
if not has_parser then
vim.bo[self.bufnr].syntax = ft
end
else
vim.api.nvim_win_set_buf(self.winid, bufnr)
self.bufnr = bufnr
end
else
vim.api.nvim_win_set_buf(self.winid, bufnr)
self.bufnr = bufnr
end

::finally::
until true
vim.opt.eventignore = eventignore
end

Expand Down Expand Up @@ -387,28 +389,15 @@ function Preview:highlight_preview_range()
end_pos = start_pos
end

local highlight = function(line, col_start, col_end)
vim.api.nvim_buf_add_highlight(
self.bufnr,
neo_tree_preview_namespace,
highlights.PREVIEW,
line,
col_start,
col_end
)
end

local start_line, end_line = start_pos[1], end_pos[1]
local start_col, end_col = start_pos[2], end_pos[2]
if start_line == end_line then
highlight(start_line, start_col, end_col)
else
highlight(start_line, start_col, -1)
for line = start_line + 1, end_line - 1 do
highlight(line, 0, -1)
end
highlight(end_line, 0, end_col)
end
vim.api.nvim_buf_set_extmark(self.bufnr, neo_tree_preview_namespace, start_line, start_col, {
hl_group = highlights.PREVIEW,
end_row = end_line,
end_col = end_col,
-- priority = priority,
strict = false,
})
end

---Clear the preview highlight in the buffer currently in the preview window.
Expand Down
10 changes: 7 additions & 3 deletions lua/neo-tree/sources/document_symbols/lib/client_filters.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---Utilities function to filter the LSP servers
local utils = require("neo-tree.utils")

---@alias neotree.lsp.RespRaw table<integer,{err: lsp.ResponseError?, result: any}>
---@class neotree.lsp.RespRaw
---@field err lsp.ResponseError?
---@field error lsp.ResponseError?
---@field result any

local M = {}

---@alias neotree.lsp.Filter fun(client_name: string): boolean

---Filter clients
---@param filter_type "first" | "all"
---@param filter_fn neotree.lsp.Filter?
---@param resp neotree.lsp.RespRaw
---@param resp table<integer, neotree.lsp.RespRaw>
---@return table<string, any>
local filter_clients = function(filter_type, filter_fn, resp)
if resp == nil or type(resp) ~= "table" then
Expand Down Expand Up @@ -51,7 +55,7 @@ local ignore = function(ignore)
end

---Main entry point for the filter
---@param resp neotree.lsp.RespRaw
---@param resp table<integer, neotree.lsp.RespRaw>
---@return table<string, any>
M.filter_resp = function(resp)
return {}
Expand Down
3 changes: 2 additions & 1 deletion lua/neo-tree/sources/document_symbols/lib/symbols_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ local function parse_resp(resp_node, id, state, parent_search_path)
end

---Callback function for lsp request
---@param lsp_resp neotree.lsp.RespRaw the response of the lsp client
---@param lsp_resp table<integer, neotree.lsp.RespRaw> the response of the lsp clients
---@param state table the state of the source
local on_lsp_resp = function(lsp_resp, state)
if lsp_resp == nil or type(lsp_resp) ~= "table" then
Expand Down Expand Up @@ -163,6 +163,7 @@ local on_lsp_resp = function(lsp_resp, state)
end

---latter is deprecated in neovim v0.11
---@diagnostic disable-next-line: deprecated
local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients
M.render_symbols = function(state)
local bufnr = state.lsp_bufnr
Expand Down
2 changes: 2 additions & 0 deletions lua/neo-tree/sources/filesystem/lib/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
local has_pre_search_folders = utils.truthy(state.open_folders_before_search)
if not has_pre_search_folders then
log.trace("No search or pre-search folders, recording pre-search folders now")
---@type table|nil
state.open_folders_before_search = renderer.get_expanded_nodes(state.tree)
end

Expand Down Expand Up @@ -159,6 +160,7 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
state.sort_function_override = sort_by_score
state.use_fzy = true
end
---@type function|nil
local callback = select_first_file
if fuzzy_finder_mode == "directory" then
callback = nil
Expand Down
Loading
Loading