From 8b189cb263b17a9cc3393cbcb037c65d99579fb8 Mon Sep 17 00:00:00 2001 From: Martin Grenfell Date: Sat, 2 May 2015 15:24:59 +0100 Subject: [PATCH] move some rendering and cursor moving functions out of autoload --- autoload/nerdtree.vim | 87 ----------------------------------- autoload/nerdtree/ui_glue.vim | 4 +- lib/nerdtree/nerdtree.vim | 30 ++++++++++++ lib/nerdtree/opener.vim | 6 +-- lib/nerdtree/ui.vim | 69 ++++++++++++++++++++++++--- plugin/NERD_tree.vim | 2 +- 6 files changed, 99 insertions(+), 99 deletions(-) diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim index 48bdb80..22283aa 100644 --- a/autoload/nerdtree.vim +++ b/autoload/nerdtree.vim @@ -128,96 +128,9 @@ function! nerdtree#echoWarning(msg) echohl normal endfunction -"FUNCTION: nerdtree#putCursorOnBookmarkTable(){{{2 -"Places the cursor at the top of the bookmarks table -function! nerdtree#putCursorOnBookmarkTable() - if !b:NERDTreeShowBookmarks - throw "NERDTree.IllegalOperationError: cant find bookmark table, bookmarks arent active" - endif - - if g:NERDTreeMinimalUI - return cursor(1, 2) - endif - - let rootNodeLine = b:NERDTree.ui.getRootLineNum() - - let line = 1 - while getline(line) !~# '^>-\+Bookmarks-\+$' - let line = line + 1 - if line >= rootNodeLine - throw "NERDTree.BookmarkTableNotFoundError: didnt find the bookmarks table" - endif - endwhile - call cursor(line, 2) -endfunction - -"FUNCTION: nerdtree#putCursorInTreeWin(){{{2 -"Places the cursor in the nerd tree window -function! nerdtree#putCursorInTreeWin() - call g:NERDTree.MustBeOpen() - call nerdtree#exec(g:NERDTree.GetWinNum() . "wincmd w") -endfunction - -"FUNCTION: nerdtree#renderBookmarks {{{2 -function! nerdtree#renderBookmarks() - - if g:NERDTreeMinimalUI == 0 - call setline(line(".")+1, ">----------Bookmarks----------") - call cursor(line(".")+1, col(".")) - 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: nerdtree#renderView {{{2 function! nerdtree#renderView() call b:NERDTree.render() endfunction -" -"FUNCTION: nerdtree#stripMarkupFromLine(line, removeLeadingSpaces){{{2 -"returns the given line with all the tree parts stripped off -" -"Args: -"line: the subject line -"removeLeadingSpaces: 1 if leading spaces are to be removed (leading spaces = -"any spaces before the actual text of the node) -function! nerdtree#stripMarkupFromLine(line, removeLeadingSpaces) - let line = a:line - "remove the tree parts and the leading space - let line = substitute (line, g:NERDTreeUI.MarkupReg(),"","") - - "strip off any read only flag - let line = substitute (line, ' \[RO\]', "","") - - "strip off any bookmark flags - let line = substitute (line, ' {[^}]*}', "","") - - "strip off any executable flags - let line = substitute (line, '*\ze\($\| \)', "","") - - "strip off any generic flags - let line = substitute (line, '\[[^]]*\]', "","") - - let wasdir = 0 - if line =~# '/$' - let wasdir = 1 - endif - let line = substitute (line,' -> .*',"","") " remove link to - if wasdir ==# 1 - let line = substitute (line, '/\?$', '/', "") - endif - - if a:removeLeadingSpaces - let line = substitute (line, '^ *', '', '') - endif - - return line -endfunction " vim: set sw=4 sts=4 et fdm=marker: diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim index 1dbfc50..3bdc1b2 100644 --- a/autoload/nerdtree/ui_glue.vim +++ b/autoload/nerdtree/ui_glue.vim @@ -278,7 +278,7 @@ function! s:findAndRevealPath() if !g:NERDTree.IsOpen() call g:NERDTreeCreator.TogglePrimary('') else - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() endif let b:NERDTreeShowHidden = g:NERDTreeShowHidden call s:chRoot(g:NERDTreeDirNode.New(p.getParent())) @@ -288,7 +288,7 @@ function! s:findAndRevealPath() endif endif endif - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() call b:NERDTreeRoot.reveal(p) if p.isUnixHiddenFile() diff --git a/lib/nerdtree/nerdtree.vim b/lib/nerdtree/nerdtree.vim index 7fec67b..8e3bdde 100644 --- a/lib/nerdtree/nerdtree.vim +++ b/lib/nerdtree/nerdtree.vim @@ -35,6 +35,36 @@ function! s:NERDTree.CloseIfQuitOnOpen() endif endfunction +"FUNCTION: s:NERDTree.CursorToBookmarkTable(){{{1 +"Places the cursor at the top of the bookmarks table +function! s:NERDTree.CursorToBookmarkTable() + if !b:NERDTreeShowBookmarks + throw "NERDTree.IllegalOperationError: cant find bookmark table, bookmarks arent active" + endif + + if g:NERDTreeMinimalUI + return cursor(1, 2) + endif + + let rootNodeLine = b:NERDTree.ui.getRootLineNum() + + let line = 1 + while getline(line) !~# '^>-\+Bookmarks-\+$' + let line = line + 1 + if line >= rootNodeLine + throw "NERDTree.BookmarkTableNotFoundError: didnt find the bookmarks table" + endif + endwhile + call cursor(line, 2) +endfunction + +"FUNCTION: s:NERDTree.CursorToTreeWin(){{{1 +"Places the cursor in the nerd tree window +function! s:NERDTree.CursorToTreeWin() + call g:NERDTree.MustBeOpen() + call nerdtree#exec(g:NERDTree.GetWinNum() . "wincmd w") +endfunction + " Function: s:NERDTree.ExistsForBuffer() {{{1 " Returns 1 if a nerd tree root exists in the current buffer function! s:NERDTree.ExistsForBuf() diff --git a/lib/nerdtree/opener.vim b/lib/nerdtree/opener.vim index 6dea3b0..cd6619b 100644 --- a/lib/nerdtree/opener.vim +++ b/lib/nerdtree/opener.vim @@ -189,7 +189,7 @@ function! s:Opener._newSplit() try exec(splitMode." sp ") catch /^Vim\%((\a\+)\)\=:E37/ - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self._path.str() ." is already open and modified." catch /^Vim\%((\a\+)\)\=:/ "do nothing @@ -219,7 +219,7 @@ function! s:Opener._newVSplit() vnew "resize the nerd tree back to the original size - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() exec("silent vertical resize ". winwidth) call nerdtree#exec('wincmd p') endfunction @@ -288,7 +288,7 @@ function! s:Opener._previousWindow() call nerdtree#exec('wincmd p') endif catch /^Vim\%((\a\+)\)\=:E37/ - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self._path.str() ." is already open and modified." catch /^Vim\%((\a\+)\)\=:/ echo v:exception diff --git a/lib/nerdtree/ui.vim b/lib/nerdtree/ui.vim index b33fc8b..dfe96a3 100644 --- a/lib/nerdtree/ui.vim +++ b/lib/nerdtree/ui.vim @@ -171,7 +171,7 @@ function! s:UI.getPath(ln) let indent = self._indentLevelFor(line) "remove the tree parts and the leading space - let curFile = nerdtree#stripMarkupFromLine(line, 0) + let curFile = self._stripMarkup(line, 0) let wasdir = 0 if curFile =~# '/$' @@ -184,7 +184,7 @@ function! s:UI.getPath(ln) while lnum > 0 let lnum = lnum - 1 let curLine = getline(lnum) - let curLineStripped = nerdtree#stripMarkupFromLine(curLine, 1) + let curLineStripped = self._stripMarkup(curLine, 1) "have we reached the top of the tree? if lnum == rootLine @@ -235,7 +235,7 @@ function! s:UI.getLineNum(file_node) let indent = self._indentLevelFor(curLine) if indent ==# curPathComponent - let curLine = nerdtree#stripMarkupFromLine(curLine, 1) + let curLine = self._stripMarkup(curLine, 1) let curPath = join(pathcomponents, '/') . '/' . curLine if stridx(fullpath, curPath, 0) ==# 0 @@ -289,6 +289,23 @@ function! s:UI.MarkupReg() return '^[ `|]*[\-+~]' endfunction +"FUNCTION: s:UI._renderBookmarks {{{1 +function! s:UI._renderBookmarks() + + if g:NERDTreeMinimalUI == 0 + call setline(line(".")+1, ">----------Bookmarks----------") + call cursor(line(".")+1, col(".")) + 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 @@ -315,7 +332,7 @@ endfunction function! s:UI.saveScreenState() let win = winnr() try - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() let self._screenState = {} let self._screenState['oldPos'] = getpos(".") let self._screenState['oldTopLine'] = line("w0") @@ -325,6 +342,46 @@ function! s:UI.saveScreenState() endtry endfunction +"FUNCTION: s:UI._stripMarkup(line, removeLeadingSpaces){{{1 +"returns the given line with all the tree parts stripped off +" +"Args: +"line: the subject line +"removeLeadingSpaces: 1 if leading spaces are to be removed (leading spaces = +"any spaces before the actual text of the node) +function! s:UI._stripMarkup(line, removeLeadingSpaces) + let line = a:line + "remove the tree parts and the leading space + let line = substitute (line, g:NERDTreeUI.MarkupReg(),"","") + + "strip off any read only flag + let line = substitute (line, ' \[RO\]', "","") + + "strip off any bookmark flags + let line = substitute (line, ' {[^}]*}', "","") + + "strip off any executable flags + let line = substitute (line, '*\ze\($\| \)', "","") + + "strip off any generic flags + let line = substitute (line, '\[[^]]*\]', "","") + + let wasdir = 0 + if line =~# '/$' + let wasdir = 1 + endif + let line = substitute (line,' -> .*',"","") " remove link to + if wasdir ==# 1 + let line = substitute (line, '/\?$', '/', "") + endif + + if a:removeLeadingSpaces + let line = substitute (line, '^ *', '', '') + endif + + return line +endfunction + "FUNCTION: s:UI.render() {{{1 function! s:UI.render() setlocal modifiable @@ -347,7 +404,7 @@ function! s:UI.render() endif if b:NERDTreeShowBookmarks - call nerdtree#renderBookmarks() + call self._renderBookmarks() endif "add the 'up a dir' line @@ -415,7 +472,7 @@ function! s:UI.toggleShowBookmarks() let b:NERDTreeShowBookmarks = !b:NERDTreeShowBookmarks if b:NERDTreeShowBookmarks call b:NERDTree.render() - call nerdtree#putCursorOnBookmarkTable() + call g:NERDTree.CursorToBookmarkTable() else call b:NERDTree.ui.renderViewSavingPosition() endif diff --git a/plugin/NERD_tree.vim b/plugin/NERD_tree.vim index 8038668..3eae93b 100644 --- a/plugin/NERD_tree.vim +++ b/plugin/NERD_tree.vim @@ -186,7 +186,7 @@ endfunction function! NERDTreeFocus() if g:NERDTree.IsOpen() - call nerdtree#putCursorInTreeWin() + call g:NERDTree.CursorToTreeWin() else call g:NERDTreeCreator.TogglePrimary("") endif