Skip to content

Commit

Permalink
perf(nvim-tree): disable sync_root_with_cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
liubang committed Nov 29, 2023
1 parent 4d5e366 commit 5b42232
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lua/lb/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ vim.api.nvim_create_autocmd("BufReadPost", {
end,
}) -- }}}

vim.api.nvim_create_autocmd("QuitPre", {
callback = function()
local tree_wins = {}
local floating_wins = {}
local wins = vim.api.nvim_list_wins()
for _, w in ipairs(wins) do
local bufname = vim.api.nvim_buf_get_name(vim.api.nvim_win_get_buf(w))
if bufname:match("NvimTree_") ~= nil then
table.insert(tree_wins, w)
end
if vim.api.nvim_win_get_config(w).relative ~= "" then
table.insert(floating_wins, w)
end
end
if 1 == #wins - #floating_wins - #tree_wins then
-- Should quit, so we close all invalid windows.
for _, w in ipairs(tree_wins) do
vim.api.nvim_win_close(w, true)
end
end
end,
})

-- vim: fdm=marker fdl=0
28 changes: 26 additions & 2 deletions lua/plugins/nvim_tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ return {
open_on_tab = false,
respect_buf_cwd = false,
sort_by = "name",
sync_root_with_cwd = true,
sync_root_with_cwd = false,
view = {
adaptive_size = false,
centralize_selection = false,
Expand Down Expand Up @@ -60,10 +60,34 @@ return {
nowait = true,
}
end

local git_add = function()
local node = api.tree.get_node_under_cursor()
local gs = node.git_status.file
-- If the current node is a directory get children status
if gs == nil then
gs = (node.git_status.dir.direct ~= nil and node.git_status.dir.direct[1])
or (node.git_status.dir.indirect ~= nil and node.git_status.dir.indirect[1])
end
-- If the file is untracked, unstaged or partially staged, we stage it
if gs == "??" or gs == "MM" or gs == "AM" or gs == " M" then
vim.cmd("silent !git add " .. node.absolute_path)
-- If the file is staged, we unstage
elseif gs == "M " or gs == "A " then
vim.cmd("silent !git restore --staged " .. node.absolute_path)
end
api.tree.reload()
end

-- default mappings
api.config.mappings.default_on_attach(bufnr)
vim.keymap.set("n", "v", api.node.open.vertical, opts("Open: Vertical Split"))
vim.keymap.set("n", "s", api.node.open.horizontal, opts("Open: Horizontal Split"))
vim.keymap.set("n", "ga", git_add, opts("Git Add"))

api.events.subscribe(api.events.Event.FileCreated, function(file)
vim.cmd("edit " .. file.fname)
end)
end,
renderer = {
add_trailing = false,
Expand All @@ -87,10 +111,10 @@ return {
webdev_colors = true,
git_placement = "after",
show = {
git = true,
file = true,
folder = true,
folder_arrow = true,
git = true,
},
padding = " ",
symlink_arrow = " 󰁔 ",
Expand Down

0 comments on commit 5b42232

Please sign in to comment.