[analysis_server] Fix semantic tokens for method tear-offs in object patterns

Fixes https://github.com/dart-lang/sdk/issues/59976

Change-Id: I75127a59670522ddfc6526d10bfe9844638fdd99
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408001
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny
2025-02-07 12:24:25 -08:00
committed by Commit Queue
parent 46a739ce20
commit a78fb5e47a
2 changed files with 23 additions and 8 deletions
@@ -1497,15 +1497,19 @@ class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<void> {
}
@override
void visitPatternFieldName(PatternFieldName node) {
var name = node.name;
void visitPatternField(PatternField node) {
var name = node.name?.name;
if (name != null) {
computer._addRegion_token(
node.name,
HighlightRegionType.INSTANCE_GETTER_REFERENCE,
);
// Patterns can be method tear-offs as well as getters:
// https://github.com/dart-lang/sdk/issues/59976#issuecomment-2613558317
var type = switch (node.element2) {
MethodElement2() => HighlightRegionType.INSTANCE_METHOD_TEAR_OFF,
_ => HighlightRegionType.INSTANCE_GETTER_REFERENCE,
};
computer._addRegion_token(name, type);
}
super.visitPatternFieldName(node);
super.visitPatternField(node);
}
@override
@@ -1726,7 +1726,9 @@ void f() {
var content = r'''
void f() {
switch (1) {
case int(isEven: var isEven) when isEven:
case int(isEven: var isEven, toString: var toString) when isEven:
isEven;
toString;
}
}
''';
@@ -1754,10 +1756,19 @@ void f() {
_Token('isEven', SemanticTokenTypes.variable, [
SemanticTokenModifiers.declaration,
]),
_Token('toString', SemanticTokenTypes.method, [
CustomSemanticTokenModifiers.instance,
]),
_Token('var', SemanticTokenTypes.keyword),
_Token('toString', SemanticTokenTypes.variable, [
SemanticTokenModifiers.declaration,
]),
_Token('when', SemanticTokenTypes.keyword, [
CustomSemanticTokenModifiers.control,
]),
_Token('isEven', SemanticTokenTypes.variable),
_Token('isEven', SemanticTokenTypes.variable),
_Token('toString', SemanticTokenTypes.variable),
];
await _initializeAndVerifyTokens(content, expected);