vim-python / python-syntax

Python syntax highlighting for Vim
MIT License
438 stars 84 forks source link

Properly highlight docstrings #83

Open wmvanvliet opened 3 years ago

wmvanvliet commented 3 years ago

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 commented 2 years ago

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 commented 1 year ago

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