Skip to content

Commit 8b9c9c1

Browse files
committed
refactor(#2826): split global View and instance Window
1 parent 5443944 commit 8b9c9c1

17 files changed

Lines changed: 282 additions & 261 deletions

File tree

lua/nvim-tree.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("nvim-tree.log")
2+
local view = require("nvim-tree.view")
23
local utils = require("nvim-tree.utils")
34
local actions = require("nvim-tree.actions")
45
local core = require("nvim-tree.core")
@@ -73,8 +74,7 @@ function M.change_root(path, bufnr)
7374
end
7475

7576
function M.tab_enter()
76-
local explorer = core.get_explorer()
77-
if explorer and explorer.window:is_visible({ any_tabpage = true }) then
77+
if view.is_visible({ any_tabpage = true }) then
7878
local bufname = vim.api.nvim_buf_get_name(0)
7979

8080
local ft
@@ -89,15 +89,17 @@ function M.tab_enter()
8989
return
9090
end
9191
end
92-
explorer.window:open({ focus_tree = false })
9392

94-
explorer.renderer:draw()
93+
local explorer = core.get_explorer()
94+
if explorer then
95+
explorer.window:open({ focus_tree = false })
96+
explorer.renderer:draw()
97+
end
9598
end
9699
end
97100

98101
function M.open_on_directory()
99-
local explorer = core.get_explorer()
100-
local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.window:is_visible()
102+
local should_proceed = _config.hijack_directories.auto_open or view.is_visible()
101103
if not should_proceed then
102104
return
103105
end
@@ -668,7 +670,9 @@ function M.purge_all_state()
668670
local explorer = core.get_explorer()
669671
if explorer then
670672
explorer.window:close_all_tabs()
671-
explorer.window:abandon_all_windows()
673+
end
674+
view.abandon_all_windows()
675+
if explorer then
672676
require("nvim-tree.git").purge_state()
673677
explorer:destroy()
674678
core.reset_explorer()

lua/nvim-tree/actions/finders/find-file.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("nvim-tree.log")
2+
local view = require("nvim-tree.view")
23
local utils = require("nvim-tree.utils")
34
local core = require("nvim-tree.core")
45

@@ -13,7 +14,7 @@ local running = {}
1314
---@param path string relative or absolute
1415
function M.fn(path)
1516
local explorer = core.get_explorer()
16-
if not explorer or not explorer.window:is_visible() then
17+
if not explorer or not view.is_visible() then
1718
return
1819
end
1920

@@ -83,9 +84,9 @@ function M.fn(path)
8384
end)
8485
:iterate()
8586

86-
if found and explorer.window:is_visible() then
87+
if found and view.is_visible() then
8788
explorer.renderer:draw()
88-
explorer.window:set_cursor({ line, 0 })
89+
view.set_cursor({ line, 0 })
8990
end
9091

9192
running[path_real] = false

lua/nvim-tree/actions/moves/item.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local utils = require("nvim-tree.utils")
2+
local view = require("nvim-tree.view")
23
local core = require("nvim-tree.core")
34
local diagnostics = require("nvim-tree.diagnostics")
45

@@ -66,9 +67,9 @@ local function move(explorer, where, what, skip_gitignored)
6667
end
6768

6869
if nex then
69-
explorer.window:set_cursor({ nex, 0 })
70+
view.set_cursor({ nex, 0 })
7071
elseif vim.o.wrapscan and first then
71-
explorer.window:set_cursor({ first, 0 })
72+
view.set_cursor({ first, 0 })
7273
end
7374
end
7475

@@ -188,13 +189,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored)
188189

189190
-- 4.3)
190191
if node_init.name == ".." then -- root node
191-
explorer.window:set_cursor({ 1, 0 }) -- move to root node (position 1)
192+
view.set_cursor({ 1, 0 }) -- move to root node (position 1)
192193
else
193194
local node_init_line = utils.find_node_line(node_init)
194195
if node_init_line < 0 then
195196
return
196197
end
197-
explorer.window:set_cursor({ node_init_line, 0 })
198+
view.set_cursor({ node_init_line, 0 })
198199
end
199200

200201
-- 4.4)

lua/nvim-tree/actions/moves/parent.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local view = require("nvim-tree.view")
12
local utils = require("nvim-tree.utils")
23

34
local DirectoryNode = require("nvim-tree.node.directory")
@@ -24,15 +25,15 @@ function M.fn(should_close)
2425
local parent = (node:get_parent_of_group() or node).parent
2526

2627
if not parent or not parent.parent then
27-
node.explorer.window:set_cursor({ 1, 0 })
28+
view.set_cursor({ 1, 0 })
2829
return
2930
end
3031

3132
local _, line = utils.find_node(parent.explorer.nodes, function(n)
3233
return n.absolute_path == parent.absolute_path
3334
end)
3435

35-
node.explorer.window:set_cursor({ line + 1, 0 })
36+
view.set_cursor({ line + 1, 0 })
3637
if should_close then
3738
parent.open = false
3839
parent.explorer.renderer:draw()

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local lib = require("nvim-tree.lib")
33
local notify = require("nvim-tree.notify")
44
local utils = require("nvim-tree.utils")
55
local core = require("nvim-tree.core")
6+
local view = require("nvim-tree.view")
67

78
local M = {}
89

@@ -19,10 +20,9 @@ end
1920
---Get all windows in the current tabpage that aren't NvimTree.
2021
---@return table with valid win_ids
2122
local function usable_win_ids()
22-
local explorer = core.get_explorer()
2323
local tabpage = vim.api.nvim_get_current_tabpage()
2424
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
25-
local tree_winid = explorer and explorer.window:get_winnr(tabpage)
25+
local tree_winid = view.get_winnr(tabpage)
2626

2727
return vim.tbl_filter(function(id)
2828
local bufid = vim.api.nvim_win_get_buf(id)
@@ -386,12 +386,7 @@ local function is_already_loaded(filename)
386386
end
387387

388388
local function edit_in_current_buf(filename)
389-
local explorer = core.get_explorer()
390-
391-
if explorer then
392-
explorer.window:abandon_current_window()
393-
end
394-
389+
require("nvim-tree.view").abandon_current_window()
395390
if M.relative_path then
396391
filename = utils.path_relative(filename, vim.fn.getcwd())
397392
end

lua/nvim-tree/actions/tree/find-file.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local core = require("nvim-tree.core")
22
local lib = require("nvim-tree.lib")
3+
local view = require("nvim-tree.view")
34
local finders_find_file = require("nvim-tree.actions.finders.find-file")
45

56
local M = {}
@@ -41,7 +42,7 @@ function M.fn(opts)
4142
end
4243

4344
local explorer = core.get_explorer()
44-
if explorer and explorer.window:is_visible() then
45+
if explorer and view.is_visible() then
4546
-- focus
4647
if opts.focus then
4748
lib.set_target_win()

lua/nvim-tree/actions/tree/open.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local core = require("nvim-tree.core")
22
local lib = require("nvim-tree.lib")
3+
local view = require("nvim-tree.view")
34
local finders_find_file = require("nvim-tree.actions.finders.find-file")
45

56
local M = {}
@@ -25,7 +26,7 @@ function M.fn(opts)
2526

2627
local explorer = core.get_explorer()
2728

28-
if explorer and explorer.window:is_visible() then
29+
if explorer and view.is_visible() then
2930
-- focus
3031
lib.set_target_win()
3132
explorer.window:focus()

lua/nvim-tree/actions/tree/toggle.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local core = require("nvim-tree.core")
22
local lib = require("nvim-tree.lib")
3+
local view = require("nvim-tree.view")
34
local finders_find_file = require("nvim-tree.actions.finders.find-file")
45

56
local M = {}
@@ -42,7 +43,7 @@ function M.fn(opts, no_focus, cwd, bang)
4243
opts.path = nil
4344
end
4445

45-
if explorer and explorer.window:is_visible() then
46+
if explorer and view.is_visible() then
4647
-- close
4748
explorer.window:close()
4849
else

lua/nvim-tree/api.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local core = require("nvim-tree.core")
2+
local view = require("nvim-tree.view")
23
local utils = require("nvim-tree.utils")
34
local actions = require("nvim-tree.actions")
45
local appearance_hi_test = require("nvim-tree.appearance.hi-test")
@@ -140,9 +141,9 @@ Api.tree.focus = Api.tree.open
140141
---@field focus boolean|nil default true
141142

142143
Api.tree.toggle = wrap(actions.tree.toggle.fn)
143-
Api.tree.close = wrap_explorer_member("view", "close")
144-
Api.tree.close_in_this_tab = wrap_explorer_member("view", "close_this_tab_only")
145-
Api.tree.close_in_all_tabs = wrap_explorer_member("view", "close_all_tabs")
144+
Api.tree.close = wrap_explorer_member("window", "close")
145+
Api.tree.close_in_this_tab = wrap_explorer_member("window", "close_this_tab_only")
146+
Api.tree.close_in_all_tabs = wrap_explorer_member("window", "close_all_tabs")
146147
Api.tree.reload = wrap_explorer("reload_explorer")
147148

148149
---@class ApiTreeResizeOpts
@@ -201,12 +202,12 @@ Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf)
201202
---@field tabpage number|nil
202203
---@field any_tabpage boolean|nil default false
203204

204-
Api.tree.is_visible = wrap_explorer_member("view", "is_visible")
205+
Api.tree.is_visible = wrap(view.is_visible)
205206

206207
---@class ApiTreeWinIdOpts
207208
---@field tabpage number|nil default nil
208209

209-
Api.tree.winid = wrap_explorer_member("view", "winid")
210+
Api.tree.winid = wrap(view.winid)
210211

211212
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
212213
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)

lua/nvim-tree/diagnostics.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local core = require("nvim-tree.core")
22
local utils = require("nvim-tree.utils")
3+
local view = require("nvim-tree.view")
34
local log = require("nvim-tree.log")
45

56
local DirectoryNode = require("nvim-tree.node.directory")
@@ -181,15 +182,10 @@ function M.update_coc()
181182
end
182183
log.profile_end(profile)
183184

184-
local explorer = core.get_explorer()
185-
186-
local bufnr
187-
if explorer then
188-
bufnr = explorer.window:get_bufnr()
189-
end
190-
185+
local bufnr = view.get_bufnr()
191186
local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
192187
if should_draw then
188+
local explorer = core.get_explorer()
193189
if explorer then
194190
explorer.renderer:draw()
195191
end

0 commit comments

Comments
 (0)