Skip to content

Commit

Permalink
Jump to source is now line precise
Browse files Browse the repository at this point in the history
discussed in #152, fixing #153
  • Loading branch information
jreybert committed Nov 23, 2017
1 parent 54a428a commit f09c83b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
10 changes: 5 additions & 5 deletions autoload/magit/helper.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ function! magit#helper#get_filename()
return substitute(getline(search(g:magit_file_re, "cbnW")), g:magit_file_re, '\2', '')
endfunction

" magit#helper#get_hunkheader: helper function to get the current hunk header,
" according to cursor position
" return: hunk header
function! magit#helper#get_hunkheader()
return getline(search(g:magit_hunk_re, "cbnW"))
" magit#helper#get_hunkheader_line_nb: helper function to get the current hunk
" header line number, according to cursor position
" return: hunk header line number
function! magit#helper#get_hunkheader_line_nb()
return search(g:magit_hunk_re, "cbnW")
endfunction

" magit#utils#get_section: helper function to get the current section, according to
Expand Down
13 changes: 9 additions & 4 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1219,10 +1219,15 @@ endfunction
function! magit#jump_to()
let section=magit#helper#get_section()
let filename=fnameescape(magit#git#top_dir() . magit#helper#get_filename())
let line=substitute(magit#helper#get_hunkheader(),
let header_line_nb=magit#helper#get_hunkheader_line_nb()

let line_in_file=substitute(getline(header_line_nb),
\ '^@@ -\d\+,\d\+ +\(\d\+\),\d\+ @@.*$', '\1', "")
let context = magit#git#get_config("diff.context", 3)
let line += context

" header_line_nb+2: +2 because we skip the header and the fist line
let hunk_extract=getline(header_line_nb+2, line('.'))
let line_in_hunk = len(filter(hunk_extract, 'v:val =~ "^[ +]"'))
let line_in_file += line_in_hunk

" winnr('#') is overwritten by magit#get_win()
let last_win = winnr('#')
Expand All @@ -1235,7 +1240,7 @@ function! magit#jump_to()
endif

try
execute "edit " . "+" . line . " " filename
execute "edit " . "+" . line_in_file . " " filename
catch
if ( v:exception == 'Vim:Interrupt' && buf_win == 0)
close
Expand Down
36 changes: 30 additions & 6 deletions test/jump.vader
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,53 @@ Execute (Jump to hunk):
wincmd p " go to previous window, should be magit
Assert( &ft == 'magit' ), 'Did not jump back to magit window'

call Search_pattern("^ \tbook_copy = models.ForeignKey(Book_copy)$")
call Search_pattern("^-\tedition = models.CharField(max_length=200, blank=True)$")
call magit#jump_to()
let buf_nr = winnr()
Assert( winnr('$') == 2 ), 'There should be 2 windows' . winnr('$')
Assert( winnr() == buf_nr ), 'It may have open a existing window'
Assert( expand("%:t") == fnamemodify(expand(Get_filename(0)), ":t")),
\ 'Did not jump in good file, expect ' . Get_filename(0) . ' and got '.
\ expand("%")
Assert( line('.') == Get_filename(2) ), 'Did not jump at good line, '.
\'expect ' .Get_filename(2) . ' and jumped to ' . getline('.')
wincmd p " go to previous window, should be magit
Assert( &ft == 'magit' ), 'Did not jump back to magit window'

call Search_pattern("^ class Borrowing(models.Model):$")
call magit#jump_to()
Assert( winnr('$') == 2 ), 'There should be 2 windows' . winnr('$')
Assert( winnr() == buf_nr ), 'It may have open a existing window'
Assert( expand("%:t") == fnamemodify(expand(Get_filename(0)), ":t")),
\ 'Did not jump in good file, expect ' . Get_filename(0) . ' and got '.
\ expand("%")
Assert( line('.') == Get_filename(3) ), 'Did not jump at good line, '.
\'expect ' .Get_filename(3) . ' and jumped to ' . getline('.')
wincmd p " go to previous window, should be magit
Assert( &ft == 'magit' ), 'Did not jump back to magit window'
call Search_file('unstaged', 3)

call Search_file('unstaged', 3)
call Search_pattern("^ def search_isbn(isbn):$")
call magit#jump_to()
Assert( winnr('$') == 2 ), 'There should be 2 windows' . winnr('$')
Assert( winnr() == buf_nr ), 'It may have open a existing window'
Assert( expand("%:t") == fnamemodify(expand(Get_filename(3)), ":t")),
\ 'Did not jump in good file, expect ' . Get_filename(3) . ' and got '.
Assert( expand("%:t") == fnamemodify(expand(Get_filename(4)), ":t")),
\ 'Did not jump in good file, expect ' . Get_filename(4) . ' and got '.
\ expand("%")
Assert( line('.') == Get_filename(5) ), 'Did not jump at good line, '.
\'expect ' .Get_filename(5) . ' and jumped to ' . getline('.')
wincmd p " go to previous window, should be magit
Assert( &ft == 'magit' ), 'Did not jump back to magit window'

call Search_pattern("-\t\tself.author = xstr(amazon_prod.author)$")
call magit#jump_to()
let buf_nr = winnr()
Assert( winnr('$') == 2 ), 'There should be 2 windows' . winnr('$')
Assert( expand("%:t") == fnamemodify(expand(Get_filename(4)), ":t")),
\ 'Did not jump in good file, expect ' . Get_filename(4) . ' and got '.
\ expand("%")
Assert( line('.') == Get_filename(4) ), 'Did not jump at good line, '.
\'expect ' .Get_filename(4) . ' and jumped to ' . getline('.')
Assert( line('.') == Get_filename(6) ), 'Did not jump at good line, '.
\'expect ' .Get_filename(6) . ' and jumped to ' . getline('.')
wincmd p " go to previous window, should be magit
Assert( &ft == 'magit' ), 'Did not jump back to magit window'

Expand Down
2 changes: 1 addition & 1 deletion test/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare -A test_scripts=(
[renameFile.vader]='manage.py|manage\ with\ spaces.py;djooks/settings\ with\ spaces.py|djooks/settings_without_spaces.py'
[ignoreFile.vader]='bootstrap'
[addDir.vader]='newdir\/'
[jump.vader]='books\/models.py|27|60|books\/isbn_search.py|54'
[jump.vader]='books\/models.py|29|42|57|books\/isbn_search.py|51|59'
[commit.vader]='books/models.py|bootstrap'
[version.vader]='foo'
#[addSubmodule.vader]='subdjooks'
Expand Down

0 comments on commit f09c83b

Please sign in to comment.