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

Invalid doctests causes wrong highlighting in complete code fragment #1991

Open
niknetniko opened this issue Sep 10, 2023 · 1 comment
Open
Labels
bugfix-request A request for a bugfix to be developed.

Comments

@niknetniko
Copy link
Contributor

Name of the lexer
python

Code sample
A sample of the code that produces the bug (note the missing ' in the return value of the doctest)

def test():
    """
    >>> test()
    'hallo
    """
    return str(5)

Online sample

Version 4.1.3:
image
Version 4.1.0:
image

Additional context
Since v4.1.1 (#1932), the Python lexer supports highlight doctests. However, if the doctests are invalid, the invalid highlighting extends beyond the scope of the doctest.

While I would normally think we shouldn't expect great highlighting if the syntax is invalid, I feel like doctests are an exception: the code in question is still valid Python code, as the doctests are comments.

Perhaps we could disable doctest highlighting if an error is found? (No clue if possible in Rouge).

@niknetniko niknetniko added the bugfix-request A request for a bugfix to be developed. label Sep 10, 2023
@jneen
Copy link
Member

jneen commented Sep 10, 2023

Thanks for the report! I think this was simply implemented incorrectly. This is a use case for recursive highlighting - scan for the docstring first and delegate. This is the proper way to do embedding when the parent language completely controls the extent of the embed.

Here's an example of <script> tags in HTML, where the parent language's </script> ending tag takes precedence over any content of the script itself:

rule %r(<\s*/\s*script\s*>)m, Name::Tag, :pop!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix-request A request for a bugfix to be developed.
Projects
None yet
Development

No branches or pull requests

2 participants