Skip to content

Commit e137035

Browse files
authoredNov 22, 2021
Dart: Added string interpolation and improved metadata (#3197)
1 parent 86028ad commit e137035

File tree

4 files changed

+237
-26
lines changed

4 files changed

+237
-26
lines changed
 

‎components/prism-dart.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@
2222
};
2323

2424
Prism.languages.dart = Prism.languages.extend('clike', {
25-
'string': [
26-
{
27-
pattern: /r?("""|''')[\s\S]*?\1/,
28-
greedy: true
29-
},
30-
{
31-
pattern: /r?(["'])(?:\\.|(?!\1)[^\\\r\n])*\1/,
32-
greedy: true
33-
}
34-
],
3525
'class-name': [
3626
className,
3727
{
@@ -46,10 +36,32 @@
4636
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
4737
});
4838

49-
Prism.languages.insertBefore('dart', 'function', {
39+
Prism.languages.insertBefore('dart', 'string', {
40+
'string-literal': {
41+
pattern: /r?(?:("""|''')[\s\S]*?\1|(["'])(?:\\.|(?!\2)[^\\\r\n])*\2(?!\2))/,
42+
greedy: true,
43+
inside: {
44+
'interpolation': {
45+
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
46+
lookbehind: true,
47+
inside: {
48+
'punctuation': /^\$\{?|\}$/,
49+
'expression': {
50+
pattern: /[\s\S]+/,
51+
inside: Prism.languages.dart
52+
}
53+
}
54+
},
55+
'string': /[\s\S]+/
56+
}
57+
},
58+
'string': undefined
59+
});
60+
61+
Prism.languages.insertBefore('dart', 'class-name', {
5062
'metadata': {
5163
pattern: /@\w+/,
52-
alias: 'symbol'
64+
alias: 'function'
5365
}
5466
});
5567

‎components/prism-dart.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+134-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,148 @@
11
@deprecated
22
@override
3-
@todo('seth', 'make this do something')
3+
@ToDo('seth', 'make this do something')
4+
5+
@DataTable("sale_orders")
6+
class SaleOrder {
7+
@DataColumn("sale_order_date")
8+
DateTime date;
9+
}
10+
11+
@table
12+
class Product {
13+
@column
14+
String name;
15+
}
16+
17+
const DataColumn column = const DataColumn();
18+
19+
const DataTable table = const DataTable();
20+
21+
class DataTable {
22+
final String name;
23+
24+
const DataTable([this.name]);
25+
}
26+
27+
class DataColumn {
28+
final String name;
29+
30+
const DataColumn([this.name]);
31+
}
432

533
----------------------------------------------------
634

735
[
836
["metadata", "@deprecated"],
37+
938
["metadata", "@override"],
10-
["metadata", "@todo"],
39+
40+
["metadata", "@ToDo"],
1141
["punctuation", "("],
12-
["string", "'seth'"],
42+
["string-literal", [
43+
["string", "'seth'"]
44+
]],
1345
["punctuation", ","],
14-
["string", "'make this do something'"],
15-
["punctuation", ")"]
46+
["string-literal", [
47+
["string", "'make this do something'"]
48+
]],
49+
["punctuation", ")"],
50+
51+
["metadata", "@DataTable"],
52+
["punctuation", "("],
53+
["string-literal", [
54+
["string", "\"sale_orders\""]
55+
]],
56+
["punctuation", ")"],
57+
58+
["keyword", "class"],
59+
["class-name", ["SaleOrder"]],
60+
["punctuation", "{"],
61+
62+
["metadata", "@DataColumn"],
63+
["punctuation", "("],
64+
["string-literal", [
65+
["string", "\"sale_order_date\""]
66+
]],
67+
["punctuation", ")"],
68+
69+
["class-name", ["DateTime"]],
70+
" date",
71+
["punctuation", ";"],
72+
73+
["punctuation", "}"],
74+
75+
["metadata", "@table"],
76+
["keyword", "class"], ["class-name", ["Product"]], ["punctuation", "{"],
77+
["metadata", "@column"],
78+
["class-name", ["String"]], " name", ["punctuation", ";"],
79+
["punctuation", "}"],
80+
81+
["keyword", "const"],
82+
["class-name", ["DataColumn"]],
83+
" column ",
84+
["operator", "="],
85+
["keyword", "const"],
86+
["class-name", ["DataColumn"]],
87+
["punctuation", "("],
88+
["punctuation", ")"],
89+
["punctuation", ";"],
90+
91+
["keyword", "const"],
92+
["class-name", ["DataTable"]],
93+
" table ",
94+
["operator", "="],
95+
["keyword", "const"],
96+
["class-name", ["DataTable"]],
97+
["punctuation", "("],
98+
["punctuation", ")"],
99+
["punctuation", ";"],
100+
101+
["keyword", "class"],
102+
["class-name", ["DataTable"]],
103+
["punctuation", "{"],
104+
105+
["keyword", "final"],
106+
["class-name", ["String"]],
107+
" name",
108+
["punctuation", ";"],
109+
110+
["keyword", "const"],
111+
["class-name", ["DataTable"]],
112+
["punctuation", "("],
113+
["punctuation", "["],
114+
["keyword", "this"],
115+
["punctuation", "."],
116+
"name",
117+
["punctuation", "]"],
118+
["punctuation", ")"],
119+
["punctuation", ";"],
120+
121+
["punctuation", "}"],
122+
123+
["keyword", "class"],
124+
["class-name", ["DataColumn"]],
125+
["punctuation", "{"],
126+
127+
["keyword", "final"],
128+
["class-name", ["String"]],
129+
" name",
130+
["punctuation", ";"],
131+
132+
["keyword", "const"],
133+
["class-name", ["DataColumn"]],
134+
["punctuation", "("],
135+
["punctuation", "["],
136+
["keyword", "this"],
137+
["punctuation", "."],
138+
"name",
139+
["punctuation", "]"],
140+
["punctuation", ")"],
141+
["punctuation", ";"],
142+
143+
["punctuation", "}"]
16144
]
17145

18146
----------------------------------------------------
19147

20-
Checks for metadata.
148+
Checks for metadata.

‎tests/languages/dart/string_feature.test

+78-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,89 @@ bar"""
88
'''foo
99
bar'''
1010

11+
'$string has ${string.length} letters'
12+
"cookie has ${cookie.number_of_chips} chips"
13+
1114
----------------------------------------------------
1215

1316
[
14-
["string", "\"\""], ["string", "''"],
15-
["string", "r\"\""], ["string", "r''"],
16-
["string", "\"\"\"\"\"\""], ["string", "''''''"],
17-
["string", "r\"\"\"\"\"\""], ["string", "r''''''"],
18-
["string", "\"fo\\\"o\""], ["string", "'fo\\'o'"],
19-
["string", "\"\"\"foo\r\nbar\"\"\""], ["string", "'''foo\r\nbar'''"]
17+
["string-literal", [
18+
["string", "\"\""]
19+
]],
20+
["string-literal", [
21+
["string", "''"]
22+
]],
23+
24+
["string-literal", [
25+
["string", "r\"\""]
26+
]],
27+
["string-literal", [
28+
["string", "r''"]
29+
]],
30+
31+
["string-literal", [
32+
["string", "\"\"\"\"\"\""]
33+
]],
34+
["string-literal", [
35+
["string", "''''''"]
36+
]],
37+
38+
["string-literal", [
39+
["string", "r\"\"\"\"\"\""]
40+
]],
41+
["string-literal", [
42+
["string", "r''''''"]
43+
]],
44+
45+
["string-literal", [
46+
["string", "\"fo\\\"o\""]
47+
]],
48+
["string-literal", [
49+
["string", "'fo\\'o'"]
50+
]],
51+
52+
["string-literal", [
53+
["string", "\"\"\"foo\r\nbar\"\"\""]
54+
]],
55+
56+
["string-literal", [
57+
["string", "'''foo\r\nbar'''"]
58+
]],
59+
60+
["string-literal", [
61+
["string", "'"],
62+
["interpolation", [
63+
["punctuation", "$"],
64+
["expression", ["string"]]
65+
]],
66+
["string", " has "],
67+
["interpolation", [
68+
["punctuation", "${"],
69+
["expression", [
70+
"string",
71+
["punctuation", "."],
72+
"length"
73+
]],
74+
["punctuation", "}"]
75+
]],
76+
["string", " letters'"]
77+
]],
78+
["string-literal", [
79+
["string", "\"cookie has "],
80+
["interpolation", [
81+
["punctuation", "${"],
82+
["expression", [
83+
"cookie",
84+
["punctuation", "."],
85+
"number_of_chips"
86+
]],
87+
["punctuation", "}"]
88+
]],
89+
["string", " chips\""]
90+
]]
2091
]
2192

2293
----------------------------------------------------
2394

2495
Checks for single quoted and double quoted strings,
25-
multi-line strings and "raw" strings.
96+
multi-line strings and "raw" strings.

0 commit comments

Comments
 (0)
Please sign in to comment.