fix(neo-tree): correctly set up cwd (#3097)

* fix(neo-tree): correctly set up `cwd`

* refactor: cleanup

---------

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Iordanis Petkakis
2024-05-12 11:08:55 +03:00
committed by GitHub
parent f7450a5236
commit 30ce84f7a7
2 changed files with 21 additions and 7 deletions

View File

@ -41,12 +41,23 @@ return {
vim.cmd([[Neotree close]]) vim.cmd([[Neotree close]])
end, end,
init = function() init = function()
if vim.fn.argc(-1) == 1 then -- FIX: use `autocmd` for lazy-loading neo-tree instead of directly requiring it,
local stat = vim.uv.fs_stat(vim.fn.argv(0)) -- because `cwd` is not set up properly.
if stat and stat.type == "directory" then vim.api.nvim_create_autocmd("BufEnter", {
require("neo-tree") group = vim.api.nvim_create_augroup("Neotree_start_directory", { clear = true }),
end desc = "Start Neo-tree with directory",
end once = true,
callback = function()
if package.loaded["neo-tree"] then
return
else
local stats = vim.uv.fs_stat(vim.fn.argv(0))
if stats and stats.type == "directory" then
require("neo-tree")
end
end
end,
})
end, end,
opts = { opts = {
sources = { "filesystem", "buffers", "git_status", "document_symbols" }, sources = { "filesystem", "buffers", "git_status", "document_symbols" },

View File

@ -142,7 +142,10 @@ function M.setup()
LazyVim.root.info() LazyVim.root.info()
end, { desc = "LazyVim roots for the current buffer" }) end, { desc = "LazyVim roots for the current buffer" })
vim.api.nvim_create_autocmd({ "LspAttach", "BufWritePost", "DirChanged" }, { -- FIX: doesn't properly clear cache in neo-tree `set_root` (which should happen presumably on `DirChanged`),
-- probably because the event is triggered in the neo-tree buffer, therefore add `BufEnter`
-- Maybe this is too frequent on `BufEnter` and something else should be done instead??
vim.api.nvim_create_autocmd({ "LspAttach", "BufWritePost", "DirChanged", "BufEnter" }, {
group = vim.api.nvim_create_augroup("lazyvim_root_cache", { clear = true }), group = vim.api.nvim_create_augroup("lazyvim_root_cache", { clear = true }),
callback = function(event) callback = function(event)
M.cache[event.buf] = nil M.cache[event.buf] = nil