Skip to content
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

refactor(type): add type annotation for defaults.lua #1333

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
014f98a
refactor(type): add type annotation for defaults.lua
pysan3 Feb 2, 2024
efcefd7
ci(lua_ls): add lua_ls actions to check type annotation
pysan3 Feb 2, 2024
fde92b3
chore: autoformat with stylua
pysan3 Feb 2, 2024
0c048b4
ci(lua_ls): force run type check on all branches for now
pysan3 Feb 2, 2024
5b8f58c
Merge branch 'add-type-annotations' of github.com:pysan3/neo-tree.nvi…
pysan3 Feb 2, 2024
f5d2207
fix(ci): fix typo
pysan3 Feb 2, 2024
913d998
ci(lua_ls): refer to types defined in the repo
pysan3 Feb 2, 2024
adee9b9
fix(type): better organized type names
pysan3 Feb 2, 2024
72776bc
refactor(type): move config types to another file
pysan3 Feb 3, 2024
ead7513
refactor(type): add types to log.lua
pysan3 Feb 3, 2024
e6236b9
refactor(type): add type for merge_config
pysan3 Feb 5, 2024
c331b7b
ci(lua_ls): add more files to check types
pysan3 Feb 5, 2024
4cfcbe2
refactor(defaults): move event_handler examples to wiki
pysan3 Feb 5, 2024
4b68620
refactor(types): add type files to library
pysan3 Feb 5, 2024
7744d57
refactor(types): ignore wrong or deprecated types
pysan3 Feb 5, 2024
e85fdcc
ci(lua_ls): remove unnecessary types to check
pysan3 Feb 5, 2024
a28b905
refactor(types): remove collections.lua from type check
pysan3 Feb 5, 2024
c4ab2c3
ci(lua_ls): make lua_ls typecheck run on PRs
pysan3 Feb 5, 2024
9ce9563
fix(type): add lacking field
pysan3 Feb 6, 2024
b42cefe
refactor(type): delete types to prevent lua_ls performance issue
pysan3 Feb 9, 2024
12a67ae
refactor(type): move component types to separate file
pysan3 Feb 9, 2024
710ddaf
refactor(type): add types to functions in common/components
pysan3 Feb 9, 2024
c1eaadb
refactor(type): capitalize all enum keys
pysan3 Feb 11, 2024
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
29 changes: 18 additions & 11 deletions lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ local define_events = function()
return args
end)



local update_opened_buffers = function(args)
args.opened_buffers = utils.get_opened_buffers()
return args
Expand Down Expand Up @@ -297,10 +295,12 @@ M.win_enter_event = function()
local buf_name, message
if vim.startswith(filename, "[No Name]#") then
buf_name = string.sub(filename, 11)
message = "Cannot close because an unnamed buffer is modified. Please save or discard this file."
message =
"Cannot close because an unnamed buffer is modified. Please save or discard this file."
else
buf_name = filename
message = "Cannot close because one of the files is modified. Please save or discard changes."
message =
"Cannot close because one of the files is modified. Please save or discard changes."
end
log.trace("close_if_last_window, showing unnamed modified buffer: ", filename)
vim.schedule(function()
Expand Down Expand Up @@ -624,9 +624,11 @@ M.merge_config = function(user_config, is_auto_config)
-- Setting new "sources" to be the parsed names of the sources
M.config.sources = all_source_names

if ( M.config.source_selector.winbar or M.config.source_selector.statusline )
if
(M.config.source_selector.winbar or M.config.source_selector.statusline)
and M.config.source_selector.sources
and not user_config.default_source then
and not user_config.default_source
then
-- Set the default source to the head of these
-- This resolves some weirdness with the source selector having
-- a different "head" item than our current default.
Expand All @@ -639,14 +641,19 @@ M.merge_config = function(user_config, is_auto_config)
-- log a warning and then "pick" the first in the sources list
local match = false
for _, source in ipairs(M.config.sources) do
if source == M.config.default_source then
match = true
break
end
if source == M.config.default_source then
match = true
break
end
end
if not match and M.config.default_source ~= "last" then
M.config.default_source = M.config.sources[1]
log.warn(string.format("Invalid default source found in configuration. Using first available source: %s", M.config.default_source))
log.warn(
string.format(
"Invalid default source found in configuration. Using first available source: %s",
M.config.default_source
)
)
end

if not M.config.enable_git_status then
Expand Down
2 changes: 1 addition & 1 deletion lua/neo-tree/sources/buffers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local git = require("neo-tree.git")

local M = {
name = "buffers",
display_name = " 󰈚 Buffers "
display_name = " 󰈚 Buffers ",
}

local wrap = function(func)
Expand Down
18 changes: 9 additions & 9 deletions lua/neo-tree/sources/common/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ M.indent = function(config, node, state)
return indent
end

local get_header = function (state, label, size)
local get_header = function(state, label, size)
if state.sort and state.sort.label == label then
local icon = state.sort.direction == 1 and "▲" or "▼"
size = size - 2
Expand All @@ -468,12 +468,12 @@ local get_header = function (state, label, size)
return string.format("%" .. size .. "s ", label)
end

M.file_size = function (config, node, state)
M.file_size = function(config, node, state)
-- Root node gets column labels
if node:get_depth() == 1 then
return {
text = get_header(state, "Size", 12),
highlight = highlights.FILE_STATS_HEADER
highlight = highlights.FILE_STATS_HEADER,
}
end

Expand All @@ -491,7 +491,7 @@ M.file_size = function (config, node, state)

return {
text = string.format("%12s ", text),
highlight = config.highlight or highlights.FILE_STATS
highlight = config.highlight or highlights.FILE_STATS,
}
end

Expand All @@ -506,7 +506,7 @@ local file_time = function(config, node, state, stat_field)
end
return {
text = get_header(state, label, 20),
highlight = highlights.FILE_STATS_HEADER
highlight = highlights.FILE_STATS_HEADER,
}
end

Expand All @@ -516,7 +516,7 @@ local file_time = function(config, node, state, stat_field)
local display = seconds and os.date("%Y-%m-%d %I:%M %p", seconds) or "-"
return {
text = string.format("%20s ", display),
highlight = config.highlight or highlights.FILE_STATS
highlight = config.highlight or highlights.FILE_STATS,
}
end

Expand All @@ -539,19 +539,19 @@ M.symlink_target = function(config, node, state)
end
end

M.type = function (config, node, state)
M.type = function(config, node, state)
local text = node.ext or node.type
-- Root node gets column labels
if node:get_depth() == 1 then
return {
text = get_header(state, "Type", 10),
highlight = highlights.FILE_STATS_HEADER
highlight = highlights.FILE_STATS_HEADER,
}
end

return {
text = string.format("%10s ", text),
highlight = highlights.FILE_STATS
highlight = highlights.FILE_STATS,
}
end

Expand Down
2 changes: 1 addition & 1 deletion lua/neo-tree/sources/common/container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ local render_content = function(config, node, state, context)
vim.list_extend(zindex_rendered[align], rendered_item)
rendered_width = rendered_width + calc_rendered_width(rendered_item)
end
::continue::
::continue::
end

max_width = math.max(max_width, rendered_width)
Expand Down
18 changes: 10 additions & 8 deletions lua/neo-tree/sources/common/filters/filter_fzy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ function M.has_match(needle, haystack, case_sensitive)
end

local function is_lower(c)
return c:match('%l')
return c:match("%l")
end

local function is_upper(c)
return c:match('%u')
return c:match("%u")
end

local function precompute_bonus(haystack)
local match_bonus = {}

local last_char = '/'
local last_char = "/"
for i = 1, string.len(haystack) do
local this_char = haystack:sub(i, i)
if last_char == '/' or last_char == '\\' then
if last_char == "/" or last_char == "\\" then
match_bonus[i] = SCORE_MATCH_SLASH
elseif last_char == '-' or last_char == '_' or last_char == ' ' then
elseif last_char == "-" or last_char == "_" or last_char == " " then
match_bonus[i] = SCORE_MATCH_WORD
elseif last_char == '.' then
elseif last_char == "." then
match_bonus[i] = SCORE_MATCH_DOT
elseif is_lower(last_char) and is_upper(this_char) then
match_bonus[i] = SCORE_MATCH_CAPITAL
Expand Down Expand Up @@ -200,7 +200,9 @@ function M.score_and_positions(needle, haystack, case_sensitive)
for i = n, 1, -1 do
while j >= 1 do
if D[i][j] ~= SCORE_MIN and (match_required or D[i][j] == T[i][j]) then
match_required = (i ~= 1) and (j ~= 1) and (T[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE)
match_required = (i ~= 1)
and (j ~= 1)
and (T[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE)
positions[i] = j
j = j - 1
break
Expand Down Expand Up @@ -240,7 +242,7 @@ function M.get_score_ceiling()
end

function M.get_implementation_name()
return 'lua'
return "lua"
end

return M
52 changes: 26 additions & 26 deletions lua/neo-tree/sources/common/hijack_cursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ local manager = require("neo-tree.sources.manager")
local M = {}

local hijack_cursor_handler = function()
if vim.o.filetype ~= "neo-tree" then
return
end
local success, source = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_source")
if not success then
log.debug("Cursor hijack failure: " .. vim.inspect(source))
return
end
local winid = nil
local _, position = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_position")
if position == "current" then
winid = vim.api.nvim_get_current_win()
end
if vim.o.filetype ~= "neo-tree" then
return
end
local success, source = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_source")
if not success then
log.debug("Cursor hijack failure: " .. vim.inspect(source))
return
end
local winid = nil
local _, position = pcall(vim.api.nvim_buf_get_var, 0, "neo_tree_position")
if position == "current" then
winid = vim.api.nvim_get_current_win()
end

local state = manager.get_state(source, nil, winid)
if state == nil then
return
end
local node = state.tree:get_node()
log.debug("Cursor moved in tree window, hijacking cursor position")
local cursor = vim.api.nvim_win_get_cursor(0)
local row = cursor[1]
local current_line = vim.api.nvim_get_current_line()
local startIndex, _ = string.find(current_line, node.name, nil, true)
if startIndex then
vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 })
end
local state = manager.get_state(source, nil, winid)
if state == nil then
return
end
local node = state.tree:get_node()
log.debug("Cursor moved in tree window, hijacking cursor position")
local cursor = vim.api.nvim_win_get_cursor(0)
local row = cursor[1]
local current_line = vim.api.nvim_get_current_line()
local startIndex, _ = string.find(current_line, node.name, nil, true)
if startIndex then
vim.api.nvim_win_set_cursor(0, { row, startIndex - 1 })
end
end

--Enables cursor hijack behavior for all sources
Expand Down
62 changes: 31 additions & 31 deletions lua/neo-tree/sources/common/node_expander.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ local log = require("neo-tree.log")
local M = {}

--- Recursively expand all loaded nodes under the given node
--- returns table with all discovered nodes that need to be loaded
--- returns table with all discovered nodes that need to be loaded
---@param node table a node to expand
---@param state table current state of the source
---@return table discovered nodes that need to be loaded
local function expand_loaded(node, state, prefetcher)
local function rec(current_node, to_load)
if prefetcher.should_prefetch(current_node) then
log.trace("Node " .. current_node:get_id() .. "not loaded, saving for later")
table.insert(to_load, current_node)
else
if not current_node:is_expanded() then
current_node:expand()
state.explicitly_opened_directories[current_node:get_id()] = true
end
local children = state.tree:get_nodes(current_node:get_id())
log.debug("Expanding childrens of " .. current_node:get_id())
for _, child in ipairs(children) do
if child.type == "directory" then
rec(child, to_load)
else
log.trace("Child: " .. child.name .. " is not a directory, skipping")
end
local function rec(current_node, to_load)
if prefetcher.should_prefetch(current_node) then
log.trace("Node " .. current_node:get_id() .. "not loaded, saving for later")
table.insert(to_load, current_node)
else
if not current_node:is_expanded() then
current_node:expand()
state.explicitly_opened_directories[current_node:get_id()] = true
end
local children = state.tree:get_nodes(current_node:get_id())
log.debug("Expanding childrens of " .. current_node:get_id())
for _, child in ipairs(children) do
if child.type == "directory" then
rec(child, to_load)
else
log.trace("Child: " .. child.name .. " is not a directory, skipping")
end
end
end
end

local to_load = {}
rec(node, to_load)
return to_load
local to_load = {}
rec(node, to_load)
return to_load
end

--- Recursively expands all nodes under the given node collecting all unloaded nodes
--- Then run prefetcher on all unloaded nodes. Finally, expand loded nodes.
--- Then run prefetcher on all unloaded nodes. Finally, expand loded nodes.
--- async method
---@param node table a node to expand
---@param state table current state of the source
local function expand_and_load(node, state, prefetcher)
local to_load = expand_loaded(node, state, prefetcher)
for _, _node in ipairs(to_load) do
prefetcher.prefetch(state, _node)
-- no need to handle results as prefetch is recursive
expand_loaded(_node, state, prefetcher)
end
local to_load = expand_loaded(node, state, prefetcher)
for _, _node in ipairs(to_load) do
prefetcher.prefetch(state, _node)
-- no need to handle results as prefetch is recursive
expand_loaded(_node, state, prefetcher)
end
end

--- Expands given node recursively loading all descendant nodes if needed
Expand All @@ -71,12 +71,12 @@ M.expand_directory_recursively = function(state, node, prefetcher)
end

M.default_prefetcher = {
prefetch = function (state, node)
prefetch = function(state, node)
log.debug("Default expander prefetch does nothing")
end,
should_prefetch = function (node)
should_prefetch = function(node)
return false
end
end,
}

return M
13 changes: 10 additions & 3 deletions lua/neo-tree/sources/filesystem/lib/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
local a_score = result_scores[a.path]
local b_score = result_scores[b.path]
if a_score == nil or b_score == nil then
log.debug(string.format([[Fzy: failed to compare %s: %s, %s: %s]], a.path, a_score, b.path, b_score))
log.debug(
string.format([[Fzy: failed to compare %s: %s, %s: %s]], a.path, a_score, b.path, b_score)
)
local config = require("neo-tree").config
if config.sort_function ~= nil then
return config.sort_function(a, b)
Expand Down Expand Up @@ -222,9 +224,14 @@ M.show_filter = function(state, search_as_you_type, fuzzy_finder_mode, use_fzy)
for lhs, cmd_name in pairs(config.filesystem.window.fuzzy_finder_mappings) do
local cmd = cmds[cmd_name]
if cmd then
input:map("i", lhs, create_input_mapping_handle(cmd, state, scroll_padding), { noremap = true })
input:map(
"i",
lhs,
create_input_mapping_handle(cmd, state, scroll_padding),
{ noremap = true }
)
else
log.warn(string.format('Invalid command in fuzzy_finder_mappings: %s = %s', lhs, cmd_name))
log.warn(string.format("Invalid command in fuzzy_finder_mappings: %s = %s", lhs, cmd_name))
end
end
end
Expand Down
Loading