Skip to content

Commit 69eca5e

Browse files
authored
Merge pull request #757 from tsuyoshicho/fix/homeexpand-20200926
fix: System.Filepath.expand_home() fix for completeslash
2 parents 352025d + b6a6d6f commit 69eca5e

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

autoload/vital/__vital__/System/Filepath.vim

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let s:is_cygwin = has('win32unix')
1515
let s:is_mac = !s:is_windows && !s:is_cygwin
1616
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
1717
\ (!isdirectory('/proc') && executable('sw_vers')))
18+
1819
let s:is_case_tolerant = filereadable(expand('<sfile>:r') . '.VIM')
1920

2021
if s:is_windows
@@ -203,8 +204,8 @@ function! s:expand_home(path) abort
203204
endif
204205
let post_home_idx = match(a:path, s:path_sep_pattern)
205206
return post_home_idx is# -1
206-
\ ? s:remove_last_separator(expand(a:path))
207-
\ : s:remove_last_separator(expand(a:path[0 : post_home_idx - 1]))
207+
\ ? s:remove_last_separator(s:expand(a:path))
208+
\ : s:remove_last_separator(s:expand(a:path[0 : post_home_idx - 1]))
208209
\ . a:path[post_home_idx :]
209210
endfunction
210211

@@ -236,7 +237,7 @@ endfunction
236237

237238
if s:is_windows
238239
function! s:realpath(path) abort
239-
if exists('&shellslash') && &shellslash
240+
if exists('+shellslash') && &shellslash
240241
return s:unixpath(a:path)
241242
else
242243
return s:winpath(a:path)
@@ -279,6 +280,23 @@ function! s:contains(path, base) abort
279280
return pathlist[: baselistlen - 1] ==# baselist
280281
endfunction
281282

283+
if exists('+completeslash')
284+
" completeslash bug in Windows and specific version range (Vim 8.1.1769 - Vim 8.2.1746)
285+
function! s:expand(path) abort
286+
let backup_completeslash = &completeslash
287+
try
288+
set completeslash&
289+
return expand(a:path)
290+
finally
291+
let &completeslash = backup_completeslash
292+
endtry
293+
endfunction
294+
else
295+
function! s:expand(path) abort
296+
return expand(a:path)
297+
endfunction
298+
endif
299+
282300
let &cpo = s:save_cpo
283301
unlet s:save_cpo
284302

test/System/Filepath.vimspec

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let s:V = vital#vital#new()
22
let s:is_windows = has('win32')
3+
let s:is_mac = has('mac')
34

45
Describe System.Filepath
56
Before all
@@ -121,6 +122,82 @@ Describe System.Filepath
121122
End
122123
End
123124

125+
Describe .expand_home()
126+
if s:is_windows
127+
It [windows] non home path are no expanded, there are same.
128+
let path = 'C:/Hoge'
129+
let ret = FP.expand_home(path)
130+
Assert Equals(ret, path)
131+
End
132+
else
133+
It [unix] non home path are no expanded, there are same.
134+
let path = '/Hoge'
135+
let ret = FP.expand_home(path)
136+
Assert Equals(ret, path)
137+
End
138+
endif
139+
It expand head ~ home path
140+
if s:is_windows
141+
let path = '~\Hoge'
142+
else
143+
let path = '~/Hoge'
144+
endif
145+
let ret = FP.expand_home(path)
146+
Assert Equals(ret, $HOME . FP.separator() . 'Hoge')
147+
End
148+
It include .. in path no-expand, but head ~ home expanded.
149+
if s:is_windows
150+
let target = '~\Hoge\..\Fuga'
151+
else
152+
let target = '~/Hoge/../Fuga'
153+
endif
154+
let path = '~' . FP.separator() . target
155+
let ret = FP.expand_home(path)
156+
Assert Equals(ret, $HOME . FP.separator() . target)
157+
End
158+
if s:is_windows
159+
" need completeslash test
160+
if exists('&completeslash')
161+
It [windows] completeslash not set home expand test
162+
let backup_completeslash = &completeslash
163+
set completeslash&
164+
let path = '~\Hoge'
165+
let ret = FP.expand_home(path)
166+
Assert Equals(ret, $HOME . FP.separator() . 'Hoge')
167+
let &completeslash = backup_completeslash
168+
End
169+
It [windows] completeslash=slash home expand test
170+
let backup_completeslash = &completeslash
171+
set completeslash=slash
172+
let path = '~\Hoge'
173+
let ret = FP.expand_home(path)
174+
Assert Equals(ret, $HOME . FP.separator() . 'Hoge')
175+
let &completeslash = backup_completeslash
176+
End
177+
It [windows] completeslash=backslash home expand test
178+
let backup_completeslash = &completeslash
179+
set completeslash=backslash
180+
let path = '~\Hoge'
181+
let ret = FP.expand_home(path)
182+
Assert Equals(ret, $HOME . FP.separator() . 'Hoge')
183+
let &completeslash = backup_completeslash
184+
End
185+
endif
186+
else
187+
" need ~user test
188+
It [unix] ~user expand check
189+
let path = '~root/Hoge'
190+
let ret = FP.expand_home(path)
191+
if s:is_mac
192+
let expect = '/var/root/Hoge'
193+
else
194+
let expect = '/root/Hoge'
195+
endif
196+
Assert Equals(ret, expect)
197+
End
198+
endif
199+
End
200+
124201
Describe .winpath()
125202
It should substitute slashes in {path} to backslashes
126203
let path = '/Foo/Bar/Hoge.txt'

0 commit comments

Comments
 (0)