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

View File

@ -1,8 +1,11 @@
" ============================================================================
" CLASS: TreeFileNode
"This class is the parent of the TreeDirNode class and is the
"'Component' part of the composite design pattern between the treenode
"classes.
"============================================================
"
" This class is the parent of the "TreeDirNode" class and is the "Component"
" part of the composite design pattern between the NERDTree node classes.
" ============================================================================
let s:TreeFileNode = {}
let g:NERDTreeFileNode = s:TreeFileNode
@ -193,14 +196,18 @@ function! s:TreeFileNode.GetRootForTab()
endfunction
" 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()
try
let path = b:NERDTree.ui.getPath(line("."))
if path ==# {}
let l:path = b:NERDTree.ui.getPath(line('.'))
if empty(l:path)
return {}
endif
return b:NERDTree.root.findNode(path)
return b:NERDTree.root.findNode(l:path)
catch /^NERDTree/
return {}
endtry

View File

@ -1,5 +1,8 @@
" ============================================================================
" CLASS: UI
"============================================================
" ============================================================================
let s:UI = {}
let g:NERDTreeUI = s:UI
@ -138,21 +141,15 @@ function! s:UI.New(nerdtree)
endfunction
" FUNCTION: s:UI.getPath(ln) {{{1
"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
" 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()
"check to see if we have the root node
if a:ln == rootLine
return self.nerdtree.root.path
endif
@ -161,6 +158,10 @@ function! s:UI.getPath(ln)
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