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

Properly highlight docstrings #83

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

wmvanvliet
Copy link

@wmvanvliet wmvanvliet commented Apr 13, 2021

Another attempt at #45 and #57, namely to properly highlight docstrings as comments.

For now, this implementation is very ugly, but it actually works. It only highlights things that should be docstrings without touching other types of multiline strings. This is accomplished by checking for : characters and combining that with nextgroup=pythonDocString to interpret the first directly following multiline string as a docstring. Another case is added for the first multiline string appearing at the top of the file.

See https://gist.github.com/wmvanvliet/36471bb456151d93b86c402b64684b0a for a bunch of challenging test cases.

The implementation is messy right now to work around a specific problem. Any pointers on how to solve this more elegantly are greatly appreciated. The problem is that nextgroup=pythonDocString skipempty will skip newlines, but not spaces in its search for the docstring, and so it will only find it when the docstring starts on the first column of a line. For now, the workaround is to match +^\s*"""+ as a docstring, i.e. also match the leading whitespace, but this causes the docstring rule to have precedence over the normal +"""+ multiline pattern. Ideally, the multiline rule should take precedence and have this precedence temporarily overwritten by the presence of a : mark.

@ZDBioHazard
Copy link

This seems to work for me, but I needed to add nextgroup=pythonDocString skipempty to pythonRun and pythonCoding to get the module-level docstrings to work when a shebang or an encoding thing is present at the top of the file.

My colorscheme has an aggressive String highlight, so adding huge docstrings to my code has been making my eyes want to explode. This PR saves me a lot of headache - literally, thanks!

@wmvanvliet
Copy link
Author

Thanks @ZDBioHazard! I've been looking for that fix.

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

Successfully merging this pull request may close these issues.

None yet

2 participants