From e2670f0d190c9c0593ad7ac7b5708a4c198e423d Mon Sep 17 00:00:00 2001 From: Eugenij <62702485+Eugenij-W@users.noreply.github.com> Date: Fri, 15 May 2020 00:24:36 +0600 Subject: [PATCH] * fix duplicated slash in s:Path.isUnder() (on windows OS) (#1122) * * fix duplicated slash in s:Path.isUnder() (on windows OS, for root directory on drive Path.str() return path with [back]slash) * * Rewrite NERDTreePath.isUnder() and NERDTreePath.isAncestor() for direct comparison of paths without transformations * Remove trailing slash, so we don't end up with two on root folder. The str() function returns "C:\" on the root folder and "C:\temp" on non-root folders, one with and one without a trailing backslash. This inconsistency needs to be handled so the stridx() function will work correctly. * Make sure the change also works in a non-windows file system. This commit handles an edge case that can be triggered with these commands: :cd /home/me :e /foobar.txt (an existing file) :NERDTreeFind What happened was the root directory name '/' was being Resolved(), and the trailing (and only) slash was being removed. The NERDTree was then created in the current working directory, instead of the root directory. :NERDTreeFind then wasn't able to find foobar.txt, and printed an error. * Remove degugging statements. * * ambiguity issue fix Co-authored-by: Phil Runninger --- CHANGELOG.md | 1 + lib/nerdtree/creator.vim | 6 +++++- lib/nerdtree/path.vim | 38 ++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bcf4d74..c68c8dec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - **.PATCH**: Pull Request Title (PR Author) [PR Number](Link to PR) --> #### 6.7 +- **.11**: Fix exception in NERDTreeFind (on windows OS and If the file is located in the root directory of the disk) [#1122](https://github.com/preservim/nerdtree/pull/1122) - **.10**: Do not consider the tree root to be "cascadable". (lifecrisis) [#1120](https://github.com/preservim/nerdtree/pull/1120) - **.9**: Force `:NERDTreeFocus` to allow events to be fired when switching windows. (PhilRunninger) [#1118](https://github.com/preservim/nerdtree/pull/1118) - **.8**: Fix example code for the `NERDTreeAddKeyMap()` function. (PhilRunninger) [#1116](https://github.com/preservim/nerdtree/pull/1116) diff --git a/lib/nerdtree/creator.vim b/lib/nerdtree/creator.vim index abaa8dbe..f845361d 100644 --- a/lib/nerdtree/creator.vim +++ b/lib/nerdtree/creator.vim @@ -249,7 +249,11 @@ function! s:Creator._pathForString(str) if dir =~# '^\.' let dir = getcwd() . g:NERDTreePath.Slash() . dir endif - let dir = g:NERDTreePath.Resolve(dir) + + "hack to prevent removing slash if dir is the root of the file system. + if dir !=# '/' + let dir = g:NERDTreePath.Resolve(dir) + endif try let path = g:NERDTreePath.New(dir) diff --git a/lib/nerdtree/path.vim b/lib/nerdtree/path.vim index 2ac8c71c..61cdfa88 100644 --- a/lib/nerdtree/path.vim +++ b/lib/nerdtree/path.vim @@ -546,26 +546,36 @@ endfunction " return 1 if this path is somewhere above the given path in the filesystem. " " a:path should be a dir -function! s:Path.isAncestor(path) - if !self.isDirectory - return 0 - endif - - let this = self.str() - let that = a:path.str() - return stridx(that, this) ==# 0 +function! s:Path.isAncestor(child) + return a:child.isUnder(self) endfunction " FUNCTION: Path.isUnder(path) {{{1 " return 1 if this path is somewhere under the given path in the filesystem. -function! s:Path.isUnder(path) - if a:path.isDirectory ==# 0 +function! s:Path.isUnder(parent) + if a:parent.isDirectory ==# 0 return 0 endif - - let this = self.str() - let that = a:path.str() - return stridx(this, that . s:Path.Slash()) ==# 0 + if nerdtree#runningWindows() && a:parent.drive !=# self.drive + return 0 + endif + let l:this_count = len(self.pathSegments) + if l:this_count ==# 0 + return 0 + endif + let l:that_count = len(a:parent.pathSegments) + if l:that_count ==# 0 + return 1 + endif + if l:that_count >= l:this_count + return 0 + endif + for i in range(0, l:that_count-1) + if self.pathSegments[i] !=# a:parent.pathSegments[i] + return 0 + endif + endfor + return 1 endfunction " FUNCTION: Path.JoinPathStrings(...) {{{1