In Python's current syntax highlighting, the words match and case are always highlighted, which could give the wrong idea about their usage. match and case are only given special meaning when used in a specific context, so it would be misleading to highlight them under the same conditions of if and else.
Added match and case as soft keywords, as described here. Modeled after Python 3.10's own IDLE behavior when dealing with soft keywords.
I contacted the maintainer of the file about this pull request.
https://github.com/vim/vim/pull/10621
(1 file)
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@dkearns commented on this pull request.
> syn keyword pythonRepeat for while syn keyword pythonOperator and in is not or syn keyword pythonException except finally raise try syn keyword pythonInclude from import syn keyword pythonAsync async await +" Soft keywords +" These keywords do not mean anything unless used in the right context +" See https://peps.python.org/pep-0634/#the-match-statement for more on this. +" In Python 3.10's IDLE for example, 'match' and 'case' are only highlighted +" when in the right context +syn match pythonConditional "\v^\s*case(.*:$)@=" +syn match pythonConditional "\v^\s*match(.*:$)@="
It looks like you need to allow trailing whitespace after the colons.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@dzfrias pushed 1 commit.
—
View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@dzfrias commented on this pull request.
> syn keyword pythonRepeat for while syn keyword pythonOperator and in is not or syn keyword pythonException except finally raise try syn keyword pythonInclude from import syn keyword pythonAsync async await +" Soft keywords +" These keywords do not mean anything unless used in the right context +" See https://peps.python.org/pep-0634/#the-match-statement for more on this. +" In Python 3.10's IDLE for example, 'match' and 'case' are only highlighted +" when in the right context +syn match pythonConditional "\v^\s*case(.*:$)@=" +syn match pythonConditional "\v^\s*match(.*:$)@="
I think this should be fixed now. Thanks for letting me know
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@zvezdan requested changes on this pull request.
Thank you for noticing this issue and working on the fix.
I asked for some changes.
If they are not clear or you don't feel like working out all the complexities, feel free to let me know and I'll add the changes to a later update of the syntax file.
> +syn match pythonConditional "\v^\s*case(.*:\s*$)@=" +syn match pythonConditional "\v^\s*match(.*:\s*$)@="
A few issues with these regular expressions:
case and match must be followed by a \s before any other content -- match subject or case pattern(s). These regular expressions are not enforcing this space separation and would accept matchX or caseY. There needs to be a \s as the first part of the content inside the parentheses for zero-width look-ahead match (@=).match subject: must be followed by a newline, indented block, and dedent, which allows for the check of $ at the end of the regex. The case pattern: is followed by a block according to grammar. This means that besides "newline, indented block, dedent" it can be followed by a simple statement. So a check for $ is not necessarily correct in that case. It can be a \s*$, but it could also be other characters that could make a simple statement between the colon and the end of the line.\v (very magic) and I'd keep the prevailing style. The non-capturing parentheses could be used \%( ... \)@= and are the only part of the regex that requires a slightly more complex writing without \v which doesn't seem too bad.^\s* makes the space in front of match or case the part of this match, while only the keywords should be matched. See some examples in the other regular expressions in this file that set \zs for the start of the match after finding the pre-requisite part in front of the match.> +" These keywords do not mean anything unless used in the right context +" See https://peps.python.org/pep-0634/#the-match-statement for more on this.
I would refer to the Python documentation section on soft keywords instead: https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords
That should be enough without any further explanation except the heading Soft keywords.
> +" In Python 3.10's IDLE for example, 'match' and 'case' are only highlighted +" when in the right context
This is irrelevant. Grammar is the relevant reason why we need this change, not what IDLE does. I would remove this part of the comment.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Thanks for reviewing this! I added the changes requested, tell me if I did something wrong. I also added the ability to make inline comments after a match statement.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@zvezdan commented on this pull request.
> syn keyword pythonRepeat for while syn keyword pythonOperator and in is not or syn keyword pythonException except finally raise try syn keyword pythonInclude from import syn keyword pythonAsync async await +" Soft keywords +" These keywords do not mean anything unless used in the right context +" See https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords +" for more on this. +syn match pythonConditional "^\s*\zscase\%(\s.*:.*$\)\@="
It's probably not frequent, but one could write case 4 which conforms to the grammar as much as case 4 -- multiple spaces after case vs. a single space. So, I think we should have \s\+ after case and before .*:.
> syn keyword pythonRepeat for while syn keyword pythonOperator and in is not or syn keyword pythonException except finally raise try syn keyword pythonInclude from import syn keyword pythonAsync async await +" Soft keywords +" These keywords do not mean anything unless used in the right context +" See https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords +" for more on this. +syn match pythonConditional "^\s*\zscase\%(\s.*:.*$\)\@=" +syn match pythonConditional "^\s*\zsmatch\%(\s.*:\s*\%(#.*\)\?$\)\@="
Same comment about \s\+ after match and before .*:.
Additionally, the regular expressions in this file use \= instead of \? consistently. Perhaps we can get that replaced too, together with the multi-space fix?
Again, thank you, and sorry for the long delay for this second review. I was unavailable, but next review should be quick. This is looking practically ready. In fact, if you're tired of changing it, we might commit as is and I can change in the subsequent commit. If you don't mind making these two small changes, even better. 🙂
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@dzfrias commented on this pull request.
> syn keyword pythonRepeat for while syn keyword pythonOperator and in is not or syn keyword pythonException except finally raise try syn keyword pythonInclude from import syn keyword pythonAsync async await +" Soft keywords +" These keywords do not mean anything unless used in the right context +" See https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords
+" for more on this. +syn match pythonConditional "^\s*\zscase\%(\s.*:.*$\)\@=" +syn match pythonConditional "^\s*\zsmatch\%(\s.*:\s*\%(#.*\)\?$\)\@="
I added those changes. No problem at all, hopefully they look okay now
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Do these look good now @zvezdan? Let me know if I need to do more or messed something up.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@zvezdan approved this pull request.
Looks good!
Thanks!
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Do these look good now @zvezdan? Let me know if I need to do more or messed something up.
Yes. I just tested them also and the highlighting looks correct.
Thanks for changing this.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
I'll include it, thanks
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Closed #10621.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()