Skip to content

Commit 17236fd

Browse files
committed
refactor: closes #69, extract common functionality out of sources
1 parent 805dadb commit 17236fd

File tree

13 files changed

+471
-655
lines changed

13 files changed

+471
-655
lines changed

doc/neo-tree.txt

+11-4
Original file line numberDiff line numberDiff line change
@@ -465,23 +465,30 @@ require("neo-tree.events.queue").define_event(event_name, {
465465
setup = <function>,
466466
seed = <function>,
467467
teardown = <function>,
468-
debounce_frequency = <number>
468+
debounce_frequency = <number>,
469+
once = <boolean>,
470+
cancelled = <boolean>
469471
})
470472

471473
The setup function is run the first time the event is subscribed to. For an
472474
autocmd event, this would define the vim autocmd to connect it to fire_event().
473475

474-
The seed function is run at the begining of every event firing. The diagnostics
476+
The `seed` function is run at the begining of every event firing. The diagnostics
475477
event uses this to collect the diagnostic information and pass it to all
476478
subscribers.
477479

478-
The teardown function is used when the last subscriber unsubscribes, and cleans
480+
The `teardown` function is used when the last subscriber unsubscribes, and cleans
479481
up. This is like Dispose in other languages.
480482

481-
debounce_frequency is the minimum number of milliseconds between each invocation
483+
`debounce_frequenc`y is the minimum number of milliseconds between each invocation
482484
of the event. The first event is gauranteed to fire, as well as the last one, but
483485
in between events may be dropped if this is set to a number greater than zero.
484486

487+
`once` means to only fire this event handler once then mark it as `cancelled`.
488+
489+
`cancelled` means that this event handler will be skipped in all future event
490+
fires, and will be discarded on the next cleanup of the queue.
491+
485492

486493
================================================================================
487494
OTHER SOURCES ~

lua/neo-tree.lua

+31-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local events = require("neo-tree.events")
77
local log = require("neo-tree.log")
88
local popups = require("neo-tree.ui.popups")
99
local highlights = require("neo-tree.ui.highlights")
10+
local manager = require("neo-tree.sources.manager")
1011

1112
-- If you add a new source, you need to add it to the sources table.
1213
-- Each source should have a defaults module that contains the default values
@@ -19,7 +20,7 @@ local sources = {
1920

2021
local M = {}
2122

22-
-- Adding this as a shortcut because the module path is so long.
23+
-- TODO: DEPRECATED in 1.19, remove in 2.0
2324
M.fs = require("neo-tree.sources.filesystem")
2425

2526
local normalize_mappings = function(config)
@@ -68,80 +69,81 @@ local define_events = function()
6869
events_setup = true
6970
end
7071

71-
local src = function(source_name)
72+
local check_source = function(source_name)
7273
if source_name == nil or source_name == "" then
7374
source_name = M.config.default_source
7475
end
75-
local success, source = pcall(require, "neo-tree.sources." .. source_name)
76+
local success, result = pcall(require, "neo-tree.sources." .. source_name)
7677
if not success then
77-
error("Source " .. source_name .. " not found.")
78+
error("Source " .. source_name .. " could not be loaded: ", result)
7879
end
79-
source.name = source_name
80-
return source
80+
return source_name
8181
end
8282

8383
M.close_all_except = function(source_name)
84-
local source = src(source_name)
85-
local target_pos = utils.get_value(M, "config." .. source.name .. ".window.position", "left")
84+
source_name = check_source(source_name)
85+
local target_pos = utils.get_value(M, "config." .. source_name .. ".window.position", "left")
8686
for _, name in ipairs(sources) do
8787
if name ~= source_name then
8888
local pos = utils.get_value(M, "config." .. name .. ".window.position", "left")
8989
if pos == target_pos then
90-
M.close(name)
90+
manager.close(source_name)
9191
end
9292
end
9393
end
94-
M.close_all("float")
94+
renderer.close_all_floating_windows()
9595
end
9696

97-
M.close = function(source_name)
98-
return src(source_name).close()
99-
end
97+
M.close = manager.close
10098

10199
M.close_all = function(at_position)
102100
renderer.close_all_floating_windows()
103101
if type(at_position) == "string" and at_position > "" then
104102
for _, name in ipairs(sources) do
105103
local pos = utils.get_value(M, "config." .. name .. ".window.position", "left")
106104
if pos == at_position then
107-
M.close(name)
105+
manager.close(name)
108106
end
109107
end
110108
else
111109
for _, name in ipairs(sources) do
112-
M.close(name)
110+
manager.close(name)
113111
end
114112
end
115113
end
116114

117115
M.float = function(source_name, toggle_if_open)
118-
source_name = src(source_name).name
116+
source_name = check_source(source_name)
119117
if toggle_if_open then
120118
if renderer.close_floating_window(source_name) then
121119
-- It was open, and now it's not.
122120
return
123121
end
124122
end
125-
M.close_all("float")
126-
M.close(source_name) -- in case this source is open in a sidebar
127-
src(source_name).float()
123+
renderer.close_all_floating_windows()
124+
manager.close(source_name) -- in case this source is open in a sidebar
125+
manager.float(source_name)
128126
end
129127

130128
M.focus = function(source_name, close_others, toggle_if_open)
129+
source_name = check_source(source_name)
131130
if toggle_if_open then
132-
if M.close(source_name) then
131+
if manager.close(source_name) then
133132
-- It was open, and now it's not.
134133
return
135134
end
136135
end
137136
if close_others == nil then
138137
close_others = true
139138
end
140-
local source = src(source_name)
141139
if close_others then
142-
M.close_all_except(source.name)
140+
M.close_all_except(source_name)
143141
end
144-
source.focus()
142+
manager.focus(source_name)
143+
end
144+
145+
M.reveal_current_file = function(source_name, toggle_if_open)
146+
manager.reveal_current_file(source_name, toggle_if_open)
145147
end
146148

147149
M.get_prior_window = function()
@@ -200,26 +202,26 @@ M.win_enter_event = function()
200202
end
201203

202204
M.show = function(source_name, do_not_focus, close_others, toggle_if_open)
205+
source_name = check_source(source_name)
203206
if toggle_if_open then
204-
if M.close(source_name) then
207+
if manager.close(source_name) then
205208
-- It was open, and now it's not.
206209
return
207210
end
208211
end
209212
if close_others == nil then
210213
close_others = true
211214
end
212-
local source = src(source_name)
213215
if close_others then
214-
M.close_all_except(source.name)
216+
M.close_all_except(source_name)
215217
end
216218
if do_not_focus then
217219
local current_win = vim.api.nvim_get_current_win()
218-
source.show(function()
220+
manager.show(source_name, function()
219221
vim.api.nvim_set_current_win(current_win)
220222
end)
221223
else
222-
source.show()
224+
manager.show(source_name)
223225
end
224226
end
225227

@@ -308,7 +310,7 @@ M.setup = function(config)
308310

309311
-- setup the sources with the combined config
310312
for _, source_name in ipairs(sources) do
311-
src(source_name).setup(M.config[source_name], M.config)
313+
manager.setup(source_name, M.config[source_name], M.config)
312314
end
313315

314316
local event_handler = {

lua/neo-tree/sources/buffers/commands.lua

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,42 @@ local vim = vim
44
local cc = require("neo-tree.sources.common.commands")
55
local buffers = require("neo-tree.sources.buffers")
66
local utils = require("neo-tree.utils")
7+
local manager = require("neo-tree.sources.manager")
78

89
local M = {}
910

1011
M.add = function(state)
11-
cc.add(state, buffers.refresh)
12+
cc.add(state, M.refresh)
1213
end
1314

1415
M.buffer_delete = function(state)
1516
local node = state.tree:get_node()
1617
if node then
1718
vim.api.nvim_buf_delete(node.extra.bufnr, { force = false, unload = false })
18-
buffers.refresh()
19+
M.refresh()
1920
end
2021
end
2122
M.close_node = cc.close_node
2223

2324
---Marks node as copied, so that it can be pasted somewhere else.
2425
M.copy_to_clipboard = function(state)
25-
cc.copy_to_clipboard(state, buffers.redraw)
26+
cc.copy_to_clipboard(state, utils.wrap(manager.redraw, "buffers"))
2627
end
2728

2829
---Marks node as cut, so that it can be pasted (moved) somewhere else.
2930
M.cut_to_clipboard = function(state)
30-
cc.cut_to_clipboard(state, buffers.redraw)
31+
cc.cut_to_clipboard(state, utils.wrap(manager.redraw, "buffers"))
3132
end
3233

3334
M.show_debug_info = cc.show_debug_info
3435

3536
---Pastes all items from the clipboard to the current directory.
3637
M.paste_from_clipboard = function(state)
37-
cc.paste_from_clipboard(state, buffers.refresh)
38+
cc.paste_from_clipboard(state, M.refresh)
3839
end
3940

4041
M.delete = function(state)
41-
cc.delete(state, buffers.refresh)
42+
cc.delete(state, M.refresh)
4243
end
4344

4445
---Navigate up one level.
@@ -51,10 +52,10 @@ M.open = cc.open
5152
M.open_split = cc.open_split
5253
M.open_vsplit = cc.open_vsplit
5354

54-
M.refresh = buffers.refresh
55+
M.refresh = utils.wrap(manager.refresh, "buffers")
5556

5657
M.rename = function(state)
57-
cc.rename(state, buffers.refresh)
58+
cc.rename(state, M.refresh)
5859
end
5960

6061
M.set_root = function(state)

0 commit comments

Comments
 (0)