Extract helper to determine position in diff hunk

References: https://github.com/tpope/vim-fugitive/issues/1171
This commit is contained in:
Tim Pope
2022-06-14 18:02:57 -04:00
parent 6ae79e75a4
commit 7dd76231e6

View File

@@ -4315,14 +4315,8 @@ function! s:StageInfo(...) abort
let sigil = matchstr(getline(lnum), '^[ @\+-]') let sigil = matchstr(getline(lnum), '^[ @\+-]')
let offset = -1 let offset = -1
if len(sigil) if len(sigil)
let type = sigil ==# '-' ? '-' : '+' let [lnum, old_lnum, new_lnum] = s:HunkPosition(lnum)
while lnum > 0 && getline(lnum) !~# '^@' let offset = sigil ==# '-' ? old_lnum : new_lnum
if getline(lnum) =~# '^[ '.type.']'
let offset += 1
endif
let lnum -= 1
endwhile
let offset += matchstr(getline(lnum), type.'\zs\d\+')
while getline(lnum) =~# '^[ @\+-]' while getline(lnum) =~# '^[ @\+-]'
let lnum -= 1 let lnum -= 1
endwhile endwhile
@@ -7513,6 +7507,25 @@ function! s:ParseDiffHeader(str) abort
return [fugitive#Unquote(get(list, 1, '')), fugitive#Unquote(get(list, 2, ''))] return [fugitive#Unquote(get(list, 1, '')), fugitive#Unquote(get(list, 2, ''))]
endfunction endfunction
function! s:HunkPosition(lnum) abort
let lnum = a:lnum + get({'@': 1, '\': -1}, getline(a:lnum)[0], 0)
let offsets = {' ': -1, '+': 0, '-': 0}
let sigil = getline(lnum)[0]
let line_char = sigil
while has_key(offsets, line_char)
let offsets[line_char] += 1
let lnum -= 1
let line_char = getline(lnum)[0]
endwhile
let starts = matchlist(getline(lnum), '^@@\+[ 0-9,-]* -\(\d\+\),\d\+ +\(\d\+\),')
if empty(starts)
return [0, 0, 0]
endif
return [lnum,
\ sigil ==# '+' ? 0 : starts[1] + offsets[' '] + offsets['-'],
\ sigil ==# '-' ? 0 : starts[2] + offsets[' '] + offsets['+']]
endfunction
function! s:MapMotion(lhs, rhs) abort function! s:MapMotion(lhs, rhs) abort
let maps = [ let maps = [
\ s:Map('n', a:lhs, ":<C-U>" . a:rhs . "<CR>", "<silent>"), \ s:Map('n', a:lhs, ":<C-U>" . a:rhs . "<CR>", "<silent>"),
@@ -7844,22 +7857,17 @@ function! s:cfile() abort
let dcmds = ['', 'Gdiffsplit! >' . myhash . '^:' . fnameescape(files[0])] let dcmds = ['', 'Gdiffsplit! >' . myhash . '^:' . fnameescape(files[0])]
endif endif
elseif getline('.') =~# '^[+-]\{3\} "\=[abciow12]\=/' elseif getline('.') =~# '^[+-]'
let ref = fugitive#Unquote(getline('.')[4:]) let [header_lnum, old_lnum, new_lnum] = s:HunkPosition(line('.'))
if new_lnum > 0
elseif getline('.') =~# '^[+-]' && search('^@@ -\d\+\%(,\d\+\)\= +\d\+','bnW') let ref = s:ParseDiffHeader(getline(search(s:diff_header_pattern, 'bnW')))[1]
let type = getline('.')[0] let dcmds = [new_lnum, 'normal!zv']
let lnum = line('.') - 1 elseif old_lnum > 0
let offset = 0 let ref = s:ParseDiffHeader(getline(search(s:diff_header_pattern, 'bnW')))[0]
while getline(lnum) !~# '^@@ -\d\+\%(,\d\+\)\= +\d\+' let dcmds = [old_lnum, 'normal!zv']
if getline(lnum) =~# '^[ '.type.']' else
let offset += 1 let ref = fugitive#Unquote(matchstr(getline('.'), '\C[+-]\{3\} \zs"\=[abciow12]/.*'))
endif endif
let lnum -= 1
endwhile
let offset += matchstr(getline(lnum), type.'\zs\d\+')
let ref = fugitive#Unquote(getline(search('^'.type.'\{3\} "\=[abciow12]/','bnW'))[4:-1])
let dcmds = [offset, 'normal!zv']
elseif getline('.') =~# '^rename from ' elseif getline('.') =~# '^rename from '
let ref = 'a/'.getline('.')[12:] let ref = 'a/'.getline('.')[12:]