Skip to content

Commit 9984b97

Browse files
derwiathda-x
authored andcommitted
Restore cwd on rustfmt failure before opening lwindow (#316)
If rustfmt fails RunRustFmt helpfully opens the lwindow to show the errors rustfmt encountered. The 'lwindow' command opens the location list window, and makes that the active vim window. Before rustfmt is executed RunRustFmt change CWD to the directory where the file to be formated resides. CWD is restored before exiting RunRustFmt. The problem was that location list window was opened before restoring CWD, which meant that we only restored the directory of the lwindow. The window holding the formatted rust file is left with CWD of that file. The obvious solution is to restore the CWD *before* opening the location list window.
1 parent 2234f0d commit 9984b97

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

autoload/rustfmt.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function! s:RunRustfmt(command, tmpname, fail_silently)
145145

146146
call delete(l:stderr_tmpname)
147147

148+
let l:open_lwindow = 0
148149
if v:shell_error == 0
149150
" remove undo point caused via BufWritePre
150151
try | silent undojoin | catch | endtry
@@ -165,7 +166,7 @@ function! s:RunRustfmt(command, tmpname, fail_silently)
165166
if s:got_fmt_error
166167
let s:got_fmt_error = 0
167168
call setloclist(0, [])
168-
lwindow
169+
let l:open_lwindow = 1
169170
endif
170171
elseif g:rustfmt_fail_silently == 0 && a:fail_silently == 0
171172
" otherwise get the errors and put them in the location list
@@ -197,7 +198,7 @@ function! s:RunRustfmt(command, tmpname, fail_silently)
197198
endif
198199

199200
let s:got_fmt_error = 1
200-
lwindow
201+
let l:open_lwindow = 1
201202
endif
202203

203204
" Restore the current directory if needed
@@ -209,6 +210,11 @@ function! s:RunRustfmt(command, tmpname, fail_silently)
209210
endif
210211
endif
211212

213+
" Open lwindow after we have changed back to the previous directory
214+
if l:open_lwindow == 1
215+
lwindow
216+
endif
217+
212218
silent! loadview
213219
endfunction
214220

0 commit comments

Comments
 (0)