Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 7 additions & 9 deletions lib/core/theme/parser/token_style_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ class TokenStyleResolver {
_cache.putIfAbsent(scope, () => _resolveUncached(scope));

TextStyle _resolveUncached(String scope) {
for (final prefix in _scopePrefixes(scope)) {
// `a.b.c` -> `a.b` -> `a`: cut at the last dot instead of splitting into a
// list and joining sublists (#1530).
var prefix = scope;
while (true) {
for (final rule in _rules) {
if (rule.scopes.contains(prefix)) {
return _styleFromRule(rule);
}
}
final dot = prefix.lastIndexOf('.');
if (dot < 0) return _defaultStyle;
prefix = prefix.substring(0, dot);
}
return _defaultStyle;
}

List<String> _scopePrefixes(String scope) {
final parts = scope.split('.');
return [
for (var i = parts.length; i >= 1; i--) parts.sublist(0, i).join('.'),
];
}

TextStyle _styleFromRule(TokenColorRule rule) {
Expand Down
26 changes: 26 additions & 0 deletions test/core/theme/parser/token_style_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,30 @@ void main() {
final b = resolver.resolve('string.quoted.double');
expect(identical(a, b), isTrue);
});

test('walks prefixes by dot, including empty segments (#1530)', () {
const rules = [
TokenColorRule(scopes: ['a'], foreground: '#010101'),
TokenColorRule(scopes: ['a.b'], foreground: '#020202'),
TokenColorRule(scopes: ['a.'], foreground: '#030303'),
TokenColorRule(scopes: [''], foreground: '#040404'),
TokenColorRule(scopes: ['x.y.z'], foreground: '#050505'),
];
final resolver = TokenStyleResolver(
rules: rules,
defaultStyle: const TextStyle(color: Color(0xFFFFFFFF)),
);

// The longest prefix wins.
expect(resolver.resolve('a.b.c.d').color, const Color(0xFF020202));
expect(resolver.resolve('a.c.d').color, const Color(0xFF010101));
expect(resolver.resolve('x.y.z.w').color, const Color(0xFF050505));
// `a..b` -> `a.` -> `a`: the empty segment is a prefix of its own.
expect(resolver.resolve('a..b').color, const Color(0xFF030303));
// The leading dot gives the empty prefix; a bare empty scope is it too.
expect(resolver.resolve('.q').color, const Color(0xFF040404));
expect(resolver.resolve('').color, const Color(0xFF040404));
// Nothing matches: the default style.
expect(resolver.resolve('zzz.top').color, const Color(0xFFFFFFFF));
});
}
Loading