Skip to content

Commit

Permalink
Extend :Gbrowse to support line anchors for commits
Browse files Browse the repository at this point in the history
When current selected lines range is fully contained in a single diff
hunk :Gbrowse will include 'diff' dictionary in metadata passed to
upstream providers containing these fields:

* 'start' and 'end': ' ', '-' or '+' when start/end of selection is on
  the context, removed or 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)
* 'oldstart' and 'oldend': line numbers for selected range in the '---'
  side
* 'newstart' and 'newend': line numbers for selected range in the '+++'
  side

For positions in added or removed lines, 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 numbering logic
as in the diff hunk header.
  • Loading branch information
odnoletkov committed Aug 21, 2019
1 parent 9abe962 commit 02b903f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions autoload/fugitive.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5318,6 +5318,35 @@ function! s:BrowseCommand(line1, line2, range, count, bang, mods, reg, arg, args
\ 'line1': line1,
\ 'line2': line2}

if type ==# 'commit' && a:count
try
let body = {' ':0,'+':0,'-':0}
let head = copy(body)
let body[getline(line2)[0]] -= 0 " throw if on hunk header
let lnum = line2 - 1
while lnum >= line1
let body[getline(lnum)[0]] += 1
let lnum -= 1
endwhile
while getline(lnum) !~# '^@@ -\d\+\%(,\d\+\)\= +\d\+'
let head[getline(lnum)[0]] += 1
let lnum -= 1
endwhile
let pattern = ' \(\/dev\/null\|[abciow12]\/\)\zs.*'
let diff = {}
let diff.oldpath = matchstr(getline(search('^---'.pattern,'bnW')), '^---'.pattern)
let diff.newpath = matchstr(getline(search('^+++'.pattern,'bnW')), '^+++'.pattern)
let diff.oldstart = head['-'] + head[' '] + matchstr(getline(lnum), '-\zs\d\+')
let diff.newstart = head['+'] + head[' '] + matchstr(getline(lnum), '+\zs\d\+')
let diff.oldend = diff.oldstart + body['-'] + body[' ']
let diff.newend = diff.newstart + body['+'] + body[' ']
let diff.start = getline(line1)[0]
let diff.end = getline(line2)[0]
let opts.diff = diff
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 02b903f

Please sign in to comment.