-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ui.vim
558 lines (471 loc) · 19.4 KB
/
ui.vim
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
" ============================================================================
" CLASS: UI
" ============================================================================
let s:UI = {}
let g:NERDTreeUI = s:UI
" FUNCTION: s:UI.centerView() {{{1
" centers the nerd tree window around the cursor (provided the nerd tree
" options permit)
function! s:UI.centerView()
if g:NERDTreeAutoCenter
let current_line = winline()
let lines_to_top = current_line
let lines_to_bottom = winheight(g:NERDTree.GetWinNum()) - current_line
if lines_to_top < g:NERDTreeAutoCenterThreshold || lines_to_bottom < g:NERDTreeAutoCenterThreshold
normal! zz
endif
endif
endfunction
" FUNCTION: s:UI._dumpHelp {{{1
" prints out the quick help
function! s:UI._dumpHelp()
if self.getShowHelp()
let help = "\" NERDTree (" . nerdtree#version() . ") quickhelp~\n"
let help .= "\" ============================\n"
let help .= "\" File node mappings~\n"
let help .= '" '. (g:NERDTreeMouseMode ==# 3 ? 'single' : 'double') ."-click,\n"
if self.nerdtree.isTabTree()
let help .= '" '. g:NERDTreeMapActivateNode .": open in prev window\n"
else
let help .= '" '. g:NERDTreeMapActivateNode .": open in current window\n"
endif
if self.nerdtree.isTabTree()
let help .= '" '. g:NERDTreeMapPreview .": preview\n"
endif
let help .= '" '. g:NERDTreeMapOpenInTab.": open in new tab\n"
let help .= '" '. g:NERDTreeMapOpenInTabSilent .": open in new tab silently\n"
let help .= "\" middle-click,\n"
let help .= '" '. g:NERDTreeMapOpenSplit .": open split\n"
let help .= '" '. g:NERDTreeMapPreviewSplit .": preview split\n"
let help .= '" '. g:NERDTreeMapOpenVSplit .": open vsplit\n"
let help .= '" '. g:NERDTreeMapPreviewVSplit .": preview vsplit\n"
let help .= '" '. g:NERDTreeMapCustomOpen .": custom open\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Directory node mappings~\n"
let help .= '" '. (g:NERDTreeMouseMode ==# 1 ? 'double' : 'single') ."-click,\n"
let help .= '" '. g:NERDTreeMapActivateNode .": open & close node\n"
let help .= '" '. g:NERDTreeMapOpenRecursively .": recursively open node\n"
let help .= '" '. g:NERDTreeMapOpenInTab.": open in new tab\n"
let help .= '" '. g:NERDTreeMapOpenInTabSilent .": open in new tab silently\n"
let help .= '" '. g:NERDTreeMapCustomOpen .": custom open\n"
let help .= '" '. g:NERDTreeMapCloseDir .": close parent of node\n"
let help .= '" '. g:NERDTreeMapCloseChildren .": close all child nodes of\n"
let help .= "\" current node recursively\n"
let help .= "\" middle-click,\n"
let help .= '" '. g:NERDTreeMapOpenExpl.": explore selected dir\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Bookmark table mappings~\n"
let help .= "\" double-click,\n"
let help .= '" '. g:NERDTreeMapJumpBookmarks .": jump to bookmark table\n"
let help .= '" '. g:NERDTreeMapActivateNode .": open bookmark\n"
let help .= '" '. g:NERDTreeMapPreview .": preview file\n"
let help .= '" '. g:NERDTreeMapPreview .": find dir in tree\n"
let help .= '" '. g:NERDTreeMapOpenInTab.": open in new tab\n"
let help .= '" '. g:NERDTreeMapOpenInTabSilent .": open in new tab silently\n"
let help .= '" '. g:NERDTreeMapOpenSplit .": open split\n"
let help .= '" '. g:NERDTreeMapPreviewSplit .": preview split\n"
let help .= '" '. g:NERDTreeMapOpenVSplit .": open vsplit\n"
let help .= '" '. g:NERDTreeMapPreviewVSplit .": preview vsplit\n"
let help .= '" '. g:NERDTreeMapCustomOpen .": custom open\n"
let help .= '" '. g:NERDTreeMapDeleteBookmark .": delete bookmark\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Tree navigation mappings~\n"
let help .= '" '. g:NERDTreeMapJumpRoot .": go to root\n"
let help .= '" '. g:NERDTreeMapJumpParent .": go to parent\n"
let help .= '" '. g:NERDTreeMapJumpFirstChild .": go to first child\n"
let help .= '" '. g:NERDTreeMapJumpLastChild .": go to last child\n"
let help .= '" '. g:NERDTreeMapJumpNextSibling .": go to next sibling\n"
let help .= '" '. g:NERDTreeMapJumpPrevSibling .": go to prev sibling\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Filesystem mappings~\n"
let help .= '" '. g:NERDTreeMapChangeRoot .": change tree root to the\n"
let help .= "\" selected dir\n"
let help .= '" '. g:NERDTreeMapUpdir .": move tree root up a dir\n"
let help .= '" '. g:NERDTreeMapUpdirKeepOpen .": move tree root up a dir\n"
let help .= "\" but leave old root open\n"
let help .= '" '. g:NERDTreeMapRefresh .": refresh cursor dir\n"
let help .= '" '. g:NERDTreeMapRefreshRoot .": refresh current root\n"
let help .= '" '. g:NERDTreeMapMenu .": Show menu\n"
let help .= '" '. g:NERDTreeMapChdir .":change the CWD to the\n"
let help .= "\" selected dir\n"
let help .= '" '. g:NERDTreeMapCWD .":change tree root to CWD\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Tree filtering mappings~\n"
let help .= '" '. g:NERDTreeMapToggleHidden .': hidden files (' . (self.getShowHidden() ? 'on' : 'off') . ")\n"
let help .= '" '. g:NERDTreeMapToggleFilters .': file filters (' . (self.isIgnoreFilterEnabled() ? 'on' : 'off') . ")\n"
let help .= '" '. g:NERDTreeMapToggleFiles .': files (' . (self.getShowFiles() ? 'on' : 'off') . ")\n"
let help .= '" '. g:NERDTreeMapToggleBookmarks .': bookmarks (' . (self.getShowBookmarks() ? 'on' : 'off') . ")\n"
let help .= '" '. g:NERDTreeMapToggleFileLines .': files lines (' . (self.getShowFileLines() ? 'on' : 'off') . ")\n"
" add quickhelp entries for each custom key map
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Custom mappings~\n"
for i in g:NERDTreeKeyMap.All()
if !empty(i.quickhelpText)
let help .= '" '. i.key .': '. i.quickhelpText ."\n"
endif
endfor
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Other mappings~\n"
let help .= '" '. g:NERDTreeMapQuit .": Close the NERDTree window\n"
let help .= '" '. g:NERDTreeMapToggleZoom .": Zoom (maximize-minimize)\n"
let help .= "\" the NERDTree window\n"
let help .= '" '. g:NERDTreeMapHelp .": toggle help\n"
let help .= "\"\n\" ----------------------------\n"
let help .= "\" Bookmark commands~\n"
let help .= "\" :Bookmark [<name>]\n"
let help .= "\" :BookmarkToRoot <name>\n"
let help .= "\" :RevealBookmark <name>\n"
let help .= "\" :OpenBookmark <name>\n"
let help .= "\" :ClearBookmarks [<names>]\n"
let help .= "\" :ClearAllBookmarks\n"
let help .= "\" :ReadBookmarks\n"
let help .= "\" :WriteBookmarks\n"
let help .= "\" :EditBookmarks\n"
silent! put =help
elseif !self.isMinimal()
let help ='" Press '. g:NERDTreeMapHelp ." for help\n"
silent! put =help
endif
endfunction
" FUNCTION: s:UI.new(nerdtree) {{{1
function! s:UI.New(nerdtree)
let newObj = copy(self)
let newObj.nerdtree = a:nerdtree
let newObj._showHelp = 0
let newObj._ignoreEnabled = 1
let newObj._showFiles = g:NERDTreeShowFiles
let newObj._showHidden = g:NERDTreeShowHidden
let newObj._showBookmarks = g:NERDTreeShowBookmarks
let newObj._showFileLines = g:NERDTreeFileLines
return newObj
endfunction
" FUNCTION: s:UI.getPath(ln) {{{1
" Return the Path object for the node that is rendered on the given line
" number. If the 'up a dir' line is selected, return the Path object for
" the parent of the root. Return the empty dictionary if the given line
" does not reference a tree node.
function! s:UI.getPath(ln)
let line = getline(a:ln)
let rootLine = self.getRootLineNum()
if a:ln ==# rootLine
return self.nerdtree.root.path
endif
if line ==# s:UI.UpDirLine()
return self.nerdtree.root.path.getParent()
endif
if a:ln < rootLine
return {}
endif
let indent = self._indentLevelFor(line)
" remove the tree parts and the leading space
let curFile = self._stripMarkup(line)
let dir = ''
let lnum = a:ln
while lnum > 0
let lnum = lnum - 1
let curLine = getline(lnum)
let curLineStripped = self._stripMarkup(curLine)
" have we reached the top of the tree?
if lnum ==# rootLine
let dir = self.nerdtree.root.path.str({'format': 'UI'}) . dir
break
endif
if curLineStripped =~# '/$'
let lpindent = self._indentLevelFor(curLine)
if lpindent < indent
let indent = indent - 1
let dir = substitute (curLineStripped,'^\\', '', '') . dir
continue
endif
endif
endwhile
let curFile = self.nerdtree.root.path.drive . dir . curFile
let toReturn = g:NERDTreePath.New(curFile)
return toReturn
endfunction
" FUNCTION: s:UI.getLineNum(node) {{{1
" Return the line number where the given node is rendered. Return -1 if the
" given node is not visible.
function! s:UI.getLineNum(node)
if a:node.isRoot()
return self.getRootLineNum()
endif
let l:pathComponents = [substitute(self.nerdtree.root.path.str({'format': 'UI'}), '/\s*$', '', '')]
let l:currentPathComponent = 1
let l:fullPath = a:node.path.str({'format': 'UI'})
for l:lineNumber in range(self.getRootLineNum() + 1, line('$'))
let l:currentLine = getline(l:lineNumber)
let l:indentLevel = self._indentLevelFor(l:currentLine)
if l:indentLevel !=# l:currentPathComponent
continue
endif
let l:currentLine = self._stripMarkup(l:currentLine)
let l:currentPath = join(l:pathComponents, '/') . '/' . l:currentLine
" Directories: If the current path 'starts with' the full path, then
" either the paths are equal or the line is a cascade containing the
" full path.
if l:fullPath[-1:] ==# '/' && stridx(l:currentPath, l:fullPath) ==# 0
return l:lineNumber
endif
" Files: The paths must exactly match.
if l:fullPath ==# l:currentPath
return l:lineNumber
endif
" Otherwise: If the full path starts with the current path and the
" current path is a directory, we add a new path component.
if stridx(l:fullPath, l:currentPath) ==# 0 && l:currentPath[-1:] ==# '/'
let l:currentLine = substitute(l:currentLine, '/\s*$', '', '')
call add(l:pathComponents, l:currentLine)
let l:currentPathComponent += 1
endif
endfor
return -1
endfunction
" FUNCTION: s:UI.getRootLineNum(){{{1
" gets the line number of the root node
function! s:UI.getRootLineNum()
let rootLine = 1
while rootLine <= line('$') && getline(rootLine) !~# '^\(/\|<\)'
let rootLine = rootLine + 1
endwhile
return rootLine
endfunction
" FUNCTION: s:UI.getShowBookmarks() {{{1
function! s:UI.getShowBookmarks()
return self._showBookmarks
endfunction
" FUNCTION: s:UI.getShowFiles() {{{1
function! s:UI.getShowFiles()
return self._showFiles
endfunction
" FUNCTION: s:UI.getShowHelp() {{{1
function! s:UI.getShowHelp()
return self._showHelp
endfunction
" FUNCTION: s:UI.getShowHidden() {{{1
function! s:UI.getShowHidden()
return self._showHidden
endfunction
" FUNCTION: s:UI.getShowFileLines() {{{1
function! s:UI.getShowFileLines()
return self._showFileLines
endfunction
" FUNCTION: s:UI._indentLevelFor(line) {{{1
function! s:UI._indentLevelFor(line)
" Replace multi-character DirArrows with a single space so the
" indentation calculation doesn't get messed up.
if g:NERDTreeDirArrowExpandable ==# ''
let l:line = ' '.a:line
else
let l:line = substitute(substitute(a:line, '\V'.g:NERDTreeDirArrowExpandable, ' ', ''), '\V'.g:NERDTreeDirArrowCollapsible, ' ', '')
endif
let leadChars = match(l:line, '\M\[^ ]')
return leadChars / s:UI.IndentWid()
endfunction
" FUNCTION: s:UI.IndentWid() {{{1
function! s:UI.IndentWid()
return 2
endfunction
" FUNCTION: s:UI.isIgnoreFilterEnabled() {{{1
function! s:UI.isIgnoreFilterEnabled()
return self._ignoreEnabled ==# 1
endfunction
" FUNCTION: s:UI.isMinimal() {{{1
function! s:UI.isMinimal()
return g:NERDTreeMinimalUI
endfunction
" FUNCTION: s:UI.MarkupReg() {{{1
function! s:UI.MarkupReg()
return '^ *['.g:NERDTreeDirArrowExpandable.g:NERDTreeDirArrowCollapsible.']\? '
endfunction
" FUNCTION: s:UI._renderBookmarks {{{1
function! s:UI._renderBookmarks()
if !self.isMinimal()
call setline(line('.')+1, '>----------Bookmarks----------')
call cursor(line('.')+1, col('.'))
endif
if g:NERDTreeBookmarksSort ==# 1 || g:NERDTreeBookmarksSort ==# 2
call g:NERDTreeBookmark.SortBookmarksList()
endif
for i in g:NERDTreeBookmark.Bookmarks()
call setline(line('.')+1, i.str())
call cursor(line('.')+1, col('.'))
endfor
call setline(line('.')+1, '')
call cursor(line('.')+1, col('.'))
endfunction
" FUNCTION: s:UI.restoreScreenState() {{{1
"
" Sets the screen state back to what it was when nerdtree#saveScreenState was last
" called.
"
" Assumes the cursor is in the NERDTree window
function! s:UI.restoreScreenState()
if !has_key(self, '_screenState')
return
endif
call nerdtree#exec('silent vertical resize ' . self._screenState['oldWindowSize'], 1)
let old_scrolloff=&scrolloff
let &scrolloff=0
call cursor(self._screenState['oldTopLine'], 0)
normal! zt
call setpos('.', self._screenState['oldPos'])
let &scrolloff=old_scrolloff
endfunction
" FUNCTION: s:UI.saveScreenState() {{{1
" Saves the current cursor position in the current buffer and the window
" scroll position
function! s:UI.saveScreenState()
let win = winnr()
let self._screenState = {}
try
call g:NERDTree.CursorToTreeWin()
let self._screenState['oldPos'] = getpos('.')
let self._screenState['oldTopLine'] = line('w0')
let self._screenState['oldWindowSize'] = winnr('$')==1 ? g:NERDTreeWinSize : winwidth('')
call nerdtree#exec(win . 'wincmd w', 1)
catch
endtry
endfunction
" FUNCTION: s:UI.setShowHidden(val) {{{1
function! s:UI.setShowHidden(val)
let self._showHidden = a:val
endfunction
" FUNCTION: s:UI._stripMarkup(line){{{1
" find the filename in the given line, and return it.
"
" Args:
" line: the subject line
function! s:UI._stripMarkup(line)
let l:line = substitute(a:line, '^.\{-}' . g:NERDTreeNodeDelimiter, '', '')
return substitute(l:line, g:NERDTreeNodeDelimiter.'.*$', '', '')
endfunction
" FUNCTION: s:UI.render() {{{1
function! s:UI.render()
setlocal noreadonly modifiable
" remember the top line of the buffer and the current line so we can
" restore the view exactly how it was
let curLine = line('.')
let curCol = col('.')
let topLine = line('w0')
" delete all lines in the buffer (being careful not to clobber a register)
silent 1,$delete _
call self._dumpHelp()
" delete the blank line before the help and add one after it
if !self.isMinimal()
call setline(line('.')+1, '')
call cursor(line('.')+1, col('.'))
endif
if self.getShowBookmarks()
call self._renderBookmarks()
endif
" add the 'up a dir' line
if !self.isMinimal()
call setline(line('.')+1, s:UI.UpDirLine())
call cursor(line('.')+1, col('.'))
endif
" draw the header line
let header = self.nerdtree.root.path.str({'format': 'UI', 'truncateTo': winwidth(0)})
call setline(line('.')+1, header)
call cursor(line('.')+1, col('.'))
" draw the tree
silent put =self.nerdtree.root.renderToString()
" delete the blank line at the top of the buffer
silent 1,1delete _
" restore the view
let old_scrolloff=&scrolloff
let &scrolloff=0
call cursor(topLine, 1)
normal! zt
call cursor(curLine, curCol)
let &scrolloff = old_scrolloff
setlocal readonly nomodifiable
endfunction
" FUNCTION: UI.renderViewSavingPosition {{{1
" Renders the tree and ensures the cursor stays on the current node or the
" current nodes parent if it is no longer available upon re-rendering
function! s:UI.renderViewSavingPosition()
let currentNode = g:NERDTreeFileNode.GetSelected()
" go up the tree till we find a node that will be visible or till we run
" out of nodes
while currentNode !=# {} && !currentNode.isVisible() && !currentNode.isRoot()
let currentNode = currentNode.parent
endwhile
call self.render()
if currentNode !=# {}
call currentNode.putCursorHere(0, 0)
endif
endfunction
" FUNCTION: s:UI.toggleHelp() {{{1
function! s:UI.toggleHelp()
let self._showHelp = !self._showHelp
endfunction
" FUNCTION: s:UI.toggleIgnoreFilter() {{{1
" toggles the use of the NERDTreeIgnore option
function! s:UI.toggleIgnoreFilter()
let self._ignoreEnabled = !self._ignoreEnabled
call self.renderViewSavingPosition()
call self.centerView()
endfunction
" FUNCTION: s:UI.setShowBookmarks() {{{1
" Sets the visibility of the Bookmark table.
function! s:UI.setShowBookmarks(value)
let self._showBookmarks = a:value
if self.getShowBookmarks()
call self.nerdtree.render()
call g:NERDTree.CursorToBookmarkTable()
else
if empty(g:NERDTreeFileNode.GetSelected())
call b:NERDTree.root.putCursorHere(0, 0)
normal! 0
endif
call self.renderViewSavingPosition()
endif
call self.centerView()
endfunction
" FUNCTION: s:UI.toggleShowBookmarks() {{{1
" Toggle the visibility of the Bookmark table.
function! s:UI.toggleShowBookmarks()
call self.setShowBookmarks(!self._showBookmarks)
endfunction
" FUNCTION: s:UI.toggleShowFiles() {{{1
" toggles the display of hidden files
function! s:UI.toggleShowFiles()
let self._showFiles = !self._showFiles
call self.renderViewSavingPosition()
call self.centerView()
endfunction
" FUNCTION: s:UI.toggleShowHidden() {{{1
" toggles the display of hidden files
function! s:UI.toggleShowHidden()
let self._showHidden = !self._showHidden
call self.renderViewSavingPosition()
call self.centerView()
endfunction
" FUNCTION: s:UI.toggleShowFileLines() {{{1
" toggles the display of file lines
function! s:UI.toggleShowFileLines()
let self._showFileLines = !self._showFileLines
call self.nerdtree.root.refresh()
call self.renderViewSavingPosition()
call self.centerView()
endfunction
" FUNCTION: s:UI.toggleZoom() {{{1
" zoom (maximize/minimize) the NERDTree window
function! s:UI.toggleZoom()
if exists('b:NERDTreeZoomed') && b:NERDTreeZoomed
setlocal nowinfixwidth
wincmd =
setlocal winfixwidth
call nerdtree#exec('silent vertical resize '. g:NERDTreeWinSize, 1)
let b:NERDTreeZoomed = 0
else
call nerdtree#exec('vertical resize '. get(g:, 'NERDTreeWinSizeMax', ''), 1)
let b:NERDTreeZoomed = 1
endif
endfunction
" FUNCTION: s:UI.UpDirLine() {{{1
function! s:UI.UpDirLine()
return '.. (up a dir)'
endfunction
" vim: set sw=4 sts=4 et fdm=marker: