Merge pull request #734 from lifecrisis/issue733

BUGFIX: Make the NERDTree aware of the 'shellslash' setting.
This commit is contained in:
Jason Franklin 2017-08-19 09:48:27 -04:00 committed by GitHub
commit 5782b228e4
3 changed files with 204 additions and 162 deletions

View File

@ -1,9 +1,16 @@
"we need to use this number many times for sorting... so we calculate it only
"once here
let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*')
" ============================================================================
" CLASS: Path
"============================================================
"
" The Path class provides an abstracted representation of a file system
" pathname. Various operations on pathnames are provided and a number of
" representations of a given path name can be accessed here.
" ============================================================================
" This constant is used throughout this script for sorting purposes.
let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*')
lockvar s:NERDTreeSortStarIndex
let s:Path = {}
let g:NERDTreePath = s:Path
@ -529,9 +536,20 @@ function! s:Path.New(path)
endfunction
" FUNCTION: Path.Slash() {{{1
"return the slash to use for the current OS
" Return the path separator used by the underlying file system. Special
" consideration is taken for the use of the 'shellslash' option on Windows
" systems.
function! s:Path.Slash()
return nerdtree#runningWindows() ? '\' : '/'
if nerdtree#runningWindows()
if exists('+shellslash') && &shellslash
return '/'
endif
return '\'
endif
return '/'
endfunction
" FUNCTION: Path.Resolve() {{{1
@ -633,27 +651,27 @@ function! s:Path.rename(newPath)
endfunction
" FUNCTION: Path.str() {{{1
" Return a string representation of this Path object.
"
"Returns a string representation of this Path
" Args:
" This function takes a single dictionary (optional) with keys and values that
" specify how the returned pathname should be formatted.
"
"Takes an optional dictionary param to specify how the output should be
"formatted.
"
"The dict may have the following keys:
" The dictionary may have the following keys:
" 'format'
" 'escape'
" 'truncateTo'
"
" The 'format' key may have a value of:
" 'Cd' - a string to be used with the :cd command
" 'Edit' - a string to be used with :e :sp :new :tabedit etc
" 'UI' - a string used in the NERD tree UI
" 'Cd' - a string to be used with ":cd" and similar commands
" 'Edit' - a string to be used with ":edit" and similar commands
" 'UI' - a string to be displayed in the NERDTree user interface
"
"The 'escape' key, if specified will cause the output to be escaped with
"shellescape()
" The 'escape' key, if specified, will cause the output to be escaped with
" Vim's internal "shellescape()" function.
"
"The 'truncateTo' key causes the resulting string to be truncated to the value
"'truncateTo' maps to. A '<' char will be prepended.
" The 'truncateTo' key shortens the length of the path to that given by the
" value associated with 'truncateTo'. A '<' is prepended.
function! s:Path.str(...)
let options = a:0 ? a:1 : {}
let toReturn = ""
@ -698,33 +716,33 @@ function! s:Path._strForUI()
endfunction
" FUNCTION: Path._strForCd() {{{1
"
" returns a string that can be used with :cd
" Return a string representation of this Path that is suitable for use as an
" argument to Vim's internal ":cd" command.
function! s:Path._strForCd()
return escape(self.str(), self._escChars())
return fnameescape(self.str())
endfunction
" FUNCTION: Path._strForEdit() {{{1
"
"Return: the string for this path that is suitable to be used with the :edit
"command
" Return a string representation of this Path that is suitable for use as an
" argument to Vim's internal ":edit" command.
function! s:Path._strForEdit()
let p = escape(self.str(), self._escChars())
"make it relative
let p = fnamemodify(p, ':.')
" Make the path relative to the current working directory, if possible.
let l:result = fnamemodify(self.str(), ':.')
"handle the edge case where the file begins with a + (vim interprets
"the +foo in `:e +foo` as an option to :edit)
if p[0] == "+"
let p = '\' . p
" On Windows, the drive letter may be removed by "fnamemodify()". Add it
" back, if necessary.
if nerdtree#runningWindows() && l:result[0] == s:Path.Slash()
let l:result = self.drive . l:result
endif
if p ==# ''
let p = '.'
let l:result = fnameescape(l:result)
if empty(l:result)
let l:result = '.'
endif
return p
return l:result
endfunction
" FUNCTION: Path._strForGlob() {{{1
@ -745,19 +763,17 @@ function! s:Path._strForGlob()
endfunction
" FUNCTION: Path._str() {{{1
"
"Gets the string path for this path object that is appropriate for the OS.
"EG, in windows c:\foo\bar
" in *nix /foo/bar
" Return the absolute pathname associated with this Path object. The pathname
" returned is appropriate for the underlying file system.
function! s:Path._str()
let lead = s:Path.Slash()
let l:separator = s:Path.Slash()
let l:leader = l:separator
"if we are running windows then slap a drive letter on the front
if nerdtree#runningWindows()
let lead = self.drive . '\'
let l:leader = self.drive . l:separator
endif
return lead . join(self.pathSegments, s:Path.Slash())
return l:leader . join(self.pathSegments, l:separator)
endfunction
" FUNCTION: Path.strTrunk() {{{1

View File

@ -243,7 +243,7 @@ function! s:TreeDirNode._glob(pattern, all)
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] == '\'
if nerdtree#runningWindows() && l:pathSpec[0] == g:NERDTreePath.Slash()
let l:pathSpec = self.path.drive . l:pathSpec
endif
endif

View File

@ -212,14 +212,40 @@ endfunction
" FUNCTION: NERDTreeListNodeWin32() {{{1
function! NERDTreeListNodeWin32()
let treenode = g:NERDTreeFileNode.GetSelected()
if treenode != {}
let metadata = split(system('DIR /Q ' . shellescape(treenode.path.str()) . ' | FINDSTR "^[012][0-9]/[0-3][0-9]/[12][0-9][0-9][0-9]"'), '\n')
call nerdtree#echo(metadata[0])
else
call nerdtree#echo("No information avaialable")
let l:node = g:NERDTreeFileNode.GetSelected()
if !empty(l:node)
let l:save_shell = &shell
set shell&
if exists('+shellslash')
let l:save_shellslash = &shellslash
set noshellslash
endif
let l:command = 'DIR /Q '
\ . shellescape(l:node.path.str())
\ . ' | FINDSTR "^[012][0-9]/[0-3][0-9]/[12][0-9][0-9][0-9]"'
let l:metadata = split(system(l:command), "\n")
if v:shell_error == 0
call nerdtree#echo(l:metadata[0])
else
call nerdtree#echoError('shell command failed')
endif
let &shell = l:save_shell
if exists('l:save_shellslash')
let &shellslash = l:save_shellslash
endif
return
endif
call nerdtree#echo('node not recognized')
endfunction
" FUNCTION: NERDTreeCopyNode() {{{1