-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
neo-tree.lua
521 lines (474 loc) · 16 KB
/
neo-tree.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
local util = require('one.util')
local executable = vim.fn.executable
local M = {
'nvim-neo-tree/neo-tree.nvim',
branch = 'v3.x',
config = function(config)
require('neo-tree').setup(config.neotree)
end,
}
local function keymapCopy(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filepath = node:get_id()
local filename = node.name
local modify = vim.fn.fnamemodify
local results = {
filepath,
modify(filepath, ':.'),
modify(filepath, ':~'),
filename,
modify(filename, ':r'),
modify(filename, ':e'),
}
-- absolute path to clipboard
vim.ui.select({
'1. Absolute path: ' .. results[1],
'2. Path relative to CWD: ' .. results[2],
'3. Path relative to HOME: ' .. results[3],
'4. Filename: ' .. results[4],
'5. Filename without extension: ' .. results[5],
'6. Extension of the filename: ' .. results[6],
}, { prompt = 'Choose to copy to clipboard:' }, function(choice)
if not choice then return end
local i = tonumber(choice:sub(1, 1))
local result = results[i]
vim.ui.select({
'Copy to nvim clipboard',
'Copy to system clipboard',
}, { prompt = 'Copy to' }, function(r)
if not r then return end
if r == 'Copy to nvim clipboard' then
vim.fn.setreg('"', result)
else
vim.fn.setreg('+', result)
end
vim.notify('Copied: ' .. result)
end)
end)
end
local moveToLastSibling = function(state)
local tree = state.tree
local node = tree:get_node()
local siblings = tree:get_nodes(node:get_parent_id())
require('neo-tree.ui.renderer').focus_node(state, siblings[#siblings]:get_id())
end
local moveToFirstSibling = function(state)
local tree = state.tree
local node = tree:get_node()
local siblings = tree:get_nodes(node:get_parent_id())
require('neo-tree.ui.renderer').focus_node(state, siblings[1]:get_id())
end
local moveToPreviousSibling = function(state)
local tree = state.tree
local node = tree:get_node()
local siblings = tree:get_nodes(node:get_parent_id())
local idx = 0
for i, v in pairs(siblings) do
if v == node then
idx = i - 1
break
end
end
if idx > 0 then --
require('neo-tree.ui.renderer').focus_node(state, siblings[idx]:get_id())
end
end
local moveToNextSibling = function(state)
local tree = state.tree
local node = tree:get_node()
local siblings = tree:get_nodes(node:get_parent_id())
local idx = 0
for i, v in pairs(siblings) do
if v == node then
idx = i + 1
break
end
end
if idx > 0 and idx <= #siblings then --
require('neo-tree.ui.renderer').focus_node(state, siblings[idx]:get_id())
end
end
M.defaultConfig = function(config)
local symbols = config.symbolMap
local filter = require('neo-tree.sources.filesystem.lib.filter')
local hide_by_name = {}
util.tbl_concat(hide_by_name, config.ignore.fileSearch.directories)
util.tbl_concat(hide_by_name, config.ignore.fileSearch.files)
local components = {
symlink = function(conf, node, state)
if node.is_link then
return {
text = string.format(conf.format or '→ %s', util.relative(node.path, node.link_to)),
highlight = conf.highlight or 'NeoTreeSymbolicLinkTarget',
}
else
return {}
end
end,
exectuable = function(conf, node)
local path = node.path
if node.is_link then path = node.link_to end
local text = ''
if executable(path) == 1 then text = conf.symbol or symbols.EXECUTABLE end
return { text = text, highlight = conf.highlight or 'NeoTreeFileExecutable' }
end,
}
local conf = {
'neotree',
{
sources = { 'filesystem', 'buffers', 'git_status' },
log_level = 'warn',
close_if_last_window = true, -- Close Neo-tree if it is the last window left in the tab
popup_border_style = 'rounded',
enable_git_status = true,
enable_diagnostics = true,
sort_case_insensitive = false, -- used when sorting files and directories in the tree
sort_function = nil, -- use a custom function for sorting files and directories in the tree
use_popups_for_input = true,
default_component_configs = {
container = {
enable_character_fade = false,
-- width = 'fit_content',
},
indent = {
indent_size = 2,
padding = 1, -- extra padding on left hand side
-- indent guides
with_markers = true,
indent_marker = '│',
last_indent_marker = '└',
highlight = 'NeoTreeIndentMarker',
-- expander config, needed for nesting files
with_expanders = nil, -- if nil and file nesting is enabled, will enable expanders
expander_collapsed = symbols.COLLAPSED_L,
expander_expanded = symbols.EXPANDED_L,
expander_highlight = 'NeoTreeExpander',
},
icon = {
folder_closed = symbols.FOLDER_CLOSED,
folder_open = symbols.FOLDER_OPEN,
folder_empty = symbols.FOLDER_EMPTY,
-- The next two settings are only a fallback, if you use nvim-web-devicons and configure default icons there
-- then these will never be used.
default = symbols.FILE,
highlight = 'NeoTreeFileIcon',
},
modified = { symbol = '', highlight = 'NeoTreeModified' },
name = { trailing_slash = false, use_git_status_colors = true, highlight = 'NeoTreeFileName' },
git_status = {
symbols = {
added = symbols.ADDED,
modified = symbols.MODIFIED,
deleted = symbols.DELETED,
renamed = symbols.RENAMED,
untracked = symbols.GIT_UNTRACKED,
ignored = symbols.GIT_IGNORED,
unstaged = symbols.GIT_UNSTAGED,
staged = symbols.GIT_STAGED,
conflict = symbols.GIT_CONFLICT,
},
},
},
renderers = {
directory = {
{ 'indent' },
{ 'icon' },
{ 'current_filter' },
{
'container',
content = {
{ 'name', zindex = 10 },
{ 'symlink', zindex = 10, highlight = 'NeoTreeSymbolicLinkTarget' },
{ 'clipboard', zindex = 10 },
{ 'diagnostics', errors_only = true, zindex = 20, align = 'right', hide_when_expanded = true },
{ 'git_status', zindex = 20, align = 'right', hide_when_expanded = true },
},
},
},
file = {
{ 'indent' },
{ 'icon' },
{
'container',
content = {
{ 'name', zindex = 10 },
{ 'exectuable', zindex = 10, align = 'right' },
{ 'symlink', zindex = 10, highlight = 'NeoTreeSymbolicLinkTarget' },
{ 'clipboard', zindex = 10 },
{ 'bufnr', zindex = 10 },
{ 'modified', zindex = 20, align = 'right' },
{ 'diagnostics', zindex = 20, align = 'right' },
{ 'git_status', zindex = 20, align = 'right' },
},
},
},
},
window = {
position = 'left',
width = 35,
mapping_options = { noremap = true, nowait = true },
mappings = {
['?'] = 'show_help',
['q'] = 'close_window',
['<C-r>'] = 'refresh',
['R'] = 'refresh',
['r'] = 'rename',
['<2-LeftMouse>'] = 'open_with_window_picker',
['<cr>'] = 'open_with_window_picker',
['o'] = 'open_with_window_picker',
['l'] = 'open_with_window_picker',
['h'] = 'close_node',
['O'] = function(state)
local node = state.tree:get_node()
local path = node:get_id()
-- :h feature-list
if vim.fn.has('mac') then
vim.api.nvim_command('silent !open -R ' .. path) -- Open it in Finder
elseif vim.fn.has('linux') then
vim.api.nvim_command(string.format('silent !xdg-open "%s"', path))
end
end,
['C'] = 'close_node',
['s'] = 'split_with_window_picker',
['v'] = 'vsplit_with_window_picker',
['t'] = 'open_tabnew',
['w'] = false,
['<space>'] = false,
['<bs>'] = false,
['z'] = 'close_all_nodes',
['Z'] = 'expand_all_nodes',
['d'] = 'delete',
['Y'] = keymapCopy,
['y'] = 'copy_to_clipboard',
['x'] = 'cut_to_clipboard',
['p'] = 'paste_from_clipboard',
['P'] = 'toggle_preview',
['a'] = {
'add',
-- some commands may take optional config options, see `:h neo-tree-mappings` for details
config = {
show_path = 'relative', -- 'none', 'relative', 'absolute'
},
},
['A'] = {
'add_directory',
config = {
show_path = 'relative', -- 'none', 'relative', 'absolute'
},
},
['c'] = {
'copy',
config = {
show_path = 'relative', -- 'none', 'relative', 'absolute'
},
},
['m'] = {
'move',
config = {
show_path = 'relative', -- 'none', 'relative', 'absolute'
},
},
['<'] = 'prev_source',
['>'] = 'next_source',
['<c-j>'] = moveToNextSibling,
['<c-k>'] = moveToPreviousSibling,
['J'] = moveToLastSibling,
['K'] = moveToFirstSibling,
},
},
nesting_rules = {},
filesystem = {
filtered_items = {
visible = false, -- when true, they will just be displayed differently than normal items
hide_dotfiles = false,
hide_gitignored = true,
hide_hidden = true, -- only works on Windows for hidden files/directories
hide_by_name = hide_by_name,
hide_by_pattern = { -- uses glob style patterns
-- '*.meta',
-- '*/src/*/tsconfig.json',
},
always_show = { -- remains visible even if other settings would normally hide it
-- '.gitignored',
},
never_show = { -- remains hidden even if visible is toggled to true, this overrides always_show
'.DS_Store',
'thumbs.db',
},
},
follow_current_file = {
enabled = false, -- This will find and focus the file in the active buffer every time the current file is changed while the tree is open.
leave_dirs_open = false, -- `false` closes auto expanded dirs, such as with `:Neotree reveal`
},
group_empty_dirs = false, -- when true, empty folders will be grouped together
hijack_netrw_behavior = 'open_default', -- netrw disabled, opening a directory opens neo-tree
-- in whatever position is specified in window.position
-- 'open_current', -- netrw disabled, opening a directory opens within the
-- window like netrw would, regardless of window.position
-- 'disabled', -- netrw left alone, neo-tree does not handle opening dirs
use_libuv_file_watcher = true, -- This will use the OS level file watchers to detect changes
-- instead of relying on nvim autocmd events.
window = {
mappings = {
['<C-h>'] = 'navigate_up',
['<C-l>'] = 'set_root',
['H'] = 'toggle_hidden',
['/'] = function(state)
filter.show_filter(state, true, true) -- let fuzzy_finder search as you type
end,
['<C-/>'] = 'clear_filter',
['D'] = 'fuzzy_finder_directory',
['f'] = 'filter_on_submit',
['F'] = 'clear_filter',
['[g'] = 'prev_git_modified',
[']g'] = 'next_git_modified',
},
fuzzy_finder_mappings = { -- use a custom function for defining keymaps in popup input window
['<down>'] = 'move_cursor_down',
['<C-n>'] = 'move_cursor_down',
['<up>'] = 'move_cursor_up',
['<C-p>'] = 'move_cursor_up',
['<C-j>'] = 'move_cursor_down',
['<C-k>'] = 'move_cursor_up',
['<C-u>'] = function(state, scroll_padding)
require('neo-tree.ui.renderer').focus_node(state, nil, true, -10, scroll_padding)
vim.cmd('redraw!')
end,
['<C-d>'] = function(state, scroll_padding)
require('neo-tree.ui.renderer').focus_node(state, nil, true, 10, scroll_padding)
end,
},
},
components = components,
},
buffers = {
follow_current_file = {
enabled = true, -- This will find and focus the file in the active buffer every time the current file is changed while the tree is open.
leave_dirs_open = false, -- `false` closes auto expanded dirs, such as with `:Neotree reveal`
},
group_empty_dirs = true, -- when true, empty folders will be grouped together
show_unloaded = true,
window = {
position = 'float',
mappings = { ['d'] = 'buffer_delete', ['<ESC>'] = 'close_window' },
},
components = components,
},
git_status = {
window = {
position = 'float',
mappings = {
['A'] = 'git_add_all',
['u'] = 'git_unstage_file',
['a'] = 'git_add_file',
['r'] = 'git_revert_file',
['gg'] = false,
['<ESC>'] = 'close_window',
},
},
components = components,
},
source_selector = {
winbar = true, -- toggle to show selector on winbar
statusline = false, -- toggle to show selector on statusline
show_scrolled_off_parent_node = false, -- boolean
sources = {
-- {
-- source = '', -- string
-- display_name = '', -- string | nil
-- },
{ source = 'filesystem', display_name = ' File' },
{ source = 'buffers', display_name = ' Buf' },
{ source = 'git_status', display_name = ' Git' },
{ source = 'diagnostics', display_name = 'Diag' },
{ source = 'zk', display_name = ' ZK' },
},
content_layout = 'start', -- string
tabs_layout = 'equal', -- string
truncation_character = '…', -- string
padding = 0, -- int | { left: int, right: int }
separator = nil,
separator_active = nil, -- set separators around the active tab. nil falls back to `source_selector.separator`
highlight_tab = 'NeoTreeTabInactive',
highlight_tab_active = 'NeoTreeTabActive',
highlight_background = 'NeoTreeTabInactive',
highlight_separator = 'NeoTreeTabSeparatorInactive',
highlight_separator_active = 'NeoTreeTabSeparatorActive',
},
diagnostics = {
components = {
linenr = function(_config, node)
local lnum = tostring(node.extra.diag_struct.lnum + 1)
local pad = string.rep(' ', 4 - #lnum)
return {
{ text = pad .. lnum, highlight = 'LineNr' },
{ text = '▕ ', highlight = 'NeoTreeDimText' },
}
end,
},
renderers = {
file = {
{ 'indent' },
{ 'icon' },
{ 'grouped_path' },
{ 'name', highlight = 'NeoTreeFileNameOpened' },
{ 'diagnostic_count', highlight = 'NeoTreeDimText', severity = 'Error', right_padding = 0 },
{ 'diagnostic_count', highlight = 'NeoTreeDimText', severity = 'Warn', right_padding = 0 },
{ 'diagnostic_count', highlight = 'NeoTreeDimText', severity = 'Info', right_padding = 0 },
{ 'diagnostic_count', highlight = 'NeoTreeDimText', severity = 'Hint', right_padding = 0 },
{ 'clipboard' },
},
diagnostic = { { 'indent' }, { 'icon' }, { 'linenr' }, { 'name' } },
},
},
},
}
if pcall(require, 'neo-tree/sources/zk') then
table.insert(conf[2].sources, 'zk')
conf[2].zk = {
follow_current_file = true,
window = { mappings = { ['n'] = 'change_query' } },
components = components,
}
end
return conf
end
M.highlights = function(config)
local c = config.colors
local activeBG = c.darkBlue
return {
NeoTreeNormalNC = { bg = c.NC_BG },
NeoTreeSymbolicLinkTarget = { fg = c.cyan },
NeoTreeDirectoryName = { fg = c.blue },
NeoTreeDirectoryIcon = { fg = c.purple },
NeoTreeRootName = { fg = c.purple },
NeoTreeFileIcon = { fg = c.blue },
NeoTreeFileName = { fg = c.white },
NeoTreeFileNameOpened = { fg = c.purple },
NeoTreeIndentMarker = { fg = c.grey4 },
NeoTreeTitleBar = { fg = c.BG, bg = c.purple },
NeoTreeFloatTitle = { fg = c.BG, bg = c.purple },
NeoTreeGitAdded = { fg = c.green },
NeoTreeGitConflict = { fg = c.blue },
NeoTreeGitDeleted = { fg = c.red },
NeoTreeGitModified = { fg = c.yellow },
NeoTreeGitIgnored = { fg = c.grey4 },
NeoTreeGitUntracked = { fg = c.grey4 },
NeoTreeModified = { fg = c.red },
NeoTreeFileExecutable = { fg = c.blue },
NeoTreeGitUnstaged = { fg = c.yellow },
NeoTreeTabActive = { fg = c.blue, bg = activeBG, bold = true },
NeoTreeTabInactive = { fg = c.grey4, bg = c.grey1, bold = true },
NeoTreeTabSeparatorActive = { fg = c.black, bg = activeBG },
NeoTreeTabSeparatorInactive = { fg = c.black, bg = c.grey1 },
}
end
M.keymaps = {
{ 'n', '<space>b', ':Neotree toggle show buffers<CR>', { silent = true } },
{ 'n', '<space>g', ':Neotree toggle show git_status<CR>', { silent = true } },
{ 'n', '<space>m', ':Neotree toggle reveal<CR>', { silent = true } },
{ 'n', '<space>M', ':Neotree focus reveal<CR>', { silent = true } },
}
return M