Skip to content

Commit

Permalink
Dart: Added string interpolation and improved metadata (#3197)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Nov 22, 2021
1 parent 86028ad commit e137035
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 26 deletions.
36 changes: 24 additions & 12 deletions components/prism-dart.js
Expand Up @@ -22,16 +22,6 @@
};

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,
{
Expand All @@ -46,10 +36,32 @@
'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
});

Prism.languages.insertBefore('dart', 'function', {
Prism.languages.insertBefore('dart', 'string', {
'string-literal': {
pattern: /r?(?:("""|''')[\s\S]*?\1|(["'])(?:\\.|(?!\2)[^\\\r\n])*\2(?!\2))/,
greedy: true,
inside: {
'interpolation': {
pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
lookbehind: true,
inside: {
'punctuation': /^\$\{?|\}$/,
'expression': {
pattern: /[\s\S]+/,
inside: Prism.languages.dart
}
}
},
'string': /[\s\S]+/
}
},
'string': undefined
});

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

Expand Down
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.

140 changes: 134 additions & 6 deletions tests/languages/dart/metadata_feature.test
@@ -1,20 +1,148 @@
@deprecated
@override
@todo('seth', 'make this do something')
@ToDo('seth', 'make this do something')

@DataTable("sale_orders")
class SaleOrder {
@DataColumn("sale_order_date")
DateTime date;
}

@table
class Product {
@column
String name;
}

const DataColumn column = const DataColumn();

const DataTable table = const DataTable();

class DataTable {
final String name;

const DataTable([this.name]);
}

class DataColumn {
final String name;

const DataColumn([this.name]);
}

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

[
["metadata", "@deprecated"],

["metadata", "@override"],
["metadata", "@todo"],

["metadata", "@ToDo"],
["punctuation", "("],
["string", "'seth'"],
["string-literal", [
["string", "'seth'"]
]],
["punctuation", ","],
["string", "'make this do something'"],
["punctuation", ")"]
["string-literal", [
["string", "'make this do something'"]
]],
["punctuation", ")"],

["metadata", "@DataTable"],
["punctuation", "("],
["string-literal", [
["string", "\"sale_orders\""]
]],
["punctuation", ")"],

["keyword", "class"],
["class-name", ["SaleOrder"]],
["punctuation", "{"],

["metadata", "@DataColumn"],
["punctuation", "("],
["string-literal", [
["string", "\"sale_order_date\""]
]],
["punctuation", ")"],

["class-name", ["DateTime"]],
" date",
["punctuation", ";"],

["punctuation", "}"],

["metadata", "@table"],
["keyword", "class"], ["class-name", ["Product"]], ["punctuation", "{"],
["metadata", "@column"],
["class-name", ["String"]], " name", ["punctuation", ";"],
["punctuation", "}"],

["keyword", "const"],
["class-name", ["DataColumn"]],
" column ",
["operator", "="],
["keyword", "const"],
["class-name", ["DataColumn"]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["keyword", "const"],
["class-name", ["DataTable"]],
" table ",
["operator", "="],
["keyword", "const"],
["class-name", ["DataTable"]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["keyword", "class"],
["class-name", ["DataTable"]],
["punctuation", "{"],

["keyword", "final"],
["class-name", ["String"]],
" name",
["punctuation", ";"],

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

["punctuation", "}"],

["keyword", "class"],
["class-name", ["DataColumn"]],
["punctuation", "{"],

["keyword", "final"],
["class-name", ["String"]],
" name",
["punctuation", ";"],

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

["punctuation", "}"]
]

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

Checks for metadata.
Checks for metadata.
85 changes: 78 additions & 7 deletions tests/languages/dart/string_feature.test
Expand Up @@ -8,18 +8,89 @@ bar"""
'''foo
bar'''

'$string has ${string.length} letters'
"cookie has ${cookie.number_of_chips} chips"

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

[
["string", "\"\""], ["string", "''"],
["string", "r\"\""], ["string", "r''"],
["string", "\"\"\"\"\"\""], ["string", "''''''"],
["string", "r\"\"\"\"\"\""], ["string", "r''''''"],
["string", "\"fo\\\"o\""], ["string", "'fo\\'o'"],
["string", "\"\"\"foo\r\nbar\"\"\""], ["string", "'''foo\r\nbar'''"]
["string-literal", [
["string", "\"\""]
]],
["string-literal", [
["string", "''"]
]],

["string-literal", [
["string", "r\"\""]
]],
["string-literal", [
["string", "r''"]
]],

["string-literal", [
["string", "\"\"\"\"\"\""]
]],
["string-literal", [
["string", "''''''"]
]],

["string-literal", [
["string", "r\"\"\"\"\"\""]
]],
["string-literal", [
["string", "r''''''"]
]],

["string-literal", [
["string", "\"fo\\\"o\""]
]],
["string-literal", [
["string", "'fo\\'o'"]
]],

["string-literal", [
["string", "\"\"\"foo\r\nbar\"\"\""]
]],

["string-literal", [
["string", "'''foo\r\nbar'''"]
]],

["string-literal", [
["string", "'"],
["interpolation", [
["punctuation", "$"],
["expression", ["string"]]
]],
["string", " has "],
["interpolation", [
["punctuation", "${"],
["expression", [
"string",
["punctuation", "."],
"length"
]],
["punctuation", "}"]
]],
["string", " letters'"]
]],
["string-literal", [
["string", "\"cookie has "],
["interpolation", [
["punctuation", "${"],
["expression", [
"cookie",
["punctuation", "."],
"number_of_chips"
]],
["punctuation", "}"]
]],
["string", " chips\""]
]]
]

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

Checks for single quoted and double quoted strings,
multi-line strings and "raw" strings.
multi-line strings and "raw" strings.

0 comments on commit e137035

Please sign in to comment.