Compare commits
27 Commits
expand-unt
...
2826-multi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
996219335c | ||
|
|
b1a5cb55b0 | ||
|
|
1fe6ed858d | ||
|
|
0d975b4cb4 | ||
|
|
c7779d93a7 | ||
|
|
d24f959bf1 | ||
|
|
8b9c9c107f | ||
|
|
54439447f1 | ||
|
|
fc81249d4f | ||
|
|
5377a3fd69 | ||
|
|
76186fa64b | ||
|
|
e0759880db | ||
|
|
6218f9c2f6 | ||
|
|
21d532fdce | ||
|
|
b5e7406df4 | ||
|
|
9594528a8a | ||
|
|
3a82885cc3 | ||
|
|
44cb3d2f0a | ||
|
|
0eb21f66f7 | ||
|
|
f309ca21e9 | ||
|
|
f098195667 | ||
|
|
0a04e435b1 | ||
|
|
b95b873626 | ||
|
|
9d3d0d220f | ||
|
|
a3fe0c9007 | ||
|
|
3c024975fd | ||
|
|
fa64574f16 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -49,7 +49,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
nvim_version: [ stable, nightly ]
|
nvim_version: [ stable, nightly ]
|
||||||
luals_version: [ 3.15.0 ]
|
luals_version: [ 3.13.9 ]
|
||||||
|
|
||||||
env:
|
env:
|
||||||
VIMRUNTIME: /home/runner/nvim-${{ matrix.nvim_version }}/share/nvim/runtime
|
VIMRUNTIME: /home/runner/nvim-${{ matrix.nvim_version }}/share/nvim/runtime
|
||||||
|
|||||||
@@ -398,7 +398,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
|
|||||||
view = {
|
view = {
|
||||||
centralize_selection = false,
|
centralize_selection = false,
|
||||||
cursorline = true,
|
cursorline = true,
|
||||||
cursorlineopt = "both",
|
|
||||||
debounce_delay = 15,
|
debounce_delay = 15,
|
||||||
side = "left",
|
side = "left",
|
||||||
preserve_window_proportions = false,
|
preserve_window_proportions = false,
|
||||||
@@ -639,7 +638,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
experimental = {
|
experimental = {
|
||||||
multi_instance = false,
|
|
||||||
},
|
},
|
||||||
log = {
|
log = {
|
||||||
enable = false,
|
enable = false,
|
||||||
@@ -651,6 +649,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
|
|||||||
dev = false,
|
dev = false,
|
||||||
diagnostics = false,
|
diagnostics = false,
|
||||||
git = false,
|
git = false,
|
||||||
|
lifecycle = false,
|
||||||
profile = false,
|
profile = false,
|
||||||
watcher = false,
|
watcher = false,
|
||||||
},
|
},
|
||||||
@@ -772,10 +771,6 @@ initially centralized, see |zz|.
|
|||||||
Enable |cursorline| in the tree window.
|
Enable |cursorline| in the tree window.
|
||||||
Type: `boolean`, Default: `true`
|
Type: `boolean`, Default: `true`
|
||||||
|
|
||||||
*nvim-tree.view.cursorlineopt*
|
|
||||||
Set |cursorlineopt| in the tree window.
|
|
||||||
Type: `string`, Default: `"both"`
|
|
||||||
|
|
||||||
*nvim-tree.view.debounce_delay*
|
*nvim-tree.view.debounce_delay*
|
||||||
Idle milliseconds before some reload / refresh operations.
|
Idle milliseconds before some reload / refresh operations.
|
||||||
Increase if you experience performance issues around screen refresh.
|
Increase if you experience performance issues around screen refresh.
|
||||||
@@ -1520,6 +1515,11 @@ Configuration options for opening a file from nvim-tree.
|
|||||||
Resizes the tree when opening a file.
|
Resizes the tree when opening a file.
|
||||||
Type: `boolean`, Default: `true`
|
Type: `boolean`, Default: `true`
|
||||||
|
|
||||||
|
*nvim-tree.experimental.actions.open_file.relative_path*
|
||||||
|
Buffers opened by nvim-tree will use with relative paths instead of
|
||||||
|
absolute.
|
||||||
|
Type: `boolean`, Default: `true`
|
||||||
|
|
||||||
*nvim-tree.actions.open_file.window_picker*
|
*nvim-tree.actions.open_file.window_picker*
|
||||||
Window picker configuration.
|
Window picker configuration.
|
||||||
|
|
||||||
@@ -2929,71 +2929,43 @@ Decorators may:
|
|||||||
- Set highlight group for the name or icons
|
- Set highlight group for the name or icons
|
||||||
- Override node icon
|
- Override node icon
|
||||||
|
|
||||||
Create a `nvim_tree.api.decorator.UserDecorator` class and register it with
|
Specify decorators and their precedence via |nvim-tree.renderer.decorators|
|
||||||
precedence via |nvim-tree.renderer.decorators|
|
e.g. defaults with a user decorator class being overridden only by Cut: >lua
|
||||||
|
{
|
||||||
See |nvim-tree-decorator-example|
|
"Git",
|
||||||
|
"Open",
|
||||||
See `nvim-tree/_meta/api_decorator.lua` for full class documentation.
|
"Hidden",
|
||||||
|
"Modified",
|
||||||
|
"Bookmark",
|
||||||
|
"Diagnostics",
|
||||||
|
"Copied",
|
||||||
|
MyDecorator,
|
||||||
|
"Cut",
|
||||||
|
}
|
||||||
|
|
||||||
|
See `nvim-tree/_meta/api_decorator.lua` for full
|
||||||
|
`nvim_tree.api.decorator.UserDecorator` class documentation.
|
||||||
|
<
|
||||||
==============================================================================
|
==============================================================================
|
||||||
11.1. DECORATOR EXAMPLE *nvim-tree-decorator-example*
|
11.1. DECORATOR EXAMPLE *nvim-tree-decorator-example*
|
||||||
|
|
||||||
A decorator class for nodes named "example", overridind all builtin decorators
|
|
||||||
except for Cut.
|
|
||||||
|
|
||||||
- Highlights node name with `IncSearch`
|
|
||||||
- Creates two icons `"1"` and `"2"` placed after the node name, highlighted with
|
|
||||||
`DiffAdd` and `DiffText`
|
|
||||||
- Replaces the node icon with `"N"`, highlighted with `Error `
|
|
||||||
|
|
||||||
Create a class file `~/.config/nvim/lua/my-decorator.lua`
|
|
||||||
|
|
||||||
Require and register it during |nvim-tree-setup|:
|
|
||||||
>lua
|
|
||||||
local MyDecorator = require("my-decorator")
|
|
||||||
|
|
||||||
require("nvim-tree").setup({
|
|
||||||
renderer = {
|
|
||||||
decorators = {
|
|
||||||
"Git",
|
|
||||||
"Open",
|
|
||||||
"Hidden",
|
|
||||||
"Modified",
|
|
||||||
"Bookmark",
|
|
||||||
"Diagnostics",
|
|
||||||
"Copied",
|
|
||||||
MyDecorator,
|
|
||||||
"Cut",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
<
|
|
||||||
Contents of `my-decorator.lua`:
|
|
||||||
>lua
|
>lua
|
||||||
|
---Create your decorator class
|
||||||
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
|
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
|
||||||
---@field private my_icon1 nvim_tree.api.HighlightedString
|
---@field private my_icon nvim_tree.api.HighlightedString
|
||||||
---@field private my_icon2 nvim_tree.api.HighlightedString
|
|
||||||
---@field private my_icon_node nvim_tree.api.HighlightedString
|
|
||||||
---@field private my_highlight_group string
|
|
||||||
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()
|
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()
|
||||||
|
|
||||||
---Mandatory constructor :new() will be called once per tree render, with no arguments.
|
---Mandatory constructor :new() will be called once per tree render, with no arguments.
|
||||||
function MyDecorator:new()
|
function MyDecorator:new()
|
||||||
self.enabled = true
|
self.enabled = true
|
||||||
self.highlight_range = "name"
|
self.highlight_range = "all"
|
||||||
self.icon_placement = "after"
|
self.icon_placement = "signcolumn"
|
||||||
|
|
||||||
-- create your icons and highlights once, applied to every node
|
-- create your icon once, for convenience
|
||||||
self.my_icon1 = { str = "1", hl = { "DiffAdd" } }
|
self.my_icon = { str = "I", hl = { "MyIcon" } }
|
||||||
self.my_icon2 = { str = "2", hl = { "DiffText" } }
|
|
||||||
self.my_icon_node = { str = "N", hl = { "Error" } }
|
|
||||||
self.my_highlight_group = "IncSearch"
|
|
||||||
|
|
||||||
-- Define the icon signs only once
|
-- Define the icon sign only once
|
||||||
-- Only needed if you are using icon_placement = "signcolumn"
|
-- Only needed if you are using icon_placement = "signcolumn"
|
||||||
-- self:define_sign(self.my_icon1)
|
self:define_sign(self.my_icon)
|
||||||
-- self:define_sign(self.my_icon2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Override node icon
|
---Override node icon
|
||||||
@@ -3001,35 +2973,33 @@ Contents of `my-decorator.lua`:
|
|||||||
---@return nvim_tree.api.HighlightedString? icon_node
|
---@return nvim_tree.api.HighlightedString? icon_node
|
||||||
function MyDecorator:icon_node(node)
|
function MyDecorator:icon_node(node)
|
||||||
if node.name == "example" then
|
if node.name == "example" then
|
||||||
return self.my_icon_node
|
return self.my_icon
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Return two icons for DecoratorIconPlacement "after"
|
---Return one icon for DecoratorIconPlacement
|
||||||
---@param node nvim_tree.api.Node
|
---@param node nvim_tree.api.Node
|
||||||
---@return nvim_tree.api.HighlightedString[]? icons
|
---@return nvim_tree.api.HighlightedString[]? icons
|
||||||
function MyDecorator:icons(node)
|
function MyDecorator:icons(node)
|
||||||
if node.name == "example" then
|
if node.name == "example" then
|
||||||
return { self.my_icon1, self.my_icon2, }
|
return { self.my_icon }
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Exactly one highlight group for DecoratorHighlightRange "name"
|
---Exactly one highlight group for DecoratorHighlightRange
|
||||||
---@param node nvim_tree.api.Node
|
---@param node nvim_tree.api.Node
|
||||||
---@return string? highlight_group
|
---@return string? highlight_group
|
||||||
function MyDecorator:highlight_group(node)
|
function MyDecorator:highlight_group(node)
|
||||||
if node.name == "example" then
|
if node.name == "example" then
|
||||||
return self.my_highlight_group
|
return "MyHighlight"
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return MyDecorator
|
|
||||||
<
|
<
|
||||||
==============================================================================
|
==============================================================================
|
||||||
12. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
|
12. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
|
||||||
@@ -3039,6 +3009,9 @@ Windows WSL and PowerShell
|
|||||||
- Executable file detection is disabled as this is non-performant and can
|
- Executable file detection is disabled as this is non-performant and can
|
||||||
freeze nvim
|
freeze nvim
|
||||||
- Some filesystem watcher error related to permissions will not be reported
|
- Some filesystem watcher error related to permissions will not be reported
|
||||||
|
- Some users have reported unspecified issues with
|
||||||
|
|nvim-tree.experimental.actions.open_file.relative_path|. Please report any
|
||||||
|
issues or disable this feature.
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
13. NETRW *nvim-tree-netrw*
|
13. NETRW *nvim-tree-netrw*
|
||||||
@@ -3173,6 +3146,7 @@ highlight group is not, hard linking as follows: >
|
|||||||
|nvim-tree.diagnostics.show_on_open_dirs|
|
|nvim-tree.diagnostics.show_on_open_dirs|
|
||||||
|nvim-tree.disable_netrw|
|
|nvim-tree.disable_netrw|
|
||||||
|nvim-tree.experimental|
|
|nvim-tree.experimental|
|
||||||
|
|nvim-tree.experimental.actions.open_file.relative_path|
|
||||||
|nvim-tree.filesystem_watchers.debounce_delay|
|
|nvim-tree.filesystem_watchers.debounce_delay|
|
||||||
|nvim-tree.filesystem_watchers.enable|
|
|nvim-tree.filesystem_watchers.enable|
|
||||||
|nvim-tree.filesystem_watchers.ignore_dirs|
|
|nvim-tree.filesystem_watchers.ignore_dirs|
|
||||||
@@ -3293,7 +3267,6 @@ highlight group is not, hard linking as follows: >
|
|||||||
|nvim-tree.update_focused_file.update_root.ignore_list|
|
|nvim-tree.update_focused_file.update_root.ignore_list|
|
||||||
|nvim-tree.view.centralize_selection|
|
|nvim-tree.view.centralize_selection|
|
||||||
|nvim-tree.view.cursorline|
|
|nvim-tree.view.cursorline|
|
||||||
|nvim-tree.view.cursorlineopt|
|
|
||||||
|nvim-tree.view.debounce_delay|
|
|nvim-tree.view.debounce_delay|
|
||||||
|nvim-tree.view.float|
|
|nvim-tree.view.float|
|
||||||
|nvim-tree.view.float.enable|
|
|nvim-tree.view.float.enable|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local actions = require("nvim-tree.actions")
|
local actions = require("nvim-tree.actions")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
@@ -73,8 +74,7 @@ function M.change_root(path, bufnr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.tab_enter()
|
function M.tab_enter()
|
||||||
local explorer = core.get_explorer()
|
if view.is_visible({ any_tabpage = true }) then
|
||||||
if explorer and explorer.view:is_visible({ any_tabpage = true }) then
|
|
||||||
local bufname = vim.api.nvim_buf_get_name(0)
|
local bufname = vim.api.nvim_buf_get_name(0)
|
||||||
|
|
||||||
local ft
|
local ft
|
||||||
@@ -89,15 +89,17 @@ function M.tab_enter()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
explorer.view:open({ focus_tree = false })
|
|
||||||
|
|
||||||
explorer.renderer:draw()
|
local explorer = core.get_explorer()
|
||||||
|
if explorer then
|
||||||
|
explorer.window:open({ focus_tree = false })
|
||||||
|
explorer.renderer:draw()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.open_on_directory()
|
function M.open_on_directory()
|
||||||
local explorer = core.get_explorer()
|
local should_proceed = _config.hijack_directories.auto_open or view.is_visible()
|
||||||
local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible()
|
|
||||||
if not should_proceed then
|
if not should_proceed then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -245,7 +247,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
|||||||
view = {
|
view = {
|
||||||
centralize_selection = false,
|
centralize_selection = false,
|
||||||
cursorline = true,
|
cursorline = true,
|
||||||
cursorlineopt = "both",
|
|
||||||
debounce_delay = 15,
|
debounce_delay = 15,
|
||||||
side = "left",
|
side = "left",
|
||||||
preserve_window_proportions = false,
|
preserve_window_proportions = false,
|
||||||
@@ -486,7 +487,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
experimental = {
|
experimental = {
|
||||||
multi_instance = false,
|
|
||||||
},
|
},
|
||||||
log = {
|
log = {
|
||||||
enable = false,
|
enable = false,
|
||||||
@@ -498,6 +498,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
|||||||
dev = false,
|
dev = false,
|
||||||
diagnostics = false,
|
diagnostics = false,
|
||||||
git = false,
|
git = false,
|
||||||
|
lifecycle = false,
|
||||||
profile = false,
|
profile = false,
|
||||||
watcher = false,
|
watcher = false,
|
||||||
},
|
},
|
||||||
@@ -668,8 +669,10 @@ end
|
|||||||
function M.purge_all_state()
|
function M.purge_all_state()
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close_all_tabs()
|
explorer.window:close_all_tabs()
|
||||||
explorer.view:abandon_all_windows("purge_all_state")
|
end
|
||||||
|
view.abandon_all_windows()
|
||||||
|
if explorer then
|
||||||
require("nvim-tree.git").purge_state()
|
require("nvim-tree.git").purge_state()
|
||||||
explorer:destroy()
|
explorer:destroy()
|
||||||
core.reset_explorer()
|
core.reset_explorer()
|
||||||
@@ -727,7 +730,6 @@ function M.setup(conf)
|
|||||||
require("nvim-tree.buffers").setup(opts)
|
require("nvim-tree.buffers").setup(opts)
|
||||||
require("nvim-tree.help").setup(opts)
|
require("nvim-tree.help").setup(opts)
|
||||||
require("nvim-tree.watcher").setup(opts)
|
require("nvim-tree.watcher").setup(opts)
|
||||||
require("nvim-tree.multi-instance-debug").setup(opts)
|
|
||||||
|
|
||||||
setup_autocommands(opts)
|
setup_autocommands(opts)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ local running = {}
|
|||||||
---@param path string relative or absolute
|
---@param path string relative or absolute
|
||||||
function M.fn(path)
|
function M.fn(path)
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if not explorer or not explorer.view:is_visible() then
|
if not explorer or not view.is_visible() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -83,9 +84,9 @@ function M.fn(path)
|
|||||||
end)
|
end)
|
||||||
:iterate()
|
:iterate()
|
||||||
|
|
||||||
if found and explorer.view:is_visible() then
|
if found and view.is_visible() then
|
||||||
explorer.renderer:draw()
|
explorer.renderer:draw()
|
||||||
explorer.view:set_cursor({ line, 0 })
|
view.set_cursor({ line, 0 })
|
||||||
end
|
end
|
||||||
|
|
||||||
running[path_real] = false
|
running[path_real] = false
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ local function close_windows(windows)
|
|||||||
-- Prevent from closing when the win count equals 1 or 2,
|
-- Prevent from closing when the win count equals 1 or 2,
|
||||||
-- where the win to remove could be the last opened.
|
-- where the win to remove could be the last opened.
|
||||||
-- For details see #2503.
|
-- For details see #2503.
|
||||||
if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then
|
if explorer and explorer.window.float.enable and #vim.api.nvim_list_wins() < 3 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -36,12 +36,12 @@ local function clear_buffer(absolute_path)
|
|||||||
for _, buf in pairs(bufs) do
|
for _, buf in pairs(bufs) do
|
||||||
if buf.name == absolute_path then
|
if buf.name == absolute_path then
|
||||||
local tree_winnr = vim.api.nvim_get_current_win()
|
local tree_winnr = vim.api.nvim_get_current_win()
|
||||||
if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then
|
if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.window.float.enable) then
|
||||||
vim.api.nvim_set_current_win(buf.windows[1])
|
vim.api.nvim_set_current_win(buf.windows[1])
|
||||||
vim.cmd(":bn")
|
vim.cmd(":bn")
|
||||||
end
|
end
|
||||||
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
|
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
|
||||||
if explorer and not explorer.view.float.quit_on_focus_loss then
|
if explorer and not explorer.window.float.quit_on_focus_loss then
|
||||||
vim.api.nvim_set_current_win(tree_winnr)
|
vim.api.nvim_set_current_win(tree_winnr)
|
||||||
end
|
end
|
||||||
if M.config.actions.remove_file.close_window then
|
if M.config.actions.remove_file.close_window then
|
||||||
@@ -71,16 +71,9 @@ local function remove_dir(cwd)
|
|||||||
|
|
||||||
-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
|
-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
|
||||||
local stat = vim.loop.fs_stat(new_cwd)
|
local stat = vim.loop.fs_stat(new_cwd)
|
||||||
-- TODO remove once 0.12 is the minimum neovim version
|
|
||||||
-- path incorrectly specified as an integer, fixed upstream for neovim 0.12 https://github.com/neovim/neovim/pull/33872
|
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
|
||||||
local lstat = vim.loop.fs_lstat(new_cwd)
|
|
||||||
|
|
||||||
local type = stat and stat.type or nil
|
local type = stat and stat.type or nil
|
||||||
-- Checks if file is a link file to ensure deletion of the symlink instead of the file it points to
|
|
||||||
local ltype = lstat and lstat.type or nil
|
|
||||||
|
|
||||||
if type == "directory" and ltype ~= "link" then
|
if type == "directory" then
|
||||||
local success = remove_dir(new_cwd)
|
local success = remove_dir(new_cwd)
|
||||||
if not success then
|
if not success then
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local diagnostics = require("nvim-tree.diagnostics")
|
local diagnostics = require("nvim-tree.diagnostics")
|
||||||
|
|
||||||
@@ -66,9 +67,9 @@ local function move(explorer, where, what, skip_gitignored)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if nex then
|
if nex then
|
||||||
explorer.view:set_cursor({ nex, 0 })
|
view.set_cursor({ nex, 0 })
|
||||||
elseif vim.o.wrapscan and first then
|
elseif vim.o.wrapscan and first then
|
||||||
explorer.view:set_cursor({ first, 0 })
|
view.set_cursor({ first, 0 })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -188,13 +189,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored)
|
|||||||
|
|
||||||
-- 4.3)
|
-- 4.3)
|
||||||
if node_init.name == ".." then -- root node
|
if node_init.name == ".." then -- root node
|
||||||
explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1)
|
view.set_cursor({ 1, 0 }) -- move to root node (position 1)
|
||||||
else
|
else
|
||||||
local node_init_line = utils.find_node_line(node_init)
|
local node_init_line = utils.find_node_line(node_init)
|
||||||
if node_init_line < 0 then
|
if node_init_line < 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
explorer.view:set_cursor({ node_init_line, 0 })
|
view.set_cursor({ node_init_line, 0 })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 4.4)
|
-- 4.4)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
@@ -24,7 +25,7 @@ function M.fn(should_close)
|
|||||||
local parent = (node:get_parent_of_group() or node).parent
|
local parent = (node:get_parent_of_group() or node).parent
|
||||||
|
|
||||||
if not parent or not parent.parent then
|
if not parent or not parent.parent then
|
||||||
node.explorer.view:set_cursor({ 1, 0 })
|
view.set_cursor({ 1, 0 })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ function M.fn(should_close)
|
|||||||
return n.absolute_path == parent.absolute_path
|
return n.absolute_path == parent.absolute_path
|
||||||
end)
|
end)
|
||||||
|
|
||||||
node.explorer.view:set_cursor({ line + 1, 0 })
|
view.set_cursor({ line + 1, 0 })
|
||||||
if should_close then
|
if should_close then
|
||||||
parent.open = false
|
parent.open = false
|
||||||
parent.explorer.renderer:draw()
|
parent.explorer.renderer:draw()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ local lib = require("nvim-tree.lib")
|
|||||||
local notify = require("nvim-tree.notify")
|
local notify = require("nvim-tree.notify")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local full_name = require("nvim-tree.renderer.components.full-name")
|
local view = require("nvim-tree.view")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -20,10 +20,9 @@ end
|
|||||||
---Get all windows in the current tabpage that aren't NvimTree.
|
---Get all windows in the current tabpage that aren't NvimTree.
|
||||||
---@return table with valid win_ids
|
---@return table with valid win_ids
|
||||||
local function usable_win_ids()
|
local function usable_win_ids()
|
||||||
local explorer = core.get_explorer()
|
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
|
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
|
||||||
local tree_winid = explorer and explorer.view:get_winnr(tabpage, "open-file.usable_win_ids")
|
local tree_winid = view.get_winnr(tabpage)
|
||||||
|
|
||||||
return vim.tbl_filter(function(id)
|
return vim.tbl_filter(function(id)
|
||||||
local bufid = vim.api.nvim_win_get_buf(id)
|
local bufid = vim.api.nvim_win_get_buf(id)
|
||||||
@@ -41,12 +40,7 @@ local function usable_win_ids()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local win_config = vim.api.nvim_win_get_config(id)
|
local win_config = vim.api.nvim_win_get_config(id)
|
||||||
return id ~= tree_winid
|
return id ~= tree_winid and win_config.focusable and not win_config.hide and not win_config.external or false
|
||||||
and id ~= full_name.popup_win
|
|
||||||
and win_config.focusable
|
|
||||||
and not win_config.hide
|
|
||||||
and not win_config.external
|
|
||||||
or false
|
|
||||||
end, win_ids)
|
end, win_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -196,7 +190,7 @@ local function open_file_in_tab(filename)
|
|||||||
if M.quit_on_open then
|
if M.quit_on_open then
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close(nil, "open-file.open_file_in_tab")
|
explorer.window:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if M.relative_path then
|
if M.relative_path then
|
||||||
@@ -209,7 +203,7 @@ local function drop(filename)
|
|||||||
if M.quit_on_open then
|
if M.quit_on_open then
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close(nil, "open-file.drop")
|
explorer.window:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if M.relative_path then
|
if M.relative_path then
|
||||||
@@ -222,7 +216,7 @@ local function tab_drop(filename)
|
|||||||
if M.quit_on_open then
|
if M.quit_on_open then
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close(nil, "open-file.tab_drop")
|
explorer.window:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if M.relative_path then
|
if M.relative_path then
|
||||||
@@ -246,7 +240,7 @@ local function on_preview(buf_loaded)
|
|||||||
end
|
end
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:focus()
|
explorer.window:focus()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -318,7 +312,7 @@ local function open_in_new_window(filename, mode)
|
|||||||
local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
|
local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
|
||||||
|
|
||||||
local new_window_side = "belowright"
|
local new_window_side = "belowright"
|
||||||
if explorer and (explorer.view.side == "right") then
|
if explorer and (explorer.window.side == "right") then
|
||||||
new_window_side = "aboveleft"
|
new_window_side = "aboveleft"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -352,7 +346,7 @@ local function open_in_new_window(filename, mode)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then
|
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.window.float.enable then
|
||||||
-- ignore "WinLeave" autocmd on preview
|
-- ignore "WinLeave" autocmd on preview
|
||||||
-- because the registered "WinLeave"
|
-- because the registered "WinLeave"
|
||||||
-- will kill the floating window immediately
|
-- will kill the floating window immediately
|
||||||
@@ -392,12 +386,7 @@ local function is_already_loaded(filename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function edit_in_current_buf(filename)
|
local function edit_in_current_buf(filename)
|
||||||
local explorer = core.get_explorer()
|
require("nvim-tree.view").abandon_current_window()
|
||||||
|
|
||||||
if explorer then
|
|
||||||
explorer.view:abandon_current_window()
|
|
||||||
end
|
|
||||||
|
|
||||||
if M.relative_path then
|
if M.relative_path then
|
||||||
filename = utils.path_relative(filename, vim.fn.getcwd())
|
filename = utils.path_relative(filename, vim.fn.getcwd())
|
||||||
end
|
end
|
||||||
@@ -445,7 +434,7 @@ function M.fn(mode, filename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if M.resize_window and explorer then
|
if M.resize_window and explorer then
|
||||||
explorer.view:resize()
|
explorer.window:resize()
|
||||||
end
|
end
|
||||||
|
|
||||||
if mode == "preview" or mode == "preview_no_picker" then
|
if mode == "preview" or mode == "preview_no_picker" then
|
||||||
@@ -453,7 +442,7 @@ function M.fn(mode, filename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if M.quit_on_open and explorer then
|
if M.quit_on_open and explorer then
|
||||||
explorer.view:close(nil, "open-file.fn")
|
explorer.window:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view)
|
|||||||
if should_change_dir() then
|
if should_change_dir() then
|
||||||
cd(M.options.global, foldername)
|
cd(M.options.global, foldername)
|
||||||
end
|
end
|
||||||
core.init(foldername, "change-dir")
|
core.init(foldername)
|
||||||
end
|
end
|
||||||
|
|
||||||
if should_open_view then
|
if should_open_view then
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
local lib = require("nvim-tree.lib")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@@ -41,11 +42,11 @@ function M.fn(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer and explorer.view:is_visible() then
|
if explorer and view.is_visible() then
|
||||||
-- focus
|
-- focus
|
||||||
if opts.focus then
|
if opts.focus then
|
||||||
lib.set_target_win()
|
lib.set_target_win()
|
||||||
explorer.view:focus()
|
explorer.window:focus()
|
||||||
end
|
end
|
||||||
elseif opts.open then
|
elseif opts.open then
|
||||||
-- open
|
-- open
|
||||||
|
|||||||
@@ -30,35 +30,17 @@ end
|
|||||||
---@param expansion_count integer
|
---@param expansion_count integer
|
||||||
---@param node Node
|
---@param node Node
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function descend_until_max_or_empty(expansion_count, node)
|
local function should_expand(expansion_count, node)
|
||||||
local dir = node:as(DirectoryNode)
|
local dir = node:as(DirectoryNode)
|
||||||
if not dir then
|
if not dir then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY
|
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY
|
||||||
local should_exclude = M.EXCLUDE[dir.name]
|
local should_exclude = M.EXCLUDE[dir.name]
|
||||||
|
return not should_halt and not dir.open and not should_exclude
|
||||||
return not should_halt and not should_exclude
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param expansion_count integer
|
local function gen_iterator()
|
||||||
---@param node Node
|
|
||||||
---@param should_descend fun(expansion_count: integer, node: Node): boolean
|
|
||||||
---@return boolean
|
|
||||||
local function should_expand(expansion_count, node, should_descend)
|
|
||||||
local dir = node:as(DirectoryNode)
|
|
||||||
if not dir then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return not dir.open and should_descend(expansion_count, node)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
---@param should_descend fun(expansion_count: integer, node: Node): boolean
|
|
||||||
---@return fun(node): any
|
|
||||||
local function gen_iterator(should_descend)
|
|
||||||
local expansion_count = 0
|
local expansion_count = 0
|
||||||
|
|
||||||
return function(parent)
|
return function(parent)
|
||||||
@@ -70,7 +52,7 @@ local function gen_iterator(should_descend)
|
|||||||
Iterator.builder(parent.nodes)
|
Iterator.builder(parent.nodes)
|
||||||
:hidden()
|
:hidden()
|
||||||
:applier(function(node)
|
:applier(function(node)
|
||||||
if should_expand(expansion_count, node, should_descend) then
|
if should_expand(expansion_count, node) then
|
||||||
expansion_count = expansion_count + 1
|
expansion_count = expansion_count + 1
|
||||||
node = node:as(DirectoryNode)
|
node = node:as(DirectoryNode)
|
||||||
if node then
|
if node then
|
||||||
@@ -79,19 +61,7 @@ local function gen_iterator(should_descend)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
:recursor(function(node)
|
:recursor(function(node)
|
||||||
if not should_descend(expansion_count, node) then
|
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes))
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if node.group_next then
|
|
||||||
return { node.group_next }
|
|
||||||
end
|
|
||||||
|
|
||||||
if node.open and node.nodes then
|
|
||||||
return node.nodes
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
|
||||||
end)
|
end)
|
||||||
:iterate()
|
:iterate()
|
||||||
|
|
||||||
@@ -102,13 +72,12 @@ local function gen_iterator(should_descend)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param node Node?
|
---@param node Node?
|
||||||
---@param expand_opts ApiTreeExpandAllOpts?
|
local function expand_node(node)
|
||||||
local function expand_node(node, expand_opts)
|
|
||||||
if not node then
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local descend_until = (expand_opts and expand_opts.descend_until) or descend_until_max_or_empty
|
|
||||||
if gen_iterator(descend_until)(node) then
|
if gen_iterator()(node) then
|
||||||
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
|
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -120,20 +89,18 @@ end
|
|||||||
|
|
||||||
---Expand the directory node or the root
|
---Expand the directory node or the root
|
||||||
---@param node Node
|
---@param node Node
|
||||||
---@param expand_opts ApiTreeExpandAllOpts?
|
function M.all(node)
|
||||||
function M.all(node, expand_opts)
|
expand_node(node and node:as(DirectoryNode) or core.get_explorer())
|
||||||
expand_node(node and node:as(DirectoryNode) or core.get_explorer(), expand_opts)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Expand the directory node or parent node
|
---Expand the directory node or parent node
|
||||||
---@param node Node
|
---@param node Node
|
||||||
---@param expand_opts ApiTreeExpandAllOpts?
|
function M.node(node)
|
||||||
function M.node(node, expand_opts)
|
|
||||||
if not node then
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode), expand_opts)
|
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode))
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
local lib = require("nvim-tree.lib")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@@ -25,10 +26,10 @@ function M.fn(opts)
|
|||||||
|
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
|
|
||||||
if explorer and explorer.view:is_visible() then
|
if explorer and view.is_visible() then
|
||||||
-- focus
|
-- focus
|
||||||
lib.set_target_win()
|
lib.set_target_win()
|
||||||
explorer.view:focus()
|
explorer.window:focus()
|
||||||
else
|
else
|
||||||
-- open
|
-- open
|
||||||
lib.open({
|
lib.open({
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ function M.fn(opts)
|
|||||||
|
|
||||||
if opts == nil then
|
if opts == nil then
|
||||||
-- reset to config values
|
-- reset to config values
|
||||||
explorer.view:configure_width()
|
explorer.window:configure_width()
|
||||||
explorer.view:resize()
|
explorer.window:resize()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -21,19 +21,19 @@ function M.fn(opts)
|
|||||||
local width_cfg = options.width
|
local width_cfg = options.width
|
||||||
|
|
||||||
if width_cfg ~= nil then
|
if width_cfg ~= nil then
|
||||||
explorer.view:configure_width(width_cfg)
|
explorer.window:configure_width(width_cfg)
|
||||||
explorer.view:resize()
|
explorer.window:resize()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if not explorer.view:is_width_determined() then
|
if not explorer.window:is_width_determined() then
|
||||||
-- {absolute} and {relative} do nothing when {width} is a function.
|
-- {absolute} and {relative} do nothing when {width} is a function.
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local absolute = options.absolute
|
local absolute = options.absolute
|
||||||
if type(absolute) == "number" then
|
if type(absolute) == "number" then
|
||||||
explorer.view:resize(absolute)
|
explorer.window:resize(absolute)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ function M.fn(opts)
|
|||||||
relative_size = "+" .. relative_size
|
relative_size = "+" .. relative_size
|
||||||
end
|
end
|
||||||
|
|
||||||
explorer.view:resize(relative_size)
|
explorer.window:resize(relative_size)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
local lib = require("nvim-tree.lib")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
local finders_find_file = require("nvim-tree.actions.finders.find-file")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
@@ -42,9 +43,9 @@ function M.fn(opts, no_focus, cwd, bang)
|
|||||||
opts.path = nil
|
opts.path = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if explorer and explorer.view:is_visible() then
|
if explorer and view.is_visible() then
|
||||||
-- close
|
-- close
|
||||||
explorer.view:close(nil, "toggle.fn")
|
explorer.window:close()
|
||||||
else
|
else
|
||||||
-- open
|
-- open
|
||||||
lib.open({
|
lib.open({
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local actions = require("nvim-tree.actions")
|
local actions = require("nvim-tree.actions")
|
||||||
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
|
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
|
||||||
@@ -140,9 +141,9 @@ Api.tree.focus = Api.tree.open
|
|||||||
---@field focus boolean|nil default true
|
---@field focus boolean|nil default true
|
||||||
|
|
||||||
Api.tree.toggle = wrap(actions.tree.toggle.fn)
|
Api.tree.toggle = wrap(actions.tree.toggle.fn)
|
||||||
Api.tree.close = wrap_explorer_member("view", "close")
|
Api.tree.close = wrap_explorer_member("window", "close")
|
||||||
Api.tree.close_in_this_tab = wrap_explorer_member("view", "close_this_tab_only")
|
Api.tree.close_in_this_tab = wrap_explorer_member("window", "close_this_tab_only")
|
||||||
Api.tree.close_in_all_tabs = wrap_explorer_member("view", "close_all_tabs")
|
Api.tree.close_in_all_tabs = wrap_explorer_member("window", "close_all_tabs")
|
||||||
Api.tree.reload = wrap_explorer("reload_explorer")
|
Api.tree.reload = wrap_explorer("reload_explorer")
|
||||||
|
|
||||||
---@class ApiTreeResizeOpts
|
---@class ApiTreeResizeOpts
|
||||||
@@ -186,10 +187,6 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn)
|
|||||||
---@field keep_buffers boolean|nil default false
|
---@field keep_buffers boolean|nil default false
|
||||||
|
|
||||||
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
|
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
|
||||||
|
|
||||||
---@class ApiTreeExpandAllOpts
|
|
||||||
---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil
|
|
||||||
|
|
||||||
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
|
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
|
||||||
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
|
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
|
||||||
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
|
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
|
||||||
@@ -205,12 +202,12 @@ Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf)
|
|||||||
---@field tabpage number|nil
|
---@field tabpage number|nil
|
||||||
---@field any_tabpage boolean|nil default false
|
---@field any_tabpage boolean|nil default false
|
||||||
|
|
||||||
Api.tree.is_visible = wrap_explorer_member("view", "is_visible")
|
Api.tree.is_visible = wrap(view.is_visible)
|
||||||
|
|
||||||
---@class ApiTreeWinIdOpts
|
---@class ApiTreeWinIdOpts
|
||||||
---@field tabpage number|nil default nil
|
---@field tabpage number|nil default nil
|
||||||
|
|
||||||
Api.tree.winid = wrap_explorer_member("view", "api_winid")
|
Api.tree.winid = wrap(view.winid)
|
||||||
|
|
||||||
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
|
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
|
||||||
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)
|
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)
|
||||||
@@ -251,7 +248,7 @@ local function edit(mode, node, edit_opts)
|
|||||||
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
|
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
|
||||||
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
|
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close(cur_tabpage, "api.edit " .. mode)
|
explorer.window:close(cur_tabpage)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -263,24 +260,11 @@ local function edit(mode, node, edit_opts)
|
|||||||
vim.cmd(":tabprev")
|
vim.cmd(":tabprev")
|
||||||
end
|
end
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:focus()
|
explorer.window:focus()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Api.tree.toggle_descend_until = wrap_node(function(node, descend_until)
|
|
||||||
local dir = node:as(DirectoryNode)
|
|
||||||
if dir then
|
|
||||||
if node.open then
|
|
||||||
dir:expand_or_collapse(nil)
|
|
||||||
else
|
|
||||||
actions.tree.modifiers.expand.all(node, { descend_until = descend_until })
|
|
||||||
end
|
|
||||||
else
|
|
||||||
edit("edit", node)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
---@param mode string
|
---@param mode string
|
||||||
---@param toggle_group boolean?
|
---@param toggle_group boolean?
|
||||||
---@return fun(node: Node, edit_opts: NodeEditOpts?)
|
---@return fun(node: Node, edit_opts: NodeEditOpts?)
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ local CMDS = {
|
|||||||
command = function(c)
|
command = function(c)
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:resize(c.args)
|
explorer.window:resize(c.args)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -9,12 +9,9 @@ local TreeExplorer = nil
|
|||||||
local first_init_done = false
|
local first_init_done = false
|
||||||
|
|
||||||
---@param foldername string
|
---@param foldername string
|
||||||
---@param callsite string
|
function M.init(foldername)
|
||||||
function M.init(foldername, callsite)
|
|
||||||
local profile = log.profile_start("core init %s", foldername)
|
local profile = log.profile_start("core init %s", foldername)
|
||||||
|
|
||||||
log.line("dev", "core.init(%s, %s)", foldername, callsite)
|
|
||||||
|
|
||||||
if TreeExplorer then
|
if TreeExplorer then
|
||||||
TreeExplorer:destroy()
|
TreeExplorer:destroy()
|
||||||
end
|
end
|
||||||
@@ -57,7 +54,7 @@ end
|
|||||||
---@return integer
|
---@return integer
|
||||||
function M.get_nodes_starting_line()
|
function M.get_nodes_starting_line()
|
||||||
local offset = 1
|
local offset = 1
|
||||||
if TreeExplorer and TreeExplorer.view:is_root_folder_visible(M.get_cwd()) then
|
if TreeExplorer and TreeExplorer.window:is_root_folder_visible(M.get_cwd()) then
|
||||||
offset = offset + 1
|
offset = offset + 1
|
||||||
end
|
end
|
||||||
if TreeExplorer and TreeExplorer.live_filter.filter then
|
if TreeExplorer and TreeExplorer.live_filter.filter then
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
@@ -181,15 +182,10 @@ function M.update_coc()
|
|||||||
end
|
end
|
||||||
log.profile_end(profile)
|
log.profile_end(profile)
|
||||||
|
|
||||||
local explorer = core.get_explorer()
|
local bufnr = view.get_bufnr()
|
||||||
|
|
||||||
local bufnr
|
|
||||||
if explorer then
|
|
||||||
bufnr = explorer.view:get_bufnr("diagnostics.update_coc")
|
|
||||||
end
|
|
||||||
|
|
||||||
local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
|
local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
|
||||||
if should_draw then
|
if should_draw then
|
||||||
|
local explorer = core.get_explorer()
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.renderer:draw()
|
explorer.renderer:draw()
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ local core = require("nvim-tree.core")
|
|||||||
local git = require("nvim-tree.git")
|
local git = require("nvim-tree.git")
|
||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local node_factory = require("nvim-tree.node.factory")
|
local node_factory = require("nvim-tree.node.factory")
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
@@ -19,7 +20,7 @@ local LiveFilter = require("nvim-tree.explorer.live-filter")
|
|||||||
local Sorter = require("nvim-tree.explorer.sorter")
|
local Sorter = require("nvim-tree.explorer.sorter")
|
||||||
local Clipboard = require("nvim-tree.actions.fs.clipboard")
|
local Clipboard = require("nvim-tree.actions.fs.clipboard")
|
||||||
local Renderer = require("nvim-tree.renderer")
|
local Renderer = require("nvim-tree.renderer")
|
||||||
local View = require("nvim-tree.explorer.view")
|
local Window = require("nvim-tree.explorer.window")
|
||||||
|
|
||||||
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
|
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ local config
|
|||||||
---@field sorters Sorter
|
---@field sorters Sorter
|
||||||
---@field marks Marks
|
---@field marks Marks
|
||||||
---@field clipboard Clipboard
|
---@field clipboard Clipboard
|
||||||
---@field view View
|
---@field window Window
|
||||||
local Explorer = RootNode:extend()
|
local Explorer = RootNode:extend()
|
||||||
|
|
||||||
---@class Explorer
|
---@class Explorer
|
||||||
@@ -61,13 +62,13 @@ function Explorer:new(args)
|
|||||||
self.open = true
|
self.open = true
|
||||||
self.opts = config
|
self.opts = config
|
||||||
|
|
||||||
self.sorters = Sorter({ explorer = self })
|
self.clipboard = Clipboard({ explorer = self })
|
||||||
self.renderer = Renderer({ explorer = self })
|
|
||||||
self.filters = Filters({ explorer = self })
|
self.filters = Filters({ explorer = self })
|
||||||
self.live_filter = LiveFilter({ explorer = self })
|
self.live_filter = LiveFilter({ explorer = self })
|
||||||
self.marks = Marks({ explorer = self })
|
self.marks = Marks({ explorer = self })
|
||||||
self.clipboard = Clipboard({ explorer = self })
|
self.renderer = Renderer({ explorer = self })
|
||||||
self.view = View({ explorer = self })
|
self.sorters = Sorter({ explorer = self })
|
||||||
|
self.window = Window({ explorer = self })
|
||||||
|
|
||||||
self:create_autocmds()
|
self:create_autocmds()
|
||||||
|
|
||||||
@@ -83,7 +84,7 @@ function Explorer:destroy()
|
|||||||
self.marks:destroy()
|
self.marks:destroy()
|
||||||
self.renderer:destroy()
|
self.renderer:destroy()
|
||||||
self.sorters:destroy()
|
self.sorters:destroy()
|
||||||
self.view:destroy()
|
self.window:destroy()
|
||||||
|
|
||||||
vim.api.nvim_del_augroup_by_id(self.augroup_id)
|
vim.api.nvim_del_augroup_by_id(self.augroup_id)
|
||||||
|
|
||||||
@@ -96,7 +97,7 @@ function Explorer:create_autocmds()
|
|||||||
group = self.augroup_id,
|
group = self.augroup_id,
|
||||||
callback = function()
|
callback = function()
|
||||||
appearance.setup()
|
appearance.setup()
|
||||||
self.view:reset_winhl()
|
self.window:reset_winhl()
|
||||||
self.renderer:draw()
|
self.renderer:draw()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@@ -105,12 +106,9 @@ function Explorer:create_autocmds()
|
|||||||
vim.api.nvim_create_autocmd("WinLeave", {
|
vim.api.nvim_create_autocmd("WinLeave", {
|
||||||
group = self.augroup_id,
|
group = self.augroup_id,
|
||||||
pattern = "NvimTree_*",
|
pattern = "NvimTree_*",
|
||||||
callback = function(data)
|
callback = function()
|
||||||
if self.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "WinLeave %s", vim.inspect(data, { newline = "" }))
|
|
||||||
end
|
|
||||||
if utils.is_nvim_tree_buf(0) then
|
if utils.is_nvim_tree_buf(0) then
|
||||||
self.view:close(nil, "WinLeave")
|
self.window:close()
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@@ -172,17 +170,14 @@ function Explorer:create_autocmds()
|
|||||||
vim.api.nvim_create_autocmd("BufWipeout", {
|
vim.api.nvim_create_autocmd("BufWipeout", {
|
||||||
group = self.augroup_id,
|
group = self.augroup_id,
|
||||||
pattern = "NvimTree_*",
|
pattern = "NvimTree_*",
|
||||||
callback = function(data)
|
callback = function()
|
||||||
if self.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "BufWipeout %s", vim.inspect(data, { newline = "" }))
|
|
||||||
end
|
|
||||||
if not utils.is_nvim_tree_buf(0) then
|
if not utils.is_nvim_tree_buf(0) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if self.opts.actions.open_file.eject then
|
if self.opts.actions.open_file.eject then
|
||||||
self.view:prevent_buffer_override()
|
self.window:prevent_buffer_override()
|
||||||
else
|
else
|
||||||
self.view:abandon_current_window()
|
view.abandon_current_window()
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
@@ -532,7 +527,7 @@ function Explorer:reload_explorer()
|
|||||||
|
|
||||||
local projects = git.reload_all_projects()
|
local projects = git.reload_all_projects()
|
||||||
self:refresh_nodes(projects)
|
self:refresh_nodes(projects)
|
||||||
if self.view:is_visible() then
|
if view.is_visible() then
|
||||||
self.renderer:draw()
|
self.renderer:draw()
|
||||||
end
|
end
|
||||||
event_running = false
|
event_running = false
|
||||||
@@ -554,7 +549,7 @@ end
|
|||||||
---nil on no explorer or invalid view win
|
---nil on no explorer or invalid view win
|
||||||
---@return integer[]|nil
|
---@return integer[]|nil
|
||||||
function Explorer:get_cursor_position()
|
function Explorer:get_cursor_position()
|
||||||
local winnr = self.view:get_winnr(nil, "Explorer:get_cursor_position")
|
local winnr = view.get_winnr()
|
||||||
if not winnr or not vim.api.nvim_win_is_valid(winnr) then
|
if not winnr or not vim.api.nvim_win_is_valid(winnr) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -569,7 +564,7 @@ function Explorer:get_node_at_cursor()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if cursor[1] == 1 and self.view:is_root_folder_visible(core.get_cwd()) then
|
if cursor[1] == 1 and self.window:is_root_folder_visible(core.get_cwd()) then
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -606,13 +601,13 @@ end
|
|||||||
---Log a lifecycle message with uid_explorer and absolute_path
|
---Log a lifecycle message with uid_explorer and absolute_path
|
||||||
---@param msg string?
|
---@param msg string?
|
||||||
function Explorer:log_new(msg)
|
function Explorer:log_new(msg)
|
||||||
log.line("dev", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
|
log.line("lifecycle", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Log a lifecycle message with uid_explorer and absolute_path
|
---Log a lifecycle message with uid_explorer and absolute_path
|
||||||
---@param msg string?
|
---@param msg string?
|
||||||
function Explorer:log_destroy(msg)
|
function Explorer:log_destroy(msg)
|
||||||
log.line("dev", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
|
log.line("lifecycle", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Explorer:setup(opts)
|
function Explorer:setup(opts)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local view = require("nvim-tree.view")
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
|
|
||||||
local Class = require("nvim-tree.classic")
|
local Class = require("nvim-tree.classic")
|
||||||
@@ -203,13 +204,13 @@ local function create_overlay(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function LiveFilter:start_filtering()
|
function LiveFilter:start_filtering()
|
||||||
self.explorer.view.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
|
self.explorer.window.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
|
||||||
self.filter = self.filter or ""
|
self.filter = self.filter or ""
|
||||||
|
|
||||||
self.explorer.renderer:draw()
|
self.explorer.renderer:draw()
|
||||||
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
|
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
|
||||||
local col = #self.prefix > 0 and #self.prefix - 1 or 1
|
local col = #self.prefix > 0 and #self.prefix - 1 or 1
|
||||||
self.explorer.view:set_cursor({ row, col })
|
view.set_cursor({ row, col })
|
||||||
-- needs scheduling to let the cursor move before initializing the window
|
-- needs scheduling to let the cursor move before initializing the window
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
return create_overlay(self)
|
return create_overlay(self)
|
||||||
@@ -218,7 +219,7 @@ end
|
|||||||
|
|
||||||
function LiveFilter:clear_filter()
|
function LiveFilter:clear_filter()
|
||||||
local node = self.explorer:get_node_at_cursor()
|
local node = self.explorer:get_node_at_cursor()
|
||||||
local last_node = self.explorer.view.live_filter.prev_focused_node
|
local last_node = self.explorer.window.live_filter.prev_focused_node
|
||||||
|
|
||||||
self.filter = nil
|
self.filter = nil
|
||||||
reset_filter(self)
|
reset_filter(self)
|
||||||
|
|||||||
@@ -1,799 +0,0 @@
|
|||||||
local appearance = require("nvim-tree.appearance")
|
|
||||||
local events = require("nvim-tree.events")
|
|
||||||
local utils = require("nvim-tree.utils")
|
|
||||||
local log = require("nvim-tree.log")
|
|
||||||
local notify = require("nvim-tree.notify")
|
|
||||||
local globals = require("nvim-tree.globals")
|
|
||||||
|
|
||||||
local Class = require("nvim-tree.classic")
|
|
||||||
|
|
||||||
---@class OpenInWinOpts
|
|
||||||
---@field hijack_current_buf boolean|nil default true
|
|
||||||
---@field resize boolean|nil default true
|
|
||||||
---@field winid number|nil 0 or nil for current
|
|
||||||
|
|
||||||
local DEFAULT_MIN_WIDTH = 30
|
|
||||||
local DEFAULT_MAX_WIDTH = -1
|
|
||||||
local DEFAULT_PADDING = 1
|
|
||||||
|
|
||||||
---@class (exact) View: Class
|
|
||||||
---@field live_filter table
|
|
||||||
---@field side string
|
|
||||||
---@field float table
|
|
||||||
---@field private explorer Explorer
|
|
||||||
---@field private adaptive_size boolean
|
|
||||||
---@field private centralize_selection boolean
|
|
||||||
---@field private hide_root_folder boolean
|
|
||||||
---@field private winopts table
|
|
||||||
---@field private height integer
|
|
||||||
---@field private preserve_window_proportions boolean
|
|
||||||
---@field private initial_width integer
|
|
||||||
---@field private width (fun():integer)|integer|string
|
|
||||||
---@field private max_width integer
|
|
||||||
---@field private padding integer
|
|
||||||
---@field private bufnr_by_tab table<integer, integer> stored per tab until multi-instance is complete
|
|
||||||
local View = Class:extend()
|
|
||||||
|
|
||||||
---@class View
|
|
||||||
---@overload fun(args: ViewArgs): View
|
|
||||||
|
|
||||||
---@class (exact) ViewArgs
|
|
||||||
---@field explorer Explorer
|
|
||||||
|
|
||||||
---@protected
|
|
||||||
---@param args ViewArgs
|
|
||||||
function View:new(args)
|
|
||||||
args.explorer:log_new("View")
|
|
||||||
|
|
||||||
self.explorer = args.explorer
|
|
||||||
self.adaptive_size = false
|
|
||||||
self.centralize_selection = self.explorer.opts.view.centralize_selection
|
|
||||||
self.float = self.explorer.opts.view.float
|
|
||||||
self.height = self.explorer.opts.view.height
|
|
||||||
self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false
|
|
||||||
self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions
|
|
||||||
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
|
|
||||||
self.live_filter = { prev_focused_node = nil, }
|
|
||||||
self.bufnr_by_tab = {}
|
|
||||||
|
|
||||||
self.winopts = {
|
|
||||||
relativenumber = self.explorer.opts.view.relativenumber,
|
|
||||||
number = self.explorer.opts.view.number,
|
|
||||||
list = false,
|
|
||||||
foldenable = false,
|
|
||||||
winfixwidth = true,
|
|
||||||
winfixheight = true,
|
|
||||||
spell = false,
|
|
||||||
signcolumn = self.explorer.opts.view.signcolumn,
|
|
||||||
foldmethod = "manual",
|
|
||||||
foldcolumn = "0",
|
|
||||||
cursorcolumn = false,
|
|
||||||
cursorline = self.explorer.opts.view.cursorline,
|
|
||||||
cursorlineopt = self.explorer.opts.view.cursorlineopt,
|
|
||||||
colorcolumn = "0",
|
|
||||||
wrap = false,
|
|
||||||
winhl = appearance.WIN_HL,
|
|
||||||
}
|
|
||||||
|
|
||||||
self:configure_width(self.explorer.opts.view.width)
|
|
||||||
self.initial_width = self:get_width()
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:destroy()
|
|
||||||
self.explorer:log_destroy("View")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- The initial state of a tab
|
|
||||||
local tabinitial = {
|
|
||||||
-- The position of the cursor { line, column }
|
|
||||||
cursor = { 0, 0 },
|
|
||||||
-- The NvimTree window number
|
|
||||||
winnr = nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
---@type { name: string, value: any }[]
|
|
||||||
local BUFFER_OPTIONS = {
|
|
||||||
{ name = "bufhidden", value = "wipe" },
|
|
||||||
{ name = "buflisted", value = false },
|
|
||||||
{ name = "buftype", value = "nofile" },
|
|
||||||
{ name = "filetype", value = "NvimTree" },
|
|
||||||
{ name = "modifiable", value = false },
|
|
||||||
{ name = "swapfile", value = false },
|
|
||||||
}
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@param bufnr integer
|
|
||||||
---@return boolean
|
|
||||||
function View:matches_bufnr(bufnr)
|
|
||||||
for _, b in pairs(globals.BUFNR_PER_TAB) do
|
|
||||||
if b == bufnr then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
function View:wipe_rogue_buffer()
|
|
||||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
|
||||||
if not self:matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
|
|
||||||
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@param bufnr integer|false|nil
|
|
||||||
function View:create_buffer(bufnr)
|
|
||||||
self:wipe_rogue_buffer()
|
|
||||||
|
|
||||||
local tab = vim.api.nvim_get_current_tabpage()
|
|
||||||
globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
|
|
||||||
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab]
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab)
|
|
||||||
|
|
||||||
bufnr = self:get_bufnr("View:create_buffer2")
|
|
||||||
for _, option in ipairs(BUFFER_OPTIONS) do
|
|
||||||
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
|
|
||||||
end
|
|
||||||
|
|
||||||
require("nvim-tree.keymap").on_attach(self:get_bufnr("View:create_buffer3"))
|
|
||||||
|
|
||||||
events._dispatch_tree_attached_post(self:get_bufnr("View:create_buffer4"))
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@param size (fun():integer)|integer|string
|
|
||||||
---@return integer
|
|
||||||
function View:get_size(size)
|
|
||||||
if type(size) == "number" then
|
|
||||||
return size
|
|
||||||
elseif type(size) == "function" then
|
|
||||||
return self:get_size(size())
|
|
||||||
end
|
|
||||||
local size_as_number = tonumber(size:sub(0, -2))
|
|
||||||
local percent_as_decimal = size_as_number / 100
|
|
||||||
return math.floor(vim.o.columns * percent_as_decimal)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param size (fun():integer)|integer|nil
|
|
||||||
---@return integer
|
|
||||||
function View:get_width(size)
|
|
||||||
if size then
|
|
||||||
return self:get_size(size)
|
|
||||||
else
|
|
||||||
return self:get_size(self.width)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local move_tbl = {
|
|
||||||
left = "H",
|
|
||||||
right = "L",
|
|
||||||
}
|
|
||||||
|
|
||||||
-- setup_tabpage sets up the initial state of a tab
|
|
||||||
---@private
|
|
||||||
---@param tabpage integer
|
|
||||||
---@param callsite string
|
|
||||||
function View:setup_tabpage(tabpage, callsite)
|
|
||||||
local winnr = vim.api.nvim_get_current_win()
|
|
||||||
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s",
|
|
||||||
tabpage,
|
|
||||||
callsite,
|
|
||||||
winnr,
|
|
||||||
globals.TABPAGES[tabpage] and vim.inspect(globals.TABPAGES[tabpage], { newline = "" }) or "tabinitial")
|
|
||||||
end
|
|
||||||
|
|
||||||
globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr })
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
function View:set_window_options_and_buffer()
|
|
||||||
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr("View:set_window_options_and_buffer"))
|
|
||||||
|
|
||||||
if vim.fn.has("nvim-0.10") == 1 then
|
|
||||||
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
|
|
||||||
vim.api.nvim_set_option_value("eventignore", "all", {})
|
|
||||||
|
|
||||||
for k, v in pairs(self.winopts) do
|
|
||||||
vim.api.nvim_set_option_value(k, v, { scope = "local" })
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_set_option_value("eventignore", eventignore, {})
|
|
||||||
else
|
|
||||||
local eventignore = vim.api.nvim_get_option("eventignore") ---@diagnostic disable-line: deprecated
|
|
||||||
vim.api.nvim_set_option("eventignore", "all") ---@diagnostic disable-line: deprecated
|
|
||||||
|
|
||||||
-- #3009 vim.api.nvim_win_set_option does not set local scope without explicit winid.
|
|
||||||
-- Revert to opt_local instead of propagating it through for just the 0.10 path.
|
|
||||||
for k, v in pairs(self.winopts) do
|
|
||||||
vim.opt_local[k] = v
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_set_option("eventignore", eventignore) ---@diagnostic disable-line: deprecated
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@return table
|
|
||||||
function View:open_win_config()
|
|
||||||
if type(self.float.open_win_config) == "function" then
|
|
||||||
return self.float.open_win_config()
|
|
||||||
else
|
|
||||||
return self.float.open_win_config
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
function View:open_window()
|
|
||||||
if self.float.enable then
|
|
||||||
vim.api.nvim_open_win(0, true, self:open_win_config())
|
|
||||||
else
|
|
||||||
vim.api.nvim_command("vsp")
|
|
||||||
self:reposition_window()
|
|
||||||
end
|
|
||||||
self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_window")
|
|
||||||
self:set_window_options_and_buffer()
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param buf integer
|
|
||||||
---@return boolean
|
|
||||||
local function is_buf_displayed(buf)
|
|
||||||
return vim.api.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1
|
|
||||||
end
|
|
||||||
|
|
||||||
---@return number|nil
|
|
||||||
local function get_alt_or_next_buf()
|
|
||||||
local alt_buf = vim.fn.bufnr("#")
|
|
||||||
if is_buf_displayed(alt_buf) then
|
|
||||||
return alt_buf
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
|
|
||||||
if is_buf_displayed(buf) then
|
|
||||||
return buf
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function switch_buf_if_last_buf()
|
|
||||||
if #vim.api.nvim_list_wins() == 1 then
|
|
||||||
local buf = get_alt_or_next_buf()
|
|
||||||
if buf then
|
|
||||||
vim.cmd("sb" .. buf)
|
|
||||||
else
|
|
||||||
vim.cmd("new")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---save_tab_state saves any state that should be preserved across redraws.
|
|
||||||
---@private
|
|
||||||
---@param tabnr integer
|
|
||||||
function View:save_tab_state(tabnr)
|
|
||||||
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
|
|
||||||
globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage, "View:save_tab_state") or 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@param tabpage integer
|
|
||||||
function View:close_internal(tabpage)
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:close_internal(t%s)", tabpage)
|
|
||||||
end
|
|
||||||
if not self:is_visible({ tabpage = tabpage }) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
self:save_tab_state(tabpage)
|
|
||||||
switch_buf_if_last_buf()
|
|
||||||
local tree_win = self:get_winnr(tabpage, "View:close_internal")
|
|
||||||
local current_win = vim.api.nvim_get_current_win()
|
|
||||||
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
|
|
||||||
if vim.api.nvim_win_get_config(win).relative == "" then
|
|
||||||
local prev_win = vim.fn.winnr("#") -- this tab only
|
|
||||||
if tree_win == current_win and prev_win > 0 then
|
|
||||||
vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win))
|
|
||||||
end
|
|
||||||
if vim.api.nvim_win_is_valid(tree_win or 0) then
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:close_internal(t%s) w%s", tabpage, tree_win)
|
|
||||||
end
|
|
||||||
local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true)
|
|
||||||
if not success then
|
|
||||||
notify.debug("Failed to close window: " .. error)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:close_this_tab_only()
|
|
||||||
self:close_internal(vim.api.nvim_get_current_tabpage())
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:close_all_tabs()
|
|
||||||
for tabpage, _ in pairs(globals.TABPAGES) do
|
|
||||||
self:close_internal(tabpage)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param tabpage integer|nil
|
|
||||||
---@param callsite string
|
|
||||||
function View:close(tabpage, callsite)
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:close(t%s, %s)", tabpage, callsite)
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.explorer.opts.tab.sync.close then
|
|
||||||
self:close_all_tabs()
|
|
||||||
elseif tabpage then
|
|
||||||
self:close_internal(tabpage)
|
|
||||||
else
|
|
||||||
self:close_this_tab_only()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param options table|nil
|
|
||||||
function View:open(options)
|
|
||||||
if self:is_visible() then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local profile = log.profile_start("view open")
|
|
||||||
|
|
||||||
events._dispatch_on_tree_pre_open()
|
|
||||||
self:create_buffer()
|
|
||||||
self:open_window()
|
|
||||||
self:resize()
|
|
||||||
|
|
||||||
local opts = options or { focus_tree = true }
|
|
||||||
if not opts.focus_tree then
|
|
||||||
vim.cmd("wincmd p")
|
|
||||||
end
|
|
||||||
events._dispatch_on_tree_open()
|
|
||||||
|
|
||||||
log.profile_end(profile)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
function View:grow()
|
|
||||||
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
|
|
||||||
local lines = vim.api.nvim_buf_get_lines(self:get_bufnr("View:grow1"), starts_at, -1, false)
|
|
||||||
-- number of columns of right-padding to indicate end of path
|
|
||||||
local padding = self:get_size(self.padding)
|
|
||||||
|
|
||||||
-- account for sign/number columns etc.
|
|
||||||
local wininfo = vim.fn.getwininfo(self:get_winnr(nil, "View:grow"))
|
|
||||||
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
|
|
||||||
padding = padding + wininfo[1].textoff
|
|
||||||
end
|
|
||||||
|
|
||||||
local resizing_width = self.initial_width - padding
|
|
||||||
local max_width
|
|
||||||
|
|
||||||
-- maybe bound max
|
|
||||||
if self.max_width == -1 then
|
|
||||||
max_width = -1
|
|
||||||
else
|
|
||||||
max_width = self:get_width(self.max_width) - padding
|
|
||||||
end
|
|
||||||
|
|
||||||
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
|
|
||||||
for line_nr, l in pairs(lines) do
|
|
||||||
local count = vim.fn.strchars(l)
|
|
||||||
-- also add space for right-aligned icons
|
|
||||||
local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr("View:grow2"), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
|
|
||||||
count = count + utils.extmarks_length(extmarks)
|
|
||||||
if resizing_width < count then
|
|
||||||
resizing_width = count
|
|
||||||
end
|
|
||||||
if self.adaptive_size and max_width >= 0 and resizing_width >= max_width then
|
|
||||||
resizing_width = max_width
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self:resize(resizing_width + padding)
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:grow_from_content()
|
|
||||||
if self.adaptive_size then
|
|
||||||
self:grow()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param size string|number|nil
|
|
||||||
function View:resize(size)
|
|
||||||
if self.float.enable and not self.adaptive_size then
|
|
||||||
-- if the floating windows's adaptive size is not desired, then the
|
|
||||||
-- float size should be defined in view.float.open_win_config
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(size) == "string" then
|
|
||||||
size = vim.trim(size)
|
|
||||||
local first_char = size:sub(1, 1)
|
|
||||||
size = tonumber(size)
|
|
||||||
|
|
||||||
if first_char == "+" or first_char == "-" then
|
|
||||||
size = self.width + size
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(size) == "number" and size <= 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if size then
|
|
||||||
self.width = size
|
|
||||||
self.height = size
|
|
||||||
end
|
|
||||||
|
|
||||||
if not self:is_visible() then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local winnr = self:get_winnr(nil, "View:resize") or 0
|
|
||||||
|
|
||||||
local new_size = self:get_width()
|
|
||||||
|
|
||||||
if new_size ~= vim.api.nvim_win_get_width(winnr) then
|
|
||||||
vim.api.nvim_win_set_width(winnr, new_size)
|
|
||||||
if not self.preserve_window_proportions then
|
|
||||||
vim.cmd(":wincmd =")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
events._dispatch_on_tree_resize(new_size)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
function View:reposition_window()
|
|
||||||
local move_to = move_tbl[self.side]
|
|
||||||
vim.api.nvim_command("wincmd " .. move_to)
|
|
||||||
self:resize()
|
|
||||||
end
|
|
||||||
|
|
||||||
---@private
|
|
||||||
---@param callsite string
|
|
||||||
function View:set_current_win(callsite)
|
|
||||||
local current_tab = vim.api.nvim_get_current_tabpage()
|
|
||||||
local current_win = vim.api.nvim_get_current_win()
|
|
||||||
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s",
|
|
||||||
callsite,
|
|
||||||
current_tab,
|
|
||||||
globals.TABPAGES[current_tab].winnr,
|
|
||||||
current_win,
|
|
||||||
(globals.TABPAGES[current_tab].winnr == current_win) and "" or "MISMATCH"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
globals.TABPAGES[current_tab].winnr = current_win
|
|
||||||
end
|
|
||||||
|
|
||||||
---Open the tree in the a window
|
|
||||||
---@param opts OpenInWinOpts|nil
|
|
||||||
function View:open_in_win(opts)
|
|
||||||
opts = opts or { hijack_current_buf = true, resize = true }
|
|
||||||
events._dispatch_on_tree_pre_open()
|
|
||||||
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
|
|
||||||
vim.api.nvim_set_current_win(opts.winid)
|
|
||||||
end
|
|
||||||
self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
|
|
||||||
self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_in_win")
|
|
||||||
self:set_current_win("View:open_in_win")
|
|
||||||
self:set_window_options_and_buffer()
|
|
||||||
if opts.resize then
|
|
||||||
self:reposition_window()
|
|
||||||
self:resize()
|
|
||||||
end
|
|
||||||
events._dispatch_on_tree_open()
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:abandon_current_window()
|
|
||||||
local tab = vim.api.nvim_get_current_tabpage()
|
|
||||||
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s",
|
|
||||||
tab,
|
|
||||||
globals.TABPAGES[tab] and globals.TABPAGES[tab].winnr or nil,
|
|
||||||
globals.BUFNR_PER_TAB[tab],
|
|
||||||
self.bufnr_by_tab[tab],
|
|
||||||
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
|
|
||||||
|
|
||||||
self.bufnr_by_tab[tab] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
-- TODO multi-instance kill the buffer instead of retaining
|
|
||||||
|
|
||||||
globals.BUFNR_PER_TAB[tab] = nil
|
|
||||||
if globals.TABPAGES[tab] then
|
|
||||||
globals.TABPAGES[tab].winnr = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param callsite string
|
|
||||||
function View:abandon_all_windows(callsite)
|
|
||||||
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s",
|
|
||||||
callsite,
|
|
||||||
tab,
|
|
||||||
globals.TABPAGES and globals.TABPAGES.winnr or nil,
|
|
||||||
globals.BUFNR_PER_TAB[tab],
|
|
||||||
self.bufnr_by_tab[tab],
|
|
||||||
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- TODO multi-instance kill the buffer instead of retaining
|
|
||||||
|
|
||||||
globals.BUFNR_PER_TAB[tab] = nil
|
|
||||||
if globals.TABPAGES[tab] then
|
|
||||||
globals.TABPAGES[tab].winnr = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param opts table|nil
|
|
||||||
---@return boolean
|
|
||||||
function View:is_visible(opts)
|
|
||||||
if opts and opts.tabpage then
|
|
||||||
if globals.TABPAGES[opts.tabpage] == nil then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
local winnr = globals.TABPAGES[opts.tabpage].winnr
|
|
||||||
return winnr and vim.api.nvim_win_is_valid(winnr)
|
|
||||||
end
|
|
||||||
|
|
||||||
if opts and opts.any_tabpage then
|
|
||||||
for _, v in pairs(globals.TABPAGES) do
|
|
||||||
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return self:get_winnr(nil, "View:is_visible1") ~= nil and vim.api.nvim_win_is_valid(self:get_winnr(nil, "View:is_visible2") or 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param opts table|nil
|
|
||||||
function View:set_cursor(opts)
|
|
||||||
if self:is_visible() then
|
|
||||||
pcall(vim.api.nvim_win_set_cursor, self:get_winnr(nil, "View:set_cursor"), opts)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param winnr number|nil
|
|
||||||
---@param open_if_closed boolean|nil
|
|
||||||
function View:focus(winnr, open_if_closed)
|
|
||||||
local wnr = winnr or self:get_winnr(nil, "View:focus1")
|
|
||||||
|
|
||||||
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
|
|
||||||
self:close(nil, "View:focus")
|
|
||||||
self:open()
|
|
||||||
wnr = self:get_winnr(nil, "View:focus2")
|
|
||||||
elseif open_if_closed and not self:is_visible() then
|
|
||||||
self:open()
|
|
||||||
end
|
|
||||||
|
|
||||||
if wnr then
|
|
||||||
vim.api.nvim_set_current_win(wnr)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Retrieve the winid of the open tree.
|
|
||||||
---@param opts ApiTreeWinIdOpts|nil
|
|
||||||
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
|
|
||||||
function View:api_winid(opts)
|
|
||||||
local tabpage = opts and opts.tabpage
|
|
||||||
if tabpage == 0 then
|
|
||||||
tabpage = vim.api.nvim_get_current_tabpage()
|
|
||||||
end
|
|
||||||
if self:is_visible({ tabpage = tabpage }) then
|
|
||||||
return self:get_winnr(tabpage, "View:winid")
|
|
||||||
else
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Restores the state of a NvimTree window if it was initialized before.
|
|
||||||
function View:restore_tab_state()
|
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
|
||||||
self:set_cursor(globals.CURSORS[tabpage])
|
|
||||||
end
|
|
||||||
|
|
||||||
--- winid containing the buffer
|
|
||||||
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
|
|
||||||
---@param callsite string
|
|
||||||
---@return integer? winid
|
|
||||||
function View:winid(tabpage, callsite)
|
|
||||||
local bufnr = self.bufnr_by_tab[tabpage]
|
|
||||||
|
|
||||||
local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite)
|
|
||||||
|
|
||||||
if bufnr then
|
|
||||||
for _, w in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do
|
|
||||||
if vim.api.nvim_win_get_buf(w) == bufnr then
|
|
||||||
log.line("dev", "%s b%d : w%s", msg, bufnr, w)
|
|
||||||
return w
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
log.line("dev", "%s no bufnr", msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Returns the window number for nvim-tree within the tabpage specified
|
|
||||||
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
|
|
||||||
---@param callsite string
|
|
||||||
---@return number|nil
|
|
||||||
function View:get_winnr(tabpage, callsite)
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
local msg = string.format("View:get_winnr(%3s, %-20.20s)", tabpage, callsite)
|
|
||||||
|
|
||||||
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
|
||||||
local tabinfo = globals.TABPAGES[tabpage]
|
|
||||||
|
|
||||||
local ret = nil
|
|
||||||
|
|
||||||
if not tabinfo then
|
|
||||||
msg = string.format("%s t%d no tabinfo", msg, tabpage)
|
|
||||||
elseif not tabinfo.winnr then
|
|
||||||
msg = string.format("%s t%d no tabinfo.winnr", msg, tabpage)
|
|
||||||
elseif not vim.api.nvim_win_is_valid(tabinfo.winnr) then
|
|
||||||
msg = string.format("%s t%d invalid tabinfo.winnr %d", msg, tabpage, tabinfo.winnr)
|
|
||||||
else
|
|
||||||
msg = string.format("%s t%d w%d", msg, tabpage, tabinfo.winnr)
|
|
||||||
ret = tabinfo.winnr
|
|
||||||
end
|
|
||||||
|
|
||||||
local winid = self:winid(tabpage, "View:get_winnr")
|
|
||||||
if ret ~= winid then
|
|
||||||
if ret then
|
|
||||||
msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid)
|
|
||||||
else
|
|
||||||
msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid)
|
|
||||||
end
|
|
||||||
notify.error(string.format("View:get_winnr w%s View:winnr w%s MISMATCH", ret, winid))
|
|
||||||
end
|
|
||||||
|
|
||||||
log.line("dev", "%s", msg)
|
|
||||||
|
|
||||||
return ret
|
|
||||||
else
|
|
||||||
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
|
||||||
local tabinfo = globals.TABPAGES[tabpage]
|
|
||||||
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
|
|
||||||
return tabinfo.winnr
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Returns the current nvim tree bufnr
|
|
||||||
---@param callsite string
|
|
||||||
---@return number
|
|
||||||
function View:get_bufnr(callsite)
|
|
||||||
local tab = vim.api.nvim_get_current_tabpage()
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:get_bufnr(%-20.20s) t%d global b%s member b%s %s",
|
|
||||||
callsite,
|
|
||||||
tab,
|
|
||||||
globals.BUFNR_PER_TAB[tab],
|
|
||||||
self.bufnr_by_tab[tab],
|
|
||||||
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
|
|
||||||
|
|
||||||
if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then
|
|
||||||
notify.error(string.format("View:get_bufnr globals.BUFNR_PER_TAB[%s] b%s view.bufnr_by_tab[%s] b%s MISMATCH",
|
|
||||||
tab, globals.BUFNR_PER_TAB[tab],
|
|
||||||
tab, self.bufnr_by_tab[tab]
|
|
||||||
))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return globals.BUFNR_PER_TAB[tab]
|
|
||||||
end
|
|
||||||
|
|
||||||
function View:prevent_buffer_override()
|
|
||||||
local view_winnr = self:get_winnr(nil, "View:prevent_buffer_override")
|
|
||||||
local view_bufnr = self:get_bufnr("View:prevent_buffer_override")
|
|
||||||
|
|
||||||
-- need to schedule to let the new buffer populate the window
|
|
||||||
-- because this event needs to be run on bufWipeout.
|
|
||||||
-- Otherwise the curwin/curbuf would match the view buffer and the view window.
|
|
||||||
vim.schedule(function()
|
|
||||||
local curwin = vim.api.nvim_get_current_win()
|
|
||||||
local curwinconfig = vim.api.nvim_win_get_config(curwin)
|
|
||||||
local curbuf = vim.api.nvim_win_get_buf(curwin)
|
|
||||||
local bufname = vim.api.nvim_buf_get_name(curbuf)
|
|
||||||
|
|
||||||
if not bufname:match("NvimTree") then
|
|
||||||
for i, tabpage in ipairs(globals.TABPAGES) do
|
|
||||||
if tabpage.winnr == view_winnr then
|
|
||||||
if self.explorer.opts.experimental.multi_instance then
|
|
||||||
log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winnr)
|
|
||||||
end
|
|
||||||
|
|
||||||
globals.TABPAGES[i] = nil
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if curwin ~= view_winnr or bufname == "" or curbuf == view_bufnr then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- patch to avoid the overriding window to be fixed in size
|
|
||||||
-- might need a better patch
|
|
||||||
vim.cmd("setlocal nowinfixwidth")
|
|
||||||
vim.cmd("setlocal nowinfixheight")
|
|
||||||
self:open({ focus_tree = false })
|
|
||||||
|
|
||||||
local explorer = require("nvim-tree.core").get_explorer()
|
|
||||||
if explorer then
|
|
||||||
explorer.renderer:draw()
|
|
||||||
end
|
|
||||||
|
|
||||||
pcall(vim.api.nvim_win_close, curwin, { force = true })
|
|
||||||
|
|
||||||
-- to handle opening a file using :e when nvim-tree is on floating mode
|
|
||||||
-- falling back to the current window instead of creating a new one
|
|
||||||
if curwinconfig.relative ~= "" then
|
|
||||||
require("nvim-tree.actions.node.open-file").fn("edit_in_place", bufname)
|
|
||||||
else
|
|
||||||
require("nvim-tree.actions.node.open-file").fn("edit", bufname)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param cwd string|nil
|
|
||||||
---@return boolean
|
|
||||||
function View:is_root_folder_visible(cwd)
|
|
||||||
return cwd ~= "/" and not self.hide_root_folder
|
|
||||||
end
|
|
||||||
|
|
||||||
-- used on ColorScheme event
|
|
||||||
function View:reset_winhl()
|
|
||||||
local winnr = self:get_winnr(nil, "View:reset_winhl1")
|
|
||||||
if winnr and vim.api.nvim_win_is_valid(winnr) then
|
|
||||||
vim.wo[self:get_winnr(nil, "View:reset_winhl2")].winhl = appearance.WIN_HL
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---Check if width determined or calculated on-fly
|
|
||||||
---@return boolean
|
|
||||||
function View:is_width_determined()
|
|
||||||
return type(self.width) ~= "function"
|
|
||||||
end
|
|
||||||
|
|
||||||
---Configure width-related config
|
|
||||||
---@param width string|function|number|table|nil
|
|
||||||
function View:configure_width(width)
|
|
||||||
if type(width) == "table" then
|
|
||||||
self.adaptive_size = true
|
|
||||||
self.width = width.min or DEFAULT_MIN_WIDTH
|
|
||||||
self.max_width = width.max or DEFAULT_MAX_WIDTH
|
|
||||||
self.padding = width.padding or DEFAULT_PADDING
|
|
||||||
elseif width == nil then
|
|
||||||
if self.explorer.opts.view.width ~= nil then
|
|
||||||
-- if we had input config - fallback to it
|
|
||||||
self:configure_width(self.explorer.opts.view.width)
|
|
||||||
else
|
|
||||||
-- otherwise - restore initial width
|
|
||||||
self.width = self.initial_width
|
|
||||||
end
|
|
||||||
else
|
|
||||||
self.adaptive_size = false
|
|
||||||
self.width = width
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return View
|
|
||||||
504
lua/nvim-tree/explorer/window.lua
Normal file
504
lua/nvim-tree/explorer/window.lua
Normal file
@@ -0,0 +1,504 @@
|
|||||||
|
local appearance = require("nvim-tree.appearance")
|
||||||
|
local events = require("nvim-tree.events")
|
||||||
|
local utils = require("nvim-tree.utils")
|
||||||
|
local log = require("nvim-tree.log")
|
||||||
|
local notify = require("nvim-tree.notify")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
|
|
||||||
|
local Class = require("nvim-tree.classic")
|
||||||
|
|
||||||
|
---@class OpenInWinOpts
|
||||||
|
---@field hijack_current_buf boolean|nil default true
|
||||||
|
---@field resize boolean|nil default true
|
||||||
|
---@field winid number|nil 0 or nil for current
|
||||||
|
|
||||||
|
local DEFAULT_MIN_WIDTH = 30
|
||||||
|
local DEFAULT_MAX_WIDTH = -1
|
||||||
|
local DEFAULT_PADDING = 1
|
||||||
|
|
||||||
|
---@class (exact) Window: Class
|
||||||
|
---@field live_filter table
|
||||||
|
---@field side string
|
||||||
|
---@field float table
|
||||||
|
---@field private explorer Explorer
|
||||||
|
---@field private adaptive_size boolean
|
||||||
|
---@field private centralize_selection boolean
|
||||||
|
---@field private hide_root_folder boolean
|
||||||
|
---@field private winopts table
|
||||||
|
---@field private height integer
|
||||||
|
---@field private preserve_window_proportions boolean
|
||||||
|
---@field private initial_width integer
|
||||||
|
---@field private width (fun():integer)|integer|string
|
||||||
|
---@field private max_width integer
|
||||||
|
---@field private padding integer
|
||||||
|
local Window = Class:extend()
|
||||||
|
|
||||||
|
---@class Window
|
||||||
|
---@overload fun(args: WindowArgs): Window
|
||||||
|
|
||||||
|
---@class (exact) WindowArgs
|
||||||
|
---@field explorer Explorer
|
||||||
|
|
||||||
|
---@protected
|
||||||
|
---@param args WindowArgs
|
||||||
|
function Window:new(args)
|
||||||
|
args.explorer:log_new("Window")
|
||||||
|
|
||||||
|
self.explorer = args.explorer
|
||||||
|
self.adaptive_size = false
|
||||||
|
self.centralize_selection = self.explorer.opts.view.centralize_selection
|
||||||
|
self.float = self.explorer.opts.view.float
|
||||||
|
self.height = self.explorer.opts.view.height
|
||||||
|
self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false
|
||||||
|
self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions
|
||||||
|
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
|
||||||
|
self.live_filter = { prev_focused_node = nil, }
|
||||||
|
|
||||||
|
self.winopts = {
|
||||||
|
relativenumber = self.explorer.opts.view.relativenumber,
|
||||||
|
number = self.explorer.opts.view.number,
|
||||||
|
list = false,
|
||||||
|
foldenable = false,
|
||||||
|
winfixwidth = true,
|
||||||
|
winfixheight = true,
|
||||||
|
spell = false,
|
||||||
|
signcolumn = self.explorer.opts.view.signcolumn,
|
||||||
|
foldmethod = "manual",
|
||||||
|
foldcolumn = "0",
|
||||||
|
cursorcolumn = false,
|
||||||
|
cursorline = self.explorer.opts.view.cursorline,
|
||||||
|
cursorlineopt = "both",
|
||||||
|
colorcolumn = "0",
|
||||||
|
wrap = false,
|
||||||
|
winhl = appearance.WIN_HL,
|
||||||
|
}
|
||||||
|
|
||||||
|
self:configure_width(self.explorer.opts.view.width)
|
||||||
|
self.initial_width = self:get_width()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Window:destroy()
|
||||||
|
self.explorer:log_destroy("Window")
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type { name: string, value: any }[]
|
||||||
|
local BUFFER_OPTIONS = {
|
||||||
|
{ name = "bufhidden", value = "wipe" },
|
||||||
|
{ name = "buflisted", value = false },
|
||||||
|
{ name = "buftype", value = "nofile" },
|
||||||
|
{ name = "filetype", value = "NvimTree" },
|
||||||
|
{ name = "modifiable", value = false },
|
||||||
|
{ name = "swapfile", value = false },
|
||||||
|
}
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@param bufnr integer|false|nil
|
||||||
|
function Window:create_buffer(bufnr)
|
||||||
|
view.wipe_rogue_buffer()
|
||||||
|
|
||||||
|
local tab = view.create_buffer(bufnr)
|
||||||
|
|
||||||
|
vim.api.nvim_buf_set_name(view.get_bufnr(), "NvimTree_" .. tab)
|
||||||
|
|
||||||
|
bufnr = view.get_bufnr()
|
||||||
|
for _, option in ipairs(BUFFER_OPTIONS) do
|
||||||
|
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
|
||||||
|
end
|
||||||
|
|
||||||
|
require("nvim-tree.keymap").on_attach(view.get_bufnr())
|
||||||
|
|
||||||
|
events._dispatch_tree_attached_post(view.get_bufnr())
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@param size (fun():integer)|integer|string
|
||||||
|
---@return integer
|
||||||
|
function Window:get_size(size)
|
||||||
|
if type(size) == "number" then
|
||||||
|
return size
|
||||||
|
elseif type(size) == "function" then
|
||||||
|
return self:get_size(size())
|
||||||
|
end
|
||||||
|
local size_as_number = tonumber(size:sub(0, -2))
|
||||||
|
local percent_as_decimal = size_as_number / 100
|
||||||
|
return math.floor(vim.o.columns * percent_as_decimal)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@param size (fun():integer)|integer|nil
|
||||||
|
---@return integer
|
||||||
|
function Window:get_width(size)
|
||||||
|
if size then
|
||||||
|
return self:get_size(size)
|
||||||
|
else
|
||||||
|
return self:get_size(self.width)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
function Window:set_window_options_and_buffer()
|
||||||
|
pcall(vim.api.nvim_command, "buffer " .. view.get_bufnr())
|
||||||
|
|
||||||
|
if vim.fn.has("nvim-0.10") == 1 then
|
||||||
|
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
|
||||||
|
vim.api.nvim_set_option_value("eventignore", "all", {})
|
||||||
|
|
||||||
|
for k, v in pairs(self.winopts) do
|
||||||
|
vim.api.nvim_set_option_value(k, v, { scope = "local" })
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_set_option_value("eventignore", eventignore, {})
|
||||||
|
else
|
||||||
|
local eventignore = vim.api.nvim_get_option("eventignore") ---@diagnostic disable-line: deprecated
|
||||||
|
vim.api.nvim_set_option("eventignore", "all") ---@diagnostic disable-line: deprecated
|
||||||
|
|
||||||
|
-- #3009 vim.api.nvim_win_set_option does not set local scope without explicit winid.
|
||||||
|
-- Revert to opt_local instead of propagating it through for just the 0.10 path.
|
||||||
|
for k, v in pairs(self.winopts) do
|
||||||
|
vim.opt_local[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_set_option("eventignore", eventignore) ---@diagnostic disable-line: deprecated
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
---@return table
|
||||||
|
function Window:open_win_config()
|
||||||
|
if type(self.float.open_win_config) == "function" then
|
||||||
|
return self.float.open_win_config()
|
||||||
|
else
|
||||||
|
return self.float.open_win_config
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
function Window:open_window()
|
||||||
|
if self.float.enable then
|
||||||
|
vim.api.nvim_open_win(0, true, self:open_win_config())
|
||||||
|
else
|
||||||
|
vim.api.nvim_command("vsp")
|
||||||
|
self:reposition_window()
|
||||||
|
end
|
||||||
|
view.setup_tabpage(vim.api.nvim_get_current_tabpage())
|
||||||
|
self:set_window_options_and_buffer()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param buf integer
|
||||||
|
---@return boolean
|
||||||
|
local function is_buf_displayed(buf)
|
||||||
|
return vim.api.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
---@return number|nil
|
||||||
|
local function get_alt_or_next_buf()
|
||||||
|
local alt_buf = vim.fn.bufnr("#")
|
||||||
|
if is_buf_displayed(alt_buf) then
|
||||||
|
return alt_buf
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
|
if is_buf_displayed(buf) then
|
||||||
|
return buf
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function switch_buf_if_last_buf()
|
||||||
|
if #vim.api.nvim_list_wins() == 1 then
|
||||||
|
local buf = get_alt_or_next_buf()
|
||||||
|
if buf then
|
||||||
|
vim.cmd("sb" .. buf)
|
||||||
|
else
|
||||||
|
vim.cmd("new")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param tabpage integer
|
||||||
|
local function close(tabpage)
|
||||||
|
if not view.is_visible({ tabpage = tabpage }) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
view.save_tab_state(tabpage)
|
||||||
|
switch_buf_if_last_buf()
|
||||||
|
local tree_win = view.get_winnr(tabpage)
|
||||||
|
local current_win = vim.api.nvim_get_current_win()
|
||||||
|
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
|
||||||
|
if vim.api.nvim_win_get_config(win).relative == "" then
|
||||||
|
local prev_win = vim.fn.winnr("#") -- this tab only
|
||||||
|
if tree_win == current_win and prev_win > 0 then
|
||||||
|
vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win))
|
||||||
|
end
|
||||||
|
if vim.api.nvim_win_is_valid(tree_win or 0) then
|
||||||
|
local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true)
|
||||||
|
if not success then
|
||||||
|
notify.debug("Failed to close window: " .. error)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Window:close_this_tab_only()
|
||||||
|
close(vim.api.nvim_get_current_tabpage())
|
||||||
|
end
|
||||||
|
|
||||||
|
function Window:close_all_tabs()
|
||||||
|
view.all_tabs_callback(function(t)
|
||||||
|
close(t)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param tabpage integer|nil
|
||||||
|
function Window:close(tabpage)
|
||||||
|
if self.explorer.opts.tab.sync.close then
|
||||||
|
self:close_all_tabs()
|
||||||
|
elseif tabpage then
|
||||||
|
close(tabpage)
|
||||||
|
else
|
||||||
|
self:close_this_tab_only()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param options table|nil
|
||||||
|
function Window:open(options)
|
||||||
|
if view.is_visible() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local profile = log.profile_start("view open")
|
||||||
|
|
||||||
|
events._dispatch_on_tree_pre_open()
|
||||||
|
self:create_buffer()
|
||||||
|
self:open_window()
|
||||||
|
self:resize()
|
||||||
|
|
||||||
|
local opts = options or { focus_tree = true }
|
||||||
|
if not opts.focus_tree then
|
||||||
|
vim.cmd("wincmd p")
|
||||||
|
end
|
||||||
|
events._dispatch_on_tree_open()
|
||||||
|
|
||||||
|
log.profile_end(profile)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
function Window:grow()
|
||||||
|
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
|
||||||
|
local lines = vim.api.nvim_buf_get_lines(view.get_bufnr(), starts_at, -1, false)
|
||||||
|
-- number of columns of right-padding to indicate end of path
|
||||||
|
local padding = self:get_size(self.padding)
|
||||||
|
|
||||||
|
-- account for sign/number columns etc.
|
||||||
|
local wininfo = vim.fn.getwininfo(view.get_winnr())
|
||||||
|
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
|
||||||
|
padding = padding + wininfo[1].textoff
|
||||||
|
end
|
||||||
|
|
||||||
|
local resizing_width = self.initial_width - padding
|
||||||
|
local max_width
|
||||||
|
|
||||||
|
-- maybe bound max
|
||||||
|
if self.max_width == -1 then
|
||||||
|
max_width = -1
|
||||||
|
else
|
||||||
|
max_width = self:get_width(self.max_width) - padding
|
||||||
|
end
|
||||||
|
|
||||||
|
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
|
||||||
|
for line_nr, l in pairs(lines) do
|
||||||
|
local count = vim.fn.strchars(l)
|
||||||
|
-- also add space for right-aligned icons
|
||||||
|
local extmarks = vim.api.nvim_buf_get_extmarks(view.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
|
||||||
|
count = count + utils.extmarks_length(extmarks)
|
||||||
|
if resizing_width < count then
|
||||||
|
resizing_width = count
|
||||||
|
end
|
||||||
|
if self.adaptive_size and max_width >= 0 and resizing_width >= max_width then
|
||||||
|
resizing_width = max_width
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self:resize(resizing_width + padding)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Window:grow_from_content()
|
||||||
|
if self.adaptive_size then
|
||||||
|
self:grow()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param size string|number|nil
|
||||||
|
function Window:resize(size)
|
||||||
|
if self.float.enable and not self.adaptive_size then
|
||||||
|
-- if the floating windows's adaptive size is not desired, then the
|
||||||
|
-- float size should be defined in view.float.open_win_config
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(size) == "string" then
|
||||||
|
size = vim.trim(size)
|
||||||
|
local first_char = size:sub(1, 1)
|
||||||
|
size = tonumber(size)
|
||||||
|
|
||||||
|
if first_char == "+" or first_char == "-" then
|
||||||
|
size = self.width + size
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(size) == "number" and size <= 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if size then
|
||||||
|
self.width = size
|
||||||
|
self.height = size
|
||||||
|
end
|
||||||
|
|
||||||
|
if not view.is_visible() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local winnr = view.get_winnr() or 0
|
||||||
|
|
||||||
|
local new_size = self:get_width()
|
||||||
|
|
||||||
|
if new_size ~= vim.api.nvim_win_get_width(winnr) then
|
||||||
|
vim.api.nvim_win_set_width(winnr, new_size)
|
||||||
|
if not self.preserve_window_proportions then
|
||||||
|
vim.cmd(":wincmd =")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
events._dispatch_on_tree_resize(new_size)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@private
|
||||||
|
function Window:reposition_window()
|
||||||
|
vim.api.nvim_command("wincmd " .. (self.side == "left" and "H" or "L"))
|
||||||
|
self:resize()
|
||||||
|
end
|
||||||
|
|
||||||
|
---Open the tree in the a window
|
||||||
|
---@param opts OpenInWinOpts|nil
|
||||||
|
function Window:open_in_win(opts)
|
||||||
|
opts = opts or { hijack_current_buf = true, resize = true }
|
||||||
|
events._dispatch_on_tree_pre_open()
|
||||||
|
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
|
||||||
|
vim.api.nvim_set_current_win(opts.winid)
|
||||||
|
end
|
||||||
|
self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
|
||||||
|
view.setup_tabpage(vim.api.nvim_get_current_tabpage())
|
||||||
|
view.set_current_win()
|
||||||
|
self:set_window_options_and_buffer()
|
||||||
|
if opts.resize then
|
||||||
|
self:reposition_window()
|
||||||
|
self:resize()
|
||||||
|
end
|
||||||
|
events._dispatch_on_tree_open()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param winnr number|nil
|
||||||
|
---@param open_if_closed boolean|nil
|
||||||
|
function Window:focus(winnr, open_if_closed)
|
||||||
|
local wnr = winnr or view.get_winnr()
|
||||||
|
|
||||||
|
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
|
||||||
|
self:close()
|
||||||
|
self:open()
|
||||||
|
wnr = view.get_winnr()
|
||||||
|
elseif open_if_closed and not view.is_visible() then
|
||||||
|
self:open()
|
||||||
|
end
|
||||||
|
|
||||||
|
if wnr then
|
||||||
|
vim.api.nvim_set_current_win(wnr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Window:prevent_buffer_override()
|
||||||
|
local view_winnr = view.get_winnr()
|
||||||
|
local view_bufnr = view.get_bufnr()
|
||||||
|
|
||||||
|
-- need to schedule to let the new buffer populate the window
|
||||||
|
-- because this event needs to be run on bufWipeout.
|
||||||
|
-- Otherwise the curwin/curbuf would match the view buffer and the view window.
|
||||||
|
vim.schedule(function()
|
||||||
|
local curwin = vim.api.nvim_get_current_win()
|
||||||
|
local curwinconfig = vim.api.nvim_win_get_config(curwin)
|
||||||
|
local curbuf = vim.api.nvim_win_get_buf(curwin)
|
||||||
|
local bufname = vim.api.nvim_buf_get_name(curbuf)
|
||||||
|
|
||||||
|
if not bufname:match("NvimTree") then
|
||||||
|
view.clear_tabpage(view_winnr)
|
||||||
|
end
|
||||||
|
if curwin ~= view_winnr or bufname == "" or curbuf == view_bufnr then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- patch to avoid the overriding window to be fixed in size
|
||||||
|
-- might need a better patch
|
||||||
|
vim.cmd("setlocal nowinfixwidth")
|
||||||
|
vim.cmd("setlocal nowinfixheight")
|
||||||
|
self:open({ focus_tree = false })
|
||||||
|
|
||||||
|
self.explorer.renderer:draw()
|
||||||
|
|
||||||
|
pcall(vim.api.nvim_win_close, curwin, { force = true })
|
||||||
|
|
||||||
|
-- to handle opening a file using :e when nvim-tree is on floating mode
|
||||||
|
-- falling back to the current window instead of creating a new one
|
||||||
|
if curwinconfig.relative ~= "" then
|
||||||
|
require("nvim-tree.actions.node.open-file").fn("edit_in_place", bufname)
|
||||||
|
else
|
||||||
|
require("nvim-tree.actions.node.open-file").fn("edit", bufname)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param cwd string|nil
|
||||||
|
---@return boolean
|
||||||
|
function Window:is_root_folder_visible(cwd)
|
||||||
|
return cwd ~= "/" and not self.hide_root_folder
|
||||||
|
end
|
||||||
|
|
||||||
|
-- used on ColorScheme event
|
||||||
|
function Window:reset_winhl()
|
||||||
|
local winnr = view.get_winnr()
|
||||||
|
if winnr and vim.api.nvim_win_is_valid(winnr) then
|
||||||
|
vim.wo[view.get_winnr()].winhl = appearance.WIN_HL
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check if width determined or calculated on-fly
|
||||||
|
---@return boolean
|
||||||
|
function Window:is_width_determined()
|
||||||
|
return type(self.width) ~= "function"
|
||||||
|
end
|
||||||
|
|
||||||
|
---Configure width-related config
|
||||||
|
---@param width string|function|number|table|nil
|
||||||
|
function Window:configure_width(width)
|
||||||
|
if type(width) == "table" then
|
||||||
|
self.adaptive_size = true
|
||||||
|
self.width = width.min or DEFAULT_MIN_WIDTH
|
||||||
|
self.max_width = width.max or DEFAULT_MAX_WIDTH
|
||||||
|
self.padding = width.padding or DEFAULT_PADDING
|
||||||
|
elseif width == nil then
|
||||||
|
if self.explorer.opts.view.width ~= nil then
|
||||||
|
-- if we had input config - fallback to it
|
||||||
|
self:configure_width(self.explorer.opts.view.width)
|
||||||
|
else
|
||||||
|
-- otherwise - restore initial width
|
||||||
|
self.width = self.initial_width
|
||||||
|
end
|
||||||
|
else
|
||||||
|
self.adaptive_size = false
|
||||||
|
self.width = width
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Window
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
-- global state, to be refactored away during multi-instance
|
|
||||||
|
|
||||||
local M = {
|
|
||||||
-- from View
|
|
||||||
TABPAGES = {},
|
|
||||||
BUFNR_PER_TAB = {},
|
|
||||||
CURSORS = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
return M
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local view = require("nvim-tree.view")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local notify = require("nvim-tree.notify")
|
local notify = require("nvim-tree.notify")
|
||||||
|
|
||||||
@@ -11,11 +12,9 @@ local M = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function M.set_target_win()
|
function M.set_target_win()
|
||||||
local explorer = core.get_explorer()
|
|
||||||
|
|
||||||
local id = vim.api.nvim_get_current_win()
|
local id = vim.api.nvim_get_current_win()
|
||||||
|
local tree_id = view.get_winnr()
|
||||||
if explorer and id == explorer.view:get_winnr(nil, "lib.set_target_win") then
|
if tree_id and id == tree_id then
|
||||||
M.target_winid = 0
|
M.target_winid = 0
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -36,7 +35,7 @@ local function open_view_and_draw()
|
|||||||
local cwd = vim.fn.getcwd()
|
local cwd = vim.fn.getcwd()
|
||||||
|
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:open()
|
explorer.window:open()
|
||||||
end
|
end
|
||||||
|
|
||||||
handle_buf_cwd(cwd)
|
handle_buf_cwd(cwd)
|
||||||
@@ -102,14 +101,14 @@ function M.open(opts)
|
|||||||
M.set_target_win()
|
M.set_target_win()
|
||||||
if not core.get_explorer() or opts.path then
|
if not core.get_explorer() or opts.path then
|
||||||
if opts.path then
|
if opts.path then
|
||||||
core.init(opts.path, "lib.open - opts.path")
|
core.init(opts.path)
|
||||||
else
|
else
|
||||||
local cwd, err = vim.loop.cwd()
|
local cwd, err = vim.loop.cwd()
|
||||||
if not cwd then
|
if not cwd then
|
||||||
notify.error(string.format("current working directory unavailable: %s", err))
|
notify.error(string.format("current working directory unavailable: %s", err))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
core.init(cwd, "lib.open - cwd")
|
core.init(cwd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -117,27 +116,24 @@ function M.open(opts)
|
|||||||
|
|
||||||
if should_hijack_current_buf() then
|
if should_hijack_current_buf() then
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:close_this_tab_only()
|
explorer.window:close_this_tab_only()
|
||||||
explorer.view:open_in_win()
|
explorer.window:open_in_win()
|
||||||
explorer.renderer:draw()
|
explorer.renderer:draw()
|
||||||
end
|
end
|
||||||
elseif opts.winid then
|
elseif opts.winid then
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid })
|
explorer.window:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid })
|
||||||
explorer.renderer:draw()
|
explorer.renderer:draw()
|
||||||
end
|
end
|
||||||
elseif opts.current_window then
|
elseif opts.current_window then
|
||||||
if explorer then
|
if explorer then
|
||||||
explorer.view:open_in_win({ hijack_current_buf = false, resize = false })
|
explorer.window:open_in_win({ hijack_current_buf = false, resize = false })
|
||||||
explorer.renderer:draw()
|
explorer.renderer:draw()
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
open_view_and_draw()
|
open_view_and_draw()
|
||||||
end
|
end
|
||||||
|
view.restore_tab_state()
|
||||||
if explorer then
|
|
||||||
explorer.view:restore_tab_state()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher"
|
---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher"
|
||||||
|
|
||||||
---@type table<LogTypes, boolean>
|
---@type table<LogTypes, boolean>
|
||||||
local types = {}
|
local types = {}
|
||||||
|
|||||||
@@ -1,112 +0,0 @@
|
|||||||
local globals = require("nvim-tree.globals")
|
|
||||||
|
|
||||||
local M = {}
|
|
||||||
|
|
||||||
--- Debugging only.
|
|
||||||
--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab.
|
|
||||||
--- Orphans for inexistent tab_ids are shown at the right.
|
|
||||||
--- lib.target_winid is always shown at the right next to a close button.
|
|
||||||
--- Enable with:
|
|
||||||
--- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()"
|
|
||||||
--- vim.opt.showtabline = 2
|
|
||||||
---@return string
|
|
||||||
function M.tab_line()
|
|
||||||
local tab_ids = vim.api.nvim_list_tabpages()
|
|
||||||
local cur_tab_id = vim.api.nvim_get_current_tabpage()
|
|
||||||
|
|
||||||
local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB)
|
|
||||||
local tabpages = vim.deepcopy(globals.TABPAGES)
|
|
||||||
|
|
||||||
local tl = "%#TabLine#"
|
|
||||||
|
|
||||||
for i, tab_id in ipairs(tab_ids) do
|
|
||||||
-- click to select
|
|
||||||
tl = tl .. "%" .. i .. "T"
|
|
||||||
|
|
||||||
-- style
|
|
||||||
if tab_id == cur_tab_id then
|
|
||||||
tl = tl .. "%#StatusLine#|"
|
|
||||||
else
|
|
||||||
tl = tl .. "|%#TabLine#"
|
|
||||||
end
|
|
||||||
|
|
||||||
-- tab_id itself
|
|
||||||
tl = tl .. " t" .. tab_id
|
|
||||||
|
|
||||||
-- winnr, if present
|
|
||||||
local tp = globals.TABPAGES[tab_id]
|
|
||||||
if tp then
|
|
||||||
tl = tl .. " w" .. (tp.winnr or "nil")
|
|
||||||
else
|
|
||||||
tl = tl .. " "
|
|
||||||
end
|
|
||||||
|
|
||||||
-- bufnr, if present
|
|
||||||
local bpt = globals.BUFNR_PER_TAB[tab_id]
|
|
||||||
if bpt then
|
|
||||||
tl = tl .. " b" .. bpt
|
|
||||||
else
|
|
||||||
tl = tl .. " "
|
|
||||||
end
|
|
||||||
|
|
||||||
tl = tl .. " "
|
|
||||||
|
|
||||||
-- remove actively mapped
|
|
||||||
bufnr_per_tab[tab_id] = nil
|
|
||||||
tabpages[tab_id] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
-- close last and reset
|
|
||||||
tl = tl .. "|%#CursorLine#%T"
|
|
||||||
|
|
||||||
-- collect orphans
|
|
||||||
local orphans = {}
|
|
||||||
for tab_id, bufnr in pairs(bufnr_per_tab) do
|
|
||||||
orphans[tab_id] = orphans[tab_id] or {}
|
|
||||||
orphans[tab_id].bufnr = bufnr
|
|
||||||
end
|
|
||||||
for tab_id, tp in pairs(tabpages) do
|
|
||||||
orphans[tab_id] = orphans[tab_id] or {}
|
|
||||||
orphans[tab_id].winnr = tp.winnr
|
|
||||||
end
|
|
||||||
|
|
||||||
-- right-align
|
|
||||||
tl = tl .. "%=%#TabLine#"
|
|
||||||
|
|
||||||
-- print orphans
|
|
||||||
for tab_id, orphan in pairs(orphans) do
|
|
||||||
-- inexistent tab
|
|
||||||
tl = tl .. "%#error#| t" .. tab_id
|
|
||||||
|
|
||||||
-- maybe winnr
|
|
||||||
if orphan.winnr then
|
|
||||||
tl = tl .. " w" .. (orphan.winnr or "nil")
|
|
||||||
else
|
|
||||||
tl = tl .. " "
|
|
||||||
end
|
|
||||||
|
|
||||||
-- maybe bufnr
|
|
||||||
if orphan.bufnr then
|
|
||||||
tl = tl .. " b" .. orphan.bufnr
|
|
||||||
else
|
|
||||||
tl = tl .. " "
|
|
||||||
end
|
|
||||||
tl = tl .. " "
|
|
||||||
end
|
|
||||||
|
|
||||||
-- target win id and close button
|
|
||||||
tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |"
|
|
||||||
|
|
||||||
return tl
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.setup(opts)
|
|
||||||
if not opts.experimental.multi_instance then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.opt.tabline = "%!v:lua.require('nvim-tree.multi-instance-debug').tab_line()"
|
|
||||||
vim.opt.showtabline = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
||||||
@@ -378,7 +378,7 @@ end
|
|||||||
|
|
||||||
---@private
|
---@private
|
||||||
function Builder:build_header()
|
function Builder:build_header()
|
||||||
if self.explorer.view:is_root_folder_visible(self.explorer.absolute_path) then
|
if self.explorer.window:is_root_folder_visible(self.explorer.absolute_path) then
|
||||||
local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label)
|
local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label)
|
||||||
table.insert(self.lines, root_name)
|
table.insert(self.lines, root_name)
|
||||||
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))
|
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
|
local view = require("nvim-tree.view")
|
||||||
local events = require("nvim-tree.events")
|
local events = require("nvim-tree.events")
|
||||||
|
|
||||||
local Class = require("nvim-tree.classic")
|
local Class = require("nvim-tree.classic")
|
||||||
@@ -101,28 +102,28 @@ function Renderer:render_hl(bufnr, hl_range_args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Renderer:draw()
|
function Renderer:draw()
|
||||||
local bufnr = self.explorer.view:get_bufnr("Renderer:draw")
|
local bufnr = view.get_bufnr()
|
||||||
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
|
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local profile = log.profile_start("draw")
|
local profile = log.profile_start("draw")
|
||||||
|
|
||||||
local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw1") or 0)
|
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr() or 0)
|
||||||
|
|
||||||
local builder = Builder(self.explorer):build()
|
local builder = Builder(self.explorer):build()
|
||||||
|
|
||||||
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)
|
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)
|
||||||
|
|
||||||
if cursor and #builder.lines >= cursor[1] then
|
if cursor and #builder.lines >= cursor[1] then
|
||||||
vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw2") or 0, cursor)
|
vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.explorer.view:grow_from_content()
|
self.explorer.window:grow_from_content()
|
||||||
|
|
||||||
log.profile_end(profile)
|
log.profile_end(profile)
|
||||||
|
|
||||||
events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr(nil, "Renderer:draw3"))
|
events._dispatch_on_tree_rendered(bufnr, view.get_winnr())
|
||||||
end
|
end
|
||||||
|
|
||||||
return Renderer
|
return Renderer
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ function M.find_node(nodes, fn)
|
|||||||
:iterate()
|
:iterate()
|
||||||
|
|
||||||
if node then
|
if node then
|
||||||
if not node.explorer.view:is_root_folder_visible() then
|
if not node.explorer.window:is_root_folder_visible() then
|
||||||
i = i - 1
|
i = i - 1
|
||||||
end
|
end
|
||||||
if node.explorer.live_filter.filter then
|
if node.explorer.live_filter.filter then
|
||||||
@@ -543,10 +543,7 @@ function M.focus_file(path)
|
|||||||
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
|
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
|
||||||
return node.absolute_path == path
|
return node.absolute_path == path
|
||||||
end)
|
end)
|
||||||
local explorer = require("nvim-tree.core").get_explorer()
|
require("nvim-tree.view").set_cursor({ i + 1, 1 })
|
||||||
if explorer then
|
|
||||||
explorer.view:set_cursor({ i + 1, 1 })
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Focus node passed as parameter if visible, otherwise focus first visible parent.
|
---Focus node passed as parameter if visible, otherwise focus first visible parent.
|
||||||
@@ -566,7 +563,7 @@ function M.focus_node_or_parent(node)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
if found_node or node.parent == nil then
|
if found_node or node.parent == nil then
|
||||||
explorer.view:set_cursor({ i + 1, 1 })
|
require("nvim-tree.view").set_cursor({ i + 1, 1 })
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
262
lua/nvim-tree/view.lua
Normal file
262
lua/nvim-tree/view.lua
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
local utils = require("nvim-tree.utils")
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local BUFNR_PER_TAB = {}
|
||||||
|
local CURSORS = {}
|
||||||
|
local TABPAGES = {}
|
||||||
|
|
||||||
|
--- Debugging only.
|
||||||
|
--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab.
|
||||||
|
--- Orphans for inexistent tab_ids are shown at the right.
|
||||||
|
--- lib.target_winid is always shown at the right next to a close button.
|
||||||
|
--- Enable with:
|
||||||
|
--- vim.opt.tabline = "%!v:lua.require('nvim-tree.view').tab_line()"
|
||||||
|
--- vim.opt.showtabline = 2
|
||||||
|
function M.tab_line()
|
||||||
|
local tab_ids = vim.api.nvim_list_tabpages()
|
||||||
|
local cur_tab_id = vim.api.nvim_get_current_tabpage()
|
||||||
|
|
||||||
|
local bufnr_per_tab = vim.deepcopy(BUFNR_PER_TAB)
|
||||||
|
local tabpages = vim.deepcopy(TABPAGES)
|
||||||
|
|
||||||
|
local tl = "%#TabLine#"
|
||||||
|
|
||||||
|
for i, tab_id in ipairs(tab_ids) do
|
||||||
|
-- click to select
|
||||||
|
tl = tl .. "%" .. i .. "T"
|
||||||
|
|
||||||
|
-- style
|
||||||
|
if tab_id == cur_tab_id then
|
||||||
|
tl = tl .. "%#StatusLine#|"
|
||||||
|
else
|
||||||
|
tl = tl .. "|%#TabLine#"
|
||||||
|
end
|
||||||
|
|
||||||
|
-- tab_id itself
|
||||||
|
tl = tl .. " t" .. tab_id
|
||||||
|
|
||||||
|
-- winnr, if present
|
||||||
|
local tp = TABPAGES[tab_id]
|
||||||
|
if tp then
|
||||||
|
tl = tl .. " w" .. tp.winnr
|
||||||
|
else
|
||||||
|
tl = tl .. " "
|
||||||
|
end
|
||||||
|
|
||||||
|
-- bufnr, if present
|
||||||
|
local bpt = BUFNR_PER_TAB[tab_id]
|
||||||
|
if bpt then
|
||||||
|
tl = tl .. " b" .. bpt
|
||||||
|
else
|
||||||
|
tl = tl .. " "
|
||||||
|
end
|
||||||
|
|
||||||
|
tl = tl .. " "
|
||||||
|
|
||||||
|
-- remove actively mapped
|
||||||
|
bufnr_per_tab[tab_id] = nil
|
||||||
|
tabpages[tab_id] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- close last and reset
|
||||||
|
tl = tl .. "|%#CursorLine#%T"
|
||||||
|
|
||||||
|
-- collect orphans
|
||||||
|
local orphans = {}
|
||||||
|
for tab_id, bufnr in pairs(bufnr_per_tab) do
|
||||||
|
orphans[tab_id] = orphans[tab_id] or {}
|
||||||
|
orphans[tab_id].bufnr = bufnr
|
||||||
|
end
|
||||||
|
for tab_id, tp in pairs(tabpages) do
|
||||||
|
orphans[tab_id] = orphans[tab_id] or {}
|
||||||
|
orphans[tab_id].winnr = tp.winnr
|
||||||
|
end
|
||||||
|
|
||||||
|
-- right-align
|
||||||
|
tl = tl .. "%=%#TabLine#"
|
||||||
|
|
||||||
|
-- print orphans
|
||||||
|
for tab_id, orphan in pairs(orphans) do
|
||||||
|
-- inexistent tab
|
||||||
|
tl = tl .. "%#error#| t" .. tab_id
|
||||||
|
|
||||||
|
-- maybe winnr
|
||||||
|
if orphan.winnr then
|
||||||
|
tl = tl .. " w" .. orphan.winnr
|
||||||
|
else
|
||||||
|
tl = tl .. " "
|
||||||
|
end
|
||||||
|
|
||||||
|
-- maybe bufnr
|
||||||
|
if orphan.bufnr then
|
||||||
|
tl = tl .. " b" .. orphan.bufnr
|
||||||
|
else
|
||||||
|
tl = tl .. " "
|
||||||
|
end
|
||||||
|
tl = tl .. " "
|
||||||
|
end
|
||||||
|
|
||||||
|
-- target win id and close button
|
||||||
|
tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |"
|
||||||
|
|
||||||
|
return tl
|
||||||
|
end
|
||||||
|
|
||||||
|
-- The initial state of a tab
|
||||||
|
local tabinitial = {
|
||||||
|
-- The position of the cursor { line, column }
|
||||||
|
cursor = { 0, 0 },
|
||||||
|
-- The NvimTree window number
|
||||||
|
winnr = nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
---@return boolean
|
||||||
|
local function matches_bufnr(bufnr)
|
||||||
|
for _, b in pairs(BUFNR_PER_TAB) do
|
||||||
|
if b == bufnr then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.wipe_rogue_buffer()
|
||||||
|
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
|
if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
|
||||||
|
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer|boolean|nil
|
||||||
|
---@return integer tab
|
||||||
|
function M.create_buffer(bufnr)
|
||||||
|
local tab = vim.api.nvim_get_current_tabpage()
|
||||||
|
BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
|
||||||
|
return tab
|
||||||
|
end
|
||||||
|
|
||||||
|
-- setup_tabpage sets up the initial state of a tab
|
||||||
|
---@param tabpage integer
|
||||||
|
function M.setup_tabpage(tabpage)
|
||||||
|
local winnr = vim.api.nvim_get_current_win()
|
||||||
|
TABPAGES[tabpage] = vim.tbl_extend("force", TABPAGES[tabpage] or tabinitial, { winnr = winnr })
|
||||||
|
end
|
||||||
|
|
||||||
|
-- save_tab_state saves any state that should be preserved across redraws.
|
||||||
|
---@param tabnr integer
|
||||||
|
function M.save_tab_state(tabnr)
|
||||||
|
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
|
||||||
|
CURSORS[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param fn fun(tabpage: integer)
|
||||||
|
function M.all_tabs_callback(fn)
|
||||||
|
for tabpage, _ in pairs(TABPAGES) do
|
||||||
|
fn(tabpage)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.set_current_win()
|
||||||
|
local current_tab = vim.api.nvim_get_current_tabpage()
|
||||||
|
TABPAGES[current_tab].winnr = vim.api.nvim_get_current_win()
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.abandon_current_window()
|
||||||
|
local tab = vim.api.nvim_get_current_tabpage()
|
||||||
|
BUFNR_PER_TAB[tab] = nil
|
||||||
|
if TABPAGES[tab] then
|
||||||
|
TABPAGES[tab].winnr = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.abandon_all_windows()
|
||||||
|
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
|
||||||
|
BUFNR_PER_TAB[tab] = nil
|
||||||
|
if TABPAGES[tab] then
|
||||||
|
TABPAGES[tab].winnr = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param opts table|nil
|
||||||
|
---@return boolean
|
||||||
|
function M.is_visible(opts)
|
||||||
|
if opts and opts.tabpage then
|
||||||
|
if TABPAGES[opts.tabpage] == nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local winnr = TABPAGES[opts.tabpage].winnr
|
||||||
|
return winnr and vim.api.nvim_win_is_valid(winnr)
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts and opts.any_tabpage then
|
||||||
|
for _, v in pairs(TABPAGES) do
|
||||||
|
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return M.get_winnr() ~= nil and vim.api.nvim_win_is_valid(M.get_winnr() or 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param opts table|nil
|
||||||
|
function M.set_cursor(opts)
|
||||||
|
if M.is_visible() then
|
||||||
|
pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Retrieve the winid of the open tree.
|
||||||
|
---@param opts ApiTreeWinIdOpts|nil
|
||||||
|
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
|
||||||
|
function M.winid(opts)
|
||||||
|
local tabpage = opts and opts.tabpage
|
||||||
|
if tabpage == 0 then
|
||||||
|
tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
|
end
|
||||||
|
if M.is_visible({ tabpage = tabpage }) then
|
||||||
|
return M.get_winnr(tabpage)
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Restores the state of a NvimTree window if it was initialized before.
|
||||||
|
function M.restore_tab_state()
|
||||||
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
|
M.set_cursor(CURSORS[tabpage])
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Returns the window number for nvim-tree within the tabpage specified
|
||||||
|
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
|
||||||
|
---@return number|nil
|
||||||
|
function M.get_winnr(tabpage)
|
||||||
|
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
||||||
|
local tabinfo = TABPAGES[tabpage]
|
||||||
|
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
|
||||||
|
return tabinfo.winnr
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Returns the current nvim tree bufnr
|
||||||
|
---@return number
|
||||||
|
function M.get_bufnr()
|
||||||
|
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param winnr number|nil
|
||||||
|
function M.clear_tabpage(winnr)
|
||||||
|
for i, tabpage in ipairs(TABPAGES) do
|
||||||
|
if tabpage.winnr == winnr then
|
||||||
|
TABPAGES[i] = nil
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user