Skip to content

Commit 8384920

Browse files
committed
fmt.vim: fix cursor staying on the same line when goimports is used
fixes #1342
1 parent ca1d712 commit 8384920

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

autoload/go/fmt.vim

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function! go#fmt#Format(withGoimport) abort
7070
endif
7171

7272
let out = go#fmt#run(bin_name, l:tmpname, expand('%'))
73+
let diff_offset = len(readfile(l:tmpname)) - line('$')
74+
7375
if go#util#ShellError() == 0
7476
call go#fmt#update_file(l:tmpname, expand('%'))
7577
elseif g:go_fmt_fail_silently == 0
@@ -95,6 +97,9 @@ function! go#fmt#Format(withGoimport) abort
9597
" Restore our cursor/windows positions.
9698
call winrestview(l:curw)
9799
endif
100+
101+
" be smart and jump to the line the new statement was added/removed
102+
call cursor(line('.') + diff_offset, 0)
98103
endfunction
99104

100105
" update_file updates the target file with the given formatted source

0 commit comments

Comments
 (0)