Skip to content

Don't fail at startup if textmate grammar can't be parsed by browser #4938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;

import '../../shared/config_specific/logger/logger.dart';
import '../../shared/primitives/utils.dart';
import '../../shared/theme.dart';
import 'span_parser.dart';
Expand Down Expand Up @@ -41,7 +42,19 @@ class SyntaxHighlighter {
final grammarJson = json.decode(
await rootBundle.loadString('assets/dart_syntax.json'),
);
_grammar = Grammar.fromJson(grammarJson);
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(
'Failed to load Dart Syntax Highlighting:\n'
'$error',
LogLevel.warning,
);
}
}
}

Expand All @@ -60,11 +73,15 @@ class SyntaxHighlighter {
.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),
scopes: SpanParser.parse(grammar, _processedSource),
),
);
}
Expand Down