From bb9b7915a6f49d3d7f6f5e3f3999a53d76cf0a09 Mon Sep 17 00:00:00 2001 From: Super_Z <1401203083@qq.com> Date: Thu, 16 Jul 2026 13:26:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20nvim-tree=20?= =?UTF-8?q?=E4=BE=A7=E8=BE=B9=E6=96=87=E4=BB=B6=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 filetree.lua:nvim-tree 常驻侧边栏 - e 开关侧边文件树(VSCode 风格) - oil.nvim 保留,用 - 键快速编辑目录 - bufferline 添加 NvimTree 偏移适配 - which-key 添加 e 分组 --- lua/plugins/filetree.lua | 68 ++++++++++++++++++++++++++++++++++++++++ lua/plugins/ui.lua | 8 ++++- lua/plugins/whichkey.lua | 1 + 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lua/plugins/filetree.lua diff --git a/lua/plugins/filetree.lua b/lua/plugins/filetree.lua new file mode 100644 index 0000000..5f628b5 --- /dev/null +++ b/lua/plugins/filetree.lua @@ -0,0 +1,68 @@ +-- ============================================================================= +-- filetree.lua — 侧边文件树 +-- ============================================================================= +-- 【新增文件】nvim-tree 作为常驻侧边栏(VSCode 风格),oil.nvim 保留用于快速编辑。 +-- 两者共存:nvim-tree 看结构,oil 改文件。 +-- 键位:e 开关侧边栏 + +return { + { + "nvim-tree/nvim-tree.lua", + version = "*", + lazy = false, -- 立即加载,确保打开就有侧边栏 + dependencies = { "nvim-tree/nvim-web-devicons" }, + config = function() + -- 禁用 netrw(避免和 oil 冲突,oil 自己也会禁一次,这里双保险) + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + + require("nvim-tree").setup({ + sort_by = "case_sensitive", + view = { + width = 30, + side = "left", + relativenumber = false, + -- ✅ 关闭文件树时自动调整窗口大小 + adaptive_size = false, + }, + renderer = { + group_empty = true, + icons = { + show = { + file = true, + folder = true, + folder_arrow = true, + git = true, + }, + }, + }, + filters = { + dotfiles = false, + custom = { "^.git$" }, + }, + git = { + enable = true, + ignore = false, + }, + actions = { + open_file = { + quit_on_open = false, -- 打开文件不关树 + window_picker = { + enable = true, + }, + }, + }, + -- ✅ 打开 nvim 时自动打开文件树 + auto_open_on_setup = false, -- 不自动开,用 e 手动开 + on_attach = function(bufnr) + local api = require("nvim-tree.api") + api.config.mappings.default_on_attach(bufnr) + end, + }) + + -- e 开关侧边文件树 + vim.keymap.set("n", "e", "NvimTreeToggle", + { desc = "侧边文件树", noremap = true, silent = true }) + end, + }, +} diff --git a/lua/plugins/ui.lua b/lua/plugins/ui.lua index 3b333a6..3b59984 100644 --- a/lua/plugins/ui.lua +++ b/lua/plugins/ui.lua @@ -43,7 +43,7 @@ return { tab_size = 18, diagnostics = "nvim_lsp", diagnostics_update_in_insert = false, - -- ✅ 适配 oil.nvim:左侧文件浏览器偏移 + -- ✅ 适配 oil.nvim 和 nvim-tree:左侧文件浏览器偏移 offsets = { { filetype = "oil", @@ -51,6 +51,12 @@ return { highlight = "Directory", text_align = "left", }, + { + filetype = "NvimTree", + text = "文件树", + highlight = "Directory", + text_align = "left", + }, }, color_icons = true, show_buffer_icons = true, diff --git a/lua/plugins/whichkey.lua b/lua/plugins/whichkey.lua index 5c335e4..7d51faa 100644 --- a/lua/plugins/whichkey.lua +++ b/lua/plugins/whichkey.lua @@ -41,6 +41,7 @@ return { -- ✅ 新增:键位分组提示 wk.add({ + { "e", group = "文件树" }, { "f", group = "搜索" }, { "g", group = "Git" }, { "h", group = "Hunk 操作" },