mirror of
https://github.com/preservim/nerdtree.git
synced 2025-11-08 11:23:48 -05:00
continue breaking down the epic autoload module
Add 2 new classes and move code into them from autoload: * NERDTree. Each nerdtree buffer now has a NERDTree object that holds the root node and will old other util functions * UI. Each NERDTree object holds a UI object which is responsible for rendering, getting the current node, etc Still a fair few methods to sort through in autoload (many of which will end up in the above classes) - need sleep though.
This commit is contained in:
@@ -74,6 +74,8 @@ function! nerdtree#loadClassFiles()
|
||||
runtime lib/nerdtree/tree_dir_node.vim
|
||||
runtime lib/nerdtree/opener.vim
|
||||
runtime lib/nerdtree/creator.vim
|
||||
runtime lib/nerdtree/nerdtree.vim
|
||||
runtime lib/nerdtree/ui.vim
|
||||
endfunction
|
||||
|
||||
" FUNCTION: nerdtree#postSourceActions() {{{2
|
||||
@@ -321,75 +323,6 @@ function! nerdtree#echoWarning(msg)
|
||||
echohl normal
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#getPath(ln) {{{2
|
||||
"Gets the full path to the node that is rendered on the given line number
|
||||
"
|
||||
"Args:
|
||||
"ln: the line number to get the path for
|
||||
"
|
||||
"Return:
|
||||
"A path if a node was selected, {} if nothing is selected.
|
||||
"If the 'up a dir' line was selected then the path to the parent of the
|
||||
"current root is returned
|
||||
function! nerdtree#getPath(ln)
|
||||
let line = getline(a:ln)
|
||||
|
||||
let rootLine = g:NERDTreeFileNode.GetRootLineNum()
|
||||
|
||||
"check to see if we have the root node
|
||||
if a:ln == rootLine
|
||||
return b:NERDTreeRoot.path
|
||||
endif
|
||||
|
||||
if !g:NERDTreeDirArrows
|
||||
" in case called from outside the tree
|
||||
if line !~# '^ *[|`▸▾ ]' || line =~# '^$'
|
||||
return {}
|
||||
endif
|
||||
endif
|
||||
|
||||
if line ==# nerdtree#treeUpDirLine()
|
||||
return b:NERDTreeRoot.path.getParent()
|
||||
endif
|
||||
|
||||
let indent = nerdtree#indentLevelFor(line)
|
||||
|
||||
"remove the tree parts and the leading space
|
||||
let curFile = nerdtree#stripMarkupFromLine(line, 0)
|
||||
|
||||
let wasdir = 0
|
||||
if curFile =~# '/$'
|
||||
let wasdir = 1
|
||||
let curFile = substitute(curFile, '/\?$', '/', "")
|
||||
endif
|
||||
|
||||
let dir = ""
|
||||
let lnum = a:ln
|
||||
while lnum > 0
|
||||
let lnum = lnum - 1
|
||||
let curLine = getline(lnum)
|
||||
let curLineStripped = nerdtree#stripMarkupFromLine(curLine, 1)
|
||||
|
||||
"have we reached the top of the tree?
|
||||
if lnum == rootLine
|
||||
let dir = b:NERDTreeRoot.path.str({'format': 'UI'}) . dir
|
||||
break
|
||||
endif
|
||||
if curLineStripped =~# '/$'
|
||||
let lpindent = nerdtree#indentLevelFor(curLine)
|
||||
if lpindent < indent
|
||||
let indent = indent - 1
|
||||
|
||||
let dir = substitute (curLineStripped,'^\\', "", "") . dir
|
||||
continue
|
||||
endif
|
||||
endif
|
||||
endwhile
|
||||
let curFile = b:NERDTreeRoot.path.drive . dir . curFile
|
||||
let toReturn = g:NERDTreePath.New(curFile)
|
||||
return toReturn
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#getTreeWinNum() {{{2
|
||||
"gets the nerd tree window number for this tab
|
||||
function! nerdtree#getTreeWinNum()
|
||||
@@ -400,17 +333,6 @@ function! nerdtree#getTreeWinNum()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#indentLevelFor(line) {{{2
|
||||
function! nerdtree#indentLevelFor(line)
|
||||
let level = match(a:line, '[^ \-+~▸▾`|]') / nerdtree#treeWid()
|
||||
" check if line includes arrows
|
||||
if match(a:line, '[▸▾]') > -1
|
||||
" decrement level as arrow uses 3 ascii chars
|
||||
let level = level - 1
|
||||
endif
|
||||
return level
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#isTreeOpen() {{{2
|
||||
function! nerdtree#isTreeOpen()
|
||||
return nerdtree#getTreeWinNum() != -1
|
||||
@@ -427,7 +349,7 @@ function! nerdtree#putCursorOnBookmarkTable()
|
||||
return cursor(1, 2)
|
||||
endif
|
||||
|
||||
let rootNodeLine = g:NERDTreeFileNode.GetRootLineNum()
|
||||
let rootNodeLine = b:NERDTree.ui.getRootLineNum()
|
||||
|
||||
let line = 1
|
||||
while getline(line) !~# '^>-\+Bookmarks-\+$'
|
||||
@@ -467,116 +389,10 @@ function! nerdtree#renderBookmarks()
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#renderView {{{2
|
||||
"The entry function for rendering the tree
|
||||
function! nerdtree#renderView()
|
||||
setlocal 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 nerdtree#dumpHelp()
|
||||
|
||||
"delete the blank line before the help and add one after it
|
||||
if g:NERDTreeMinimalUI == 0
|
||||
call setline(line(".")+1, "")
|
||||
call cursor(line(".")+1, col("."))
|
||||
endif
|
||||
|
||||
if b:NERDTreeShowBookmarks
|
||||
call nerdtree#renderBookmarks()
|
||||
endif
|
||||
|
||||
"add the 'up a dir' line
|
||||
if !g:NERDTreeMinimalUI
|
||||
call setline(line(".")+1, nerdtree#treeUpDirLine())
|
||||
call cursor(line(".")+1, col("."))
|
||||
endif
|
||||
|
||||
"draw the header line
|
||||
let header = b:NERDTreeRoot.path.str({'format': 'UI', 'truncateTo': winwidth(0)})
|
||||
call setline(line(".")+1, header)
|
||||
call cursor(line(".")+1, col("."))
|
||||
|
||||
"draw the tree
|
||||
let old_o = @o
|
||||
let @o = b:NERDTreeRoot.renderToString()
|
||||
silent put o
|
||||
let @o = old_o
|
||||
|
||||
"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 nomodifiable
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#renderViewSavingPosition {{{2
|
||||
"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! nerdtree#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 nerdtree#renderView()
|
||||
|
||||
if currentNode != {}
|
||||
call currentNode.putCursorHere(0, 0)
|
||||
endif
|
||||
call b:NERDTree.render()
|
||||
endfunction
|
||||
"
|
||||
"FUNCTION: nerdtree#restoreScreenState() {{{2
|
||||
"
|
||||
"Sets the screen state back to what it was when nerdtree#saveScreenState was last
|
||||
"called.
|
||||
"
|
||||
"Assumes the cursor is in the NERDTree window
|
||||
function! nerdtree#restoreScreenState()
|
||||
if !exists("b:NERDTreeOldTopLine") || !exists("b:NERDTreeOldPos") || !exists("b:NERDTreeOldWindowSize")
|
||||
return
|
||||
endif
|
||||
exec("silent vertical resize ".b:NERDTreeOldWindowSize)
|
||||
|
||||
let old_scrolloff=&scrolloff
|
||||
let &scrolloff=0
|
||||
call cursor(b:NERDTreeOldTopLine, 0)
|
||||
normal! zt
|
||||
call setpos(".", b:NERDTreeOldPos)
|
||||
let &scrolloff=old_scrolloff
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#saveScreenState() {{{2
|
||||
"Saves the current cursor position in the current buffer and the window
|
||||
"scroll position
|
||||
function! nerdtree#saveScreenState()
|
||||
let win = winnr()
|
||||
try
|
||||
call nerdtree#putCursorInTreeWin()
|
||||
let b:NERDTreeOldPos = getpos(".")
|
||||
let b:NERDTreeOldTopLine = line("w0")
|
||||
let b:NERDTreeOldWindowSize = winwidth("")
|
||||
call nerdtree#exec(win . "wincmd w")
|
||||
catch /^NERDTree.InvalidOperationError/
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
"FUNCTION: nerdtree#stripMarkupFromLine(line, removeLeadingSpaces){{{2
|
||||
"returns the given line with all the tree parts stripped off
|
||||
"
|
||||
|
||||
@@ -119,7 +119,7 @@ function! nerdtree#ui_glue#bookmarkNode(...)
|
||||
endif
|
||||
try
|
||||
call currentNode.bookmark(name)
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
catch /^NERDTree.IllegalBookmarkNameError/
|
||||
call nerdtree#echo("bookmark names must not contain spaces")
|
||||
endtry
|
||||
@@ -141,7 +141,7 @@ endfunction
|
||||
" changes the current root to the selected one
|
||||
function! s:chRoot(node)
|
||||
call a:node.makeRoot()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call b:NERDTreeRoot.putCursorHere(0, 0)
|
||||
endfunction
|
||||
|
||||
@@ -173,14 +173,14 @@ function! nerdtree#ui_glue#clearBookmarks(bookmarks)
|
||||
call bookmark.delete()
|
||||
endfor
|
||||
endif
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
endfunction
|
||||
|
||||
" FUNCTION: s:closeChildren(node) {{{1
|
||||
" closes all childnodes of the current node
|
||||
function! s:closeChildren(node)
|
||||
call a:node.closeChildren()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call a:node.putCursorHere(0, 0)
|
||||
endfunction
|
||||
|
||||
@@ -200,7 +200,7 @@ function! s:closeCurrentDir(node)
|
||||
endif
|
||||
endwhile
|
||||
call parent.close()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call parent.putCursorHere(0, 0)
|
||||
endif
|
||||
endfunction
|
||||
@@ -227,7 +227,7 @@ function! s:deleteBookmark(bm)
|
||||
if nr2char(getchar()) ==# 'y'
|
||||
try
|
||||
call a:bm.delete()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
redraw
|
||||
catch /^NERDTree/
|
||||
call nerdtree#echoWarning("Could not remove bookmark")
|
||||
@@ -242,7 +242,7 @@ endfunction
|
||||
" toggles the help display
|
||||
function! s:displayHelp()
|
||||
let b:treeShowHelp = b:treeShowHelp ? 0 : 1
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call nerdtree#centerView()
|
||||
endfunction
|
||||
|
||||
@@ -486,7 +486,7 @@ endfunction
|
||||
function! s:openNodeRecursively(node)
|
||||
call nerdtree#echo("Recursively opening node. Please wait...")
|
||||
call a:node.openRecursively()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
redraw
|
||||
call nerdtree#echo("Recursively opening node. Please wait... DONE")
|
||||
endfunction
|
||||
@@ -523,7 +523,7 @@ endfunction
|
||||
function! s:refreshRoot()
|
||||
call nerdtree#echo("Refreshing the root node. This could take a while...")
|
||||
call b:NERDTreeRoot.refresh()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
redraw
|
||||
call nerdtree#echo("Refreshing the root node. This could take a while... DONE")
|
||||
endfunction
|
||||
@@ -538,7 +538,7 @@ function! s:refreshCurrent(node)
|
||||
|
||||
call nerdtree#echo("Refreshing node. This could take a while...")
|
||||
call node.refresh()
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
redraw
|
||||
call nerdtree#echo("Refreshing node. This could take a while... DONE")
|
||||
endfunction
|
||||
@@ -573,7 +573,7 @@ endfunction
|
||||
" toggles the use of the NERDTreeIgnore option
|
||||
function! s:toggleIgnoreFilter()
|
||||
let b:NERDTreeIgnoreEnabled = !b:NERDTreeIgnoreEnabled
|
||||
call nerdtree#renderViewSavingPosition()
|
||||
call b:NERDTree.ui.renderViewSavingPosition()
|
||||
call nerdtree#centerView()
|
||||
endfunction
|
||||
|
||||
@@ -582,10 +582,10 @@ endfunction
|
||||
function! s:toggleShowBookmarks()
|
||||
let b:NERDTreeShowBookmarks = !b:NERDTreeShowBookmarks
|
||||
if b:NERDTreeShowBookmarks
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call nerdtree#putCursorOnBookmarkTable()
|
||||
else
|
||||
call nerdtree#renderViewSavingPosition()
|
||||
call b:NERDTree.ui.renderViewSavingPosition()
|
||||
endif
|
||||
call nerdtree#centerView()
|
||||
endfunction
|
||||
@@ -594,7 +594,7 @@ endfunction
|
||||
" toggles the display of hidden files
|
||||
function! s:toggleShowFiles()
|
||||
let b:NERDTreeShowFiles = !b:NERDTreeShowFiles
|
||||
call nerdtree#renderViewSavingPosition()
|
||||
call b:NERDTree.ui.renderViewSavingPosition()
|
||||
call nerdtree#centerView()
|
||||
endfunction
|
||||
|
||||
@@ -602,7 +602,7 @@ endfunction
|
||||
" toggles the display of hidden files
|
||||
function! s:toggleShowHidden()
|
||||
let b:NERDTreeShowHidden = !b:NERDTreeShowHidden
|
||||
call nerdtree#renderViewSavingPosition()
|
||||
call b:NERDTree.ui.renderViewSavingPosition()
|
||||
call nerdtree#centerView()
|
||||
endfunction
|
||||
|
||||
@@ -650,7 +650,7 @@ function! nerdtree#ui_glue#upDir(keepState)
|
||||
call b:NERDTreeRoot.path.changeToDir()
|
||||
endif
|
||||
|
||||
call nerdtree#renderView()
|
||||
call b:NERDTree.render()
|
||||
call oldRoot.putCursorHere(0, 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
Reference in New Issue
Block a user