Skip to content

Commit 8aa4526

Browse files
committed
WIP, start work on creating BaseSource to use class inheritence
1 parent 4c74126 commit 8aa4526

File tree

3 files changed

+119
-261
lines changed

3 files changed

+119
-261
lines changed

lua/neo-tree.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ M.setup = function(config)
268268

269269
-- setup the sources with the combined config
270270
for _, source_name in ipairs(sources) do
271-
src(source_name).setup(M.config[source_name], M.config)
271+
if source_name == "filesystem" then
272+
src(source_name):setup(M.config[source_name], M.config)
273+
else
274+
src(source_name).setup(M.config[source_name], M.config)
275+
end
272276
end
273277

274278
local event_handler = {

lua/neo-tree/sources/common/base_source.lua

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,38 @@ local fs_scan = require("neo-tree.sources.filesystem.lib.fs_scan")
77
local renderer = require("neo-tree.ui.renderer")
88
local events = require("neo-tree.events")
99
local log = require("neo-tree.log")
10+
local inputs = require("neo-tree.ui.inputs")
1011

12+
local state_by_tab = {}
1113
local BaseSource = {}
14+
1215
function BaseSource:new()
13-
local props = {}
16+
local props = { default_config = {} }
1417
setmetatable(props, self)
1518
self.__index = self
1619
return props
1720
end
1821

19-
local default_config = nil
20-
local state_by_tab = {}
22+
function BaseSource:get_default_config()
23+
return self.default_config
24+
end
2125

22-
local get_state = function(tabnr)
26+
function BaseSource:set_default_config(config)
27+
self.default_config = config
28+
end
29+
30+
function BaseSource:get_state(tabnr)
2331
tabnr = tabnr or vim.api.nvim_get_current_tabpage()
2432
local state = state_by_tab[tabnr]
2533
if not state then
26-
state = utils.table_copy(default_config)
34+
state = utils.table_copy(BaseSource:get_default_config())
2735
state.tabnr = tabnr
2836
state_by_tab[tabnr] = state
2937
end
3038
return state
3139
end
3240

33-
local get_path_to_reveal = function()
41+
function BaseSource:get_path_to_reveal()
3442
if vim.bo.filetype == "neo-tree" then
3543
return nil
3644
end
@@ -41,31 +49,31 @@ local get_path_to_reveal = function()
4149
return path
4250
end
4351

44-
BaseSource.subscribe = function(event)
45-
local state = get_state()
52+
function BaseSource:subscribe(event)
53+
local state = BaseSource:get_state()
4654
if not state.subscriptions then
4755
state.subscriptions = {}
4856
end
4957
state.subscriptions[event] = true
5058
events.subscribe(event)
5159
end
5260

53-
BaseSource.unsubscribe = function(event, state)
54-
state = state or get_state()
61+
function BaseSource:unsubscribe(event, state)
62+
state = state or BaseSource:get_state()
5563
if state.subscriptions then
5664
state.subscriptions[event] = false
5765
end
5866
events.unsubscribe(event)
5967
end
6068

61-
BaseSource.close = function()
62-
local state = get_state()
69+
function BaseSource:close()
70+
local state = BaseSource:get_state()
6371
return renderer.close(state)
6472
end
6573

6674
---Redraws the tree with updated diagnostics without scanning the filesystem again.
67-
BaseSource.diagnostics_changed = function(args)
68-
local state = get_state()
75+
function BaseSource:diagnostics_changed(args)
76+
local state = BaseSource:get_state()
6977
args = args or {}
7078
state.diagnostics_lookup = args.diagnostics_lookup
7179
if renderer.window_exists(state) then
@@ -74,48 +82,48 @@ BaseSource.diagnostics_changed = function(args)
7482
end
7583

7684
---Called by autocmds when the cwd dir is changed. This will change the root.
77-
BaseSource.dir_changed = function()
78-
local state = get_state()
85+
function BaseSource:dir_changed()
86+
local state = BaseSource:get_state()
7987
local cwd = vim.fn.getcwd()
8088
if state.path and cwd == state.path then
8189
return
8290
end
8391
if state.path and renderer.window_exists(state) then
84-
BaseSource.navigate(cwd)
92+
BaseSource:navigate(cwd)
8593
end
8694
end
8795

88-
BaseSource.dispose = function(tabnr)
89-
local state = get_state(tabnr)
96+
function BaseSource:dispose(tabnr)
97+
local state = BaseSource:get_state(tabnr)
9098
fs_scan.stop_watchers(state)
9199
renderer.close(state)
92100
for event, subscribed in pairs(state.subscriptions) do
93101
if subscribed then
94-
unsubscribe(event, state)
102+
BaseSource:unsubscribe(event, state)
95103
end
96104
end
97105
state_by_tab[state.tabnr] = nil
98106
end
99107

100-
BaseSource.float = function()
101-
local state = get_state()
108+
function BaseSource:float()
109+
local state = BaseSource:get_state()
102110
state.force_float = true
103-
local path_to_reveal = get_path_to_reveal()
104-
BaseSource.navigate(state.path, path_to_reveal)
111+
local path_to_reveal = BaseSource:get_path_to_reveal()
112+
BaseSource:navigate(state.path, path_to_reveal)
105113
end
106114

107115
---Focus the window, opening it if it is not already open.
108116
---@param path_to_reveal string Node to focus after the items are loaded.
109117
---@param callback function Callback to call after the items are loaded.
110-
BaseSource.focus = function(path_to_reveal, callback)
111-
local state = get_state()
118+
function BaseSource:focus(path_to_reveal, callback)
119+
local state = BaseSource:get_state()
112120
if path_to_reveal then
113-
BaseSource.navigate(state.path, path_to_reveal, callback)
121+
BaseSource:navigate(state.path, path_to_reveal, callback)
114122
else
115123
if renderer.window_exists(state) then
116124
vim.api.nvim_set_current_win(state.winid)
117125
else
118-
BaseSource.navigate(state.path, nil, callback)
126+
BaseSource:navigate(state.path, nil, callback)
119127
end
120128
end
121129
end
@@ -124,50 +132,82 @@ end
124132
---@param path string Path to navigate to. If empty, will navigate to the cwd.
125133
---@param path_to_reveal string Node to focus after the items are loaded.
126134
---@param callback function Callback to call after the items are loaded.
127-
BaseSource.navigate = function(path, path_to_reveal, callback)
128-
local state = get_state()
135+
function BaseSource:navigate(path, path_to_reveal, callback)
136+
local state = BaseSource:get_state()
129137
log.error(state.name .. ".navigate() must be overwritten!")
130138
end
131139

132-
BaseSource.reveal_current_file = function(toggle_if_open)
133-
local state = get_state()
134-
log.error(state.name .. ".reveal_current_file() must be overwritten!")
140+
function BaseSource:reveal_current_file(toggle_if_open)
141+
log.trace("Revealing current file")
142+
if toggle_if_open then
143+
if BaseSource:close() then
144+
-- It was open, and now it's not.
145+
return
146+
end
147+
end
148+
local state = BaseSource:get_state()
149+
require("neo-tree").close_all_except(state.name)
150+
local path = BaseSource:get_path_to_reveal()
151+
if not path then
152+
BaseSource:focus()
153+
return
154+
end
155+
local cwd = state.path
156+
if cwd == nil then
157+
cwd = vim.fn.getcwd()
158+
end
159+
if string.sub(path, 1, string.len(cwd)) ~= cwd then
160+
cwd, _ = utils.split_path(path)
161+
inputs.confirm("File not in cwd. Change cwd to " .. cwd .. "?", function(response)
162+
if response == true then
163+
state.path = cwd
164+
BaseSource:focus(path)
165+
end
166+
end)
167+
return
168+
end
169+
if path then
170+
if not renderer.focus_node(state, path) then
171+
BaseSource:focus(path)
172+
end
173+
end
135174
end
136175

137176
---Redraws the tree without loading items again. Use this after
138177
-- making changes to the nodes that would affect how their components are
139178
-- rendered.
140-
BaseSource.redraw = function()
141-
local state = get_state()
179+
function BaseSource:redraw()
180+
local state = BaseSource:get_state()
142181
if renderer.window_exists(state) then
143182
state.tree:render()
144183
end
145184
end
146185

147186
---Refreshes the tree by loading the items again.
148-
BaseSource.refresh = function(callback)
149-
local state = get_state()
187+
function BaseSource:refresh(callback)
188+
local state = BaseSource:get_state()
150189
if state.path and renderer.window_exists(state) then
151190
if type(callback) ~= "function" then
152191
callback = nil
153192
end
154-
BaseSource.navigate(state.path, nil, callback)
193+
BaseSource:navigate(state.path, nil, callback)
155194
end
156195
end
157196

158197
---Configures the plugin, should be called before the plugin is used.
159198
---@param config table Configuration table containing any keys that the user
160-
--wants to change from the defaults. May be empty to accept default values.
161-
BaseSource.setup = function(config, global_config)
162-
default_config = config
199+
--wants to change from the defaults. BaseSource:y be empty to accept default values.
200+
function BaseSource:setup(config, global_config)
201+
BaseSource:set_default_config(config)
202+
local state = BaseSource:get_state()
163203
log.error(state.name .. ".setup() must be overwritten!")
164204
end
165205

166206
---Opens the tree and displays the current path or cwd.
167207
---@param callback function Callback to call after the items are loaded.
168-
BaseSource.show = function(callback)
169-
local state = get_state()
170-
BaseSource.navigate(state.path, nil, callback)
208+
function BaseSource:show(callback)
209+
local state = BaseSource:get_state()
210+
BaseSource:navigate(state.path, nil, callback)
171211
end
172212

173213
return BaseSource

0 commit comments

Comments
 (0)