Skip to content

Commit b4046ce

Browse files
authored
enh(dart) highlight built-in nullable types (#2598)
* Dart: allow built-in nullable types with trailing ? to be highlighted
1 parent a010c15 commit b4046ce

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Language Improvements:
4444
- fix(typescript) `=>` function with nested `()` in params now works (#2502) [Josh Goebel][]
4545
- fix(yaml) Fix tags to include non-word characters (#2486) [Peter Plantinga][]
4646
- fix(swift) `@objcMembers` was being partially highlighted (#2543) [Nick Randall][]
47-
- enh(dart) Add `late` and `required` keywords, and `Never` built-in type (#2550) [Sam Rawlins][]
47+
- enh(dart) Add `late` and `required` keywords, the `Never` built-in type, and nullable built-in types (#2550) [Sam Rawlins][]
4848
- enh(erlang) Add underscore separators to numeric literals (#2554) [Sergey Prokhorov][]
4949
- enh(handlebars) Support for sub-expressions, path-expressions, hashes, block-parameters and literals (#2344) [Nils Knappmeier][]
5050
- enh(protobuf) Support multiline comments (#2597) [Pavel Evstigneev][]

src/languages/dart.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ Category: scripting
88
*/
99

1010
export default function(hljs) {
11-
var SUBST = {
11+
const SUBST = {
1212
className: 'subst',
1313
variants: [{
1414
begin: '\\$[A-Za-z0-9_]+'
1515
}],
1616
};
1717

18-
var BRACED_SUBST = {
18+
const BRACED_SUBST = {
1919
className: 'subst',
2020
variants: [{
2121
begin: '\\${',
2222
end: '}'
23-
}, ],
23+
}],
2424
keywords: 'true false null this is new super',
2525
};
2626

27-
var STRING = {
27+
const STRING = {
2828
className: 'string',
2929
variants: [{
3030
begin: 'r\'\'\'',
@@ -72,17 +72,59 @@ export default function(hljs) {
7272
hljs.C_NUMBER_MODE, STRING
7373
];
7474

75-
var KEYWORDS = {
75+
const BUILT_IN_TYPES = [
76+
// dart:core
77+
'Comparable',
78+
'DateTime',
79+
'Duration',
80+
'Function',
81+
'Iterable',
82+
'Iterator',
83+
'List',
84+
'Map',
85+
'Match',
86+
'Object',
87+
'Pattern',
88+
'RegExp',
89+
'Set',
90+
'Stopwatch',
91+
'String',
92+
'StringBuffer',
93+
'StringSink',
94+
'Symbol',
95+
'Type',
96+
'Uri',
97+
'bool',
98+
'double',
99+
'int',
100+
'num',
101+
// dart:html
102+
'Element',
103+
'ElementList',
104+
];
105+
const NULLABLE_BUILT_IN_TYPES = BUILT_IN_TYPES.map((e) => `${e}?`);
106+
107+
const KEYWORDS = {
76108
keyword: 'abstract as assert async await break case catch class const continue covariant default deferred do ' +
77109
'dynamic else enum export extends extension external factory false final finally for Function get hide if ' +
78110
'implements import in inferface is late library mixin new null on operator part required rethrow return set ' +
79111
'show static super switch sync this throw true try typedef var void while with yield',
80112
built_in:
81-
// dart:core
82-
'Comparable DateTime Duration Function Iterable Iterator List Map Match Never Null Object Pattern RegExp ' +
83-
'Set Stopwatch String StringBuffer StringSink Symbol Type Uri bool double dynamic int num print ' +
84-
// dart:html
85-
'Element ElementList document querySelector querySelectorAll window'
113+
BUILT_IN_TYPES
114+
.concat(NULLABLE_BUILT_IN_TYPES)
115+
.concat([
116+
// dart:core
117+
'Never',
118+
'Null',
119+
'dynamic',
120+
'print',
121+
// dart:html
122+
'document',
123+
'querySelector',
124+
'querySelectorAll',
125+
'window',
126+
]).join(' '),
127+
$pattern: /[A-Za-z][A-Za-z0-9_]*\??/
86128
};
87129

88130
return {
@@ -130,5 +172,5 @@ export default function(hljs) {
130172
begin: '=>' // No markup, just a relevance booster
131173
}
132174
]
133-
}
175+
};
134176
}

0 commit comments

Comments
 (0)