Skip to content

Commit

Permalink
Extend :Gbrowse to support line anchors for commits
Browse files Browse the repository at this point in the history
If the first line of :Gbrowse range is inside commit's diff hunk, add
'diff' field to provider metadata with dictionary containing:

  * 'cursor': ' '/'-'/'+' when cursor is on the
  context/removed/added line
  * 'oldpath': path of the '---' side of the diff hunk (empty for
  additions)
  * 'newpath': path of the '+++' side of the diff hunk (empty for
  removals)
  * 'oldline': line number for the cursor position in the '---' side
  * 'newline': line number for the cursor position in the '+++' side

When cursor is on the added or removed line, opposite side line number
is taken as for the *next* context line (even if it doesn't exist).
Exception is completely missing paths (additions or removals) -
corresponding line number is always 0. This is the same behavior as in
the diff hunk header.
  • Loading branch information
odnoletkov committed Feb 10, 2019
1 parent 8899468 commit 171bd8a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions autoload/fugitive.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3967,6 +3967,25 @@ function! s:Browse(bang,line1,count,...) abort
\ 'line1': line1,
\ 'line2': line2}

if type ==# 'commit' && a:count
let lnum = line('.') - 1
let offsets = {' ':0,'+':0,'-':0}
try
while getline(lnum) !~# '^@@ -\d\+\%(,\d\+\)\= +\d\+'
let offsets[getline(lnum)[0]] += 1
let lnum -= 1
endwhile
let pattern = ' \(\/dev\/null\|[abciow12]\/\)\zs.*'
call extend(opts, {'diff': {
\ 'oldpath': matchstr(getline(search('^---'.pattern,'bnW')), '^---'.pattern),
\ 'newpath': matchstr(getline(search('^+++'.pattern,'bnW')), '^+++'.pattern),
\ 'oldline' : offsets['-'] + offsets[' '] + matchstr(getline(lnum), '-\zs\d\+'),
\ 'newline' : offsets['+'] + offsets[' '] + matchstr(getline(lnum), '+\zs\d\+'),
\ 'cursor': getline('.')[0]}})
catch /^Vim\%((\a\+)\)\=:E734/
endtry
endif

let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
Expand Down

0 comments on commit 171bd8a

Please sign in to comment.