[vim/vim] Add python f-string and string-prefix highlight (PR #14048)

21 views
Skip to first unread message

A4-Tacks

unread,
Feb 17, 2024, 3:09:05 AM2/17/24
to vim/vim, Subscribed

close #14033


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/14048

Commit Summary

  • 781ca4e Add python f-string and string-prefix highlight

File Changes

(1 file)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048@github.com>

dkearns

unread,
Feb 17, 2024, 9:30:05 AM2/17/24
to vim/vim, Subscribed

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.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048/c1950223797@github.com>

A4-Tacks

unread,
Feb 17, 2024, 9:42:42 AM2/17/24
to vim/vim, Subscribed

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


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048/c1950226261@github.com>

dkearns

unread,
Feb 19, 2024, 7:14:33 AM2/19/24
to vim/vim, Subscribed

@dkearns requested changes on this pull request.

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.


In runtime/syntax/python.vim:

>  
 syn match   pythonEscape	+\\[abfnrtv'"\\]+ contained
 syn match   pythonEscape	"\\\o\{1,3}" contained
 syn match   pythonEscape	"\\x\x\{2}" contained
 syn match   pythonEscape	"\%(\\u\x\{4}\|\\U\x\{8}\)" contained
 " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
 syn match   pythonEscape	"\\N{\a\+\%(\s\a\+\)*}" contained
-syn match   pythonEscape	"\\$"
+syn match   pythonEscape	"\\\r\=$"
+
+syn region  pythonFStringValue matchgroup=pythonFStringBrace
+      \ start=+{+ end=+=\=\%(![rsa]\)\=\%(:[^{}]*\%({[^{}]*\%({[^{}]*}[^{}]*\)*}[^{}]*\)*\)\=}+ contains=TOP contained excludenl

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


In runtime/syntax/python.vim:

>  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

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


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048/review/1888252285@github.com>

Christian Brabandt

unread,
Nov 11, 2024, 3:44:36 PM11/11/24
to vim/vim, Subscribed

closing this one and awaiting a new patch from the maintainer.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048/c2468990151@github.com>

Christian Brabandt

unread,
Nov 11, 2024, 3:44:36 PM11/11/24
to vim/vim, Subscribed

Closed #14048.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14048/issue_event/15255294442@github.com>

Reply all
Reply to author
Forward
0 new messages