diff --git a/autoload/magit/helper.vim b/autoload/magit/helper.vim index dbb5cff53847f6a22880acf5460c3d0e8c30004a..3908511d24b5d6e34c029c89cc9cd79f4b2301b3 100644 --- a/autoload/magit/helper.vim +++ b/autoload/magit/helper.vim @@ -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 diff --git a/plugin/magit.vim b/plugin/magit.vim index a14b2d8d126604400203da7b5b3410fc49731687..6bfc6816fcdcc66972461dbda3715ffcf2b28fe7 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -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('#') @@ -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 diff --git a/test/jump.vader b/test/jump.vader index 390fb6caa8d0145e6a7081739861af12cc23f300..ba0a122ec210a24e1ce8e235df3865f55fdb4ea0 100644 --- a/test/jump.vader +++ b/test/jump.vader @@ -19,10 +19,10 @@ 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("%") @@ -30,6 +30,18 @@ Execute (Jump to hunk): \'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) @@ -37,11 +49,23 @@ Execute (Jump to hunk): 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' diff --git a/test/test.config b/test/test.config index 3e496eb5a3e673d73013b6525c8110c0c4ddb42e..0878e42a594d8bcbc6d5b1b2cf27246a38127e6a 100644 --- a/test/test.config +++ b/test/test.config @@ -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'