-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathsyntax_highlighter.dart
More file actions
293 lines (263 loc) · 9.56 KB
/
Copy pathsyntax_highlighter.dart
File metadata and controls
293 lines (263 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2021 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'dart:collection';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:logging/logging.dart';
import '../../shared/primitives/utils.dart';
import '../../shared/ui/colors.dart';
import 'span_parser.dart';
final _log = Logger('syntax_highlighter');
class SyntaxHighlighter {
SyntaxHighlighter({String? source}) : source = source ?? '';
@visibleForTesting
SyntaxHighlighter.withGrammar({Grammar? grammar, String? source})
: source = source ?? '' {
_grammar = grammar;
}
final String source;
late String _processedSource;
final _spanStack = ListQueue<ScopeSpan>();
int _currentPosition = 0;
late Map<String, TextStyle> _scopeStyles;
static Grammar? _grammar;
static Future<void> initialize() async {
if (_grammar == null) {
// Ensure the framework is initialized before trying to load the syntax
// from the bundled assets.
WidgetsFlutterBinding.ensureInitialized();
final grammarJson = json.decode(
await rootBundle.loadString('assets/dart_syntax.json'),
);
try {
_grammar = Grammar.fromJson(grammarJson);
} catch (error) {
// Safari does not support negative-lookbehind regex which are currently
// required by the syntax highlighting. An unhandled exception here will
// prevent DevTools initializing, so just print the error and leave
// syntax highlighting disabled if this happens.
_log.warning(
'Failed to load Dart Syntax Highlighting:\n'
'$error',
);
}
}
}
/// Returns the highlighted [source] in [TextSpan] form.
///
/// If [lineRange] is provided, only the lines between
/// `[lineRange.begin, lineRange.end]` will be returned.
TextSpan highlight(BuildContext context, {LineRange? lineRange}) {
// Generate the styling for the various scopes based on the current theme.
_scopeStyles = _buildSyntaxColorTable(Theme.of(context));
_currentPosition = 0;
_processedSource = source;
if (lineRange != null) {
_processedSource = _processedSource
.split('\n')
.sublist(lineRange.begin - 1, lineRange.end)
.join('\n');
}
final grammar = _grammar;
if (grammar == null) {
return TextSpan(text: _processedSource);
}
return TextSpan(
children: _highlightLoopHelper(
currentScope: null,
loopCondition: () => _currentPosition < _processedSource.length,
scopes: SpanParser.parse(grammar, _processedSource),
),
);
}
/// Returns the [TextStyle] for the current span based on the current scopes.
///
/// If there are multiple scopes for a span, styling for each scope is
/// applied in the order the scopes are listed (i.e., later scope styles take
/// precedence).
TextStyle _getStyleForSpan() {
if (_spanStack.isEmpty) {
return const TextStyle();
}
final scopes = _spanStack.last.scopes;
if (scopes.isEmpty) {
return const TextStyle();
} else if (scopes.length == 1) {
return _scopeStyles[scopes.first] ?? const TextStyle();
} else {
var style = const TextStyle();
for (final scope in scopes) {
style = style.merge(_scopeStyles[scope] ?? const TextStyle());
}
return style;
}
}
/// Enters a new scope for a span of text. Returns a [List<TextSpan>]
/// containing the stylized text from within the scope.
List<TextSpan> _scope(ScopeSpan currentScope, List<ScopeSpan> scopes) {
return _highlightLoopHelper(
currentScope: currentScope,
loopCondition: () => currentScope.contains(_currentPosition),
scopes: scopes,
);
}
List<TextSpan> _highlightLoopHelper({
required ScopeSpan? currentScope,
required bool Function() loopCondition,
required List<ScopeSpan> scopes,
}) {
final sourceSpans = <TextSpan>[];
int? currentScopeBegin = _currentPosition;
if (currentScope != null) {
_spanStack.addLast(currentScope);
}
while (loopCondition()) {
if (scopes.isNotEmpty && scopes.first.contains(_currentPosition)) {
// Encountered the next scoped span. Close the current span and enter
// the next.
final text = _processedSource.substring(
currentScopeBegin!,
_currentPosition,
);
if (text.isNotEmpty) {
sourceSpans.add(TextSpan(style: _getStyleForSpan(), text: text));
}
sourceSpans.addAll(_scope(scopes.removeAt(0), scopes));
// Reset the beginning of the current span to the first position after
// the close of the span that was just processed.
currentScopeBegin = _currentPosition;
} else if (_atNewline()) {
currentScopeBegin = _processNewlines(sourceSpans, currentScopeBegin!);
} else {
++_currentPosition;
}
}
// Reached the end of the text covered by the current span. Close the span
// and exit the scope.
final text = _processedSource.substring(
currentScopeBegin!,
_currentPosition,
);
if (text.isNotEmpty) {
sourceSpans.add(TextSpan(style: _getStyleForSpan(), text: text));
}
if (currentScope != null) {
_spanStack.removeLast();
}
return sourceSpans;
}
bool _atNewline() =>
String.fromCharCode(_processedSource.codeUnitAt(_currentPosition)) ==
'\n';
int? _processNewlines(List<TextSpan> sourceSpans, int currentScopeBegin) {
final text = _processedSource.substring(
currentScopeBegin,
_currentPosition,
);
if (text.isNotEmpty) {
sourceSpans.add(
TextSpan(
style: _getStyleForSpan(),
text: _processedSource.substring(currentScopeBegin, _currentPosition),
),
);
}
// We artificially break up spans if they contain a newline so it's easier
// to find line boundaries when we try and populate the code view.
do {
sourceSpans.add(const TextSpan(text: '\n'));
++_currentPosition;
} while ((_currentPosition < _processedSource.length) &&
(String.fromCharCode(_processedSource.codeUnitAt(_currentPosition)) ==
'\n'));
return _currentPosition;
}
Map<String, TextStyle> _buildSyntaxColorTable(ThemeData theme) {
final commentStyle = TextStyle(color: theme.colorScheme.commentSyntaxColor);
final functionStyle = TextStyle(
color: theme.colorScheme.functionSyntaxColor,
);
final declarationStyle = TextStyle(
color: theme.colorScheme.declarationsSyntaxColor,
);
final modifierStyle = TextStyle(
color: theme.colorScheme.modifierSyntaxColor,
);
final controlFlowStyle = TextStyle(
color: theme.colorScheme.controlFlowSyntaxColor,
);
final variableStyle = TextStyle(
color: theme.colorScheme.variableSyntaxColor,
);
final stringStyle = TextStyle(color: theme.colorScheme.stringSyntaxColor);
final numericConstantStyle = TextStyle(
color: theme.colorScheme.numericConstantSyntaxColor,
);
// Note: these scopes are defined in assets/dart_syntax.json
const modifierScopes = <String>[
'constant.language.dart',
'keyword.cast.dart',
'keyword.declaration.dart',
'keyword.other.import.dart',
'storage.modifier.dart',
'storage.type.annotation.dart',
'storage.type.primitive.dart',
];
const commentScopes = <String>[
'comment.block.dart',
'comment.block.documentation.dart',
'comment.block.empty.dart',
'comment.line.double-slash.dart',
];
const declarationScopes = <String>[
'support.class.dart',
'variable.language.dart',
];
const numericConstantScopes = <String>['constant.numeric.dart'];
const functionScopes = <String>['entity.name.function.dart'];
const controlFlowScopes = <String>[
'keyword.control.catch-exception.dart',
'keyword.control.dart',
'keyword.control.return.dart',
// While 'new' is not a control flow keyword, it uses the control flow
// color scheme so we include it here.
'keyword.control.new.dart',
];
const stringScopes = <String>[
'string.interpolated.double.dart',
'string.interpolated.single.dart',
'string.interpolated.triple.double.dart',
'string.interpolated.triple.single.dart',
'string.quoted.double.dart',
'string.quoted.single.dart',
'string.quoted.triple.double.dart',
'string.quoted.triple.single.dart',
];
const variableScopes = <String>[
// DartDoc code reference
'variable.name.source.dart',
// DartDoc in-line code
'variable.other.source.dart',
// Highlights code in strings (e.g., '$foo' or '${foo.bar()}')
'variable.parameter.dart',
];
Map<String, TextStyle> scopeTextStyleMapper(
List<String> scopes,
TextStyle style,
) {
return {for (final scope in scopes) scope: style};
}
return <String, TextStyle>{
...scopeTextStyleMapper(modifierScopes, modifierStyle),
...scopeTextStyleMapper(commentScopes, commentStyle),
...scopeTextStyleMapper(declarationScopes, declarationStyle),
...scopeTextStyleMapper(numericConstantScopes, numericConstantStyle),
...scopeTextStyleMapper(functionScopes, functionStyle),
...scopeTextStyleMapper(controlFlowScopes, controlFlowStyle),
...scopeTextStyleMapper(stringScopes, stringStyle),
...scopeTextStyleMapper(variableScopes, variableStyle),
};
}
}