Refactoring: introduce s:parse_hunk()

This commit is contained in:
Marco Hinz 2019-08-01 17:23:23 +02:00
parent ac23bd95d5
commit 0f763b514a
No known key found for this signature in database
GPG Key ID: 1C980A1B657B4A4F
2 changed files with 14 additions and 13 deletions

View File

@ -376,11 +376,7 @@ function! s:strip_context(context)
if state == 0 if state == 0
if line =~ "^@@ " if line =~ "^@@ "
let tokens = matchlist(line, '^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)') let [old_line, new_line, old_count, new_count] = sy#sign#parse_hunk(line)
let old_line = str2nr(tokens[1])
let new_line = str2nr(tokens[3])
let old_count = empty(tokens[2]) ? 1 : str2nr(tokens[2])
let new_count = empty(tokens[4]) ? 1 : str2nr(tokens[4])
let hunk = [] let hunk = []
let state = 1 let state = 1
else else

View File

@ -60,13 +60,7 @@ function! sy#sign#process_diff(sy, vcs, diff) abort
let a:sy.lines = [] let a:sy.lines = []
let ids = [] let ids = []
let tokens = matchlist(line, '^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)') let [old_line, new_line, old_count, new_count] = sy#sign#parse_hunk(line)
let old_line = str2nr(tokens[1])
let new_line = str2nr(tokens[3])
let old_count = empty(tokens[2]) ? 1 : str2nr(tokens[2])
let new_count = empty(tokens[4]) ? 1 : str2nr(tokens[4])
" Workaround for non-conventional diff output in older Fossil versions: " Workaround for non-conventional diff output in older Fossil versions:
" https://fossil-scm.org/forum/forumpost/834ce0f1e1 " https://fossil-scm.org/forum/forumpost/834ce0f1e1
@ -227,6 +221,18 @@ function! sy#sign#remove_all_signs(bufnr) abort
let sy.hunks = [] let sy.hunks = []
endfunction endfunction
" Function: #parse_hunk {{{1
" Parse a hunk as '@@ -273,3 +267,14' into [old_line, new_line, old_count, new_count]
function! sy#sign#parse_hunk(diffline) abort
let tokens = matchlist(a:diffline, '^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)')
return [
\ str2nr(tokens[1]),
\ str2nr(tokens[3]),
\ empty(tokens[2]) ? 1 : str2nr(tokens[2]),
\ empty(tokens[4]) ? 1 : str2nr(tokens[4])
\ ]
endfunction
" Function: s:add_sign {{{1 " Function: s:add_sign {{{1
function! s:add_sign(sy, line, type, ...) abort function! s:add_sign(sy, line, type, ...) abort
call add(a:sy.lines, a:line) call add(a:sy.lines, a:line)
@ -272,4 +278,3 @@ function! s:external_sign_present(sy, line) abort
return 1 return 1
endif endif
endfunction endfunction