Merge pull request #795 from lifecrisis/empty-line

Fix unstable behaviour in the "NERDTreeUI.getPath()" method.
This commit is contained in:
Jason Franklin 2018-01-07 08:33:01 -05:00 committed by GitHub
commit 183bb53485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 196 additions and 187 deletions

View File

@ -14,21 +14,23 @@ lockvar s:NERDTreeSortStarIndex
let s:Path = {} let s:Path = {}
let g:NERDTreePath = s:Path let g:NERDTreePath = s:Path
" FUNCTION: Path.AbsolutePathFor(str) {{{1 " FUNCTION: Path.AbsolutePathFor(pathStr) {{{1
function! s:Path.AbsolutePathFor(str) function! s:Path.AbsolutePathFor(pathStr)
let prependCWD = 0 let l:prependWorkingDir = 0
if nerdtree#runningWindows() if nerdtree#runningWindows()
let prependCWD = a:str !~# '^.:\(\\\|\/\)' && a:str !~# '^\(\\\\\|\/\/\)' let l:prependWorkingDir = a:pathStr !~# '^.:\(\\\|\/\)' && a:pathStr !~# '^\(\\\\\|\/\/\)'
else else
let prependCWD = a:str !~# '^/' let l:prependWorkingDir = a:pathStr !~# '^/'
endif endif
let toReturn = a:str let l:result = a:pathStr
if prependCWD
let toReturn = getcwd() . s:Path.Slash() . a:str if l:prependWorkingDir
let l:result = getcwd() . s:Path.Slash() . a:pathStr
endif endif
return toReturn return l:result
endfunction endfunction
" FUNCTION: Path.bookmarkNames() {{{1 " FUNCTION: Path.bookmarkNames() {{{1
@ -541,17 +543,16 @@ function! s:Path.equals(path)
return self.str() ==# a:path.str() return self.str() ==# a:path.str()
endfunction endfunction
" FUNCTION: Path.New() {{{1 " FUNCTION: Path.New(pathStr) {{{1
" The Constructor for the Path object function! s:Path.New(pathStr)
function! s:Path.New(path) let l:newPath = copy(self)
let newPath = copy(self)
call newPath.readInfoFromDisk(s:Path.AbsolutePathFor(a:path)) call l:newPath.readInfoFromDisk(s:Path.AbsolutePathFor(a:pathStr))
let newPath.cachedDisplayString = "" let l:newPath.cachedDisplayString = ''
let newPath.flagSet = g:NERDTreeFlagSet.New() let l:newPath.flagSet = g:NERDTreeFlagSet.New()
return newPath return l:newPath
endfunction endfunction
" FUNCTION: Path.Slash() {{{1 " FUNCTION: Path.Slash() {{{1

View File

@ -1,8 +1,11 @@
" ============================================================================
" CLASS: TreeFileNode " CLASS: TreeFileNode
"This class is the parent of the TreeDirNode class and is the "
"'Component' part of the composite design pattern between the treenode " This class is the parent of the "TreeDirNode" class and is the "Component"
"classes. " part of the composite design pattern between the NERDTree node classes.
"============================================================ " ============================================================================
let s:TreeFileNode = {} let s:TreeFileNode = {}
let g:NERDTreeFileNode = s:TreeFileNode let g:NERDTreeFileNode = s:TreeFileNode
@ -193,14 +196,18 @@ function! s:TreeFileNode.GetRootForTab()
endfunction endfunction
" FUNCTION: TreeFileNode.GetSelected() {{{1 " FUNCTION: TreeFileNode.GetSelected() {{{1
"gets the treenode that the cursor is currently over " If the cursor is currently positioned on a tree node, return the node.
" Otherwise, return the empty dictionary.
function! s:TreeFileNode.GetSelected() function! s:TreeFileNode.GetSelected()
try try
let path = b:NERDTree.ui.getPath(line(".")) let l:path = b:NERDTree.ui.getPath(line('.'))
if path ==# {}
if empty(l:path)
return {} return {}
endif endif
return b:NERDTree.root.findNode(path)
return b:NERDTree.root.findNode(l:path)
catch /^NERDTree/ catch /^NERDTree/
return {} return {}
endtry endtry

View File

@ -1,5 +1,8 @@
" ============================================================================
" CLASS: UI " CLASS: UI
"============================================================ " ============================================================================
let s:UI = {} let s:UI = {}
let g:NERDTreeUI = s:UI let g:NERDTreeUI = s:UI
@ -138,21 +141,15 @@ function! s:UI.New(nerdtree)
endfunction endfunction
" FUNCTION: s:UI.getPath(ln) {{{1 " FUNCTION: s:UI.getPath(ln) {{{1
"Gets the full path to the node that is rendered on the given line number " 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
"Args: " the parent of the root. Return the empty dictionary if the given line
"ln: the line number to get the path for " does not reference a tree node.
"
"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! s:UI.getPath(ln) function! s:UI.getPath(ln)
let line = getline(a:ln) let line = getline(a:ln)
let rootLine = self.getRootLineNum() let rootLine = self.getRootLineNum()
"check to see if we have the root node
if a:ln == rootLine if a:ln == rootLine
return self.nerdtree.root.path return self.nerdtree.root.path
endif endif
@ -161,6 +158,10 @@ function! s:UI.getPath(ln)
return self.nerdtree.root.path.getParent() return self.nerdtree.root.path.getParent()
endif endif
if a:ln < rootLine
return {}
endif
let indent = self._indentLevelFor(line) let indent = self._indentLevelFor(line)
" remove the tree parts and the leading space " remove the tree parts and the leading space