Skip to content

Commit a33a747

Browse files
authored
Don't fail at startup if textmate grammar can't be parsed by browser (flutter#4938)
Fixes flutter#2856.
1 parent d695482 commit a33a747

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/devtools_app/lib/src/screens/debugger/syntax_highlighter.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:convert';
88
import 'package:flutter/material.dart';
99
import 'package:flutter/services.dart' show rootBundle;
1010

11+
import '../../shared/config_specific/logger/logger.dart';
1112
import '../../shared/primitives/utils.dart';
1213
import '../../shared/theme.dart';
1314
import 'span_parser.dart';
@@ -41,7 +42,19 @@ class SyntaxHighlighter {
4142
final grammarJson = json.decode(
4243
await rootBundle.loadString('assets/dart_syntax.json'),
4344
);
44-
_grammar = Grammar.fromJson(grammarJson);
45+
try {
46+
_grammar = Grammar.fromJson(grammarJson);
47+
} catch (error) {
48+
// Safari does not support negative-lookbehind regex which are currently
49+
// required by the syntax highlighting. An unhandled exception here will
50+
// prevent DevTools initializing, so just print the error and leave
51+
// syntax highlighting disabled if this happens.
52+
log(
53+
'Failed to load Dart Syntax Highlighting:\n'
54+
'$error',
55+
LogLevel.warning,
56+
);
57+
}
4558
}
4659
}
4760

@@ -60,11 +73,15 @@ class SyntaxHighlighter {
6073
.sublist(lineRange.begin - 1, lineRange.end)
6174
.join('\n');
6275
}
76+
final grammar = _grammar;
77+
if (grammar == null) {
78+
return TextSpan(text: _processedSource);
79+
}
6380
return TextSpan(
6481
children: _highlightLoopHelper(
6582
currentScope: null,
6683
loopCondition: () => _currentPosition < _processedSource.length,
67-
scopes: SpanParser.parse(_grammar!, _processedSource),
84+
scopes: SpanParser.parse(grammar, _processedSource),
6885
),
6986
);
7087
}

0 commit comments

Comments
 (0)