feat: add NvimTreeFindFile!, root_dirs and prefer_startup_root

This commit is contained in:
lixvbnet 2022-06-18 13:32:56 +08:00 committed by GitHub
parent 84c2bd77ff
commit b08003f546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 20 deletions

View File

@ -165,6 +165,8 @@ Setup may only be run once; subsequent calls will result in a warning.
open_on_setup_file = false, open_on_setup_file = false,
open_on_tab = false, open_on_tab = false,
sort_by = "name", sort_by = "name",
root_dirs = {},
prefer_startup_root = false,
update_cwd = false, update_cwd = false,
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
@ -246,6 +248,7 @@ Setup may only be run once; subsequent calls will result in a warning.
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_cwd = false, update_cwd = false,
update_root = false,
ignore_list = {}, ignore_list = {},
}, },
ignore_ft_on_setup = {}, ignore_ft_on_setup = {},
@ -385,6 +388,16 @@ Opens in place of the unnamed buffer if it's empty.
Keeps the cursor on the first letter of the filename when moving in the tree. Keeps the cursor on the first letter of the filename when moving in the tree.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
*nvim-tree.root_dirs*
Preferred root directories.
Only relevant when `update_focused_file.update_root` is `true`
Type: `{string}`, Default: `{}`
*nvim-tree.prefer_startup_root*
Prefer startup root directory when updating root directory of the tree.
Only relevant when `update_focused_file.update_root` is `true`
Type: `boolean`, Default: `false`
*nvim-tree.update_cwd* *nvim-tree.update_cwd*
Changes the tree root directory on `DirChanged` and refreshes the tree. Changes the tree root directory on `DirChanged` and refreshes the tree.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
@ -419,11 +432,19 @@ until it finds the file.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
*nvim-tree.update_focused_file.update_cwd* *nvim-tree.update_focused_file.update_cwd*
(deprecated, use `update_focused_file.update_root`)
Update the root directory of the tree to the one of the folder containing Update the root directory of the tree to the one of the folder containing
the file if the file is not under the current root directory. the file if the file is not under the current root directory.
Only relevant when `update_focused_file.enable` is `true` Only relevant when `update_focused_file.enable` is `true`
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
*nvim-tree.update_focused_file.update_root*
Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file.
Only relevant when `update_focused_file.enable` is `true`
Type: `boolean`, Default: `false`
*nvim-tree.update_focused_file.ignore_list* *nvim-tree.update_focused_file.ignore_list*
List of buffer names and filetypes that will not update the root dir List of buffer names and filetypes that will not update the root dir
of the tree if the file isn't found under the current root directory. of the tree if the file isn't found under the current root directory.

View File

@ -18,6 +18,7 @@ local _config = {}
local M = { local M = {
setup_called = false, setup_called = false,
init_root = "",
} }
function M.focus() function M.focus()
@ -25,6 +26,47 @@ function M.focus()
view.focus() view.focus()
end end
function M.change_root(filepath, bufnr)
-- skip if current file is in ignore_list
local ft = api.nvim_buf_get_option(bufnr, "filetype") or ""
for _, value in pairs(_config.update_focused_file.ignore_list) do
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
return
end
end
local cwd = core.get_cwd()
local vim_cwd = vim.fn.getcwd()
-- test if in vim_cwd
if utils.path_relative(filepath, vim_cwd) ~= filepath then
if vim_cwd ~= cwd then
lib.open(vim_cwd)
end
return
end
-- test if in cwd
if utils.path_relative(filepath, cwd) ~= filepath then
return
end
-- otherwise test M.init_root
if _config.prefer_startup_root and utils.path_relative(filepath, M.init_root) ~= filepath then
lib.open(M.init_root)
return
end
-- otherwise root_dirs
for _, dir in pairs(_config.root_dirs) do
dir = vim.fn.fnamemodify(dir, ":p")
if utils.path_relative(filepath, dir) ~= filepath then
lib.open(dir)
return
end
end
-- finally fall back to the folder containing the file
lib.open(vim.fn.fnamemodify(filepath, ":p:h"))
end
---@deprecated ---@deprecated
M.on_keypress = require("nvim-tree.actions").on_keypress M.on_keypress = require("nvim-tree.actions").on_keypress
@ -96,20 +138,7 @@ local function is_file_readable(fname)
return stat and stat.type == "file" and luv.fs_access(fname, "R") return stat and stat.type == "file" and luv.fs_access(fname, "R")
end end
local function update_base_dir_with_filepath(filepath, bufnr) function M.find_file(with_open, bufnr, bang)
local ft = api.nvim_buf_get_option(bufnr, "filetype") or ""
for _, value in pairs(_config.update_focused_file.ignore_list) do
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
return
end
end
if not vim.startswith(filepath, core.get_cwd()) then
change_dir.fn(vim.fn.fnamemodify(filepath, ":p:h"))
end
end
function M.find_file(with_open, bufnr)
if not with_open and not core.get_explorer() then if not with_open and not core.get_explorer() then
return return
end end
@ -125,10 +154,10 @@ function M.find_file(with_open, bufnr)
M.open() M.open()
end end
-- if we don't schedule, it will search for NvimTree
vim.schedule(function() vim.schedule(function()
-- if we don't schedule, it will search for NvimTree if bang or _config.update_focused_file.update_cwd or _config.update_focused_file.update_root then
if _config.update_focused_file.update_cwd then M.change_root(filepath, bufnr)
update_base_dir_with_filepath(filepath, bufnr)
end end
require("nvim-tree.actions.find-file").fn(filepath) require("nvim-tree.actions.find-file").fn(filepath)
end) end)
@ -267,9 +296,9 @@ local function setup_vim_commands()
api.nvim_create_user_command("NvimTreeFocus", M.focus, {}) api.nvim_create_user_command("NvimTreeFocus", M.focus, {})
api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {}) api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {})
api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {}) api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {})
api.nvim_create_user_command("NvimTreeFindFile", function() api.nvim_create_user_command("NvimTreeFindFile", function(res)
M.find_file(true) M.find_file(true, nil, res.bang)
end, {}) end, { bang = true })
api.nvim_create_user_command("NvimTreeFindFileToggle", function(res) api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
M.toggle(true, false, res.args) M.toggle(true, false, res.args)
end, { nargs = "?", complete = "dir" }) end, { nargs = "?", complete = "dir" })
@ -373,6 +402,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
open_on_setup_file = false, open_on_setup_file = false,
open_on_tab = false, open_on_tab = false,
sort_by = "name", sort_by = "name",
root_dirs = {},
prefer_startup_root = false,
update_cwd = false, update_cwd = false,
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
@ -454,6 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_cwd = false, update_cwd = false,
update_root = false,
ignore_list = {}, ignore_list = {},
}, },
ignore_ft_on_setup = {}, ignore_ft_on_setup = {},
@ -594,6 +626,7 @@ function M.setup(conf)
return return
end end
M.setup_called = true M.setup_called = true
M.init_root = vim.fn.getcwd()
legacy.migrate_legacy_options(conf or {}) legacy.migrate_legacy_options(conf or {})
@ -602,6 +635,8 @@ function M.setup(conf)
local opts = merge_options(conf) local opts = merge_options(conf)
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
_config.root_dirs = opts.root_dirs
_config.prefer_startup_root = opts.prefer_startup_root
_config.update_focused_file = opts.update_focused_file _config.update_focused_file = opts.update_focused_file
_config.open_on_setup = opts.open_on_setup _config.open_on_setup = opts.open_on_setup
_config.open_on_setup_file = opts.open_on_setup_file _config.open_on_setup_file = opts.open_on_setup_file