Function "s:UI.getLineNum()" doesn't always work on cascades. (#882)

This PR is a rewrite of the "s:UI.getLineNum()" function.  This change
improves the logic and clarity of this function, and it fixes the function's
ability to work with cascades.

Fixes #529.
This commit is contained in:
Jason Franklin 2018-09-13 08:39:09 -04:00 committed by GitHub
parent b3804dcd71
commit e9d3f72d9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -194,51 +194,52 @@ function! s:UI.getPath(ln)
return toReturn return toReturn
endfunction endfunction
" FUNCTION: s:UI.getLineNum(file_node){{{1 " FUNCTION: s:UI.getLineNum(node) {{{1
" returns the line number this node is rendered on, or -1 if it isnt rendered " Return the line number where the given node is rendered. Return -1 if the
function! s:UI.getLineNum(file_node) " given node is not visible.
" if the node is the root then return the root line no. function! s:UI.getLineNum(node)
if a:file_node.isRoot()
if a:node.isRoot()
return self.getRootLineNum() return self.getRootLineNum()
endif endif
let totalLines = line("$") let l:pathComponents = [substitute(self.nerdtree.root.path.str({'format': 'UI'}), '/\s*$', '', '')]
let l:currentPathComponent = 1
" the path components we have matched so far let l:fullPath = a:node.path.str({'format': 'UI'})
let pathcomponents = [substitute(self.nerdtree.root.path.str({'format': 'UI'}), '/ *$', '', '')]
" the index of the component we are searching for
let curPathComponent = 1
let fullpath = a:file_node.path.str({'format': 'UI'}) for l:lineNumber in range(self.getRootLineNum() + 1, line('$'))
let l:currentLine = getline(l:lineNumber)
let l:indentLevel = self._indentLevelFor(l:currentLine)
let lnum = self.getRootLineNum() if l:indentLevel != l:currentPathComponent
while lnum > 0 continue
let lnum = lnum + 1
" have we reached the bottom of the tree?
if lnum ==# totalLines+1
return -1
endif endif
let curLine = getline(lnum) let l:currentLine = self._stripMarkup(l:currentLine)
let l:currentPath = join(l:pathComponents, '/') . '/' . l:currentLine
let indent = self._indentLevelFor(curLine) " Directories: If the current path "starts with" the full path, then
if indent ==# curPathComponent " either the paths are equal or the line is a cascade containing the
let curLine = self._stripMarkup(curLine) " full path.
if l:fullPath[-1:] == '/' && stridx(l:currentPath, l:fullPath) == 0
let curPath = join(pathcomponents, '/') . '/' . curLine return l:lineNumber
if stridx(fullpath, curPath, 0) ==# 0
if fullpath ==# curPath || strpart(fullpath, len(curPath)-1,1) ==# '/'
let curLine = substitute(curLine, '/ *$', '', '')
call add(pathcomponents, curLine)
let curPathComponent = curPathComponent + 1
if fullpath ==# curPath
return lnum
endif
endif
endif
endif endif
endwhile
" Files: The paths must exactly match.
if l:fullPath ==# l:currentPath
return l:lineNumber
endif
" Otherwise: If the full path starts with the current path and the
" current path is a directory, we add a new path component.
if stridx(l:fullPath, l:currentPath) == 0 && l:currentPath[-1:] == '/'
let l:currentLine = substitute(l:currentLine, '/\s*$', '', '')
call add(l:pathComponents, l:currentLine)
let l:currentPathComponent += 1
endif
endfor
return -1 return -1
endfunction endfunction