Skip to content

Commit

Permalink
Preserve trailing slash in maktaba#path#Split
Browse files Browse the repository at this point in the history
Fixes #137 and #175.
  • Loading branch information
dbarnett committed Apr 29, 2017
1 parent 6cc1f66 commit 8c93cb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
20 changes: 15 additions & 5 deletions autoload/maktaba/path.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ else
endif
let s:trailing_slash = s:unescaped_slash . '$'
let s:trailing_slashes = s:unescaped_slash . '+$'
let s:nontrailing_slash = s:unescaped_slash . '\ze.'

let s:drive_backslash = '\v^\a:\\\\'
let s:drive_frontslash = '\v^\a://'
Expand Down Expand Up @@ -153,11 +154,21 @@ endfunction


""
" Splits {path} on the system separator character.
" Splits {path} on the system separator character, preserving root and trailing
" slash, if any.
" For example: >
" :echomsg maktaba#path#Split('relative/path')
" :echomsg maktaba#path#Split('/absolute/path')
" :echomsg maktaba#path#Split('path/to/dir/')
" <
" will echo
" - `['relative', 'path']`
" - `['/absolute/path']`
" - `['path', 'to', 'dir/']`
function! maktaba#path#Split(path) abort
" /foo/bar/baz/ splits to root '/' and components ['foo', 'bar', 'baz/'].
let l:root = maktaba#path#RootComponent(a:path)
let l:components = split(a:path[len(l:root):], s:unescaped_slash)
" /foo/bar/baz splits to ['/', 'foo', 'bar', 'baz'].
let l:components = split(a:path[len(l:root):], s:nontrailing_slash, 1)
if !empty(l:root)
call insert(l:components, l:root)
endif
Expand Down Expand Up @@ -277,8 +288,7 @@ function! maktaba#path#MakeDirectory(dir) abort
let l:dir = a:dir
" Vim bug before 7.4 patch 6: mkdir chokes when a path has a trailing slash.
if v:version < 704 || (v:version == 704 && !has('patch6'))
" This is a hackish way to remove a trailing slash.
let l:dir = maktaba#path#Join(maktaba#path#Split(l:dir))
let l:dir = substitute(l:dir, s:trailing_slashes, '', '')
endif

try
Expand Down
12 changes: 11 additions & 1 deletion doc/maktaba.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,17 @@ maktaba#path#Join({components}) *maktaba#path#Join()*
is '/absolute'

maktaba#path#Split({path}) *maktaba#path#Split()*
Splits {path} on the system separator character.
Splits {path} on the system separator character, preserving root and
trailing slash, if any. For example:
>
:echomsg maktaba#path#Split('relative/path')
:echomsg maktaba#path#Split('/absolute/path')
:echomsg maktaba#path#Split('path/to/dir/')
<
will echo
`['relative', 'path']`
`['/absolute/path']`
`['path', 'to', 'dir/']`

maktaba#path#Basename({path}) *maktaba#path#Basename()*
The basename of {path}. Trailing slash matters. Consider:
Expand Down
4 changes: 2 additions & 2 deletions vroom/path.vroom
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ helps you recognize relative paths.
:echomsg string(maktaba#path#Split('relative/path'))
~ ['relative', 'path']

Trailing slashes make no difference, though:
Trailing slashes are preserved as well:

:echomsg string(maktaba#path#Split('relative/path/'))
~ ['relative', 'path']
~ ['relative', 'path/']



Expand Down

0 comments on commit 8c93cb5

Please sign in to comment.