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

fix(setup): defer merging config until needed #1658

Merged
merged 6 commits into from
Jan 26, 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
26 changes: 13 additions & 13 deletions lua/neo-tree.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local vim = vim
local utils = require("neo-tree.utils")
local log = require("neo-tree.log")
local M = {}

-- DEPRECATED: to be removed in a future release, use this instead:
Expand All @@ -11,13 +9,18 @@ M.close_all = function()
require("neo-tree.command").execute({ action = "close" })
end

local new_user_config = nil

---Updates the config of neo-tree using the latest user config passed through setup, if any.
M.ensure_config = function()
if not M.config then
M.setup({ log_to_file = false }, true)
if not M.config or new_user_config then
M.config = require("neo-tree.setup").merge_config(new_user_config)
new_user_config = nil
end
end

M.get_prior_window = function(ignore_filetypes, ignore_winfixbuf)
local utils = require("neo-tree.utils")
ignore_filetypes = ignore_filetypes or {}
local ignore = utils.list_to_dict(ignore_filetypes)
ignore["neo-tree"] = true
Expand Down Expand Up @@ -47,6 +50,7 @@ M.get_prior_window = function(ignore_filetypes, ignore_winfixbuf)
end

M.paste_default_config = function()
local utils = require("neo-tree.utils")
local base_path = debug.getinfo(utils.truthy).source:match("@(.*)/utils/init.lua$")
local config_path = base_path .. utils.path_separator .. "defaults.lua"
local lines = vim.fn.readfile(config_path)
Expand All @@ -70,20 +74,16 @@ M.paste_default_config = function()
end

M.set_log_level = function(level)
log.set_level(level)
require("neo-tree.log").set_level(level)
end

M.setup = function(config, is_auto_config)
M.config = require("neo-tree.setup").merge_config(config, is_auto_config)
local netrw = require("neo-tree.setup.netrw")
if not is_auto_config and netrw.get_hijack_netrw_behavior() ~= "disabled" then
vim.cmd("silent! autocmd! FileExplorer *")
netrw.hijack()
end
M.setup = function(config)
-- merging is deferred until ensure_config
new_user_config = config
end

M.show_logs = function()
vim.cmd("tabnew " .. log.outfile)
vim.cmd("tabnew " .. require("neo-tree.log").outfile)
end

return M
4 changes: 2 additions & 2 deletions lua/neo-tree/command/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ M.complete_args = function(argLead, cmdLine)
return get_ref_completions(key .. "=")
elseif arg_type == parser.LIST then
local valid_values = parser.arguments[key].values
if valid_values and not parsed[key] then
if valid_values and not (parsed[key] and #parsed[key] > 0) then
for _, vv in ipairs(valid_values) do
if vv:find(value) then
if vv:find(value, 1, true) then
table.insert(candidates, key .. "=" .. vv)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/neo-tree/setup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ local merge_renderers = function(default_config, source_default_config, user_con
end
end

M.merge_config = function(user_config, is_auto_config)
M.merge_config = function(user_config)
local default_config = vim.deepcopy(defaults)
user_config = vim.deepcopy(user_config or {})

Expand Down
16 changes: 8 additions & 8 deletions lua/neo-tree/setup/netrw.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
local nt = require("neo-tree")
local utils = require("neo-tree.utils")
local log = require("neo-tree.log")
local manager = require("neo-tree.sources.manager")
local command = require("neo-tree.command")
local M = {}

local get_position = function(source_name)
local nt = require("neo-tree")
local pos = utils.get_value(nt.config, source_name .. ".window.position", "left", true)
return pos
end

M.get_hijack_netrw_behavior = function()
local nt = require("neo-tree")
M.get_hijack_behavior = function()
nt.ensure_config()
local option = "filesystem.hijack_netrw_behavior"
local hijack_behavior = utils.get_value(nt.config, option, "open_default", true)
if hijack_behavior == "disabled" then
Expand All @@ -21,13 +18,16 @@ M.get_hijack_netrw_behavior = function()
elseif hijack_behavior == "open_current" then
return hijack_behavior
else
log.error("Invalid value for " .. option .. ": " .. hijack_behavior)
require("neo-tree.log").error("Invalid value for " .. option .. ": " .. hijack_behavior)
return "disabled"
end
end

---@return boolean hijacked Whether the hijack was successful
M.hijack = function()
local hijack_behavior = M.get_hijack_netrw_behavior()
local manager = require("neo-tree.sources.manager")
local log = require("neo-tree.log")
local hijack_behavior = M.get_hijack_behavior()
if hijack_behavior == "disabled" then
return false
end
Expand Down
4 changes: 1 addition & 3 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local vim = vim
local log = require("neo-tree.log")
local filesize = require("neo-tree.utils.filesize.filesize")
local bit = require("bit")
local ffi_available, ffi = pcall(require, "ffi")

Expand Down Expand Up @@ -215,7 +213,7 @@ end
---@param size any
---@return string
M.human_size = function(size)
local human = filesize(size, { output = "string" })
local human = require("neo-tree.utils.filesize.filesize")(size, { output = "string" })
---@cast human string
return human
end
Expand Down
46 changes: 46 additions & 0 deletions plugin/neo-tree.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if vim.g.loaded_neo_tree == 1 or vim.g.loaded_neo_tree == true then
return
end

-- Possibly convert this to lua using customlist instead of custom in the future?
vim.api.nvim_create_user_command("Neotree", function(ctx)
require("neo-tree.command")._command(unpack(ctx.fargs))
end, {
nargs = "*",
complete = "custom,v:lua.require'neo-tree.command'.complete_args",
})

---@param path string? The path to check
---@return boolean hijacked Whether the hijack worked
local function try_netrw_hijack(path)
if not path or #path == 0 then
return false
end

local netrw = require("neo-tree.setup.netrw")
if netrw.get_hijack_behavior() ~= "disabled" then
vim.cmd("silent! autocmd! FileExplorer *")
local stats = (vim.uv or vim.loop).fs_stat(path)
if stats and stats.type == "directory" then
return netrw.hijack()
end
end
return false
end

-- currently need to check first arg to not break hijacked on
-- configs that already lazy-load neo-tree (e.g. lazyvim)
local first_arg = vim.fn.argv(0) --[[@as string]]
if not try_netrw_hijack(first_arg) then
local augroup = vim.api.nvim_create_augroup("NeoTree_NetrwDeferred", { clear = true })
vim.api.nvim_create_autocmd("BufEnter", {
group = augroup,
callback = function(args)
if try_netrw_hijack(args.file) then
vim.api.nvim_del_augroup_by_id(augroup)
end
end,
})
end

vim.g.loaded_neo_tree = 1
8 changes: 0 additions & 8 deletions plugin/neo-tree.vim

This file was deleted.

6 changes: 2 additions & 4 deletions tests/mininit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ vim.cmd([[
vim.opt.swapfile = false

vim.cmd([[
runtime plugin/neo-tree.vim
runtime plugin/neo-tree.lua
]])

-- For debugging
P = function(...)
print(unpack(vim.tbl_map(vim.inspect, { ... })))
end
P = vim.print
4 changes: 2 additions & 2 deletions tests/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ end
---@param linenr_end? integer (1-indexed, inclusive)
function mod.assert_buf_lines(bufnr, lines, linenr_start, linenr_end)
mod.eq(
lines,
vim.api.nvim_buf_get_lines(
bufnr,
linenr_start and linenr_start - 1 or 0,
linenr_end or -1,
false
),
lines
)
)
end

Expand Down
Loading