You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for using the "diff" lexer + code syntax highlighting #1043
(Original issue 1339 created by javiereguiluz on 2017-03-13T13:54:48.610737+00:00)
We've started using "diff" in some code samples as an easier way to show the reader the changes that they need to make. However, using "diff" disables all the original code syntax highlighting.
My feature request is to add support for diff + syntax highlighting, as done by GitHub.
(Original comment by tshatch on 2018-12-05T05:14:14.039283+00:00)
Two potential problems with this -- if the diff hunk starts in the middle of something (like a string), and if the change results in a difference of the context lines' highlighting. It's worth coding a prototype though.
In the next version of pytest, we will start using pygments to highlight diffs in assertion failure messages (we are already using pygments to highlight other code segments). The diffs are of Python code, and it would be nicer to "overlay" the diff over the Python highlighter, instead of just the diff highlighting.
Well, this is not really possible to do properly with the current API. To highlight a diff, you need all the context before and after (imagine the diff is in the middle of a giant multiline string, for example). I think what you want is to take two files, highlight them both, then diff the series of (string, token-type) pairs (using something like difflib), and turn that into a colored diff.
I don't really know if we'd consider that in-scope for Pygments.
Isn't that more of a post-process? I.e. first you apply normal syntax highlighting, then you create a diff, and then you apply the diff colors as an overlay on the original colors? In which case you'd be writing this on top of Pygments, but Pygments has no way to express a dependency chain of operations, nor is there sufficient context in the diff output to make this work. I don't see that adding diffing capabilities is in-scope for Pygments. It should have all the building blocks though to implement this.
Activity
Anteru commentedon Aug 31, 2019
(Original comment by hbarcelos_ec on 2018-04-03T19:04:20.503504+00:00)
@javiereguiluz what is the exact syntax Github uses to allow this?
Anteru commentedon Aug 31, 2019
(Original comment by javiereguiluz on 2018-04-04T06:18:03.953558+00:00)
You can't do that your self. If you use
diff, you only get diff highlighting, not code + diff highlighting. They apply this highlight automatically for any of their own diff listings. Example: https://github.com/symfony/symfony/pull/26695/filesAnteru commentedon Aug 31, 2019
(Original comment by tshatch on 2018-12-05T05:14:14.039283+00:00)
Two potential problems with this -- if the diff hunk starts in the middle of something (like a string), and if the change results in a difference of the context lines' highlighting. It's worth coding a prototype though.
bluetech commentedon Oct 25, 2023
In the next version of pytest, we will start using pygments to highlight diffs in assertion failure messages (we are already using pygments to highlight other code segments). The diffs are of Python code, and it would be nicer to "overlay" the diff over the Python highlighter, instead of just the diff highlighting.
jeanas commentedon Oct 25, 2023
Well, this is not really possible to do properly with the current API. To highlight a diff, you need all the context before and after (imagine the diff is in the middle of a giant multiline string, for example). I think what you want is to take two files, highlight them both, then diff the series of (string, token-type) pairs (using something like difflib), and turn that into a colored diff.
I don't really know if we'd consider that in-scope for Pygments.
Anteru commentedon Oct 25, 2023
Isn't that more of a post-process? I.e. first you apply normal syntax highlighting, then you create a diff, and then you apply the diff colors as an overlay on the original colors? In which case you'd be writing this on top of Pygments, but Pygments has no way to express a dependency chain of operations, nor is there sufficient context in the diff output to make this work. I don't see that adding diffing capabilities is in-scope for Pygments. It should have all the building blocks though to implement this.