From 869c064721a6c2091f22c3541e8f0ff958361771 Mon Sep 17 00:00:00 2001 From: Epheien Date: Sun, 7 Jul 2024 10:51:43 +0800 Subject: [PATCH] feat(#2127): add experimental.actions.open_file.relative_path to open files with a relative path rather than absolute (#2805) * temp workaround for issue #2803 * fix #2127 and #2803 * chore(#2127): read the configuration correctly * feat(#2127): add help * feat(#2127): normalise relative_path in config hierarchy * feat(#2127): update help --------- Co-authored-by: eph Co-authored-by: Alexander Courtis --- doc/nvim-tree-lua.txt | 15 ++++++++++++++- lua/nvim-tree.lua | 8 +++++++- lua/nvim-tree/actions/node/open-file.lua | 20 +++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 99316b54..e1649208 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -620,7 +620,13 @@ Following is the default configuration. See |nvim-tree-opts| for details. default_yes = false, }, }, - experimental = {}, + experimental = { + actions = { + open_file = { + relative_path = false, + }, + }, + }, log = { enable = false, truncate = false, @@ -1539,6 +1545,12 @@ Confirmation prompts. Experimental features that may become default or optional functionality. In the event of a problem please disable the experiment and raise an issue. + *nvim-tree.experimental.actions.open_file.relative_path* + Buffers opened by nvim-tree will use with relative paths instead of + absolute. + Execute |:ls| to see the paths of all open buffers. + Type: `boolean`, Default: `false` + ============================================================================== 5.20 OPTS: LOG *nvim-tree-opts-log* @@ -2774,6 +2786,7 @@ highlight group is not, hard linking as follows: > |nvim-tree.diagnostics.show_on_open_dirs| |nvim-tree.disable_netrw| |nvim-tree.experimental| +|nvim-tree.experimental.actions.open_file.relative_path| |nvim-tree.filesystem_watchers.debounce_delay| |nvim-tree.filesystem_watchers.enable| |nvim-tree.filesystem_watchers.ignore_dirs| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 1b295da9..343196a0 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -584,7 +584,13 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS default_yes = false, }, }, - experimental = {}, + experimental = { + actions = { + open_file = { + relative_path = false, + }, + }, + }, log = { enable = false, truncate = false, diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 39026253..13010610 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -190,6 +190,9 @@ local function open_file_in_tab(filename) if M.quit_on_open then view.close() end + if M.relative_path then + filename = utils.path_relative(filename, vim.fn.getcwd()) + end vim.cmd("tabe " .. vim.fn.fnameescape(filename)) end @@ -197,6 +200,9 @@ local function drop(filename) if M.quit_on_open then view.close() end + if M.relative_path then + filename = utils.path_relative(filename, vim.fn.getcwd()) + end vim.cmd("drop " .. vim.fn.fnameescape(filename)) end @@ -204,6 +210,9 @@ local function tab_drop(filename) if M.quit_on_open then view.close() end + if M.relative_path then + filename = utils.path_relative(filename, vim.fn.getcwd()) + end vim.cmd("tab :drop " .. vim.fn.fnameescape(filename)) end @@ -310,7 +319,12 @@ local function open_in_new_window(filename, mode) end end - local fname = utils.escape_special_chars(vim.fn.fnameescape(filename)) + local fname + if M.relative_path then + fname = utils.escape_special_chars(vim.fn.fnameescape(utils.path_relative(filename, vim.fn.getcwd()))) + else + fname = utils.escape_special_chars(vim.fn.fnameescape(filename)) + end local command if create_new_window then @@ -346,6 +360,9 @@ end local function edit_in_current_buf(filename) require("nvim-tree.view").abandon_current_window() + if M.relative_path then + filename = utils.path_relative(filename, vim.fn.getcwd()) + end vim.cmd("keepalt keepjumps edit " .. vim.fn.fnameescape(filename)) end @@ -402,6 +419,7 @@ end function M.setup(opts) M.quit_on_open = opts.actions.open_file.quit_on_open M.resize_window = opts.actions.open_file.resize_window + M.relative_path = opts.experimental.actions.open_file.relative_path if opts.actions.open_file.window_picker.chars then opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper() end