Skip to content

Commit d0bcd07

Browse files
authored
Dart: Improved support for classes & generics (#2810)
1 parent 933af80 commit d0bcd07

File tree

5 files changed

+167
-35
lines changed

5 files changed

+167
-35
lines changed

components/prism-dart.js

+65-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
1-
Prism.languages.dart = Prism.languages.extend('clike', {
2-
'string': [
3-
{
4-
pattern: /r?("""|''')[\s\S]*?\1/,
5-
greedy: true
6-
},
7-
{
8-
pattern: /r?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
9-
greedy: true
10-
}
11-
],
12-
'keyword': [
1+
(function (Prism) {
2+
var keywords = [
133
/\b(?:async|sync|yield)\*/,
14-
/\b(?:abstract|assert|async|await|break|case|catch|class|const|continue|covariant|default|deferred|do|dynamic|else|enum|export|extension|external|extends|factory|final|finally|for|Function|get|hide|if|implements|interface|import|in|library|mixin|new|null|on|operator|part|rethrow|return|set|show|static|super|switch|sync|this|throw|try|typedef|var|void|while|with|yield)\b/
15-
],
16-
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
17-
});
4+
/\b(?:abstract|assert|async|await|break|case|catch|class|const|continue|covariant|default|deferred|do|dynamic|else|enum|export|extension|external|extends|factory|final|finally|for|get|hide|if|implements|interface|import|in|library|mixin|new|null|on|operator|part|rethrow|return|set|show|static|super|switch|sync|this|throw|try|typedef|var|void|while|with|yield)\b/
5+
];
6+
7+
// Handles named imports, such as http.Client
8+
var packagePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
9+
10+
// based on the dart naming conventions
11+
var className = {
12+
pattern: RegExp(packagePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
13+
lookbehind: true,
14+
inside: {
15+
'namespace': {
16+
pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
17+
inside: {
18+
'punctuation': /\./
19+
}
20+
},
21+
}
22+
};
1823

19-
Prism.languages.insertBefore('dart','function',{
20-
'metadata': {
21-
pattern: /@\w+/,
22-
alias: 'symbol'
23-
}
24-
});
24+
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+
],
35+
'class-name': [
36+
className,
37+
{
38+
// variables and parameters
39+
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
40+
pattern: RegExp(packagePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
41+
lookbehind: true,
42+
inside: className.inside
43+
}
44+
],
45+
'keyword': keywords,
46+
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
47+
});
48+
49+
Prism.languages.insertBefore('dart','function',{
50+
'metadata': {
51+
pattern: /@\w+/,
52+
alias: 'symbol'
53+
}
54+
});
55+
56+
Prism.languages.insertBefore('dart','class-name',{
57+
'generics': {
58+
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
59+
inside: {
60+
'class-name': className,
61+
'keyword': keywords,
62+
'punctuation': /[<>(),.:]/,
63+
'operator': /[?&|]/
64+
}
65+
},
66+
});
67+
}(Prism));

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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
class Foo with ns.Bar {
2+
const Foo(this.bar);
3+
4+
final Bar bar;
5+
6+
Baz<ns.Bat> baz(ns.Bat bat) {
7+
return Baz<ns.Bat>(bat);
8+
}
9+
10+
}
11+
12+
----------------------------------------------------
13+
14+
[
15+
["keyword", "class"],
16+
["class-name", ["Foo"]],
17+
["keyword", "with"],
18+
["class-name", [
19+
["namespace", [
20+
"ns",
21+
["punctuation", "."]
22+
]],
23+
"Bar"
24+
]],
25+
["punctuation", "{"],
26+
27+
["keyword", "const"],
28+
["class-name", ["Foo"]],
29+
["punctuation", "("],
30+
["keyword", "this"],
31+
["punctuation", "."],
32+
"bar",
33+
["punctuation", ")"],
34+
["punctuation", ";"],
35+
36+
["keyword", "final"],
37+
["class-name", ["Bar"]],
38+
" bar",
39+
["punctuation", ";"],
40+
41+
["class-name", ["Baz"]],
42+
["generics", [
43+
["punctuation", "<"],
44+
["class-name", [
45+
["namespace", [
46+
"ns",
47+
["punctuation", "."]
48+
]],
49+
"Bat"
50+
]],
51+
["punctuation", ">"]
52+
]],
53+
["function", "baz"],
54+
["punctuation", "("],
55+
["class-name", [
56+
["namespace", [
57+
"ns",
58+
["punctuation", "."]
59+
]],
60+
"Bat"
61+
]],
62+
" bat",
63+
["punctuation", ")"],
64+
["punctuation", "{"],
65+
["keyword", "return"],
66+
["class-name", [
67+
"Baz"
68+
]],
69+
["generics", [
70+
["punctuation", "<"],
71+
["class-name", [
72+
["namespace", [
73+
"ns",
74+
["punctuation", "."]
75+
]],
76+
"Bat"
77+
]],
78+
["punctuation", ">"]
79+
]],
80+
["punctuation", "("],
81+
"bat",
82+
["punctuation", ")"],
83+
["punctuation", ";"],
84+
["punctuation", "}"],
85+
86+
["punctuation", "}"]
87+
]
88+
89+
----------------------------------------------------
90+
91+
Checks class names and generics

tests/languages/dart/keyword_feature.test

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ continue covariant default deferred
77
do dynamic else enum
88
export extension external
99
extends;
10-
factory final finally for Function
10+
factory final finally for
1111
get hide if
1212
implements;
1313
interface;
@@ -34,7 +34,7 @@ void while with yield
3434
["keyword", "do"], ["keyword", "dynamic"], ["keyword", "else"], ["keyword", "enum"],
3535
["keyword", "export"], ["keyword", "extension"], ["keyword", "external"],
3636
["keyword", "extends"], ["punctuation", ";"],
37-
["keyword", "factory"], ["keyword", "final"], ["keyword", "finally"], ["keyword", "for"], ["keyword", "Function"],
37+
["keyword", "factory"], ["keyword", "final"], ["keyword", "finally"], ["keyword", "for"],
3838
["keyword", "get"], ["keyword", "hide"], ["keyword", "if"],
3939
["keyword", "implements"], ["punctuation", ";"],
4040
["keyword", "interface"], ["punctuation", ";"],
@@ -51,4 +51,4 @@ void while with yield
5151

5252
----------------------------------------------------
5353

54-
Checks for all keywords.
54+
Checks for all keywords.
+7-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
++ --
22
* / % ~/
33
+ - ! ~
4-
<< >> ?
5-
& ^ |
6-
>= > <= <
4+
< << <= <<=
5+
> >> >= >>=
6+
& ^ | ?
77
as is is!
88
== != && ||
99
= *= /= ~/=
1010
%= += -=
11-
<<= >>=
1211
&= ^= |=
1312

1413
----------------------------------------------------
@@ -17,17 +16,16 @@ as is is!
1716
["operator", "++"], ["operator", "--"],
1817
["operator", "*"], ["operator", "/"], ["operator", "%"], ["operator", "~/"],
1918
["operator", "+"], ["operator", "-"], ["operator", "!"], ["operator", "~"],
20-
["operator", "<<"], ["operator", ">>"], ["operator", "?"],
21-
["operator", "&"], ["operator", "^"], ["operator", "|"],
22-
["operator", ">="], ["operator", ">"], ["operator", "<="], ["operator", "<"],
19+
["operator", "<"], ["operator", "<<"], ["operator", "<="], ["operator", "<<="],
20+
["operator", ">"], ["operator", ">>"], ["operator", ">="], ["operator", ">>="],
21+
["operator", "&"], ["operator", "^"], ["operator", "|"], ["operator", "?"],
2322
["operator", "as"], ["operator", "is"], ["operator", "is!"],
2423
["operator", "=="], ["operator", "!="], ["operator", "&&"], ["operator", "||"],
2524
["operator", "="], ["operator", "*="], ["operator", "/="], ["operator", "~/="],
2625
["operator", "%="], ["operator", "+="], ["operator", "-="],
27-
["operator", "<<="], ["operator", ">>="],
2826
["operator", "&="], ["operator", "^="], ["operator", "|="]
2927
]
3028

3129
----------------------------------------------------
3230

33-
Checks for all operators.
31+
Checks for all operators.

0 commit comments

Comments
 (0)