Skip to content

Commit

Permalink
Improve support for Dart classes & generics
Browse files Browse the repository at this point in the history
  • Loading branch information
brianegan committed Mar 17, 2021
1 parent 933af80 commit 7b42bc5
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 35 deletions.
87 changes: 65 additions & 22 deletions components/prism-dart.js
@@ -1,24 +1,67 @@
Prism.languages.dart = Prism.languages.extend('clike', {
'string': [
{
pattern: /r?("""|''')[\s\S]*?\1/,
greedy: true
},
{
pattern: /r?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
greedy: true
}
],
'keyword': [
(function (Prism) {
var keywords = [
/\b(?:async|sync|yield)\*/,
/\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/
],
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
});
/\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/
];

// Handles named imports, such as http.Client
var packagePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;

// based on the dart naming conventions
var className = {
pattern: RegExp(packagePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
lookbehind: true,
inside: {
'namespace': {
pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
inside: {
'punctuation': /\./
}
},
}
};

Prism.languages.insertBefore('dart','function',{
'metadata': {
pattern: /@\w+/,
alias: 'symbol'
}
});
Prism.languages.dart = Prism.languages.extend('clike', {
'string': [
{
pattern: /r?("""|''')[\s\S]*?\1/,
greedy: true
},
{
pattern: /r?(["'])(?:\\.|(?!\1)[^\\\r\n])*\1/,
greedy: true
}
],
'class-name': [
className,
{
// variables and parameters
// this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
pattern: RegExp(packagePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
lookbehind: true,
inside: className.inside
}
],
'keyword': keywords,
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
});

Prism.languages.insertBefore('dart','function',{
'metadata': {
pattern: /@\w+/,
alias: 'symbol'
}
});

Prism.languages.insertBefore('dart','class-name',{
'generics': {
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
inside: {
'class-name': className,
'keyword': keywords,
'punctuation': /[<>(),.:]/,
'operator': /[?&|]/
}
},
});
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-dart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions tests/languages/dart/class-name_feature.test
@@ -0,0 +1,91 @@
class Foo with ns.Bar {
const Foo(this.bar);

final Bar bar;

Baz<ns.Bat> baz(ns.Bat bat) {
return Baz<ns.Bat>(bat);
}

}

----------------------------------------------------

[
["keyword", "class"],
["class-name", ["Foo"]],
["keyword", "with"],
["class-name", [
["namespace", [
"ns",
["punctuation", "."]
]],
"Bar"
]],
["punctuation", "{"],

["keyword", "const"],
["class-name", ["Foo"]],
["punctuation", "("],
["keyword", "this"],
["punctuation", "."],
"bar",
["punctuation", ")"],
["punctuation", ";"],

["keyword", "final"],
["class-name", ["Bar"]],
" bar",
["punctuation", ";"],

["class-name", ["Baz"]],
["generics", [
["punctuation", "<"],
["class-name", [
["namespace", [
"ns",
["punctuation", "."]
]],
"Bat"
]],
["punctuation", ">"]
]],
["function", "baz"],
["punctuation", "("],
["class-name", [
["namespace", [
"ns",
["punctuation", "."]
]],
"Bat"
]],
" bat",
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["class-name", [
"Baz"
]],
["generics", [
["punctuation", "<"],
["class-name", [
["namespace", [
"ns",
["punctuation", "."]
]],
"Bat"
]],
["punctuation", ">"]
]],
["punctuation", "("],
"bat",
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],

["punctuation", "}"]
]

----------------------------------------------------

Checks class names and generics
6 changes: 3 additions & 3 deletions tests/languages/dart/keyword_feature.test
Expand Up @@ -7,7 +7,7 @@ continue covariant default deferred
do dynamic else enum
export extension external
extends;
factory final finally for Function
factory final finally for
get hide if
implements;
interface;
Expand All @@ -34,7 +34,7 @@ void while with yield
["keyword", "do"], ["keyword", "dynamic"], ["keyword", "else"], ["keyword", "enum"],
["keyword", "export"], ["keyword", "extension"], ["keyword", "external"],
["keyword", "extends"], ["punctuation", ";"],
["keyword", "factory"], ["keyword", "final"], ["keyword", "finally"], ["keyword", "for"], ["keyword", "Function"],
["keyword", "factory"], ["keyword", "final"], ["keyword", "finally"], ["keyword", "for"],
["keyword", "get"], ["keyword", "hide"], ["keyword", "if"],
["keyword", "implements"], ["punctuation", ";"],
["keyword", "interface"], ["punctuation", ";"],
Expand All @@ -51,4 +51,4 @@ void while with yield

----------------------------------------------------

Checks for all keywords.
Checks for all keywords.
16 changes: 7 additions & 9 deletions tests/languages/dart/operator_feature.test
@@ -1,14 +1,13 @@
++ --
* / % ~/
+ - ! ~
<< >> ?
& ^ |
>= > <= <
< << <= <<=
> >> >= >>=
& ^ | ?
as is is!
== != && ||
= *= /= ~/=
%= += -=
<<= >>=
&= ^= |=

----------------------------------------------------
Expand All @@ -17,17 +16,16 @@ as is is!
["operator", "++"], ["operator", "--"],
["operator", "*"], ["operator", "/"], ["operator", "%"], ["operator", "~/"],
["operator", "+"], ["operator", "-"], ["operator", "!"], ["operator", "~"],
["operator", "<<"], ["operator", ">>"], ["operator", "?"],
["operator", "&"], ["operator", "^"], ["operator", "|"],
["operator", ">="], ["operator", ">"], ["operator", "<="], ["operator", "<"],
["operator", "<"], ["operator", "<<"], ["operator", "<="], ["operator", "<<="],
["operator", ">"], ["operator", ">>"], ["operator", ">="], ["operator", ">>="],
["operator", "&"], ["operator", "^"], ["operator", "|"], ["operator", "?"],
["operator", "as"], ["operator", "is"], ["operator", "is!"],
["operator", "=="], ["operator", "!="], ["operator", "&&"], ["operator", "||"],
["operator", "="], ["operator", "*="], ["operator", "/="], ["operator", "~/="],
["operator", "%="], ["operator", "+="], ["operator", "-="],
["operator", "<<="], ["operator", ">>="],
["operator", "&="], ["operator", "^="], ["operator", "|="]
]

----------------------------------------------------

Checks for all operators.
Checks for all operators.

0 comments on commit 7b42bc5

Please sign in to comment.