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

Diff syntax highlight doesn't handle all cases #1022

Closed
danilaml opened this issue Oct 29, 2018 · 2 comments
Closed

Diff syntax highlight doesn't handle all cases #1022

danilaml opened this issue Oct 29, 2018 · 2 comments

Comments

@danilaml
Copy link
Contributor

For example this diff is not syntax highlighted (but GitHub handles it just fine):

diff a.txt b.txt 
2d1
< 2
4,5c3
< 4
< 5
---
> 2
@vidarh
Copy link
Contributor

vidarh commented Jan 17, 2019

Thanks. In fact the current rouge diff lexer only handles unified diffs (-u flag). This is the default with git as well, which I presume is why it's not been fixed.

This will fix it; I'll prepare a pull-request as well:

diff --git adiff --git a/lib/rouge/lexers/diff.rb b/lib/rouge/lexers/diff.rb
index 0f44832..28d2ded 100644
--- a/lib/rouge/lexers/diff.rb
+++ b/lib/rouge/lexers/diff.rb
@@ -19,14 +19,18 @@ module Rouge
 
       state :root do
         rule(/^ .*$\n?/, Text)
-        rule(/^---$\n?/, Text)
+        rule(/^---$\n?/, Punctuation)
+        rule(/^[+>]+.*$\n?/, Generic::Inserted)
         rule(/^\+.*$\n?/, Generic::Inserted)
-        rule(/^-+.*$\n?/, Generic::Deleted)
+        rule(/^[-<]+.*$\n?/, Generic::Deleted)
         rule(/^!.*$\n?/, Generic::Strong)
-        rule(/^@.*$\n?/, Generic::Subheading)
         rule(/^([Ii]ndex|diff).*$\n?/, Generic::Heading)
+        rule(/^(@@[^@]*@@)(.*)$\n?/) do
+          groups Punctuation, Text
+        end
+        rule(/^\w.*$\n?/, Punctuation)
         rule(/^=.*$\n?/, Generic::Heading)
-        rule(/.*$\n?/, Text)
+        rule(/\s.*$\n?/, Text)
       end
     end
   end

vidarh added a commit to vidarh/rouge that referenced this issue Jan 18, 2019
Close rouge-ruby#846 as it conflicts and this commit differentiates further.
pyrmont added a commit to pyrmont/rouge that referenced this issue May 12, 2019
In addition to handling diffs done without the `-u` flag, this also
more closely matches `git diff` output for unified diffs by treating `@@
[reference ] @@` as punctuation and the rest of the line as text, making
`---` punctuation, and making the diff/index lines at the top headings.
pyrmont pushed a commit to pyrmont/rouge that referenced this issue May 17, 2019
This fixes rouge-ruby#1022. It also close rouge-ruby#846 as that PR is incompatible with
this one.
pyrmont pushed a commit that referenced this issue May 28, 2019
This commit adds support for non-unified diffs. it fixes #846 and #1022.
@pyrmont
Copy link
Contributor

pyrmont commented May 28, 2019

This has been fixed by #1068. Thanks @vidarh for getting this one licked :)

@pyrmont pyrmont closed this as completed May 28, 2019
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

3 participants