-
-
Notifications
You must be signed in to change notification settings - Fork 634
refactor(#2988): multi instance change_dir and dir_up #3209
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ local events = require("nvim-tree.events") | |
| local help = require("nvim-tree.help") | ||
| local keymap = require("nvim-tree.keymap") | ||
| local notify = require("nvim-tree.notify") | ||
| local Explorer = require("nvim-tree.explorer") | ||
|
|
||
| local DirectoryNode = require("nvim-tree.node.directory") | ||
| local FileLinkNode = require("nvim-tree.node.file-link") | ||
|
|
@@ -159,16 +160,17 @@ end) | |
|
|
||
| Api.tree.change_root_to_node = wrap_node(function(node) | ||
| if node.name == ".." or node:is(RootNode) then | ||
| actions.root.change_dir.fn("..") | ||
| Explorer.change_dir.fn("..") | ||
| else | ||
| node = node:as(DirectoryNode) | ||
| if node then | ||
| actions.root.change_dir.fn(node:last_group_node().absolute_path) | ||
| Explorer.change_dir.fn(node:last_group_node().absolute_path) | ||
| end | ||
| end | ||
| end) | ||
|
|
||
| Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn) | ||
|
|
||
| Api.tree.change_root_to_parent = wrap_node(Explorer.dir_up) | ||
|
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. This change calls the new wrap_node(wrap_explorer("dir_up"))instead. Why is that a problem? When require("nvim-tree.log").line("dev", "self %s", self and self.absolute_path or "no self")
require("nvim-tree.log").line("dev", "node %s", node and node.absolute_path or "no node")
Expected: |
||
| Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor") | ||
| Api.tree.get_nodes = wrap_explorer("get_nodes") | ||
|
|
||
|
|
@@ -274,7 +276,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group) | |
| local dir = node:as(DirectoryNode) | ||
|
|
||
| if root or node.name == ".." then | ||
| actions.root.change_dir.fn("..") | ||
| Explorer.change_dir.fn("..") | ||
| elseif dir then | ||
| dir:expand_or_collapse(toggle_group) | ||
| elseif not toggle_group then | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,7 @@ end | |
|
|
||
| ---@return boolean | ||
| local function should_change_dir() | ||
| local explorer = core.get_explorer() | ||
|
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. This variable This line should be deleted. |
||
| return M.options.enable and vim.tbl_isempty(vim.v.event) | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,17 @@ local core = require("nvim-tree.core") | |
|
|
||
| local M = {} | ||
|
|
||
| ---@param node Node | ||
| function M.fn(node) | ||
|
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. This function has been moved to |
||
| if not node or node.name == ".." then | ||
| require("nvim-tree.actions.root.change-dir").fn("..") | ||
| require("lua.nvim-tree.explorer.change-dir").fn("..") | ||
| else | ||
| local cwd = core.get_cwd() | ||
| if cwd == nil then | ||
| return | ||
| end | ||
|
|
||
| local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") | ||
| require("nvim-tree.actions.root.change-dir").fn(newdir) | ||
| require("lua.nvim-tree.explorer.change-dir").fn(newdir) | ||
| require("nvim-tree.actions.finders.find-file").fn(node.absolute_path) | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,17 @@ local Renderer = require("nvim-tree.renderer") | |
|
|
||
| local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON | ||
|
|
||
| local change_dir = require("nvim-tree.explorer.change-dir") | ||
|
|
||
|
|
||
| local config | ||
|
|
||
| ---@class (exact) Explorer: RootNode | ||
| ---@field uid_explorer number vim.loop.hrtime() at construction time | ||
| ---@field opts table user options | ||
| ---@field augroup_id integer | ||
| ---@field renderer Renderer | ||
| ---@field change_dir any | ||
| ---@field filters Filters | ||
| ---@field live_filter LiveFilter | ||
| ---@field sorters Sorter | ||
|
|
@@ -665,8 +669,26 @@ function Explorer:get_nodes() | |
| return self:clone() | ||
| end | ||
|
|
||
| ---@param node Node | ||
|
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. Annotations are necessary for linting and LSP integration. Thank you for adding them. |
||
| function Explorer:dir_up(node) | ||
| if not node or node.name == ".." then | ||
| require("nvim-tree.explorer.change-dir").fn("..") | ||
| else | ||
| local cwd = core.get_cwd() | ||
| if cwd == nil then | ||
| return | ||
| end | ||
| local newdir = vim.fn.fnamemodify(utils.path_remove_trailing(cwd), ":h") | ||
| require("nvim-tree.explorer.change-dir").fn(newdir) | ||
|
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. The M.change_dir.fn(newdir) |
||
| require("nvim-tree.actions.finders.find-file").fn(node.absolute_path) | ||
|
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. The module local find_file = require("nvim-tree.actions.finders.find-file")Why do we have some inline requires? There are circular dependencies that forced it. One of the goals of this refactoring work is to remove circular dependencies. |
||
| end | ||
| end | ||
|
|
||
| Explorer.change_dir = change_dir | ||
|
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.
|
||
|
|
||
| function Explorer:setup(opts) | ||
| config = opts | ||
| change_dir.setup(opts) | ||
| end | ||
|
|
||
| return Explorer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ end | |
| ---@param cwd string | ||
| local function handle_buf_cwd(cwd) | ||
| if M.respect_buf_cwd and cwd ~= core.get_cwd() then | ||
| require("nvim-tree.actions.root.change-dir").fn(cwd) | ||
| require("lua.nvim-tree.explorer.change-dir").fn(cwd) | ||
|
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. This change requires the moved See the exception in section "Fail: respect_buf_cwd = true" in the test document. |
||
| end | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Unnecessary code gone!