Skip to content

Commit b89b1aa

Browse files
authored
fix(zprint): don't cd unless dir exists (#139)
This avoids the syscall failing if the directory doesn't exist yet. It's an unlikely scenario, but I managed to hit it, and it's annoying when tooling breaks in unlikely scenarios...
1 parent 0609431 commit b89b1aa

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

autoload/codefmt/zprint.vim

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ function! codefmt#zprint#GetFormatter() abort
5757
let l:opts = codefmt#formatterhelpers#ResolveFlagToArray(
5858
\ 'zprint_options')
5959

60-
" Prepare the syscall, changing to the containing directory in case the user
61-
" has configured {:search-config? true} in ~/.zprintrc
62-
let l:cmd = maktaba#syscall#Create(l:exe + l:opts).WithCwd(expand('%:p:h'))
60+
" Prepare the syscall
61+
let l:syscall = maktaba#syscall#Create(l:exe + l:opts)
62+
if isdirectory(expand('%:p:h'))
63+
" Change to the containing directory in case the user has configured
64+
" {:search-config? true} in ~/.zprintrc
65+
let l:syscall = l:syscall.WithCwd(expand('%:p:h'))
66+
endif
6367

6468
" zprint does not support range formatting yet:
6569
" https://github.com/kkinnear/zprint/issues/122
@@ -68,7 +72,7 @@ function! codefmt#zprint#GetFormatter() abort
6872
call codefmt#formatterhelpers#AttemptFakeRangeFormatting(
6973
\ a:startline,
7074
\ a:endline,
71-
\ l:cmd)
75+
\ l:syscall)
7276
endfunction
7377

7478
return l:formatter

vroom/zprint.vroom

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ Zprint is the default formatter for the clojure file type, so calling
8484
! cd .* zprint .*
8585
:set filetype&
8686

87+
If the directory containing the file doesn't exist yet, then the plugin won't
88+
try to change directories:
89+
90+
:silent file /does-not-exist/x.clj
91+
:set filetype=clojure
92+
:FormatCode
93+
! zprint .*
94+
:set filetype&
95+
:0file
96+
8797
The default setting of zprint_options propagates Vim's textwidth setting to
8898
zprint's command-line.
8999

0 commit comments

Comments
 (0)