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

[Python] Fix debugging f-string replacements #3218

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 71 additions & 60 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1983,8 +1983,7 @@ contexts:
scope: constant.character.escape.python
- match: \{\s*\}
scope: invalid.illegal.empty-expression.python
- match: (?=\{)
push: f-string-replacement
- include: f-string-replacements
- match: \}
scope: invalid.illegal.stray-brace.python

Expand All @@ -1999,75 +1998,87 @@ contexts:
scope: constant.character.escape.python
- match: \{\s*\}
scope: invalid.illegal.empty-expression.python
- match: (?=\{)
push: f-string-replacement-reset
- include: f-string-replacements-reset
- match: \}
scope: invalid.illegal.stray-brace.python

f-string-replacement:
f-string-replacements:
- match: \{
scope: punctuation.section.interpolation.begin.python
push:
- f-string-replacement-meta
- f-string-replacement-modifier
- f-string-replacement-expression

f-string-replacement-meta:
- clear_scopes: 1
- match: \}
scope: meta.interpolation.python punctuation.section.interpolation.end.python
pop: true
- meta_include_prototype: false
- meta_scope: meta.interpolation.python
- match: ''
pop: 1

f-string-replacement-expression:
- meta_content_scope: source.python.embedded
- match: (?=\s*(?:=\s*)?(![^=]|:|\}))
pop: 1
- match: \\
scope: invalid.illegal.backslash-in-fstring.python
- include: inline-for
- include: expression-in-a-group

f-string-replacement-modifier:
- include: f-string-replacement-end
- include: f-string-replacement-common
- match: =
scope: storage.modifier.debug.python

f-string-replacements-reset:
- match: \{
scope: punctuation.section.interpolation.begin.python
push:
- meta_scope: meta.interpolation.python
- match: (?=\})
pop: true
- match: '![ars]'
scope: storage.modifier.conversion.python
- match: =
scope: storage.modifier.debug.python
- match: ':'
push:
- meta_scope: meta.format-spec.python constant.other.format-spec.python
# Because replacements can also be used *within* the format-spec,
# basically any character is valid and matching {{format_spec}} is useless.
# - match: '{{format_spec}}'
- match: (?=\})
pop: true
- include: f-string-content
- match: ''
push:
- meta_content_scope: source.python.embedded
- match: (?==?(![^=]|:|\}))
pop: true
- match: \\
scope: invalid.illegal.backslash-in-fstring.python
- include: inline-for
- include: expression-in-a-group
- f-string-replacement-reset-meta
- f-string-replacement-reset-modifier
- f-string-replacement-reset-expression

f-string-replacement-reset:
f-string-replacement-reset-meta:
# Same as f-string-replacement, but with clear_scopes: true
- clear_scopes: true
- meta_scope: source.python meta.string.interpolated.python
- meta_include_prototype: false
- meta_scope: source.python meta.string.interpolated.python meta.interpolation.python
- match: ''
pop: 1

f-string-replacement-reset-expression:
- meta_content_scope: source.python.embedded
- match: (?=![^=]|:|\})
pop: 1
- match: \\
scope: invalid.illegal.backslash-in-fstring.python
- include: inline-for
- include: expression-in-a-group

f-string-replacement-reset-modifier:
- include: f-string-replacement-end
- include: f-string-replacement-common

f-string-replacement-common:
- match: '![ars]'
scope: storage.modifier.conversion.python
- match: ':'
set: f-string-format-spec

f-string-format-spec:
- meta_scope: meta.format-spec.python constant.other.format-spec.python
# Because replacements can also be used *within* the format-spec,
# basically any character is valid and matching {{format_spec}} is useless.
# - match: '{{format_spec}}'
- include: f-string-replacement-end
- include: f-string-content

f-string-replacement-end:
- match: \}
scope: meta.interpolation.python punctuation.section.interpolation.end.python
pop: true
- match: \{
scope: punctuation.section.interpolation.begin.python
push:
- meta_scope: meta.interpolation.python
- match: (?=\})
pop: true
- match: '![ars]'
scope: storage.modifier.conversion.python
- match: ':'
push:
- meta_scope: meta.format-spec.python constant.other.format-spec.python
- match: (?=\})
pop: true
- include: f-string-content
- match: ''
push:
- meta_content_scope: source.python.embedded
- match: (?=![^=]|:|\})
pop: true
- match: \\
scope: invalid.illegal.backslash-in-fstring.python
- include: inline-for
- include: expression-in-a-group
scope: punctuation.section.interpolation.end.python
pop: 2

string-quoted-double-block:
# Triple-quoted capital R raw string, unicode or not, no syntax embedding
Expand Down
21 changes: 21 additions & 0 deletions Python/syntax_test_python_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,27 @@
# ^^^^^ meta.format-spec.python
f'{"Σ"=}'
# ^ storage.modifier.debug.python
f'{"Σ"= }'
# ^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^ storage.modifier.debug.python
f'{"Σ" =}'
# ^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^ storage.modifier.debug.python
f'{"Σ" = }'
# ^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python source.python.embedded
# ^^^^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^ storage.modifier.debug.python
f'{"Σ" = !s}'
# ^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^^^ meta.string.interpolated.python meta.interpolation.python source.python.embedded
# ^^^^^^ meta.string.interpolated.python meta.interpolation.python - source.python.embedded
# ^ storage.modifier.debug.python
# ^^ storage.modifier.conversion.python
f'{0==1}'
# ^^ -storage.modifier.debug.python
f'{0!=1}'
Expand Down