Skip to content

Commit 15cd98f

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
[Analyzer] Add a custom semantic tokens modifier to named parameter labels
Change-Id: I5b5e4030a17bd894872884960d11eaa748c12e5c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179769 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent 0345403 commit 15cd98f

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

pkg/analysis_server/lib/src/computer/computer_highlights.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,9 @@ class DartUnitHighlightsComputer {
380380
var type = node.inDeclarationContext()
381381
? HighlightRegionType.PARAMETER_DECLARATION
382382
: HighlightRegionType.PARAMETER_REFERENCE;
383-
return _addRegion_node(node, type);
383+
var modifiers =
384+
node.parent is Label ? {CustomSemanticTokenModifiers.label} : null;
385+
return _addRegion_node(node, type, semanticTokenModifiers: modifiers);
384386
}
385387

386388
bool _addIdentifierRegion_typeParameter(SimpleIdentifier node) {
@@ -459,10 +461,21 @@ class DartUnitHighlightsComputer {
459461
}
460462
}
461463

462-
bool _addRegion_node(AstNode node, HighlightRegionType type) {
464+
bool _addRegion_node(
465+
AstNode node,
466+
HighlightRegionType type, {
467+
SemanticTokenTypes semanticTokenType,
468+
Set<SemanticTokenModifiers> semanticTokenModifiers,
469+
}) {
463470
var offset = node.offset;
464471
var length = node.length;
465-
_addRegion(offset, length, type);
472+
_addRegion(
473+
offset,
474+
length,
475+
type,
476+
semanticTokenType: semanticTokenType,
477+
semanticTokenModifiers: semanticTokenModifiers,
478+
);
466479
return true;
467480
}
468481

pkg/analysis_server/lib/src/lsp/constants.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,23 @@ abstract class CustomMethods {
100100
}
101101

102102
abstract class CustomSemanticTokenModifiers {
103-
// A modifier applied to control keywords like if/for/etc. so they can be
104-
// coloured differently to other keywords (void, import, etc), matching the
105-
// original Dart textmate grammar.
106-
// https://github.com/dart-lang/dart-syntax-highlight/blob/84a8e84f79bc917ebd959a4587349c865dc945e0/grammars/dart.json#L244-L261
103+
/// A modifier applied to control keywords like if/for/etc. so they can be
104+
/// coloured differently to other keywords (void, import, etc), matching the
105+
/// original Dart textmate grammar.
106+
/// https://github.com/dart-lang/dart-syntax-highlight/blob/84a8e84f79bc917ebd959a4587349c865dc945e0/grammars/dart.json#L244-L261
107107
static const control = SemanticTokenModifiers('control');
108108

109+
/// A modifier applied to parameter references to indicate they are the name/label
110+
/// to allow theming them differently to the values. For example in the code
111+
/// `foo({String a}) => foo(a: a)` the a's will be differentiated as:
112+
/// - parameter.declaration
113+
/// - parameter.label
114+
/// - parameter
115+
static const label = SemanticTokenModifiers('label');
116+
109117
/// All custom semantic token modifiers, used to populate the LSP Legend which must
110118
/// include all used modifiers.
111-
static const values = [control];
119+
static const values = [control, label];
112120
}
113121

114122
abstract class CustomSemanticTokenTypes {

pkg/analysis_server/test/lsp/semantic_tokens_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,33 @@ class MyClass {}
600600
expect(decoded, equals(expected));
601601
}
602602

603+
Future<void> test_namedArguments() async {
604+
final content = '''
605+
f({String a}) {
606+
f(a: a);
607+
}
608+
''';
609+
610+
final expected = [
611+
_Token('f', SemanticTokenTypes.function,
612+
[SemanticTokenModifiers.declaration, SemanticTokenModifiers.static]),
613+
_Token('String', SemanticTokenTypes.class_),
614+
_Token('a', SemanticTokenTypes.parameter,
615+
[SemanticTokenModifiers.declaration]),
616+
_Token('f', SemanticTokenTypes.function),
617+
_Token('a', SemanticTokenTypes.parameter,
618+
[CustomSemanticTokenModifiers.label]),
619+
_Token('a', SemanticTokenTypes.parameter),
620+
];
621+
622+
await initialize();
623+
await openFile(mainFileUri, withoutMarkers(content));
624+
625+
final tokens = await getSemanticTokens(mainFileUri);
626+
final decoded = decodeSemanticTokens(content, tokens);
627+
expect(decoded, equals(expected));
628+
}
629+
603630
Future<void> test_range() async {
604631
final content = '''
605632
/// class docs

0 commit comments

Comments
 (0)