Skip to content

Commit

Permalink
Add support for normal diff syntax (#2321)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Jan 29, 2023
1 parent 4d8bca5 commit fdb05f4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pygments/lexers/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ class DiffLexer(RegexLexer):
tokens = {
'root': [
(r'( )(.*)(\n)', bygroups(Whitespace, Text, Whitespace)),
(r'(\+.*)(\n)', bygroups(Generic.Inserted, Whitespace)),
(r'(-.*)(\n)', bygroups(Generic.Deleted, Whitespace)),
(r'(!.*)(\n)', bygroups(Generic.Strong, Whitespace)),
(r'(@.*)(\n)', bygroups(Generic.Subheading, Whitespace)),
(r'(!.*|---)(\n)', bygroups(Generic.Strong, Whitespace)),
(r'((?:< |-).*)(\n)', bygroups(Generic.Deleted, Whitespace)),
(r'((?:> |\+).*)(\n)', bygroups(Generic.Inserted, Whitespace)),
(
r'(@.*|\d(?:,\d+)?(?:a|c|d)\d+(?:,\d+)?)(\n)',
bygroups(Generic.Subheading, Whitespace),
),
(r'((?:[Ii]ndex|diff).*)(\n)', bygroups(Generic.Heading, Whitespace)),
(r'(=.*)(\n)', bygroups(Generic.Heading, Whitespace)),
(r'(.*)(\n)', Whitespace),
(r'(.*)(\n)', bygroups(Text, Whitespace)),
]
}

Expand Down
38 changes: 38 additions & 0 deletions tests/snippets/diff/normal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---input---
1,2d0
< A
< A
4c2
< C
---
> F
5a4
> E

---tokens---
'1,2d0' Generic.Subheading
'\n' Text.Whitespace

'< A' Generic.Deleted
'\n' Text.Whitespace

'< A' Generic.Deleted
'\n' Text.Whitespace

'4c2' Generic.Subheading
'\n' Text.Whitespace

'< C' Generic.Deleted
'\n' Text.Whitespace

'---' Generic.Strong
'\n' Text.Whitespace

'> F' Generic.Inserted
'\n' Text.Whitespace

'5a4' Generic.Subheading
'\n' Text.Whitespace

'> E' Generic.Inserted
'\n' Text.Whitespace
44 changes: 44 additions & 0 deletions tests/snippets/diff/unified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---input---
--- old.txt 2023-01-17 21:02:15.449417575 -0700
+++ new.txt 2023-01-17 21:02:12.489441682 -0700
@@ -1,5 +1,4 @@
-A
-A
B
-C
+F
D
+E

---tokens---
'--- old.txt\t2023-01-17 21:02:15.449417575 -0700' Generic.Deleted
'\n' Text.Whitespace

'+++ new.txt\t2023-01-17 21:02:12.489441682 -0700' Generic.Inserted
'\n' Text.Whitespace

'@@ -1,5 +1,4 @@' Generic.Subheading
'\n' Text.Whitespace

'-A' Generic.Deleted
'\n' Text.Whitespace

'-A' Generic.Deleted
'\n' Text.Whitespace

' ' Text.Whitespace
'B' Text
'\n' Text.Whitespace

'-C' Generic.Deleted
'\n' Text.Whitespace

'+F' Generic.Inserted
'\n' Text.Whitespace

' ' Text.Whitespace
'D' Text
'\n' Text.Whitespace

'+E' Generic.Inserted
'\n' Text.Whitespace

0 comments on commit fdb05f4

Please sign in to comment.