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 <eph@MacBook-Pro.local>
Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Epheien 2024-07-07 10:51:43 +08:00 committed by GitHub
parent 74e94625b1
commit 869c064721
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 3 deletions

View file

@ -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|

View file

@ -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,

View file

@ -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