Skip to content

Add support for using the "diff" lexer + code syntax highlighting #1043

Open

Description

@Anteru

(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.

Thanks!

Activity

  1. Anteru commented on Aug 31, 2019

    @Anteru
    CollaboratorAuthor

    (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?

  2. Anteru commented on Aug 31, 2019

    @Anteru
    CollaboratorAuthor

    (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/files

  3. Anteru commented on Aug 31, 2019

    @Anteru
    CollaboratorAuthor

    (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.

  4. bluetech commented on Oct 25, 2023

    @bluetech

    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.

  5. jeanas commented on Oct 25, 2023

    @jeanas
    Contributor

    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.

  6. Anteru commented on Oct 25, 2023

    @Anteru
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    S-minorseverity: minorT-featuretype: a new featureX-importedimported from Bitbucket

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions