Skip to content

Commit

Permalink
smarter cursor position when refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
jreybert committed Mar 16, 2018
1 parent 621eb2d commit f32316d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
8 changes: 8 additions & 0 deletions autoload/magit/state.vim
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ function! magit#state#get_files(mode) dict
return self.dict[a:mode]
endfunction

" magit#state#get_files_nb: returns the number of files in a given section
" param[in] mode: mode to select, can be 'staged' or 'unstaged'
" return number of files of this section
function! magit#state#get_files_nb(mode) dict
return len(self.dict[a:mode])
endfunction

" magit#state#get_files: global dict file objects (copy) getter function
" param[in] mode: mode to select, can be 'staged' or 'unstaged'
" return ordered list of file objects belonging to mode
Expand Down Expand Up @@ -366,6 +373,7 @@ let magit#state#state = {
\ 'nb_diff_lines': 0,
\ 'get_file': function("magit#state#get_file"),
\ 'get_files': function("magit#state#get_files"),
\ 'get_files_nb': function("magit#state#get_files_nb"),
\ 'get_files_ordered': function("magit#state#get_files_ordered"),
\ 'get_filenames': function("magit#state#get_filenames"),
\ 'add_file': function("magit#state#add_file"),
Expand Down
58 changes: 35 additions & 23 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -654,31 +654,43 @@ function! magit#update_buffer(...)
endif

if ( a:0 >= 3 )
" if, in this order, current file, next file, previous file exists in
" current section, move cursor to it
let cur_file = 1
for fname in [cur_filename, next_filename, prev_filename]
try
let file = b:state.get_file(cur_section, fname)
if ( cur_file )
let hunk_id = max([0, min([len(file.get_hunks())-1, cur_hunk_id])])
let cur_file = 0
else
let hunk_id = 0
endif
if (b:state.get_files_nb(cur_section) > 0)
" if, in this order, current file, next file, previous file exists in
" current section, move cursor to it
let cur_file = 1
for fname in [cur_filename, next_filename, prev_filename]
try
let file = b:state.get_file(cur_section, fname)
if ( cur_file )
let hunk_id = max([0, min([len(file.get_hunks())-1, cur_hunk_id])])
let cur_file = 0
else
let hunk_id = 0
endif

if ( file.is_visible() )
call cursor(file.get_hunks()[hunk_id].line_pos, 0)
if ( g:magit_auto_foldopen )
foldopen
if ( file.is_visible() )
call cursor(file.get_hunks()[hunk_id].line_pos, 0)
if ( g:magit_auto_foldopen )
foldopen
endif
else
call cursor(file.line_pos, 0)
endif
else
call cursor(file.line_pos, 0)
endif
break
catch 'file_doesnt_exists'
endtry
endfor
break
catch 'file_doesnt_exists'
endtry
endfor
else
" if current section is empty, move cursor to top to other section
if (cur_section == 'staged')
let cur_section = 'unstaged'
elseif (cur_section == 'unstaged')
let cur_section = 'staged'
endif
let section_line=search(g:magit_sections[cur_section], "bnw")
call cursor(section_line, 0)
endif
silent execute "normal! zt"

This comment has been minimized.

Copy link
@rafi

rafi Aug 10, 2019

@jreybert can normal! zt be optional? In my opinion it's very irritating

This comment has been minimized.

Copy link
@jreybert

jreybert Aug 22, 2019

Author Owner

Without zt, with successive stages, the cursor ends in the bottom of the window, and the next hunk is hard to read (you need to move the cursor).

Instead of making it optional, maybe I could add some sort of hook at the end of refresh, default at zt. Then, you could overwrite this default to nothing, or whatever suits your preference.

This comment has been minimized.

Copy link
@rafi

rafi Aug 22, 2019

The hook sounds good @jreybert

This comment has been minimized.

Copy link
@masaeedu

masaeedu Feb 11, 2021

@jreybert Was there a hook like this added at some point? My scroll buffer jumps around every time I stage a line using L and it's very hard to keep track of what I was looking at.

This comment has been minimized.

Copy link
@mckellygit

mckellygit Feb 11, 2021

Hi,
In update_buffer(...) I commented out the zt which seemed to stop it for me -

			call cursor(section_line, 0)
		endif
		" mck - skip scroll for now
		"silent execute "normal! zt"
	endif

This comment has been minimized.

Copy link
@mckellygit

mckellygit Feb 11, 2021

Perhaps we can mark the line and return to it or something ? I can look.

endif

if exists(':AirlineRefresh')
Expand Down

0 comments on commit f32316d

Please sign in to comment.