Skip to content

Commit 5b4dc61

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes coloring for record fields access in extension
Fixes: #60625 Change-Id: Idce84fbdc9b58b2e91c2e40a058bf466e8d5fb65 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426342 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 07680e9 commit 5b4dc61

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import 'package:analyzer/source/source_range.dart';
2828
import 'package:analyzer/src/dart/ast/ast.dart';
2929
import 'package:analyzer/src/dart/ast/extensions.dart';
3030
import 'package:analyzer/src/dart/element/extensions.dart';
31+
import 'package:analyzer/src/utilities/extensions/ast.dart';
3132
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
3233

3334
/// A computer for [HighlightRegion]s and LSP [SemanticTokenInfo] in a Dart [CompilationUnit].
@@ -292,11 +293,16 @@ class DartUnitHighlightsComputer {
292293
} else {
293294
type = HighlightRegionType.INSTANCE_SETTER_REFERENCE;
294295
}
295-
} else if (element == null &&
296-
parent is PropertyAccess &&
297-
nameToken == parent.propertyName.token) {
296+
} else if (element == null) {
297+
DartType? staticType;
298+
if (parent is PropertyAccess && nameToken == parent.propertyName.token) {
299+
staticType = parent.realTarget.staticType;
300+
} else if (parent.enclosingInstanceElement case ExtensionElement(
301+
:var extendedType,
302+
) when parent is! PrefixedIdentifier) {
303+
staticType = extendedType;
304+
}
298305
// Handle tokens that are references to record fields.
299-
var staticType = parent.realTarget.staticType;
300306
if (staticType is RecordType) {
301307
type =
302308
staticType.fieldByName(nameToken.lexeme) != null

pkg/analysis_server/test/lsp/semantic_tokens_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,34 @@ f({String? a, dynamic b}) {
14921492
await _initializeAndVerifyTokens(content, expected);
14931493
}
14941494

1495+
Future<void> test_namedRecordFields_extension() async {
1496+
var content = '''
1497+
extension on ({int field,}) {
1498+
get other => field + this.field;
1499+
}
1500+
''';
1501+
1502+
var expected = [
1503+
_Token('extension', SemanticTokenTypes.keyword),
1504+
_Token('on', SemanticTokenTypes.keyword),
1505+
_Token('int', SemanticTokenTypes.class_),
1506+
_Token('get', SemanticTokenTypes.keyword),
1507+
_Token('other', SemanticTokenTypes.property, [
1508+
SemanticTokenModifiers.declaration,
1509+
CustomSemanticTokenModifiers.instance,
1510+
]),
1511+
_Token('field', SemanticTokenTypes.property, [
1512+
CustomSemanticTokenModifiers.instance,
1513+
]),
1514+
_Token('this', SemanticTokenTypes.keyword),
1515+
_Token('field', SemanticTokenTypes.property, [
1516+
CustomSemanticTokenModifiers.instance,
1517+
]),
1518+
];
1519+
1520+
await _initializeAndVerifyTokens(content, expected);
1521+
}
1522+
14951523
Future<void> test_never() async {
14961524
var content = '''
14971525
Never f() => throw '';
@@ -1847,6 +1875,34 @@ void f() {
18471875
await _initializeAndVerifyTokens(content, expected);
18481876
}
18491877

1878+
Future<void> test_positionalRecordFields_extension() async {
1879+
var content = r'''
1880+
extension on (int field, double,) {
1881+
get other => $1 + $2;
1882+
}
1883+
''';
1884+
1885+
var expected = [
1886+
_Token('extension', SemanticTokenTypes.keyword),
1887+
_Token('on', SemanticTokenTypes.keyword),
1888+
_Token('int', SemanticTokenTypes.class_),
1889+
_Token('double', SemanticTokenTypes.class_),
1890+
_Token('get', SemanticTokenTypes.keyword),
1891+
_Token('other', SemanticTokenTypes.property, [
1892+
SemanticTokenModifiers.declaration,
1893+
CustomSemanticTokenModifiers.instance,
1894+
]),
1895+
_Token(r'$1', SemanticTokenTypes.property, [
1896+
CustomSemanticTokenModifiers.instance,
1897+
]),
1898+
_Token(r'$2', SemanticTokenTypes.property, [
1899+
CustomSemanticTokenModifiers.instance,
1900+
]),
1901+
];
1902+
1903+
await _initializeAndVerifyTokens(content, expected);
1904+
}
1905+
18501906
Future<void> test_range() async {
18511907
var content = '''
18521908
/// class docs

0 commit comments

Comments
 (0)