dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
23.01k stars 382 forks source link

🐛 Syntax highlighting is sometimes wrong #316

Closed kofls closed 4 years ago

kofls commented 4 years ago

Syntax highlighting seems to be considering the diff chunk independent to the rest of the file and sometimes results in highlighting like: image

Here, the function is highlighted as a comment instead of the two surrounding comments.

Example code:

"""
Some comment here
"""
def function1():
    pass

"""
Some other comment here
"""
def function2():
    pass

git --no-pager diff:

diff --git a/main.py b/main.py
index 2f09b8b..e90a142 100644
--- a/main.py
+++ b/main.py
@@ -2,7 +2,7 @@
 Some comment here
 """
 def function1():
-    pass
+    #pass

 """
 Some other comment here

delta --version: delta 0.4.1 delta --show-config:

    commit-style                  = raw
    file-style                    = blue
    hunk-header-style             = syntax
    minus-style                   = normal "#3f0001"
    minus-non-emph-style          = normal "#3f0001"
    minus-emph-style              = normal "#901011"
    minus-empty-line-marker-style = normal "#3f0001"
    zero-style                    = syntax
    plus-style                    = syntax "#002800"
    plus-non-emph-style           = syntax "#002800"
    plus-emph-style               = syntax "#006000"
    plus-empty-line-marker-style  = normal "#002800"
    whitespace-error-style        = reverse purple
    24-bit-color                  = true
    file-added-label              = 'added:'
    file-modified-label           = ''
    file-removed-label            = 'removed:'
    file-renamed-label            = 'renamed:'
    hyperlinks                    = false
    inspect-raw-lines             = true
    keep-plus-minus-markers       = false
    line-numbers                  = true
    line-numbers-minus-style      = 88
    line-numbers-zero-style       = "#444444"
    line-numbers-plus-style       = 28
    line-numbers-left-style       = blue
    line-numbers-right-style      = blue
    line-numbers-left-format      = '{nm:^4}⋮'
    line-numbers-right-format     = '{np:^4}│'
    max-line-distance             = 0.6
    navigate                      = false
    paging                        = auto
    side-by-side                  = false
    syntax-theme                  = Monokai Extended
    width                         = 254
    tabs                          = 4
    word-diff-regex               = '\w+'
dandavison commented 4 years ago

Hi @kofls, thanks for this. You're right, this is definitely a problem. This is actually a duplicate of #117 (but it didn't have a very helpful title so not surprising you didn't find it; I just changed the title). So -- the question is -- do you have any ideas about how we should address it?

kofls commented 4 years ago

Hi @dandavison, Sorry for the duplicate. I looked through the open issues but not well enough I guess. You can close this issue then.

I think a simple solution would be to get the syntax style per file and use that for each diff section. Not sure how expensive that would be but no doubt it would be slower than the current implementation. If it is significantly slower, maybe this could be enabled by a flag and the default behavior be left as is.

There probably is a smarter way to do this by only calculating the syntax style for a part of the file. For the python example, getting the syntax style for the above and below classes from the diff line should be enough to correctly highlight the diff. But in general, I have no idea how this subsection can be identified.

dandavison commented 4 years ago

Thanks! I replied to you in https://github.com/dandavison/delta/issues/117#issuecomment-687281613 so as to keep the conversation in one place.