Skip to content

Commit 5ce1fb1

Browse files
committed
Merge pull request #496 from fatih/fmt-in-write
Fmt in write
2 parents bb64fa1 + 14830b7 commit 5ce1fb1

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

autoload/go/fmt.vim

+11-25
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ function! go#fmt#Format(withGoimport)
5757
" save cursor position and many other things
5858
let l:curw=winsaveview()
5959

60-
" needed for testing if gofmt fails or not
61-
let l:tmpname=tempname()
62-
call writefile(getline(1,'$'), l:tmpname)
63-
60+
" Write current unsaved buffer to a temp file
61+
let l:tmpname = tempname()
62+
call writefile(getline(1, '$'), l:tmpname)
6463

6564
if g:go_fmt_experimental == 1
6665
" save our undo file to be restored after we are done. This is needed to
@@ -95,11 +94,10 @@ function! go#fmt#Format(withGoimport)
9594
endif
9695

9796
" populate the final command with user based fmt options
98-
let command = fmt_command . ' ' . g:go_fmt_options
97+
let command = fmt_command . ' -w ' . g:go_fmt_options
9998

10099
" execute our command...
101100
let out = system(command . " " . l:tmpname)
102-
let splitted = split(out, '\n')
103101

104102
if fmt_command != "gofmt"
105103
let $GOPATH = old_gopath
@@ -113,21 +111,10 @@ function! go#fmt#Format(withGoimport)
113111
" remove undo point caused via BufWritePre
114112
try | silent undojoin | catch | endtry
115113

116-
" do not include stderr to the buffer, this is due to goimports/gofmt
117-
" tha fails with a zero exit return value (sad yeah).
118-
let default_srr = &srr
119-
set srr=>%s
120-
121-
" delete any leftover before we replace the whole file. Suppose the
122-
" file had 20 lines, but new output has 10 lines, only 1-10 are
123-
" replaced with setline, remaining lines 11-20 won't get touched. So
124-
" remove them.
125-
if line('$') > len(splitted)
126-
execute len(splitted) .',$delete'
127-
endif
128-
129-
" setline iterates over the list and replaces each line
130-
call setline(1, splitted)
114+
" Replace current file with temp file, then reload buffer
115+
call rename(l:tmpname, expand('%'))
116+
silent edit!
117+
let &syntax = &syntax
131118

132119
" only clear quickfix if it was previously set, this prevents closing
133120
" other quickfixes
@@ -136,10 +123,8 @@ function! go#fmt#Format(withGoimport)
136123
call setqflist([])
137124
cwindow
138125
endif
139-
140-
" put back the users srr setting
141-
let &srr = default_srr
142126
elseif g:go_fmt_fail_silently == 0
127+
let splitted = split(out, '\n')
143128
"otherwise get the errors and put them to quickfix window
144129
let errors = []
145130
for line in splitted
@@ -160,6 +145,8 @@ function! go#fmt#Format(withGoimport)
160145
endif
161146
let s:got_fmt_error = 1
162147
cwindow
148+
" We didn't use the temp file, so clean up
149+
call delete(l:tmpname)
163150
endif
164151

165152
if g:go_fmt_experimental == 1
@@ -169,7 +156,6 @@ function! go#fmt#Format(withGoimport)
169156
endif
170157

171158
" restore our cursor/windows positions
172-
call delete(l:tmpname)
173159
call winrestview(l:curw)
174160
endfunction
175161

0 commit comments

Comments
 (0)