From 2c71ffcc0d05a09f497c5bf14a45eeb9024d4e3c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 23 Apr 2022 18:45:23 +1000 Subject: [PATCH] add actions.change_dir.restrict_above_cwd (#1171) --- README.md | 1 + doc/nvim-tree-lua.txt | 6 ++++++ lua/nvim-tree.lua | 1 + lua/nvim-tree/actions/change-dir.lua | 10 +++------- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 699eec16..b2509f28 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS change_dir = { enable = true, global = false, + restrict_above_cwd = false, }, open_file = { quit_on_open = false, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 243fcf0e..addc714f 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -163,6 +163,7 @@ function. change_dir = { enable = true, global = false, + restrict_above_cwd = false, }, open_file = { quit_on_open = false, @@ -486,6 +487,11 @@ Here is a list of the options available in the setup call: type: `boolean` default: `false` + - |actions.change_dir.restrict_above_cwd|: restrict changing to a directory + above the global current working directory. + type: `boolean` + default: `false` + - |actions.open_file.quit_on_open|: closes the explorer when opening a file. It will also disable preventing a buffer overriding the tree. type: `boolean` diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4e8b49b3..651bbf62 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -388,6 +388,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS change_dir = { enable = true, global = false, + restrict_above_cwd = false, }, open_file = { quit_on_open = false, diff --git a/lua/nvim-tree/actions/change-dir.lua b/lua/nvim-tree/actions/change-dir.lua index 5c0a7e91..1de542b7 100644 --- a/lua/nvim-tree/actions/change-dir.lua +++ b/lua/nvim-tree/actions/change-dir.lua @@ -7,10 +7,6 @@ local diagnostics = require "nvim-tree.diagnostics" local M = { current_tab = a.nvim_get_current_tabpage(), - options = { - global = false, - change_cwd = true, - }, } function M.fn(name, with_open) @@ -20,6 +16,7 @@ function M.fn(name, with_open) local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h") or name local no_cwd_change = vim.fn.expand(foldername) == core.get_cwd() + or M.options.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1) local new_tab = a.nvim_get_current_tabpage() local is_window = (vim.v.event.scope == "window" or vim.v.event.changed_window) and new_tab == M.current_tab if no_cwd_change or is_window then @@ -32,7 +29,7 @@ end function M.force_dirchange(foldername, with_open) local ps = log.profile_start("change dir %s", foldername) - if M.options.change_cwd and vim.tbl_isempty(vim.v.event) then + if M.options.enable and vim.tbl_isempty(vim.v.event) then if M.options.global then vim.cmd("cd " .. vim.fn.fnameescape(foldername)) else @@ -51,8 +48,7 @@ function M.force_dirchange(foldername, with_open) end function M.setup(options) - M.options.change_cwd = options.actions.change_dir.enable - M.options.global = options.actions.change_dir.global + M.options = options.actions.change_dir end return M