-
-
Notifications
You must be signed in to change notification settings - Fork 632
refactor(#2988): multi-instance change-dir #3233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alex-courtis
merged 20 commits into
nvim-tree:master
from
Uanela:moving-change-dir-to-explorer
Jan 24, 2026
Merged
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6b1f5b8
chore: moving root action dir-up to explorer class
Uanela 41585a8
chore(explorer): moving requires to top level
Uanela a12890c
wip(explorer): moved all change-dir functions to explorer class
Uanela fd5ee1d
wip(explorer): rename M.current_tab to self.current_tab
Uanela 85b621b
chore(explorer): move change_dir root action to explorer and replace …
Uanela 896e629
refactor(explorer): changing _foldername to folder_name
Uanela 2ea9992
merge with master
Uanela 5084dd4
wip(explorer): rename expand method to expand_dir_node and all places…
Uanela d5b8ead
wip(explorer): mark private methods and remove add_profiling_to method
Uanela 0d6eefd
fix: allow changing root to file`s parent directory and fix restrict_…
Uanela db36b04
refactor(api): moving change_root_to_node to Explorer:change_dir_to_node
Uanela 189c036
Merge branch 'master' of https://github.com/uanela/nvim-tree.lua into…
Uanela 195f924
fix(explorer): get new explorer before redrawing on <C-]>
Uanela fd300ed
docs(explorer): adding link to PR discussion for more context
Uanela 4995699
fix(explorer): correctly check config.actions.change_dir for change_d…
Uanela 986c79a
fix: correctly pass explorer as first arg when calling explorer metho…
Uanela e4f7cc5
fix: correctly pass args to change_dir
Uanela 0933b46
fix: check for explorer before force_dirchange in open_on_directory
Uanela 47c8a7e
Merge branch 'master' into moving-change-dir-to-explorer
Uanela b97299e
fix: check if force_dirchange should call core.init
Uanela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,6 @@ local Clipboard = require("nvim-tree.actions.fs.clipboard") | |||||
| local Renderer = require("nvim-tree.renderer") | ||||||
|
|
||||||
| local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON | ||||||
| local change_dir = require("nvim-tree.actions.root.change-dir") | ||||||
| local find_file = require("nvim-tree.actions.finders.find-file") | ||||||
|
|
||||||
| local config | ||||||
|
|
@@ -31,6 +30,7 @@ local config | |||||
| ---@field uid_explorer number vim.loop.hrtime() at construction time | ||||||
| ---@field opts table user options | ||||||
| ---@field augroup_id integer | ||||||
| ---@field current_tab integer | ||||||
| ---@field renderer Renderer | ||||||
| ---@field filters Filters | ||||||
| ---@field live_filter LiveFilter | ||||||
|
|
@@ -60,12 +60,15 @@ function Explorer:new(args) | |||||
| self.open = true | ||||||
| self.opts = config | ||||||
|
|
||||||
| self.sorters = Sorter({ explorer = self }) | ||||||
| self.renderer = Renderer({ explorer = self }) | ||||||
| self.filters = Filters({ explorer = self }) | ||||||
| self.live_filter = LiveFilter({ explorer = self }) | ||||||
| self.marks = Marks({ explorer = self }) | ||||||
| self.clipboard = Clipboard({ explorer = self }) | ||||||
|
|
||||||
| self.sorters = Sorter({ explorer = self }) | ||||||
| self.renderer = Renderer({ explorer = self }) | ||||||
| self.filters = Filters({ explorer = self }) | ||||||
| self.live_filter = LiveFilter({ explorer = self }) | ||||||
| self.marks = Marks({ explorer = self }) | ||||||
| self.clipboard = Clipboard({ explorer = self }) | ||||||
|
|
||||||
| self.current_tab = vim.api.nvim_get_current_tabpage() | ||||||
|
alex-courtis marked this conversation as resolved.
|
||||||
|
|
||||||
| self:create_autocmds() | ||||||
|
|
||||||
|
|
@@ -664,15 +667,15 @@ end | |||||
| ---@param node Node | ||||||
| function Explorer:dir_up(node) | ||||||
| if not node or node.name == ".." then | ||||||
| change_dir.fn("..") | ||||||
| self:change_dir("..") | ||||||
| else | ||||||
| local cwd = core.get_cwd() | ||||||
| if cwd == nil then | ||||||
| return | ||||||
| end | ||||||
|
alex-courtis marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") | ||||||
| change_dir.fn(newdir) | ||||||
| self:change_dir(newdir) | ||||||
| find_file.fn(node.absolute_path) | ||||||
| end | ||||||
| end | ||||||
|
|
@@ -683,6 +686,102 @@ function Explorer:get_nodes() | |||||
| return self:clone() | ||||||
| end | ||||||
|
|
||||||
| ---@param new_tabpage integer | ||||||
| ---@return boolean | ||||||
| function Explorer:is_window_event(new_tabpage) | ||||||
| local is_event_scope_window = vim.v.event.scope == "window" or vim.v.event.changed_window or false | ||||||
| return is_event_scope_window and new_tabpage == self.current_tab | ||||||
| end | ||||||
|
|
||||||
| ---@param name string | ||||||
| ---@return string|nil | ||||||
| function Explorer:clean_input_cwd(name) | ||||||
|
alex-courtis marked this conversation as resolved.
|
||||||
| name = vim.fn.fnameescape(name) | ||||||
| local cwd = core.get_cwd() | ||||||
| if cwd == nil then | ||||||
| return | ||||||
| end | ||||||
| local root_parent_cwd = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") | ||||||
| if name == ".." and root_parent_cwd then | ||||||
| return vim.fn.expand(root_parent_cwd) | ||||||
| else | ||||||
| return vim.fn.expand(name) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| ---@param foldername string | ||||||
| ---@return boolean | ||||||
| function Explorer:prevent_cwd_change(foldername) | ||||||
| local is_same_cwd = foldername == core.get_cwd() | ||||||
| local is_restricted_above = config.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1) | ||||||
|
alex-courtis marked this conversation as resolved.
Outdated
|
||||||
| return is_same_cwd or is_restricted_above | ||||||
| end | ||||||
|
|
||||||
| ---@param f function | ||||||
| ---@return fun(foldername: string, should_open_view: boolean|nil) | ||||||
| function Explorer:add_profiling_to(f) | ||||||
|
alex-courtis marked this conversation as resolved.
Outdated
|
||||||
| return function(foldername, should_open_view) | ||||||
| local profile = log.profile_start("change dir %s", foldername) | ||||||
| f(foldername, should_open_view) | ||||||
| log.profile_end(profile) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| ---@return boolean | ||||||
| function Explorer:should_change_dir() | ||||||
| return config.enable and vim.tbl_isempty(vim.v.event) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| ---@param global boolean | ||||||
| ---@param path string | ||||||
| function Explorer:cd(global, path) | ||||||
| vim.cmd((global and "cd " or "lcd ") .. vim.fn.fnameescape(path)) | ||||||
| end | ||||||
|
|
||||||
| function Explorer:force_dirchange(folder_name, with_open) | ||||||
| local fn = self:add_profiling_to(function(foldername, should_open_view) | ||||||
| local valid_dir = vim.fn.isdirectory(foldername) == 1 -- prevent problems on non existing dirs | ||||||
| if valid_dir then | ||||||
| if self:should_change_dir() then | ||||||
| self:cd(config.global, foldername) | ||||||
| end | ||||||
| core.init(foldername) | ||||||
| end | ||||||
|
|
||||||
| if should_open_view then | ||||||
| require("nvim-tree.lib").open() | ||||||
| else | ||||||
| local explorer = core.get_explorer() | ||||||
| if explorer then | ||||||
| explorer.renderer:draw() | ||||||
| end | ||||||
|
alex-courtis marked this conversation as resolved.
Outdated
|
||||||
| end | ||||||
| end) | ||||||
|
|
||||||
| fn(folder_name, with_open) | ||||||
| end | ||||||
|
|
||||||
| ---@param input_cwd string | ||||||
| ---@param with_open boolean|nil | ||||||
| function Explorer:change_dir(input_cwd, with_open) | ||||||
| if not core.get_explorer() then | ||||||
| return | ||||||
| end | ||||||
|
alex-courtis marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| local new_tabpage = vim.api.nvim_get_current_tabpage() | ||||||
| if self:is_window_event(new_tabpage) then | ||||||
| return | ||||||
| end | ||||||
|
|
||||||
| local foldername = self:clean_input_cwd(input_cwd) | ||||||
| if foldername == nil or self:prevent_cwd_change(foldername) then | ||||||
| return | ||||||
| end | ||||||
|
|
||||||
| self.current_tab = new_tabpage | ||||||
| self:force_dirchange(foldername, with_open) | ||||||
| end | ||||||
|
|
||||||
| function Explorer:setup(opts) | ||||||
| config = opts | ||||||
| end | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.