Skip to content

Commit

Permalink
Extend :GBrowse to support line anchors for commits
Browse files Browse the repository at this point in the history
In commit buffers :GBrowse with range will include 'diff' dictionary
with 'path', 'line1' and 'line2' values for the range on the '---' side
of the diff. Root 'path', 'line1' and 'line2' values specify range on
the '+++' side. Line number is 0 when position on the corresponding side
of the diff is ambiguous because it is inside deleted hunk.
  • Loading branch information
odnoletkov committed Apr 6, 2021
1 parent f29c9e5 commit b4dafb9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions autoload/fugitive.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6271,6 +6271,21 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
\ 'line1': line1,
\ 'line2': line2}

if type ==# 'commit' && a:count
let pos1 = s:DiffLines(line1)
let pos2 = s:DiffLines(line2)
if !empty(pos1) && !empty(pos2)
let pattern = ' \(\/dev\/null\|[abciow12]\/\)\zs.*'
let opts.path = matchstr(getline(search('^+++'.pattern,'bnW')), '^+++'.pattern)
let opts.line1 = getline(line1)[0] == '-' ? 0 : pos1.new
let opts.line2 = getline(line2)[0] == '-' ? 0 : pos2.new
let opts.diff = {}
let opts.diff.path = matchstr(getline(search('^---'.pattern,'bnW')), '^---'.pattern)
let opts.diff.line1 = getline(line1)[0] == '+' ? 0 : pos1.old
let opts.diff.line2 = getline(line2)[0] == '+' ? 0 : pos2.old
endif
endif

let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
Expand All @@ -6289,6 +6304,23 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
endtry
endfunction

function! s:DiffLines(line) abort
try
let offsets = {' ':0,'+':0,'-':0}
let offsets[getline(a:line)[0]] -= 0 " throw if on hunk header
let lnum = a:line - 1
while getline(lnum) !~# '^@@ -\d\+\%(,\d\+\)\= +\d\+'
let offsets[getline(lnum)[0]] += 1
let lnum -= 1
endwhile
return {
\ 'old': offsets['-'] + offsets[' '] + matchstr(getline(lnum), '-\zs\d\+'),
\ 'new': offsets['+'] + offsets[' '] + matchstr(getline(lnum), '+\zs\d\+'),
\}
catch /^Vim\%((\a\+)\)\=:E734/
endtry
endfunction

" Section: Go to file

let s:ref_header = '\%(Merge\|Rebase\|Upstream\|Pull\|Push\)'
Expand Down

0 comments on commit b4dafb9

Please sign in to comment.