refactor the Path#str* methods

This commit is contained in:
marty 2009-09-01 17:15:48 +12:00
parent 8fc72fd352
commit 0dda0ce5d7
4 changed files with 78 additions and 63 deletions

View File

@ -30,7 +30,7 @@ function! NERDTreeExecFile()
let treenode = g:NERDTreeFileNode.GetSelected() let treenode = g:NERDTreeFileNode.GetSelected()
echo "==========================================================\n" echo "==========================================================\n"
echo "Complete the command to execute (add arguments etc):\n" echo "Complete the command to execute (add arguments etc):\n"
let cmd = treenode.path.strForOS(1) let cmd = treenode.path.str({'format': 'OS', 'escape': 1})
let cmd = input(':!', cmd . ' ') let cmd = input(':!', cmd . ' ')
if cmd != '' if cmd != ''

View File

@ -57,7 +57,7 @@ function! NERDTreeAddNode()
let newNodeName = input("Add a childnode\n". let newNodeName = input("Add a childnode\n".
\ "==========================================================\n". \ "==========================================================\n".
\ "Enter the dir/file name to be created. Dirs end with a '/'\n" . \ "Enter the dir/file name to be created. Dirs end with a '/'\n" .
\ "", curDirNode.path.strForGlob() . g:NERDTreePath.Slash()) \ "", curDirNode.path.str({'format': 'Glob'}) . g:NERDTreePath.Slash())
if newNodeName ==# '' if newNodeName ==# ''
call s:echo("Node Creation Aborted.") call s:echo("Node Creation Aborted.")
@ -85,7 +85,7 @@ function! NERDTreeMoveNode()
let newNodePath = input("Rename the current node\n" . let newNodePath = input("Rename the current node\n" .
\ "==========================================================\n" . \ "==========================================================\n" .
\ "Enter the new path for the node: \n" . \ "Enter the new path for the node: \n" .
\ "", curNode.path.strForOS(0)) \ "", curNode.path.str({'format': 'OS'})
if newNodePath ==# '' if newNodePath ==# ''
call s:echo("Node Renaming Aborted.") call s:echo("Node Renaming Aborted.")

View File

@ -66,11 +66,11 @@ endfunction
function! NERDTreeGitMove() function! NERDTreeGitMove()
let node = g:NERDTreeFileNode.GetSelected() let node = g:NERDTreeFileNode.GetSelected()
let path = node.path let path = node.path
let p = path.strForOS(1) let p = path.str({'format': 'OS', 'escape': 1})
let newPath = input("==========================================================\n" . let newPath = input("==========================================================\n" .
\ "Enter the new path for the file: \n" . \ "Enter the new path for the file: \n" .
\ "", node.path.strForOS(0)) \ "", node.path.str({'format': 'OS'}))
if newPath ==# '' if newPath ==# ''
call s:echo("git mv aborted.") call s:echo("git mv aborted.")
return return
@ -82,19 +82,19 @@ endfunction
function! NERDTreeGitAdd() function! NERDTreeGitAdd()
let node = g:NERDTreeFileNode.GetSelected() let node = g:NERDTreeFileNode.GetSelected()
let path = node.path let path = node.path
call s:execGitCmd('add ' . path.strForOS(1)) call s:execGitCmd('add ' . path.str({'format': 'OS', 'escape': 1}))
endfunction endfunction
function! NERDTreeGitRemove() function! NERDTreeGitRemove()
let node = g:NERDTreeFileNode.GetSelected() let node = g:NERDTreeFileNode.GetSelected()
let path = node.path let path = node.path
call s:execGitCmd('rm ' . path.strForOS(1)) call s:execGitCmd('rm ' . path.str({'format': 'OS', 'escape': 1}))
endfunction endfunction
function! NERDTreeGitCheckout() function! NERDTreeGitCheckout()
let node = g:NERDTreeFileNode.GetSelected() let node = g:NERDTreeFileNode.GetSelected()
let path = node.path let path = node.path
call s:execGitCmd('checkout ' . path.strForOS(1)) call s:execGitCmd('checkout ' . path.str({'format': 'OS', 'escape': 1}))
endfunction endfunction
function! s:execGitCmd(sub_cmd) function! s:execGitCmd(sub_cmd)

View File

@ -79,7 +79,9 @@ endif
"once here "once here
let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*') let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*')
call s:initVariable("g:NERDTreeStatusline", "%{b:NERDTreeRoot.path.strForOS(0)}") if !exists('g:NERDTreeStatusline')
let g:NERDTreeStatusline = "%{b:NERDTreeRoot.path._strForOS()}"
endif
call s:initVariable("g:NERDTreeWinPos", "left") call s:initVariable("g:NERDTreeWinPos", "left")
call s:initVariable("g:NERDTreeWinSize", 31) call s:initVariable("g:NERDTreeWinSize", 31)
@ -340,7 +342,7 @@ function! s:Bookmark.mustExist()
if !self.path.exists() if !self.path.exists()
call s:Bookmark.CacheBookmarks(1) call s:Bookmark.CacheBookmarks(1)
throw "NERDTree.BookmarkPointsToInvalidLocationError: the bookmark \"". throw "NERDTree.BookmarkPointsToInvalidLocationError: the bookmark \"".
\ self.name ."\" points to a non existing location: \"". self.path.strForOS(0) \ self.name ."\" points to a non existing location: \"". self.path.str({'format': 'OS'})
endif endif
endfunction endfunction
" FUNCTION: Bookmark.New(name, path) {{{3 " FUNCTION: Bookmark.New(name, path) {{{3
@ -374,7 +376,7 @@ function! s:Bookmark.str()
let pathStrMaxLen = pathStrMaxLen - &numberwidth let pathStrMaxLen = pathStrMaxLen - &numberwidth
endif endif
let pathStr = self.path.strForOS(0) let pathStr = self.path.str({'format': 'OS'})
if len(pathStr) > pathStrMaxLen if len(pathStr) > pathStrMaxLen
let pathStr = '<' . strpart(pathStr, len(pathStr) - pathStrMaxLen) let pathStr = '<' . strpart(pathStr, len(pathStr) - pathStrMaxLen)
endif endif
@ -419,7 +421,7 @@ endfunction
function! s:Bookmark.Write() function! s:Bookmark.Write()
let bookmarkStrings = [] let bookmarkStrings = []
for i in s:Bookmark.Bookmarks() for i in s:Bookmark.Bookmarks()
call add(bookmarkStrings, i.name . ' ' . i.path.strForOS(0)) call add(bookmarkStrings, i.name . ' ' . i.path.str({'format': 'OS'}))
endfor endfor
"add a blank line before the invalid ones "add a blank line before the invalid ones
@ -1047,7 +1049,7 @@ function! s:TreeFileNode.makeRoot()
"change dir to the dir of the new root if instructed to "change dir to the dir of the new root if instructed to
if g:NERDTreeChDirMode ==# 2 if g:NERDTreeChDirMode ==# 2
exec "cd " . b:NERDTreeRoot.path.strForEditCmd() exec "cd " . b:NERDTreeRoot.path.str({'format': 'EditCmd'})
endif endif
endfunction endfunction
"FUNCTION: TreeFileNode.New(path) {{{3 "FUNCTION: TreeFileNode.New(path) {{{3
@ -1075,12 +1077,12 @@ endfunction
"treenode: file node to open "treenode: file node to open
function! s:TreeFileNode.open() function! s:TreeFileNode.open()
if b:NERDTreeType ==# "secondary" if b:NERDTreeType ==# "secondary"
exec 'edit ' . self.path.strForEditCmd() exec 'edit ' . self.path.str({'format': 'EditCmd'})
return return
endif endif
"if the file is already open in this tab then just stick the cursor in it "if the file is already open in this tab then just stick the cursor in it
let winnr = bufwinnr('^' . self.path.strForOS(0) . '$') let winnr = bufwinnr('^' . self.path.str({'format': 'OS'}) . '$')
if winnr != -1 if winnr != -1
call s:exec(winnr . "wincmd w") call s:exec(winnr . "wincmd w")
@ -1094,7 +1096,7 @@ function! s:TreeFileNode.open()
else else
call s:exec('wincmd p') call s:exec('wincmd p')
endif endif
exec ("edit " . self.path.strForEditCmd()) exec ("edit " . self.path.str({'format': 'EditCmd'}))
catch /^Vim\%((\a\+)\)\=:E37/ catch /^Vim\%((\a\+)\)\=:E37/
call s:putCursorInTreeWin() call s:putCursorInTreeWin()
throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self.path.str() ." is already open and modified." throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self.path.str() ." is already open and modified."
@ -1109,7 +1111,7 @@ endfunction
function! s:TreeFileNode.openSplit() function! s:TreeFileNode.openSplit()
if b:NERDTreeType ==# "secondary" if b:NERDTreeType ==# "secondary"
exec "split " . self.path.strForEditCmd() exec "split " . self.path.str({'format': 'EditCmd'})
return return
endif endif
@ -1150,7 +1152,7 @@ function! s:TreeFileNode.openSplit()
" Open the new window " Open the new window
try try
exec(splitMode." sp " . self.path.strForEditCmd()) exec(splitMode." sp " . self.path.str({'format': 'EditCmd'}))
catch /^Vim\%((\a\+)\)\=:E37/ catch /^Vim\%((\a\+)\)\=:E37/
call s:putCursorInTreeWin() call s:putCursorInTreeWin()
throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self.path.str() ." is already open and modified." throw "NERDTree.FileAlreadyOpenAndModifiedError: ". self.path.str() ." is already open and modified."
@ -1174,7 +1176,7 @@ endfunction
"Open this node in a new vertical window "Open this node in a new vertical window
function! s:TreeFileNode.openVSplit() function! s:TreeFileNode.openVSplit()
if b:NERDTreeType ==# "secondary" if b:NERDTreeType ==# "secondary"
exec "vnew " . self.path.strForEditCmd() exec "vnew " . self.path.str({'format': 'EditCmd'})
return return
endif endif
@ -1184,7 +1186,7 @@ function! s:TreeFileNode.openVSplit()
endif endif
call s:exec("wincmd p") call s:exec("wincmd p")
exec "vnew " . self.path.strForEditCmd() exec "vnew " . self.path.str({'format': 'EditCmd'})
"resize the nerd tree back to the original size "resize the nerd tree back to the original size
call s:putCursorInTreeWin() call s:putCursorInTreeWin()
@ -1535,7 +1537,8 @@ function! s:TreeDirNode._initChildren(silent)
"get an array of all the files in the nodes dir "get an array of all the files in the nodes dir
let dir = self.path let dir = self.path
let filesStr = globpath(dir.strForGlob(), '*') . "\n" . globpath(dir.strForGlob(), '.*') let globDir = dir.str({'format': 'Glob'})
let filesStr = globpath(globDir, '*') . "\n" . globpath(globDir, '.*')
let files = split(filesStr, "\n") let files = split(filesStr, "\n")
if !a:silent && len(files) > g:NERDTreeNotificationThreshold if !a:silent && len(files) > g:NERDTreeNotificationThreshold
@ -1616,7 +1619,7 @@ function! s:TreeDirNode.openExplorer()
call s:exec('wincmd p') call s:exec('wincmd p')
call self.openSplit() call self.openSplit()
else else
exec ("silent edit " . self.path.strForEditCmd()) exec ("silent edit " . self.path.str({'format': 'EditCmd'}))
endif endif
endfunction endfunction
"FUNCTION: TreeDirNode.openRecursively() {{{3 "FUNCTION: TreeDirNode.openRecursively() {{{3
@ -1662,7 +1665,8 @@ function! s:TreeDirNode.refresh()
let newChildNodes = [] let newChildNodes = []
let invalidFilesFound = 0 let invalidFilesFound = 0
let dir = self.path let dir = self.path
let filesStr = globpath(dir.strForGlob(), '*') . "\n" . globpath(dir.strForGlob(), '.*') let globDir = dir.str({'format': 'Glob'})
let filesStr = globpath(globDir, '*') . "\n" . globpath(globDir, '.*')
let files = split(filesStr, "\n") let files = split(filesStr, "\n")
for i in files for i in files
"filter out the .. and . directories "filter out the .. and . directories
@ -1914,10 +1918,10 @@ function! s:Path.copy(dest)
let dest = s:Path.WinToUnixPath(a:dest) let dest = s:Path.WinToUnixPath(a:dest)
let cmd = g:NERDTreeCopyCmd . " " . self.strForOS(0) . " " . dest let cmd = g:NERDTreeCopyCmd . " " . self.str({'format': 'OS'}) . " " . dest
let success = system(cmd) let success = system(cmd)
if success != 0 if success != 0
throw "NERDTree.CopyError: Could not copy ''". self.strForOS(0) ."'' to: '" . a:dest . "'" throw "NERDTree.CopyError: Could not copy ''". self.str({'format': 'OS'}) ."'' to: '" . a:dest . "'"
endif endif
endfunction endfunction
@ -1958,14 +1962,14 @@ endfunction
function! s:Path.delete() function! s:Path.delete()
if self.isDirectory if self.isDirectory
let cmd = g:NERDTreeRemoveDirCmd . self.strForOS(1) let cmd = g:NERDTreeRemoveDirCmd . self.str({'format': 'OS', 'escape': 1})
let success = system(cmd) let success = system(cmd)
if v:shell_error != 0 if v:shell_error != 0
throw "NERDTree.PathDeletionError: Could not delete directory: '" . self.strForOS(0) . "'" throw "NERDTree.PathDeletionError: Could not delete directory: '" . self.str({'format': 'OS'}) . "'"
endif endif
else else
let success = delete(self.strForOS(0)) let success = delete(self.str({'format': 'OS'}))
if success != 0 if success != 0
throw "NERDTree.PathDeletionError: Could not delete file: '" . self.str() . "'" throw "NERDTree.PathDeletionError: Could not delete file: '" . self.str() . "'"
endif endif
@ -2003,7 +2007,8 @@ endfunction
"FUNCTION: Path.exists() {{{3 "FUNCTION: Path.exists() {{{3
"return 1 if this path points to a location that is readable or is a directory "return 1 if this path points to a location that is readable or is a directory
function! s:Path.exists() function! s:Path.exists()
return filereadable(self.strForOS(0)) || isdirectory(self.strForOS(0)) let p = self.str({'format': 'OS'})
return filereadable(p) || isdirectory(p)
endfunction endfunction
"FUNCTION: Path.getDir() {{{3 "FUNCTION: Path.getDir() {{{3
" "
@ -2179,7 +2184,7 @@ endfunction
"FUNCTION: Path.refresh() {{{3 "FUNCTION: Path.refresh() {{{3
function! s:Path.refresh() function! s:Path.refresh()
call self.readInfoFromDisk(self.strForOS(0)) call self.readInfoFromDisk(self.str({'format': 'OS'}))
call self.cacheDisplayString() call self.cacheDisplayString()
endfunction endfunction
@ -2191,9 +2196,9 @@ function! s:Path.rename(newPath)
throw "NERDTree.InvalidArgumentsError: Invalid newPath for renaming = ". a:newPath throw "NERDTree.InvalidArgumentsError: Invalid newPath for renaming = ". a:newPath
endif endif
let success = rename(self.strForOS(0), a:newPath) let success = rename(self.str({'format': 'OS'}), a:newPath)
if success != 0 if success != 0
throw "NERDTree.PathRenameError: Could not rename: '" . self.strForOS(0) . "'" . 'to:' . a:newPath throw "NERDTree.PathRenameError: Could not rename: '" . self.str({'format': 'OS'}) . "'" . 'to:' . a:newPath
endif endif
call self.readInfoFromDisk(a:newPath) call self.readInfoFromDisk(a:newPath)
@ -2204,10 +2209,33 @@ function! s:Path.rename(newPath)
call s:Bookmark.Write() call s:Bookmark.Write()
endfunction endfunction
"FUNCTION: Path.str(esc) {{{3 "FUNCTION: Path.str() {{{3
" "
"Gets the actual string path that this obj represents. "Gets the actual string path that this obj represents.
function! s:Path.str() function! s:Path.str(...)
let options = a:0 ? a:1 : {}
let toReturn = ""
if has_key(options, 'format')
let format = options['format']
if has_key(self, '_strFor' . format)
exec 'let toReturn = self._strFor' . format . '()'
else
raise 'NERDTree.UnknownFormatError: unknown format "'. format .'"'
endif
else
let toReturn = self._str()
endif
if has_key(options, 'escape') && options['escape']
let toReturn = shellescape(toReturn)
endif
return toReturn
endfunction
"FUNCTION: Path._str() {{{3
function! s:Path._str()
let toReturn = '/' . join(self.pathSegments, '/') let toReturn = '/' . join(self.pathSegments, '/')
if self.isDirectory && toReturn != '/' if self.isDirectory && toReturn != '/'
let toReturn = toReturn . '/' let toReturn = toReturn . '/'
@ -2215,26 +2243,26 @@ function! s:Path.str()
return toReturn return toReturn
endfunction endfunction
"FUNCTION: Path.strForCd() {{{3 "FUNCTION: Path._strForCd() {{{3
" "
" returns a string that can be used with :cd " returns a string that can be used with :cd
function! s:Path.strForCd() function! s:Path._strForCd()
if s:running_windows if s:running_windows
return self.strForOS(0) return self.str({'format': 'OS'})
else else
return self.strForOS(1) return self.str({'format': 'OS', 'escape': 1})
endif endif
endfunction endfunction
"FUNCTION: Path.strForEditCmd() {{{3 "FUNCTION: Path._strForEditCmd() {{{3
" "
"Return: the string for this path that is suitable to be used with the :edit "Return: the string for this path that is suitable to be used with the :edit
"command "command
function! s:Path.strForEditCmd() function! s:Path._strForEditCmd()
let p = self.str() let p = self.str()
let cwd = getcwd() let cwd = getcwd()
if s:running_windows if s:running_windows
let p = tolower(self.strForOS(0)) let p = tolower(self.str({'format': 'OS'}))
let cwd = tolower(getcwd()) let cwd = tolower(getcwd())
endif endif
@ -2252,8 +2280,8 @@ function! s:Path.strForEditCmd()
return p return p
endfunction endfunction
"FUNCTION: Path.strForGlob() {{{3 "FUNCTION: Path._strForGlob() {{{3
function! s:Path.strForGlob() function! s:Path._strForGlob()
let lead = s:Path.Slash() let lead = s:Path.Slash()
"if we are running windows then slap a drive letter on the front "if we are running windows then slap a drive letter on the front
@ -2268,16 +2296,12 @@ function! s:Path.strForGlob()
endif endif
return toReturn return toReturn
endfunction endfunction
"FUNCTION: Path.strForOS(esc) {{{3 "FUNCTION: Path._strForOS() {{{3
" "
"Gets the string path for this path object that is appropriate for the OS. "Gets the string path for this path object that is appropriate for the OS.
"EG, in windows c:\foo\bar "EG, in windows c:\foo\bar
" in *nix /foo/bar " in *nix /foo/bar
" function! s:Path._strForOS()
"Args:
"esc: if 1 then all the tricky chars in the returned string will be
" escaped. If we are running windows then the str is double quoted instead.
function! s:Path.strForOS(esc)
let lead = s:Path.Slash() let lead = s:Path.Slash()
"if we are running windows then slap a drive letter on the front "if we are running windows then slap a drive letter on the front
@ -2285,16 +2309,7 @@ function! s:Path.strForOS(esc)
let lead = self.drive . '\' let lead = self.drive . '\'
endif endif
let toReturn = lead . join(self.pathSegments, s:Path.Slash()) return lead . join(self.pathSegments, s:Path.Slash())
if a:esc
if s:running_windows
let toReturn = '"' . toReturn . '"'
else
let toReturn = escape(toReturn, s:escape_chars)
endif
endif
return toReturn
endfunction endfunction
"FUNCTION: Path.strTrunk() {{{3 "FUNCTION: Path.strTrunk() {{{3
@ -2519,7 +2534,7 @@ function! s:initNerdTreeMirror()
while i < len(treeBufNames) while i < len(treeBufNames)
let bufName = treeBufNames[i] let bufName = treeBufNames[i]
let treeRoot = getbufvar(bufName, "NERDTreeRoot") let treeRoot = getbufvar(bufName, "NERDTreeRoot")
let options[i+1 . '. ' . treeRoot.path.strForOS(0) . ' (buf name: ' . bufName . ')'] = bufName let options[i+1 . '. ' . treeRoot.path.str({'format': 'OS'}) . ' (buf name: ' . bufName . ')'] = bufName
let i = i + 1 let i = i + 1
endwhile endwhile
@ -3738,9 +3753,9 @@ function! s:openInNewTab(stayCurrentTab)
if treenode != {} if treenode != {}
if treenode.path.isDirectory if treenode.path.isDirectory
tabnew tabnew
call s:initNerdTree(treenode.path.strForOS(0)) call s:initNerdTree(treenode.path.str({'format': 'OS'}))
else else
exec "tabedit " . treenode.path.strForEditCmd() exec "tabedit " . treenode.path.str({'format': 'EditCmd'})
endif endif
else else
let bookmark = s:getSelectedBookmark() let bookmark = s:getSelectedBookmark()
@ -3749,7 +3764,7 @@ function! s:openInNewTab(stayCurrentTab)
tabnew tabnew
call s:initNerdTree(bookmark.name) call s:initNerdTree(bookmark.name)
else else
exec "tabedit " . bookmark.path.strForEditCmd() exec "tabedit " . bookmark.path.str({'format': 'EditCmd'})
endif endif
endif endif
endif endif