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 'start' and
'end' dictionaries to metadata passed to upstram providers containing
fields:

* 'cursor': ' ', '-' or '+' when position 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)
* 'oldline': line number for the '---' side
* 'newline': line number for 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 23, 2019
1 parent f04a227 commit 7c481ab
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions autoload/fugitive.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5317,6 +5317,12 @@ function! s:BrowseCommand(line1, line2, range, count, bang, mods, reg, arg, args
\ 'line1': line1,
\ 'line2': line2}


if type ==# 'commit' && a:count
let opts.start = s:DiffPosition(line1)
let opts.end = s:DiffPosition(line2)
endif

let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
Expand Down Expand Up @@ -5352,6 +5358,26 @@ function! s:BrowseCommand(line1, line2, range, count, bang, mods, reg, arg, args
endtry
endfunction

function! s:DiffPosition(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
let pattern = ' \(\/dev\/null\|[abciow12]\/\)\zs.*'
return {
\ '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(a:line)[0]}
catch /^Vim\%((\a\+)\)\=:E734/
endtry
endfunction

" Section: Go to file

nnoremap <SID>: :<C-U><C-R>=v:count ? v:count : ''<CR>
Expand Down

0 comments on commit 7c481ab

Please sign in to comment.