/
lib.lua
286 lines (248 loc) · 6.42 KB
/
lib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
local api = vim.api
local luv = vim.loop
local renderer = require'lib.renderer'
local config = require'lib.config'
local git = require'lib.git'
local pops = require'lib.populate'
local populate = pops.populate
local refresh_entries = pops.refresh_entries
local window_opts = config.window_options()
local M = {}
M.Tree = {
entries = {},
buf_name = 'LuaTree',
cwd = nil,
win_width = vim.g.lua_tree_width or 30,
loaded = false,
bufnr = nil,
winnr = nil,
buf_options = {
'sidescroll=5',
'noswapfile',
'splitbelow',
'noruler',
'noshowmode',
'noshowcmd'
},
win_options = {
relativenumber = false,
number = false,
list = false,
winfixwidth = true,
winfixheight = true,
foldenable = false,
spell = false,
foldmethod = 'manual',
foldcolumn = '0'
}
}
function M.init(with_open, with_render)
M.Tree.cwd = luv.cwd()
populate(M.Tree.entries, M.Tree.cwd, M.Tree)
local stat = luv.fs_stat(M.Tree.cwd)
M.Tree.last_modified = stat.mtime.sec
if with_open then
M.open()
end
if with_render then
renderer.draw(M.Tree, true)
M.Tree.loaded = true
end
end
local function get_node_at_line(line)
local index = 2
local function iter(entries)
for _, node in ipairs(entries) do
if index == line then
return node
end
index = index + 1
if node.open == true then
local child = iter(node.entries)
if child ~= nil then return child end
end
end
end
return iter
end
function M.get_node_at_cursor()
local cursor = api.nvim_win_get_cursor(M.Tree.winnr)
local line = cursor[1]
if line == 1 and M.Tree.cwd ~= "/" then
return { name = ".." }
end
if M.Tree.cwd == "/" then
line = line + 1
end
return get_node_at_line(line)(M.Tree.entries)
end
function M.unroll_dir(node)
node.open = not node.open
if #node.entries > 0 then
renderer.draw(M.Tree, true)
else
populate(node.entries, node.absolute_path)
renderer.draw(M.Tree, true)
end
end
local function refresh_git(node)
git.update_status(node.entries, node.absolute_path or node.cwd)
for _, entry in pairs(node.entries) do
if entry.entries ~= nil then
refresh_git(entry)
end
end
end
-- TODO update only entries where directory has changed
local function refresh_nodes(node)
refresh_entries(node.entries, node.absolute_path or node.cwd)
for _, entry in ipairs(node.entries) do
if entry.entries and entry.open then
refresh_nodes(entry)
end
end
end
function M.refresh_tree()
-- local stat = luv.fs_stat(M.Tree.cwd)
-- if stat.mtime.sec ~= M.Tree.last_modified then
refresh_nodes(M.Tree)
-- end
if config.get_icon_state().show_git_icon then
git.reload_roots()
refresh_git(M.Tree)
end
if M.win_open() then
renderer.draw(M.Tree, true)
else
M.Tree.loaded = false
end
end
function M.set_index_and_redraw(fname)
local i
if M.Tree.cwd == '/' then
i = 0
else
i = 1
end
local reload = false
local function iter(entries)
for _, entry in ipairs(entries) do
i = i + 1
if entry.absolute_path == fname then
return i
end
if fname:match(entry.match_path..'/') ~= nil then
if #entry.entries == 0 then
reload = true
populate(entry.entries, entry.absolute_path)
end
if entry.open == false then
reload = true
entry.open = true
end
if iter(entry.entries) ~= nil then
return i
end
elseif entry.open == true then
iter(entry.entries)
end
end
end
local index = iter(M.Tree.entries)
if not M.win_open() then
M.Tree.loaded = false
return
end
renderer.draw(M.Tree, reload)
if index then
api.nvim_win_set_cursor(M.Tree.winnr, {index, 0})
end
end
function M.open_file(mode, filename)
api.nvim_command('noautocmd wincmd '..window_opts.open_command)
if mode == 'preview' then
api.nvim_command(string.format("edit %s", filename))
api.nvim_command('noautocmd wincmd '..window_opts.preview_command)
else
api.nvim_command(string.format("%s %s", mode, filename))
end
end
function M.change_dir(foldername)
api.nvim_command('cd '..foldername)
M.Tree.entries = {}
M.init(false, M.Tree.bufnr ~= nil)
end
local function set_mappings()
if vim.g.lua_tree_disable_keybindings == 1 then
return
end
local buf = M.Tree.bufnr
local bindings = config.get_bindings()
local mappings = {
['<2-LeftMouse>'] = 'on_keypress("edit")';
['<2-RightMouse>'] = 'on_keypress("cd")';
[bindings.cd] = 'on_keypress("cd")';
[bindings.edit] = 'on_keypress("edit")';
[bindings.edit_vsplit] = 'on_keypress("vsplit")';
[bindings.edit_split] = 'on_keypress("split")';
[bindings.edit_tab] = 'on_keypress("tabnew")';
[bindings.create] = 'on_keypress("create")';
[bindings.remove] = 'on_keypress("remove")';
[bindings.rename] = 'on_keypress("rename")';
[bindings.preview] = 'on_keypress("preview")';
gx = "xdg_open()";
}
for k,v in pairs(mappings) do
api.nvim_buf_set_keymap(buf, 'n', k, ':lua require"tree".'..v..'<cr>', {
nowait = true, noremap = true, silent = true
})
end
end
local function create_buf()
local options = {
bufhidden = 'wipe';
buftype = 'nofile';
modifiable = false;
}
M.Tree.bufnr = api.nvim_create_buf(false, true)
api.nvim_buf_set_name(M.Tree.bufnr, M.Tree.buf_name)
for opt, val in pairs(options) do
api.nvim_buf_set_option(M.Tree.bufnr, opt, val)
end
set_mappings()
end
local function create_win()
api.nvim_command("vsplit")
api.nvim_command("wincmd "..window_opts.side)
api.nvim_command("vertical resize "..M.Tree.win_width)
M.Tree.winnr = api.nvim_get_current_win()
for opt, val in pairs(M.Tree.win_options) do
api.nvim_win_set_option(M.Tree.winnr, opt, val)
end
end
function M.close()
api.nvim_win_close(M.Tree.winnr, true)
M.Tree.winnr = nil
M.Tree.bufnr = nil
end
function M.open()
create_buf()
create_win()
api.nvim_win_set_buf(M.Tree.winnr, M.Tree.bufnr)
for _, opt in pairs(M.Tree.buf_options) do
api.nvim_command('setlocal '..opt)
end
api.nvim_command('setlocal '..window_opts.split_command)
renderer.draw(M.Tree, not M.Tree.loaded)
M.Tree.loaded = true
api.nvim_buf_set_option(M.Tree.bufnr, 'filetype', M.Tree.buf_name)
end
function M.win_open()
for _, win in pairs(api.nvim_list_wins()) do
if win == M.Tree.winnr then
return true
end
end
return false
end
return M