Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Filepath: Add to_slash() and from_slash() #586

Merged
merged 5 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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