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

Add python f-string and string-prefix highlight #14048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

A4-Tacks
Copy link

close #14033

@dkearns
Copy link
Contributor

dkearns commented Feb 17, 2024

Following up on comments in #14033

You can add the pythonFStringValue group to its own contains list to simplify matching these nested replacement fields. Then the end pattern can probably be reduced to a closing brace.

I cannot add it to its own contains list, it should only take effect on the right side of the colon, and the expression needs to be highlighted on the left side

I don't know, but I suspect that the likelihood of collection literal matches in the expression is much lower than computed format specs. I found some of the latter and none of the former in a quick search. Simply containing it recursively would probably only result in highlighted braces in the expression, in the worst case, which might be worth living with.

I just skimmed the PEP (not a Python user, take with a grain of salt) and it seems that this could be matched properly with a bit of effort.

You could create a series of regions contained in pythonFStringValue for the expression, conversion and format specification and have the format specification contain pythonFStringValue. It would probably be simplest to anchor the expression region to the start of the pythonFStringValue region with a look-behind and match them sequentially with :syn-nextgroup.

Unfortunately, the syntax file doesn't match other brace delimited syntax but those could be added and mixed with :syn-skip patterns to ignore parenthesised ! and :.

I'm not suggesting that this should be done, just that it's possible.

@A4-Tacks
Copy link
Author

Following up on comments in #14033

You can add the pythonFStringValue group to its own contains list to simplify matching these nested replacement fields. Then the end pattern can probably be reduced to a closing brace.

I cannot add it to its own contains list, it should only take effect on the right side of the colon, and the expression needs to be highlighted on the left side

I don't know, but I suspect that the likelihood of collection literal matches in the expression is much lower than computed format specs. I found some of the latter and none of the former in a quick search. Simply containing it recursively would probably only result in highlighted braces in the expression, in the worst case, which might be worth living with.

I just skimmed the PEP (not a Python user, take with a grain of salt) and it seems that this could be matched properly with a bit of effort.

You could create a series of regions contained in pythonFStringValue for the expression, conversion and format specification and have the format specification contain pythonFStringValue. It would probably be simplest to anchor the expression region to the start of the pythonFStringValue region with a look-behind and match them sequentially with :syn-nextgroup.

Unfortunately, the syntax file doesn't match other brace delimited syntax but those could be added and mixed with :syn-skip patterns to ignore parenthesised ! and :.

I'm not suggesting that this should be done, just that it's possible.

I don't want incorrect highlighting like f'{exec("if 1:{}")}', and if I want to contains it myself, I won't be able to use TOP because it can only be excluded

Copy link
Contributor

@dkearns dkearns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want incorrect highlighting like f'{exec("if 1:{}")}', and if I want to contains it myself, I won't be able to use TOP because it can only be excluded

None of my suggestions would highlight that example incorrectly. The exec argument would just be matched as a plain string.

TOP is just a convenience. It would be nice if the contains syntax was a bit more expressive to allow groups to be added as well but you can define a new cluster including only the required expression groups. Technically, of course, TOP matches too much anyway.

syn match pythonEscape "\\\r\=$"

syn region pythonFStringValue matchgroup=pythonFStringBrace
\ start=+{+ end=+=\=\%(![rsa]\)\=\%(:[^{}]*\%({[^{}]*\%({[^{}]*}[^{}]*\)*}[^{}]*\)*\)\=}+ contains=TOP contained excludenl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excludenl shouldn't be needed here as the pattern doesn't match EOL.

hi def link pythonQuotes String
hi def link pythonTripleQuotes pythonQuotes
hi def link pythonEscape Special
hi def link pythonFStringEscapedBrace Special
hi def link pythonFStringValue Normal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't usually specified as it is the default if no highlight group is linked but does no harm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

python syntax file, support f-string
2 participants