@@ -52,13 +52,25 @@ endif
52
52
" this and have VimL experience, please look at the function for
53
53
" improvements, patches are welcome :)
54
54
function ! go#fmt#Format (withGoimport)
55
- " save cursor position, folds and many other things
56
- let l: curw = {}
57
- try
58
- mkview !
59
- catch
55
+ if g: go_fmt_experimental == 1
56
+ " Using winsaveview to save/restore cursor state has the problem of
57
+ " closing folds on save:
58
+ " https://github.com/fatih/vim-go/issues/502
59
+ " One fix is to use mkview instead. Unfortunately, this sometimes causes
60
+ " other bad side effects:
61
+ " https://github.com/fatih/vim-go/issues/728
62
+ " and still closes all folds if foldlevel>0:
63
+ " https://github.com/fatih/vim-go/issues/732
64
+ let l: curw = {}
65
+ try
66
+ mkview !
67
+ catch
68
+ let l: curw= winsaveview ()
69
+ endtry
70
+ else
71
+ " Save cursor position and many other things.
60
72
let l: curw= winsaveview ()
61
- endtry
73
+ endif
62
74
63
75
" Write current unsaved buffer to a temp file
64
76
let l: tmpname = tempname ()
@@ -181,10 +193,15 @@ function! go#fmt#Format(withGoimport)
181
193
call delete (tmpundofile)
182
194
endif
183
195
184
- " restore our cursor/windows positions, folds, etc..
185
- if empty (l: curw )
186
- silent ! loadview
196
+ if g: go_fmt_experimental == 1
197
+ " Restore our cursor/windows positions, folds, etc.
198
+ if empty (l: curw )
199
+ silent ! loadview
200
+ else
201
+ call winrestview (l: curw )
202
+ endif
187
203
else
204
+ " Restore our cursor/windows positions.
188
205
call winrestview (l: curw )
189
206
endif
190
207
endfunction
0 commit comments