From b0c44c7be1a094dc6b9e8e93bd5c4325ab99ad04 Mon Sep 17 00:00:00 2001 From: Jason Franklin Date: Sat, 24 Jun 2017 10:44:28 -0400 Subject: [PATCH] Refactor the TreeDirNode glob method Pull request #710 correctly noted that TreeDirNode directories must be passed to "globpath()" as relative paths (i.e., to the working directory) if 'wildignore' rules for relative paths are to be obeyed. The solution was to use "fnamemodify()" to get a relative path to the TreeDirNode object's directory, if possible. However, this method does not modify our TreeDirNode path if it IS the current working directory. Thus, immediate children of the node are seen as absolute paths in glob results when our PWD is pointing to their parent. This is not consistent behavior. This commit defines the result of this function as ',' when this special case arises to fix this problem. See ":h 'path'" for an explanation of how this works. --- lib/nerdtree/tree_dir_node.vim | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/nerdtree/tree_dir_node.vim b/lib/nerdtree/tree_dir_node.vim index aaa8d33..1ae944e 100644 --- a/lib/nerdtree/tree_dir_node.vim +++ b/lib/nerdtree/tree_dir_node.vim @@ -206,23 +206,27 @@ function! s:TreeDirNode.getDirChildren() return filter(self.children, 'v:val.path.isDirectory == 1') endfunction -"FUNCTION: TreeDirNode._getGlobDir() {{{1 -"Return a string giving the pathname related to this TreeDirNode. The returned -"pathname is in a glob-friendly format and is relative to the current working -"directory, if this TreeDirNode's path is under the current working directory. +" FUNCTION: TreeDirNode._getGlobDir() {{{1 +" Return a path specification for this TreeDirNode that is suitable as an +" argument to "globpath()". +" +" Note: The result is constructed such that "globpath()" will return paths +" relative to the working directory, if possible. This is necessary to ensure +" that 'wildignore' rules for relative paths are obeyed. function! s:TreeDirNode._getGlobDir() - " Gets a relative path, if possible. This ensures that 'wildignore' rules - " for relative paths will be obeyed. - let l:globDir = fnamemodify(self.path.str({'format': 'Glob'}), ':.') - " Calling fnamemodify() with ':.' on Windows systems strips the leading - " drive letter from paths that aren't under the working directory. Here, - " the drive letter is added back to the pathname. - if nerdtree#runningWindows() && l:globDir[0] == '\' - let l:globDir = self.path.drive . l:globDir + if self.path.str() == getcwd() + let l:pathSpec = ',' + else + let l:pathSpec = fnamemodify(self.path.str({'format': 'Glob'}), ':.') + + " On Windows, the drive letter may be removed by "fnamemodify()". + if nerdtree#runningWindows() && l:pathSpec[0] == '\' + let l:pathSpec = self.path.drive . l:pathSpec + endif endif - return l:globDir + return l:pathSpec endfunction "FUNCTION: TreeDirNode.GetSelected() {{{1