@@ -7,30 +7,38 @@ local fs_scan = require("neo-tree.sources.filesystem.lib.fs_scan")
7
7
local renderer = require (" neo-tree.ui.renderer" )
8
8
local events = require (" neo-tree.events" )
9
9
local log = require (" neo-tree.log" )
10
+ local inputs = require (" neo-tree.ui.inputs" )
10
11
12
+ local state_by_tab = {}
11
13
local BaseSource = {}
14
+
12
15
function BaseSource :new ()
13
- local props = {}
16
+ local props = { default_config = {} }
14
17
setmetatable (props , self )
15
18
self .__index = self
16
19
return props
17
20
end
18
21
19
- local default_config = nil
20
- local state_by_tab = {}
22
+ function BaseSource :get_default_config ()
23
+ return self .default_config
24
+ end
21
25
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 )
23
31
tabnr = tabnr or vim .api .nvim_get_current_tabpage ()
24
32
local state = state_by_tab [tabnr ]
25
33
if not state then
26
- state = utils .table_copy (default_config )
34
+ state = utils .table_copy (BaseSource : get_default_config () )
27
35
state .tabnr = tabnr
28
36
state_by_tab [tabnr ] = state
29
37
end
30
38
return state
31
39
end
32
40
33
- local get_path_to_reveal = function ()
41
+ function BaseSource : get_path_to_reveal ()
34
42
if vim .bo .filetype == " neo-tree" then
35
43
return nil
36
44
end
@@ -41,31 +49,31 @@ local get_path_to_reveal = function()
41
49
return path
42
50
end
43
51
44
- BaseSource . subscribe = function (event )
45
- local state = get_state ()
52
+ function BaseSource : subscribe (event )
53
+ local state = BaseSource : get_state ()
46
54
if not state .subscriptions then
47
55
state .subscriptions = {}
48
56
end
49
57
state .subscriptions [event ] = true
50
58
events .subscribe (event )
51
59
end
52
60
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 ()
55
63
if state .subscriptions then
56
64
state .subscriptions [event ] = false
57
65
end
58
66
events .unsubscribe (event )
59
67
end
60
68
61
- BaseSource . close = function ()
62
- local state = get_state ()
69
+ function BaseSource : close ()
70
+ local state = BaseSource : get_state ()
63
71
return renderer .close (state )
64
72
end
65
73
66
74
--- 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 ()
69
77
args = args or {}
70
78
state .diagnostics_lookup = args .diagnostics_lookup
71
79
if renderer .window_exists (state ) then
@@ -74,48 +82,48 @@ BaseSource.diagnostics_changed = function(args)
74
82
end
75
83
76
84
--- 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 ()
79
87
local cwd = vim .fn .getcwd ()
80
88
if state .path and cwd == state .path then
81
89
return
82
90
end
83
91
if state .path and renderer .window_exists (state ) then
84
- BaseSource . navigate (cwd )
92
+ BaseSource : navigate (cwd )
85
93
end
86
94
end
87
95
88
- BaseSource . dispose = function (tabnr )
89
- local state = get_state (tabnr )
96
+ function BaseSource : dispose (tabnr )
97
+ local state = BaseSource : get_state (tabnr )
90
98
fs_scan .stop_watchers (state )
91
99
renderer .close (state )
92
100
for event , subscribed in pairs (state .subscriptions ) do
93
101
if subscribed then
94
- unsubscribe (event , state )
102
+ BaseSource : unsubscribe (event , state )
95
103
end
96
104
end
97
105
state_by_tab [state .tabnr ] = nil
98
106
end
99
107
100
- BaseSource . float = function ()
101
- local state = get_state ()
108
+ function BaseSource : float ()
109
+ local state = BaseSource : get_state ()
102
110
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 )
105
113
end
106
114
107
115
--- Focus the window, opening it if it is not already open.
108
116
--- @param path_to_reveal string Node to focus after the items are loaded.
109
117
--- @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 ()
112
120
if path_to_reveal then
113
- BaseSource . navigate (state .path , path_to_reveal , callback )
121
+ BaseSource : navigate (state .path , path_to_reveal , callback )
114
122
else
115
123
if renderer .window_exists (state ) then
116
124
vim .api .nvim_set_current_win (state .winid )
117
125
else
118
- BaseSource . navigate (state .path , nil , callback )
126
+ BaseSource : navigate (state .path , nil , callback )
119
127
end
120
128
end
121
129
end
@@ -124,50 +132,82 @@ end
124
132
--- @param path string Path to navigate to. If empty , will navigate to the cwd.
125
133
--- @param path_to_reveal string Node to focus after the items are loaded.
126
134
--- @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 ()
129
137
log .error (state .name .. " .navigate() must be overwritten!" )
130
138
end
131
139
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
135
174
end
136
175
137
176
--- Redraws the tree without loading items again. Use this after
138
177
-- making changes to the nodes that would affect how their components are
139
178
-- rendered.
140
- BaseSource . redraw = function ()
141
- local state = get_state ()
179
+ function BaseSource : redraw ()
180
+ local state = BaseSource : get_state ()
142
181
if renderer .window_exists (state ) then
143
182
state .tree :render ()
144
183
end
145
184
end
146
185
147
186
--- 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 ()
150
189
if state .path and renderer .window_exists (state ) then
151
190
if type (callback ) ~= " function" then
152
191
callback = nil
153
192
end
154
- BaseSource . navigate (state .path , nil , callback )
193
+ BaseSource : navigate (state .path , nil , callback )
155
194
end
156
195
end
157
196
158
197
--- Configures the plugin, should be called before the plugin is used.
159
198
--- @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 ()
163
203
log .error (state .name .. " .setup() must be overwritten!" )
164
204
end
165
205
166
206
--- Opens the tree and displays the current path or cwd.
167
207
--- @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 )
171
211
end
172
212
173
213
return BaseSource
0 commit comments