From 98bc6f9a151430d49fc4bfd7096cef78b6abdc7c Mon Sep 17 00:00:00 2001 From: srdusr Date: Wed, 16 Aug 2023 23:58:03 +0200 Subject: Various changes to nvim-tree and improve layout of config --- lua/plugins/nvim-tree.lua | 325 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 243 insertions(+), 82 deletions(-) (limited to 'lua/plugins') diff --git a/lua/plugins/nvim-tree.lua b/lua/plugins/nvim-tree.lua index e2298b5..31c77a3 100644 --- a/lua/plugins/nvim-tree.lua +++ b/lua/plugins/nvim-tree.lua @@ -1,104 +1,268 @@ -local status_ok, nvim_tree = pcall(require, "nvim-tree") -if not status_ok then - return -end +----------------------------------------------------------- +-- Neovim File Tree Configuration +----------------------------------------------------------- -local config_status_ok, nvim_tree_config = pcall(require, "nvim-tree.config") -if not config_status_ok then - return -end +--- To see mappings `g?` on nvim-tree +--- To see default mappings `:nvim-tree-default-mappings` -local tree_cb = nvim_tree_config.nvim_tree_callback +local icons = { + git_placement = "after", + modified_placement = "after", + padding = " ", + show = { + file = true, + folder = true, + folder_arrow = true, + git = true, + modified = true, + }, --- To see mappings `g?` on nvim-tree --- To see default mappings `:nvim-tree-default-mappings` + glyphs = { + default = "󰈔", + symlink = "", + folder = { + arrow_open = "", + arrow_closed = "", + default = " ", + open = " ", + empty = " ", + empty_open = " ", + symlink = "", + symlink_open = "", + }, + git = { + deleted = "", + unmerged = "", + untracked = "", + unstaged = "", + staged = "", + renamed = "➜", + ignored = "◌", + }, + }, +} -nvim_tree.setup({ - update_focused_file = { +local renderer = { + indent_width = 2, + indent_markers = { enable = true, - update_cwd = true, - }, - git = { - enable = true, - ignore = false, - timeout = 500, -}, - renderer = { - root_folder_modifier = ":t", - indent_markers = { - enable = true, - }, + inline_arrows = true, icons = { - glyphs = { - default = "", - symlink = "", - folder = { - arrow_open = "", - arrow_closed = "", - default = "", - open = "", - empty = "", - empty_open = "", - symlink = "", - symlink_open = "", - }, - git = { - unstaged = "", - staged = "S", - unmerged = "", - renamed = "➜", - untracked = "U", - deleted = "", - ignored = "◌", - }, - }, + corner = "└", + edge = "│", + item = "│", + bottom = "─", + none = " ", }, }, + icons = icons, +} + +local system_open = { cmd = "zathura" } + +local HEIGHT_RATIO = 0.8 +local WIDTH_RATIO = 0.15 + +local float = { + enable = false, + open_win_config = function() + local screen_w = vim.opt.columns:get() + local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get() + local window_w = screen_w * WIDTH_RATIO + local window_h = screen_h * HEIGHT_RATIO + local window_w_int = math.floor(window_w) + local window_h_int = math.floor(window_h) + local center_x = (screen_w - window_w) / 2 + local center_y = ((vim.opt.lines:get() - window_h) / 2) - vim.opt.cmdheight:get() + return { + border = "rounded", + relative = "editor", + row = center_y, + col = center_x, + width = window_w_int, + height = window_h_int, + } + end, +} + +local view = { + cursorline = false, + hide_root_folder = false, + float = float, + signcolumn = "no", + width = function() + return math.floor(vim.opt.columns:get() * WIDTH_RATIO) + end, + side = "left", +} + +local api = require("nvim-tree.api") +local function on_attach(bufnr) + local function opts(desc) + return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } + end + + local mappings = { + [""] = { api.tree.change_root_to_node, "CD" }, + [""] = { api.node.open.replace_tree_buffer, "Open: In Place" }, + [""] = { api.node.show_info_popup, "Info" }, + [""] = { api.fs.rename_sub, "Rename: Omit Filename" }, + [""] = { api.node.open.tab, "Open: New Tab" }, + [""] = { api.node.open.vertical, "Open: Vertical Split" }, + [""] = { api.node.open.horizontal, "Open: Horizontal Split" }, + [""] = { api.node.navigate.parent_close, "Close Directory" }, + -- [""] = { api.node.open.edit, "Open" }, + [""] = { api.node.open.preview, "Open Preview" }, + [">"] = { api.node.navigate.sibling.next, "Next Sibling" }, + ["<"] = { api.node.navigate.sibling.prev, "Previous Sibling" }, + ["."] = { api.node.run.cmd, "Run Command" }, + ["-"] = { api.tree.change_root_to_parent, "Up" }, + ["a"] = { api.fs.create, "Create" }, + ["bmv"] = { api.marks.bulk.move, "Move Bookmarked" }, + ["B"] = { api.tree.toggle_no_buffer_filter, "Toggle No Buffer" }, + ["c"] = { api.fs.copy.node, "Copy" }, + -- ["C"] = { api.tree.toggle_git_clean_filter, "Toggle Git Clean" }, + ["[c"] = { api.node.navigate.git.prev, "Prev Git" }, + ["]c"] = { api.node.navigate.git.next, "Next Git" }, + ["d"] = { api.fs.remove, "Delete" }, + ["D"] = { api.fs.trash, "Trash" }, + ["E"] = { api.tree.expand_all, "Expand All" }, + ["e"] = { api.fs.rename_basename, "Rename: Basename" }, + ["]e"] = { api.node.navigate.diagnostics.next, "Next Diagnostic" }, + ["[e"] = { api.node.navigate.diagnostics.prev, "Prev Diagnostic" }, + ["F"] = { api.live_filter.clear, "Clean Filter" }, + ["f"] = { api.live_filter.start, "Filter" }, + ["g?"] = { api.tree.toggle_help, "Help" }, + ["gy"] = { api.fs.copy.absolute_path, "Copy Absolute Path" }, + ["H"] = { api.tree.toggle_hidden_filter, "Toggle Dotfiles" }, + ["I"] = { api.tree.toggle_gitignore_filter, "Toggle Git Ignore" }, + ["J"] = { api.node.navigate.sibling.last, "Last Sibling" }, + ["K"] = { api.node.navigate.sibling.first, "First Sibling" }, + ["m"] = { api.marks.toggle, "Toggle Bookmark" }, + -- ["o"] = { api.node.open.edit, "Open" }, + ["O"] = { api.node.open.no_window_picker, "Open: No Window Picker" }, + ["p"] = { api.fs.paste, "Paste" }, + ["P"] = { api.node.navigate.parent, "Parent Directory" }, + ["q"] = { api.tree.close, "Close" }, + ["r"] = { api.fs.rename, "Rename" }, + ["R"] = { api.tree.reload, "Refresh" }, + ["s"] = { api.node.run.system, "Run System" }, + ["S"] = { api.tree.search_node, "Search" }, + ["U"] = { api.tree.toggle_custom_filter, "Toggle Hidden" }, + ["W"] = { api.tree.collapse_all, "Collapse" }, + ["x"] = { api.fs.cut, "Cut" }, + ["y"] = { api.fs.copy.filename, "Copy Name" }, + ["Y"] = { api.fs.copy.relative_path, "Copy Relative Path" }, + ["<2-LeftMouse>"] = { api.node.open.edit, "Open" }, + ["<2-RightMouse>"] = { api.tree.change_root_to_node, "CD" }, + + -- Mappings migrated from view.mappings.list + ["l"] = { api.node.open.edit, "Open" }, + [""] = { api.node.open.edit, "Open" }, + ["o"] = { api.node.open.edit, "Open" }, + ["h"] = { api.node.navigate.parent_close, "Close Directory" }, + ["v"] = { api.node.open.vertical, "Open: Vertical Split" }, + ["C"] = { api.tree.change_root_to_node, "CD" }, + } + for keys, mapping in pairs(mappings) do + vim.keymap.set("n", keys, mapping[1], opts(mapping[2])) + end +end +api.events.subscribe(api.events.Event.FileCreated, function(file) + vim.cmd("edit" .. file.fname) +end) + +require("nvim-tree").setup({ + disable_netrw = true, + hijack_netrw = true, + auto_reload_on_write = true, + update_focused_file = { + enable = true, + update_cwd = false, + update_root = false, + ignore_list = {}, + }, + root_dirs = {}, + prefer_startup_root = true, + hijack_cursor = true, + --hijack_directories = { + -- enable = false, + --}, + --respect_buf_cwd = false, + sync_root_with_cwd = true, + --reload_on_bufenter = false, + view = view, + system_open = system_open, + renderer = renderer, + on_attach = on_attach, + notify = { + threshold = vim.log.levels.ERROR, + }, + git = { ignore = false }, diagnostics = { enable = true, show_on_dirs = true, icons = { - hint = "", - info = "", - warning = "", + hint = "⚑", + info = "􀅳", + warning = "▲", error = "", }, }, - view = { - width = 30, - --height = 30, - side = "left", - --mappings = { - -- list = { - -- { key = { "l", "", "o" }, cb = tree_cb("edit") }, - -- { key = "h", cb = tree_cb("close_node") }, - -- { key = "v", cb = tree_cb("vsplit") }, - -- { key = "u", action = "dir_up" }, - -- }, - --}, - }, trash = { cmd = "gio trash", require_confirm = true, }, + modified = { + enable = false, + show_on_dirs = true, + show_on_open_dirs = true, + }, + actions = { + use_system_clipboard = true, + change_dir = { + enable = true, + global = false, + restrict_above_cwd = false, + }, + remove_file = { + close_window = true, + }, + open_file = { + quit_on_open = true, + resize_window = false, + window_picker = { + enable = true, + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", + exclude = { + filetype = { "notify", "packer", "qf", "diff", "fugitive", "fugitiveblame" }, + buftype = { "nofile", "terminal", "help" }, + }, + }, + }, + }, }) -vim.api.nvim_create_autocmd("QuitPre", { - callback = function() - local invalid_win = {} - 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(invalid_win, w) - end - end - if #invalid_win == #wins - 1 then - -- Should quit, so we close all invalid windows. - for _, w in ipairs(invalid_win) do vim.api.nvim_win_close(w, true) end - end +local api = require("nvim-tree.api") +local event = api.events.Event +api.events.subscribe(event.TreeOpen, function(_) + vim.cmd([[setlocal statuscolumn=\ ]]) + vim.cmd([[setlocal cursorlineopt=number]]) + vim.cmd([[setlocal fillchars+=vert:🮇]]) + vim.cmd([[setlocal fillchars+=horizup:🮇]]) + vim.cmd([[setlocal fillchars+=vertright:🮇]]) +end) + +local function open_nvim_tree(data) + vim.cmd.cd(data.file:match("(.+)/[^/]*$")) + local directory = vim.fn.isdirectory(data.file) == 1 + if not directory then + return end -}) + require("nvim-tree.api").tree.open() +end +vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) + -- Highlight Groups vim.api.nvim_command("highlight NvimTreeNormal guibg=none") --vim.api.nvim_command("highlight NvimTreeSymlink ") @@ -137,7 +301,6 @@ vim.api.nvim_command("highlight NvimTreeNormal guibg=none") --vim.api.nvim_command("highlight NvimTreeWinSeparator ") --(WinSeparator) --vim.api.nvim_command("highlight NvimTreeCursorColumn ") --(CursorColumn) - --vim.api.nvim_command("highlight NvimTreeFileDirty ") --(NvimTreeGitDirty) --vim.api.nvim_command("highlight NvimTreeFileStaged ") --(NvimTreeGitStaged) --vim.api.nvim_command("highlight NvimTreeFileMerge ") --(NvimTreeGitMerge) @@ -146,9 +309,7 @@ vim.api.nvim_command("highlight NvimTreeNormal guibg=none") --vim.api.nvim_command("highlight NvimTreeFileDeleted ") --(NvimTreeGitDeleted) --vim.api.nvim_command("highlight NvimTreeFileIgnored ") --(NvimTreeGitIgnored) - --vim.api.nvim_command("highlight NvimTreeLiveFilterPrefix ") --vim.api.nvim_command("highlight NvimTreeLiveFilterValue ") - --vim.api.nvim_command("highlight NvimTreeBookmark ") -- cgit v1.2.3