Skip to content

Commit fb56c6a

Browse files
committed
Update string formatting scopes.
Add two scopes for finer control of format highlighting: - meta.format.percent.python - meta.format.brace.python Issue #106.
1 parent f340032 commit fb56c6a

17 files changed

+252
-237
lines changed

grammars/MagicPython.cson

+33-26
Original file line numberDiff line numberDiff line change
@@ -917,23 +917,28 @@ repository:
917917
name: "constant.language.python"
918918
match: "\\\\$"
919919
"string-formatting":
920-
name: "constant.character.format.placeholder.other.python"
920+
name: "meta.format.percent.python"
921921
match: '''
922922
(?x)
923-
% (\\([\\w\\s]*\\))?
924-
[-+#0 ]*
925-
(\\d+|\\*)? (\\.(\\d+|\\*))?
926-
([hlL])?
927-
[diouxXeEfFgGcrsa%]
923+
(
924+
% (\\([\\w\\s]*\\))?
925+
[-+#0 ]*
926+
(\\d+|\\*)? (\\.(\\d+|\\*))?
927+
([hlL])?
928+
[diouxXeEfFgGcrsa%]
929+
)
928930
929931
'''
932+
captures:
933+
"1":
934+
name: "constant.character.format.placeholder.other.python"
930935
"string-brace-formatting":
931936
patterns: [
932937
{
933-
name: "constant.character.format.placeholder.other.python"
938+
name: "meta.format.brace.python"
934939
match: '''
935940
(?x)
936-
(?:
941+
(
937942
{{ | }}
938943
| (?:
939944
{
@@ -946,36 +951,38 @@ repository:
946951
947952
'''
948953
captures:
949-
"2":
950-
name: "storage.type.format.python"
954+
"1":
955+
name: "constant.character.format.placeholder.other.python"
951956
"3":
952957
name: "storage.type.format.python"
958+
"4":
959+
name: "storage.type.format.python"
953960
}
954961
{
955-
name: "constant.character.format.placeholder.other.python"
956-
begin: '''
962+
name: "meta.format.brace.python"
963+
match: '''
957964
(?x)
958-
\\{
965+
(
966+
{
959967
\\w*? (\\.[[:alpha:]_]\\w*? | \\[[^\\]'"]+\\])*?
960968
(![rsa])?
961969
(:)
962-
(?=[^'"}\\n]*\\})
970+
(
971+
[^'"{}\\n]+?
972+
|
973+
\\{ [^'"}\\n]*? \\}
974+
)*
975+
}
976+
)
963977
964978
'''
965-
end: "\\}"
966-
beginCaptures:
967-
"2":
968-
name: "storage.type.format.python"
979+
captures:
980+
"1":
981+
name: "constant.character.format.placeholder.other.python"
969982
"3":
970983
name: "storage.type.format.python"
971-
patterns: [
972-
{
973-
match: '''
974-
(?x) \\{ [^'"}\\n]*? \\} (?=.*?\\})
975-
976-
'''
977-
}
978-
]
984+
"4":
985+
name: "storage.type.format.python"
979986
}
980987
]
981988
"fstring-formatting":

grammars/MagicPython.tmLanguage

+43-26
Original file line numberDiff line numberDiff line change
@@ -1411,26 +1411,36 @@
14111411
<key>string-formatting</key>
14121412
<dict>
14131413
<key>name</key>
1414-
<string>constant.character.format.placeholder.other.python</string>
1414+
<string>meta.format.percent.python</string>
14151415
<key>match</key>
14161416
<string>(?x)
1417-
% (\([\w\s]*\))?
1418-
[-+#0 ]*
1419-
(\d+|\*)? (\.(\d+|\*))?
1420-
([hlL])?
1421-
[diouxXeEfFgGcrsa%]
1417+
(
1418+
% (\([\w\s]*\))?
1419+
[-+#0 ]*
1420+
(\d+|\*)? (\.(\d+|\*))?
1421+
([hlL])?
1422+
[diouxXeEfFgGcrsa%]
1423+
)
14221424
</string>
1425+
<key>captures</key>
1426+
<dict>
1427+
<key>1</key>
1428+
<dict>
1429+
<key>name</key>
1430+
<string>constant.character.format.placeholder.other.python</string>
1431+
</dict>
1432+
</dict>
14231433
</dict>
14241434
<key>string-brace-formatting</key>
14251435
<dict>
14261436
<key>patterns</key>
14271437
<array>
14281438
<dict>
14291439
<key>name</key>
1430-
<string>constant.character.format.placeholder.other.python</string>
1440+
<string>meta.format.brace.python</string>
14311441
<key>match</key>
14321442
<string>(?x)
1433-
(?:
1443+
(
14341444
{{ | }}
14351445
| (?:
14361446
{
@@ -1443,52 +1453,59 @@
14431453
</string>
14441454
<key>captures</key>
14451455
<dict>
1446-
<key>2</key>
1456+
<key>1</key>
14471457
<dict>
14481458
<key>name</key>
1449-
<string>storage.type.format.python</string>
1459+
<string>constant.character.format.placeholder.other.python</string>
14501460
</dict>
14511461
<key>3</key>
14521462
<dict>
14531463
<key>name</key>
14541464
<string>storage.type.format.python</string>
14551465
</dict>
1466+
<key>4</key>
1467+
<dict>
1468+
<key>name</key>
1469+
<string>storage.type.format.python</string>
1470+
</dict>
14561471
</dict>
14571472
</dict>
14581473
<dict>
14591474
<key>name</key>
1460-
<string>constant.character.format.placeholder.other.python</string>
1461-
<key>begin</key>
1475+
<string>meta.format.brace.python</string>
1476+
<key>match</key>
14621477
<string>(?x)
1463-
\{
1478+
(
1479+
{
14641480
\w*? (\.[[:alpha:]_]\w*? | \[[^\]'"]+\])*?
14651481
(![rsa])?
14661482
(:)
1467-
(?=[^'"}\n]*\})
1483+
(
1484+
[^'"{}\n]+?
1485+
|
1486+
\{ [^'"}\n]*? \}
1487+
)*
1488+
}
1489+
)
14681490
</string>
1469-
<key>end</key>
1470-
<string>\}</string>
1471-
<key>beginCaptures</key>
1491+
<key>captures</key>
14721492
<dict>
1473-
<key>2</key>
1493+
<key>1</key>
14741494
<dict>
14751495
<key>name</key>
1476-
<string>storage.type.format.python</string>
1496+
<string>constant.character.format.placeholder.other.python</string>
14771497
</dict>
14781498
<key>3</key>
14791499
<dict>
14801500
<key>name</key>
14811501
<string>storage.type.format.python</string>
14821502
</dict>
1483-
</dict>
1484-
<key>patterns</key>
1485-
<array>
1503+
<key>4</key>
14861504
<dict>
1487-
<key>match</key>
1488-
<string>(?x) \{ [^'"}\n]*? \} (?=.*?\})
1489-
</string>
1505+
<key>name</key>
1506+
<string>storage.type.format.python</string>
14901507
</dict>
1491-
</array>
1508+
</dict>
14921509
</dict>
14931510
</array>
14941511
</dict>

grammars/src/MagicPython.syntax.yaml

+33-20
Original file line numberDiff line numberDiff line change
@@ -769,21 +769,25 @@ repository:
769769
match: \\$
770770

771771
string-formatting:
772-
name: constant.character.format.placeholder.other.python
772+
name: meta.format.percent.python
773773
match: |
774774
(?x)
775-
% (\([\w\s]*\))?
776-
[-+#0 ]*
777-
(\d+|\*)? (\.(\d+|\*))?
778-
([hlL])?
779-
[diouxXeEfFgGcrsa%]
775+
(
776+
% (\([\w\s]*\))?
777+
[-+#0 ]*
778+
(\d+|\*)? (\.(\d+|\*))?
779+
([hlL])?
780+
[diouxXeEfFgGcrsa%]
781+
)
782+
captures:
783+
'1': {name: constant.character.format.placeholder.other.python}
780784

781785
string-brace-formatting:
782786
patterns:
783-
- name: constant.character.format.placeholder.other.python
787+
- name: meta.format.brace.python
784788
match: |
785789
(?x)
786-
(?:
790+
(
787791
{{ | }}
788792
| (?:
789793
{
@@ -801,24 +805,33 @@ repository:
801805
# "storage.type..." is more likely to have special
802806
# highlighting in any given color scheme than
803807
# "support.other..."
804-
#
805-
'2': {name: storage.type.format.python}
808+
'1': {name: constant.character.format.placeholder.other.python}
806809
'3': {name: storage.type.format.python}
807-
- name: constant.character.format.placeholder.other.python
808-
begin: |
810+
'4': {name: storage.type.format.python}
811+
812+
# The reason for 2 separate rules is to establish which one
813+
# should be matched with a higher priority. The rule below is
814+
# more generic, so it should be matched only if the specific one
815+
# didn't.
816+
- name: meta.format.brace.python
817+
match: |
809818
(?x)
810-
\{
819+
(
820+
{
811821
\w*? (\.[[:alpha:]_]\w*? | \[[^\]'"]+\])*?
812822
(![rsa])?
813823
(:)
814-
(?=[^'"}\n]*\})
815-
end: \}
816-
beginCaptures:
817-
'2': {name: storage.type.format.python}
824+
(
825+
[^'"{}\n]+?
826+
|
827+
\{ [^'"}\n]*? \}
828+
)*
829+
}
830+
)
831+
captures:
832+
'1': {name: constant.character.format.placeholder.other.python}
818833
'3': {name: storage.type.format.python}
819-
patterns:
820-
- match: |
821-
(?x) \{ [^'"}\n]*? \} (?=.*?\})
834+
'4': {name: storage.type.format.python}
822835

823836
fstring-formatting:
824837
patterns:

misc/scopes

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ meta.backreference.regexp
6565
meta.character.set.regexp
6666
meta.class.inheritance.python
6767
meta.class.python
68+
meta.format.brace.python
69+
meta.format.percent.python
6870
meta.fstring.python
6971
meta.function-call.generic.python
7072
meta.function-call.python

0 commit comments

Comments
 (0)