Skip to content

Commit 0cb04e9

Browse files
rzvxahsnks100
andauthoredNov 15, 2023
Show file lines (#1384)
* Added showing lines of files in NERDTRee. * Added dynamic binding and documentation --------- Co-authored-by: Diallo Han <diallo@hpcnt.com>
1 parent 4c588f1 commit 0cb04e9

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed
 

‎README.markdown

+22
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ let g:NERDTreeDirArrowCollapsible = '?'
188188
```
189189
The preceding values are the non-Windows default arrow symbols. Setting these variables to empty strings will remove the arrows completely and shift the entire tree two character positions to the left. See `:h NERDTreeDirArrowExpandable` for more details.
190190
191+
### How can I show lines of files?
192+
193+
```vim
194+
let g:NERDTreeFileLines = 1
195+
```
196+
197+
Lines in the file are displayed as shown below.
198+
```
199+
</pack/packer/start/nerdtree/
200+
▸ autoload/
201+
▸ doc/
202+
▸ lib/
203+
▸ nerdtree_plugin/
204+
▸ plugin/
205+
▸ syntax/
206+
_config.yml (1)
207+
CHANGELOG.md (307)
208+
LICENCE (13)
209+
README.markdown (234)
210+
screenshot.png (219)
211+
```
212+
191213
### Can NERDTree access remote files via scp or ftp?
192214
193215
Short answer: No, and there are no plans to add that functionality. However, Vim ships with a plugin that does just that. It's called netrw, and by adding the following lines to your `.vimrc`, you can use it to open files over the `scp:`, `ftp:`, or other protocols, while still using NERDTree for all local files. The function seamlessly makes the decision to open NERDTree or netrw, and other supported protocols can be added to the regular expression.

‎autoload/nerdtree/ui_glue.vim

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function! nerdtree#ui_glue#createDefaultBindings() abort
5757
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapToggleFilters, 'scope': 'all', 'callback': s.'toggleIgnoreFilter' })
5858
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapToggleFiles, 'scope': 'all', 'callback': s.'toggleShowFiles' })
5959
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapToggleBookmarks, 'scope': 'all', 'callback': s.'toggleShowBookmarks' })
60+
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapToggleFileLines, 'scope': 'all', 'callback': s.'toggleShowFileLines' })
6061

6162
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapCloseDir, 'scope': 'Node', 'callback': s.'closeCurrentDir' })
6263
call NERDTreeAddKeyMap({ 'key': g:NERDTreeMapCloseChildren, 'scope': 'DirNode', 'callback': s.'closeChildren' })
@@ -685,6 +686,12 @@ function! s:toggleShowHidden() abort
685686
call b:NERDTree.ui.toggleShowHidden()
686687
endfunction
687688

689+
" FUNCTION: s:toggleShowFileLines() {{{1
690+
" toggles the display of hidden files
691+
function! s:toggleShowFileLines() abort
692+
call b:NERDTree.ui.toggleShowFileLines()
693+
endfunction
694+
688695
" FUNCTION: s:toggleZoom() {{{1
689696
function! s:toggleZoom() abort
690697
call b:NERDTree.ui.toggleZoom()

‎doc/NERDTree.txt

+23
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ I........Toggle whether hidden files displayed......................|NERDTree-I|
287287
f........Toggle whether the file filters are used...................|NERDTree-f|
288288
F........Toggle whether files are displayed.........................|NERDTree-F|
289289
B........Toggle whether the bookmark table is displayed.............|NERDTree-B|
290+
L........Toggle whether the bookmark table is displayed.............|NERDTree-L|
290291

291292
q........Close the NERDTree window..................................|NERDTree-q|
292293
A........Zoom (maximize/minimize) the NERDTree window...............|NERDTree-A|
@@ -601,6 +602,14 @@ Applies to: no restrictions.
601602

602603
Toggles whether the bookmarks table is displayed.
603604

605+
------------------------------------------------------------------------------
606+
*NERDTree-L*
607+
Default key: L
608+
Map setting: *NERDTreeMapToggleFileLiness*
609+
Applies to: no restrictions.
610+
611+
Toggles whether the number of lines in files is displayed.
612+
604613
------------------------------------------------------------------------------
605614
*NERDTree-q*
606615
Default key: q
@@ -1063,6 +1072,20 @@ This setting can be toggled dynamically, per tree, with the |NERDTree-F|
10631072
mapping and is useful for drastically shrinking the tree when you are
10641073
navigating to a different part of the tree.
10651074

1075+
------------------------------------------------------------------------------
1076+
*NERDTreeShowFilesLines*
1077+
Values: 0 or 1.
1078+
Default: 0.
1079+
1080+
If this setting is set to 1 then the NERDTree shows number of lines for each
1081+
file.
1082+
1083+
This setting can be toggled dynamically, per tree, with the |NERDTree-L|
1084+
mapping.
1085+
Use one of the follow lines for this setting: >
1086+
let NERDTreeShowFilesLines=0
1087+
let NERDTreeShowFilesLines=1
1088+
<
10661089
------------------------------------------------------------------------------
10671090
*NERDTreeShowHidden*
10681091
Values: 0 or 1.

‎lib/nerdtree/path.vim

+19
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ function! s:Path.cacheDisplayString() abort
6565
let self.cachedDisplayString = self.addDelimiter(self.cachedDisplayString) . ' -> ' . self.symLinkDest
6666
endif
6767

68+
if !self.isDirectory && b:NERDTree.ui.getShowFileLines() != 0
69+
let l:bufname = self.str({'format': 'Edit'})
70+
let l:lines = 0
71+
if executable('wc')
72+
let l:lines = split(system('wc -l "'.l:bufname.'"'))[0]
73+
elseif nerdtree#runningWindows()
74+
let l:lines = substitute(system('type "'.l:bufname.'" | find /c /v ""'), '\n', '', 'g')
75+
else
76+
let s:lines = readfile(l:bufname)
77+
let l:lines = 0
78+
for s:line in s:lines
79+
let l:lines += 1
80+
if l:lines >= 20000
81+
break
82+
endif
83+
endfor
84+
endif
85+
let self.cachedDisplayString = self.addDelimiter(self.cachedDisplayString) . ' ('.l:lines.')'
86+
endif
6887
if self.isReadOnly
6988
let self.cachedDisplayString = self.addDelimiter(self.cachedDisplayString) . ' ['.g:NERDTreeGlyphReadOnly.']'
7089
endif

‎lib/nerdtree/ui.vim

+16
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function! s:UI._dumpHelp()
103103
let help .= '" '. g:NERDTreeMapToggleFilters .': file filters (' . (self.isIgnoreFilterEnabled() ? 'on' : 'off') . ")\n"
104104
let help .= '" '. g:NERDTreeMapToggleFiles .': files (' . (self.getShowFiles() ? 'on' : 'off') . ")\n"
105105
let help .= '" '. g:NERDTreeMapToggleBookmarks .': bookmarks (' . (self.getShowBookmarks() ? 'on' : 'off') . ")\n"
106+
let help .= '" '. g:NERDTreeMapToggleFileLines .': files lines (' . (self.getShowFileLines() ? 'on' : 'off') . ")\n"
106107

107108
" add quickhelp entries for each custom key map
108109
let help .= "\"\n\" ----------------------------\n"
@@ -147,6 +148,7 @@ function! s:UI.New(nerdtree)
147148
let newObj._showFiles = g:NERDTreeShowFiles
148149
let newObj._showHidden = g:NERDTreeShowHidden
149150
let newObj._showBookmarks = g:NERDTreeShowBookmarks
151+
let newObj._showFileLines = g:NERDTreeFileLines
150152

151153
return newObj
152154
endfunction
@@ -284,6 +286,11 @@ function! s:UI.getShowHidden()
284286
return self._showHidden
285287
endfunction
286288

289+
" FUNCTION: s:UI.getShowFileLines() {{{1
290+
function! s:UI.getShowFileLines()
291+
return self._showFileLines
292+
endfunction
293+
287294
" FUNCTION: s:UI._indentLevelFor(line) {{{1
288295
function! s:UI._indentLevelFor(line)
289296
" Replace multi-character DirArrows with a single space so the
@@ -512,6 +519,15 @@ function! s:UI.toggleShowHidden()
512519
call self.centerView()
513520
endfunction
514521

522+
" FUNCTION: s:UI.toggleShowFileLines() {{{1
523+
" toggles the display of file lines
524+
function! s:UI.toggleShowFileLines()
525+
let self._showFileLines = !self._showFileLines
526+
call self.nerdtree.root.refresh()
527+
call self.renderViewSavingPosition()
528+
call self.centerView()
529+
endfunction
530+
515531
" FUNCTION: s:UI.toggleZoom() {{{1
516532
" zoom (maximize/minimize) the NERDTree window
517533
function! s:UI.toggleZoom()

‎plugin/NERD_tree.vim

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ let g:NERDTreeShowFiles = get(g:, 'NERDTreeShowFiles', 1
5353
let g:NERDTreeShowHidden = get(g:, 'NERDTreeShowHidden', 0)
5454
let g:NERDTreeShowLineNumbers = get(g:, 'NERDTreeShowLineNumbers', 0)
5555
let g:NERDTreeSortDirs = get(g:, 'NERDTreeSortDirs', 1)
56+
let g:NERDTreeFileLines = get(g:, 'NERDTreeFileLines', 0)
5657

5758

5859
if !nerdtree#runningWindows() && !nerdtree#runningCygwin()
@@ -130,6 +131,7 @@ let g:NERDTreeMapToggleBookmarks = get(g:, 'NERDTreeMapToggleBookmarks', 'B')
130131
let g:NERDTreeMapToggleFiles = get(g:, 'NERDTreeMapToggleFiles', 'F')
131132
let g:NERDTreeMapToggleFilters = get(g:, 'NERDTreeMapToggleFilters', 'f')
132133
let g:NERDTreeMapToggleHidden = get(g:, 'NERDTreeMapToggleHidden', 'I')
134+
let g:NERDTreeMapToggleFileLines = get(g:, 'NERDTreeMapToggleFileLines', 'L')
133135
let g:NERDTreeMapToggleZoom = get(g:, 'NERDTreeMapToggleZoom', 'A')
134136
let g:NERDTreeMapUpdir = get(g:, 'NERDTreeMapUpdir', 'u')
135137
let g:NERDTreeMapUpdirKeepOpen = get(g:, 'NERDTreeMapUpdirKeepOpen', 'U')
@@ -144,6 +146,7 @@ call nerdtree#loadClassFiles()
144146
"============================================================
145147
call nerdtree#ui_glue#setupCommands()
146148

149+
147150
" SECTION: Auto commands {{{1
148151
"============================================================
149152
augroup NERDTree

0 commit comments

Comments
 (0)
Please sign in to comment.