Skip to content

Commit

Permalink
Merge pull request #586 from vim-jp/filepath-slash
Browse files Browse the repository at this point in the history
System.Filepath: Add to_slash() and from_slash()
  • Loading branch information
ujihisa authored Jun 11, 2018
2 parents b471130 + 7e54598 commit cf11958
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions autoload/vital/__vital__/System/Filepath.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ let s:is_mac = !s:is_windows && !s:is_cygwin
\ (!isdirectory('/proc') && executable('sw_vers')))
let s:is_case_tolerant = filereadable(expand('<sfile>:r') . '.VIM')

if s:is_windows
function! s:to_slash(path) abort
return tr(a:path, '\', '/')
endfunction
else
function! s:to_slash(path) abort
return a:path
endfunction
endif

if s:is_windows
function! s:from_slash(path) abort
return tr(a:path, '/', '\')
endfunction
else
function! s:from_slash(path) abort
return a:path
endfunction
endif


" Get the directory separator.
function! s:separator() abort
return fnamemodify('.', ':p')[-1 :]
Expand Down
23 changes: 23 additions & 0 deletions doc/vital/System/Filepath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ INTERFACE *Vital.System.Filepath-interface*
------------------------------------------------------------------------------
FUNCTIONS *Vital.System.Filepath-functions*

to_slash({path}) *Vital.System.Filepath.to_slash()*
Use when you quote path string for scripting languages, without
worrying about the runtime operating system is if windows or not.

e.g.
>
let code = printf(
\ '(neoclojure-init "%s")',
\ shellescape(s:F.to_slash(path)))
..
let path = s:F.from_slash(
\ s:get_path_like_string_from_something())
<

This function intentionally ignores 'shellslash' option, so that you
can simply use this function for path string not only from Vim but
also from others, such as environment vars, problem output, or really
anything.
See also: |Vital.System.Filepath.from_slash()|

from_slash({path}) *Vital.System.Filepath.from_slash()*
This is opposite to |Vital.System.Filepath.to_slash()|. See its doc.

separator() *Vital.System.Filepath.separator()*
Return OS related directory separator as string.
This returns "/" on non-Windows, or Windows and |'shellslash'| is on.
Expand Down
21 changes: 21 additions & 0 deletions test/System/Filepath.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ Describe System.Filepath
let is_case_sensitive_file_system = !(isdirectory(tolower(g:root)) && isdirectory(toupper(g:root)))
End

Describe .to_slash()
It converts / to \ only when it's necessary
Assert Equals('a/b', FP.to_slash('a/b'))
if has('win32') || has('win64')
Assert Equals('a/b', FP.to_slash('a\b'))
else
Assert Equals('a\b', FP.to_slash('a\b'))
endif
End
End

Describe .from_slash()
It converts \ to / only when it's necessary
if has('win32') || has('win64')
Assert Equals('a\b', FP.from_slash('a/b'))
else
Assert Equals('a/b', FP.from_slash('a/b'))
endif
End
End

Describe .is_absolute()
if !s:is_windows
It returns TRUE if {path} is an absolute path
Expand Down

0 comments on commit cf11958

Please sign in to comment.