@@ -57,10 +57,9 @@ function! go#fmt#Format(withGoimport)
57
57
" save cursor position and many other things
58
58
let l: curw= winsaveview ()
59
59
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 )
64
63
65
64
if g: go_fmt_experimental == 1
66
65
" save our undo file to be restored after we are done. This is needed to
@@ -95,11 +94,10 @@ function! go#fmt#Format(withGoimport)
95
94
endif
96
95
97
96
" 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
99
98
100
99
" execute our command...
101
100
let out = system (command . " " . l: tmpname )
102
- let splitted = split (out, ' \n' )
103
101
104
102
if fmt_command != " gofmt"
105
103
let $GOPATH = old_gopath
@@ -113,21 +111,10 @@ function! go#fmt#Format(withGoimport)
113
111
" remove undo point caused via BufWritePre
114
112
try | silent undojoin | catch | endtry
115
113
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
131
118
132
119
" only clear quickfix if it was previously set, this prevents closing
133
120
" other quickfixes
@@ -136,10 +123,8 @@ function! go#fmt#Format(withGoimport)
136
123
call setqflist ([])
137
124
cwindow
138
125
endif
139
-
140
- " put back the users srr setting
141
- let &srr = default_srr
142
126
elseif g: go_fmt_fail_silently == 0
127
+ let splitted = split (out, ' \n' )
143
128
" otherwise get the errors and put them to quickfix window
144
129
let errors = []
145
130
for line in splitted
@@ -160,6 +145,8 @@ function! go#fmt#Format(withGoimport)
160
145
endif
161
146
let s: got_fmt_error = 1
162
147
cwindow
148
+ " We didn't use the temp file, so clean up
149
+ call delete (l: tmpname )
163
150
endif
164
151
165
152
if g: go_fmt_experimental == 1
@@ -169,7 +156,6 @@ function! go#fmt#Format(withGoimport)
169
156
endif
170
157
171
158
" restore our cursor/windows positions
172
- call delete (l: tmpname )
173
159
call winrestview (l: curw )
174
160
endfunction
175
161
0 commit comments