From e12c71d61c2c69a08029ada9729d6e6a1c2cf6c4 Mon Sep 17 00:00:00 2001 From: Grzegorz Rozdzialik Date: Mon, 22 Mar 2021 22:11:22 +0100 Subject: [PATCH 1/3] Show refs in log in Gstatus Display refs (the output of %d from git log's format=pretty) next to commit summaries in Gstatus. Use basic syntax highlighting for all refs. Use a special marker and concealing to avoid false-positives when commit subjects begin with parentheses. Obvious refs (e.g. "HEAD", "origin/master" when showing unpulled changes from "origin/master") are hidden. --- autoload/fugitive.vim | 39 +++++++++++++++++++++++++++++++-------- syntax/fugitive.vim | 5 ++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index c577bd32f8..a1e6e80bae 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -1766,15 +1766,38 @@ function! s:ReplaceCmd(cmd) abort endif endfunction -function! s:QueryLog(refspec) abort - let lines = s:LinesError(['log', '-n', '256', '--pretty=format:%h%x09%s', a:refspec, '--'])[0] +function! s:IgnoreRefs(refs, refsToIgnore) abort + function! ShouldPreserveRef(refsToIgnore, index, ref) abort + return index(a:refsToIgnore, a:ref) == -1 + endfunction + + let refs = split(a:refs[1:-2], ", ") + let refs = filter(refs, function('ShouldPreserveRef', [a:refsToIgnore])) + + if len(a:refs) == 0 + return '' + endif + + return '(' . join(refs, ", ") . ')' +endfunction + +function! s:QueryLog(refspec, refToIgnore) abort + let lines = s:LinesError(['log', '-n', '256', '--pretty=format:%h%x09%d%x09%s', a:refspec, '--'])[0] call map(lines, 'split(v:val, "\t", 1)') - call map(lines, '{"type": "Log", "commit": v:val[0], "subject": join(v:val[1 : -1], "\t")}') + let refsToIgnore = [a:refToIgnore, 'HEAD'] + + function! HandleRefs(refsToIgnore, refs) abort + return s:IgnoreRefs(trim(a:refs), a:refsToIgnore) + endfunction + + call map(lines, '{"type": "Log", "commit": v:val[0], "refs": ' . string(function('HandleRefs', [refsToIgnore])) . '(v:val[1]), "subject": join(v:val[2 : -1], "\t")}') return lines endfunction function! s:FormatLog(dict) abort - return a:dict.commit . ' ' . a:dict.subject + setlocal conceallevel=3 + setlocal concealcursor=nc + return a:dict.commit . ' ' . ((len(a:dict.refs) > 0) ? ("\x1f" . a:dict.refs . ' ') : '') . a:dict.subject endfunction function! s:FormatRebase(dict) abort @@ -2062,16 +2085,16 @@ function! fugitive#BufReadStatus() abort let staged_end = len(staged) ? line('$') : 0 if len(pull) && get(props, 'branch.ab') !~# ' -0$' - call s:AddSection('Unpulled from ' . pull, s:QueryLog(head . '..' . pull)) + call s:AddSection('Unpulled from ' . pull, s:QueryLog(head . '..' . pull, pull)) endif if len(push) && push !=# pull - call s:AddSection('Unpulled from ' . push, s:QueryLog(head . '..' . push)) + call s:AddSection('Unpulled from ' . push, s:QueryLog(head . '..' . push, push)) endif if len(pull) && push !=# pull - call s:AddSection('Unpushed to ' . pull, s:QueryLog(pull . '..' . head)) + call s:AddSection('Unpushed to ' . pull, s:QueryLog(pull . '..' . head, pull)) endif if len(push) && !(push ==# pull && get(props, 'branch.ab') =~# '^+0 ') - call s:AddSection('Unpushed to ' . push, s:QueryLog(push . '..' . head)) + call s:AddSection('Unpushed to ' . push, s:QueryLog(push . '..' . head, push)) endif setlocal nomodified readonly noswapfile diff --git a/syntax/fugitive.vim b/syntax/fugitive.vim index e84eba4e76..7425e855b3 100644 --- a/syntax/fugitive.vim +++ b/syntax/fugitive.vim @@ -23,8 +23,10 @@ syn match fugitiveDone /^done\>/ contained containedin=@fugitiveSection nextgrou syn match fugitiveStop /^stop\>/ contained containedin=@fugitiveSection nextgroup=fugitiveHash skipwhite syn match fugitiveModifier /^[MADRCU?]\{1,2} / contained containedin=@fugitiveSection syn match fugitiveSymbolicRef /\.\@!\%(\.\.\@!\|[^[:space:][:cntrl:]\:.]\)\+\.\@ Date: Thu, 25 Mar 2021 08:22:35 +0100 Subject: [PATCH 2/3] Prevent false-positive ref syntax highlighting in Gstatus Subject lines that begin with an open parenthesis and have no other refs attached have their first parenthesised text syntax highlighted. Use concealed artificial parentheses in such cases. --- autoload/fugitive.vim | 26 +++++++++++++++++++++----- syntax/fugitive.vim | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index a1e6e80bae..ad6ed7e97b 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -1771,6 +1771,7 @@ function! s:IgnoreRefs(refs, refsToIgnore) abort return index(a:refsToIgnore, a:ref) == -1 endfunction + " Strip the outer parentheses let refs = split(a:refs[1:-2], ", ") let refs = filter(refs, function('ShouldPreserveRef', [a:refsToIgnore])) @@ -1778,7 +1779,7 @@ function! s:IgnoreRefs(refs, refsToIgnore) abort return '' endif - return '(' . join(refs, ", ") . ')' + return '(' . join(refs, ", ") . ') ' endfunction function! s:QueryLog(refspec, refToIgnore) abort @@ -1786,18 +1787,33 @@ function! s:QueryLog(refspec, refToIgnore) abort call map(lines, 'split(v:val, "\t", 1)') let refsToIgnore = [a:refToIgnore, 'HEAD'] - function! HandleRefs(refsToIgnore, refs) abort - return s:IgnoreRefs(trim(a:refs), a:refsToIgnore) + function! HandleRefs(refsToIgnore, subject, refs) abort + let refs = trim(a:refs) + let refs = s:IgnoreRefs(refs, a:refsToIgnore) + + if len(refs) == 0 && a:subject[0] == '(' + " Add artificial (concealed) empty parentheses for refs. Helps avoid + " syntax highlighting false-positives (subject lines beginning with an + " open parenthesis). + return '()' + endif + + return refs + endfunction + + function! CreateLineDict(refsToIgnore, index, parts) abort + let subject = join(a:parts[2 : -1], "\t") + return {"type": "Log", "commit": a:parts[0], "refs": HandleRefs(a:refsToIgnore, subject, a:parts[1]), "subject": subject} endfunction - call map(lines, '{"type": "Log", "commit": v:val[0], "refs": ' . string(function('HandleRefs', [refsToIgnore])) . '(v:val[1]), "subject": join(v:val[2 : -1], "\t")}') + call map(lines, function('CreateLineDict', [refsToIgnore])) return lines endfunction function! s:FormatLog(dict) abort setlocal conceallevel=3 setlocal concealcursor=nc - return a:dict.commit . ' ' . ((len(a:dict.refs) > 0) ? ("\x1f" . a:dict.refs . ' ') : '') . a:dict.subject + return a:dict.commit . ' ' . ((len(a:dict.refs) > 0) ? a:dict.refs : '') . a:dict.subject endfunction function! s:FormatRebase(dict) abort diff --git a/syntax/fugitive.vim b/syntax/fugitive.vim index 7425e855b3..49e79d64fb 100644 --- a/syntax/fugitive.vim +++ b/syntax/fugitive.vim @@ -23,9 +23,9 @@ syn match fugitiveDone /^done\>/ contained containedin=@fugitiveSection nextgrou syn match fugitiveStop /^stop\>/ contained containedin=@fugitiveSection nextgroup=fugitiveHash skipwhite syn match fugitiveModifier /^[MADRCU?]\{1,2} / contained containedin=@fugitiveSection syn match fugitiveSymbolicRef /\.\@!\%(\.\.\@!\|[^[:space:][:cntrl:]\:.]\)\+\.\@ Date: Thu, 25 Mar 2021 18:16:03 +0100 Subject: [PATCH 3/3] Highlight only individual refs in Gstatus Stop highlighting comma separators and arrows in the list of refs shown in Gstatus. --- syntax/fugitive.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/syntax/fugitive.vim b/syntax/fugitive.vim index 49e79d64fb..43cf5c83cd 100644 --- a/syntax/fugitive.vim +++ b/syntax/fugitive.vim @@ -26,7 +26,8 @@ syn match fugitiveSymbolicRef /\.\@!\%(\.\.\@!\|[^[:space:][:cntrl:]\:.]\)\+\.\@ syn match fugitiveHash /^\x\{4,\}\S\@!/ contained containedin=@fugitiveSection nextgroup=fugitiveRefs skipwhite syn match fugitiveHash /\S\@/ contained syn region fugitiveHunk start=/^\%(@@\+ -\)\@=/ end=/^\%([A-Za-z?@]\|$\)\@=/ contains=@fugitiveDiff containedin=@fugitiveSection fold @@ -53,7 +54,7 @@ hi def link fugitiveStagedModifier Typedef hi def link fugitiveInstruction Type hi def link fugitiveStop Function hi def link fugitiveHash Identifier -hi def link fugitiveRefs Function +hi def link fugitiveRef Function hi def link fugitiveSymbolicRef Function hi def link fugitiveCount Number