Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] git range-diff syntax support #89

Open
KSR-Yasuda opened this issue Jul 12, 2023 · 3 comments
Open

[Feature Request] git range-diff syntax support #89

KSR-Yasuda opened this issue Jul 12, 2023 · 3 comments

Comments

@KSR-Yasuda
Copy link

KSR-Yasuda commented Jul 12, 2023

Could you support syntax highlighting for git range-diff output?

It's useful in rebasing...


Sample output of the command is below:

% git range-diff base..tmp base..master
1:  b92fe39 = 1:  b92fe39 a
2:  4a49bd2 = 2:  4a49bd2 b
3:  cd2481d = 3:  cd2481d c
4:  b8f778d ! 4:  b804dd4 e
    @@ Metadata
     Author: xxxxxxxx <[email protected]>
     
      ## Commit message ##
    -    e
    +    e~
     
      ## test.txt ##
     @@
      a
      b
      c
    -+e
     +e
      c
      b
% git log --graph --all -p
* commit b804dd468388efe6d08c2f4230c5cd3ceda3f00c (HEAD -> master)
| Author: xxxxxxxx <[email protected]>
| Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
| 
|     e~
| 
| diff --git a/test.txt b/test.txt
| index 2d4ca2c..3a3e0e5 100644
| --- a/test.txt
| +++ b/test.txt
| @@ -1,6 +1,7 @@
|  a
|  b
|  c
| +e
|  c
|  b
|  a
|   
| * commit b8f778db7a4794d4fe429a2adc77442990087dad (tmp)
|/  Author: xxxxxxxx <[email protected]>
|   Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
|   
|       e
|   
|   diff --git a/test.txt b/test.txt
|   index 2d4ca2c..2f04e0a 100644
|   --- a/test.txt
|   +++ b/test.txt
|   @@ -1,6 +1,8 @@
|    a
|    b
|    c
|   +e
|   +e
|    c
|    b
|    a
| 
* commit cd2481dd1d9e9ad0dcd649cdd48d61de9c1d1810
| Author: xxxxxxxx <[email protected]>
| Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
| 
|     c
| 
| diff --git a/test.txt b/test.txt
| index f7a2241..2d4ca2c 100644
| --- a/test.txt
| +++ b/test.txt
| @@ -1,4 +1,6 @@
|  a
|  b
| +c
| +c
|  b
|  a
| 
* commit 4a49bd20b6bedcaa74d4b3121a578f48baaba1d7
| Author: xxxxxxxx <[email protected]>
| Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
| 
|     b
| 
| diff --git a/test.txt b/test.txt
| index 7e8a165..f7a2241 100644
| --- a/test.txt
| +++ b/test.txt
| @@ -1,2 +1,4 @@
|  a
| +b
| +b
|  a
| 
* commit b92fe39785c92aa52a471f822ff77e9302314279
| Author: xxxxxxxx <[email protected]>
| Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
| 
|     a
| 
| diff --git a/test.txt b/test.txt
| new file mode 100644
| index 0000000..7e8a165
| --- /dev/null
| +++ b/test.txt
| @@ -0,0 +1,2 @@
| +a
| +a
| 
* commit b8fbc899b2aa83bba97dccaf2b4dc1a89dffef19 (base)
  Author: xxxxxxxx <[email protected]>
  Date:   xxx xxx xx xx:xx:xx xxxx +xxxx
  
      Initial commit

Now I sets some :match highlighting by self as below.

I was wondering if such highlighting was supported natively...

let g:match_rangeDiff  = []

let g:match_rangeDiff += [ { 'group' : 'DiffAdd'     , 'pattern' : '^    \zs+.*'      , 'priority' : 10 , 'id' : 100 } ]
let g:match_rangeDiff += [ { 'group' : 'DiffDelete'  , 'pattern' : '^    \zs-.*'      , 'priority' : 10 , 'id' : 101 } ]
let g:match_rangeDiff += [ { 'group' : 'diffAdded'   , 'pattern' : '^    [ +-]\zs+.*' , 'priority' : 10 , 'id' : 102 } ]
let g:match_rangeDiff += [ { 'group' : 'diffRemoved' , 'pattern' : '^    [ +-]\zs-.*' , 'priority' : 10 , 'id' : 103 } ]

call setmatches(g:match_rangeDiff)

G -p range-diff base..tmp base..master

@KSR-Yasuda
Copy link
Author

KSR-Yasuda commented Jul 21, 2023

Like this?

if exists("b:current_syntax")
  finish
endif

syn case match

syn match gitrangediffIDL      '\v^\s*\zs(\d+|-+):' skipwhite                                         nextgroup=gitrangediffCommitL
syn match gitrangediffCommitL  '\v<\x{7,}>|-{7,}'   skipwhite contained contains=gitrangediffIDL      nextgroup=gitrangediffComp
syn match gitrangediffComp     '\m[<=>!]            skipwhite contained contains=gitrangediffCommitL  nextgroup=gitrangediffIDR
syn match gitrangediffIDR      '\v(\d+|-+):'        skipwhite contained contains=gitrangediffComp     nextgroup=gitrangediffCommitR
syn match gitrangediffCommitR  '\v<\x{7,}>|-{7,}'   skipwhite contained contains=gitrangediffIDR      nextgroup=gitrangediffSummary
syn match gitrangediffSummary  '\m.*'                         contained contains=gitrangediffCommitR

hi def link gitrangediffID      Label
hi def link gitrangediffIDL     gitrangediffID
hi def link gitrangediffIDR     gitrangediffID
hi def link gitrangediffCommit  Identifier
hi def link gitrangediffCommitL gitrangediffCommit
hi def link gitrangediffCommitR gitrangediffCommit
hi def link gitrangediffComp    Boolean
hi def link gitrangediffSummary Comment

syn match gitrangediffMainNorm '\m^    \zs \ze .*'
syn match gitrangediffMainNorm '\m^    \zs \ze[+-]'  nextgroup=gitrangediffSubAdd,gitrangediffSubDel
syn match gitrangediffMainAdd  '\m^    \zs+\ze[ +-]' nextgroup=gitrangediffSubAdd,gitrangediffSubDel
syn match gitrangediffMainDel  '\m^    \zs-\ze[ +-]' nextgroup=gitrangediffSubAdd,gitrangediffSubDel
syn match gitrangediffMainAdd  '\m^    \zs+ .*'
syn match gitrangediffMainDel  '\m^    \zs- .*'

syn match gitrangediffSubAdd  '\m+.*' contains=gitrangediffMainNorm,gitrangediffMainAdd,gitrangediffMainDel contained
syn match gitrangediffSubDel  '\m-.*' contains=gitrangediffMainNorm,gitrangediffMainAdd,gitrangediffMainDel contained

hi def link gitrangediffMainAdd DiffAdd
hi def link gitrangediffMainDel DiffDelete
hi def link gitrangediffSubAdd  diffAdded
hi def link gitrangediffSubDel  diffRemoved

let b:current_syntax = "gitrangediff"

syntax_rangediff_sample

Perhaps it should create another syntax besides git,
and let fugitive.vim set the new one.

@KSR-Yasuda
Copy link
Author

And more.

" Highlight the commit hashes in the range-diff head.
let g:match_rangeDiff += [ { 'group' : 'diffAdded'   , 'pattern' : '^\v\s*[0-9-]+:\s+[0-9a-fA-F-]+\s+[!>]\s+\zs[0-9-]+:\s+[0-9a-fA-F-]+\ze\s+' , 'priority' : 10 , 'id' : 104 } ]
let g:match_rangeDiff += [ { 'group' : 'diffRemoved' , 'pattern' : '^\v\s*\zs[0-9-]+:\s+[0-9a-fA-F-]+\ze\s+[!<]\s+[0-9-]+:\s+[0-9a-fA-F-]+\s+' , 'priority' : 10 , 'id' : 105 } ]

" Diff separator
let g:match_rangeDiff += [ { 'group' : 'Label'       , 'pattern' : '^    \([ +-]\?\zs@@ .*\|  \zs## .* ##\)$'                                  , 'priority' : 10 , 'id' : 106 } ]

@zivarah
Copy link

zivarah commented Nov 4, 2024

I would be very interested in seeing syntax highlighting in range-diff output as well.

I currently have a custom mapping that runs a range-diff in an embedded terminal to get that, but I lose the ability to open the commits via fugitive's standard <CR>/gO/O mappings that way. Those mappings are especially useful given that range-diff doesn't actually show the diff for added/removed commits. I think that having syntax highlighting for the range-diff output would really enhance the workflow substantially in all the same ways that the handling of :G log does.

@tpope do you have any concerns with this request conceptually? It seems non-controversial enough; if you agree I'd be happy to tackle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants