Skip to content

Commit 5f64a57

Browse files
committed
Merge pull request fatih#745 from cespare/mkview-experimental
Put mkview/loadview changes behind go_fmt_experimental
2 parents c264aec + db213d8 commit 5f64a57

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

autoload/go/fmt.vim

+26-9
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ endif
5252
" this and have VimL experience, please look at the function for
5353
" improvements, patches are welcome :)
5454
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.
6072
let l:curw=winsaveview()
61-
endtry
73+
endif
6274

6375
" Write current unsaved buffer to a temp file
6476
let l:tmpname = tempname()
@@ -181,10 +193,15 @@ function! go#fmt#Format(withGoimport)
181193
call delete(tmpundofile)
182194
endif
183195

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
187203
else
204+
" Restore our cursor/windows positions.
188205
call winrestview(l:curw)
189206
endif
190207
endfunction

0 commit comments

Comments
 (0)