vim-python / python-syntax

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

Add pythonStringModifier group #95

Open adigitoleo opened 11 months ago

adigitoleo commented 11 months ago

The single-letter prefixes in Python3 are a bit nuts, and get lost in the code. I played aroung a bit and found that I can add a pythonStringModifier group to catch these (and highlight them in a strong color, like Operator).

The solution requires defining said group as contained and adding that group to the contains list for all of the various string groups defined in the syntax file. Here's a snippet for after/syntax/python.vim that doesn't cover python 2 or multi-letter cases like fr". Maybe such an approach could be added in a more complete way to support this, if there is interest.

syn match pythonStringModifier '\<[brf]"'me=e-1 contained
syn region pythonFString   start=+[fF]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell,pythonStringModifier
syn region pythonFString   start=+[fF]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell,pythonStringModifier
syn region pythonFString   start=+[fF]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell,pythonStringModifier
syn region pythonFString   start=+[fF]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell,pythonStringModifier
syn region pythonBytes    start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell,pythonStringModifier
syn region pythonBytes    start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell,pythonStringModifier
syn region pythonBytes    start=+[bB]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell,pythonStringModifier
syn region pythonBytes    start=+[bB]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell,pythonStringModifier
syn region pythonRawString  start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell,pythonStringModifier
syn region pythonRawString  start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell,pythonStringModifier
syn region pythonRawString  start=+[rR]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell,pythonStringModifier
syn region pythonRawString  start=+[rR]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell,pythonStringModifier

hi link pythonStringModifier Operator
adigitoleo commented 10 months ago

Ah, the match still isn't great, and will pick up the second f in:

msg = f"using mode f"

My vimL syn-pattern knowledge is not good enough to solve this (tried a few things including making it into a region that ends in '[^"]+"' but no luck). Maybe someone would know how to fix it otherwise I guess it's not really suitable for addition.