Skip to content

Commit 35f84de

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes navigation to pattern variable with multiple declarations
Bug: #60398 Change-Id: Ibdb1681075c4a097a50483628af50da055cc751d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417905 Reviewed-by: Samuel Rawlins <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 8bf801f commit 35f84de

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/analysis_server/test/lsp/definition_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,45 @@ void otherUnrelatedFunction() {}
874874
expect(loc.targetSelectionRange, equals(rangeOfString(partCode, 'add')));
875875
}
876876

877+
Future<void> test_patternVariable_ifCase_logicalOr() async {
878+
setLocationLinkSupport();
879+
880+
var code = TestCode.parse('''
881+
void f(Object? x) {
882+
if (x case int /*[0*//*0*/test/*0]*/ || [int /*[1*/test/*1]*/] when test > 0) {
883+
/*[2*//*1*/test/*2]*/ = 1;
884+
}
885+
}
886+
''', positionShorthand: false);
887+
888+
await initialize();
889+
await openFile(mainFileUri, code.code);
890+
891+
// Selecting on the first declaration of `test`
892+
var res = await getDefinitionAsLocationLinks(
893+
mainFileUri,
894+
code.positions.first.position,
895+
);
896+
expect(res, hasLength(2));
897+
for (var (index, loc) in res.indexed) {
898+
expect(loc.originSelectionRange, equals(code.ranges.first.range));
899+
expect(loc.targetRange, equals(code.ranges[index].range));
900+
expect(loc.targetSelectionRange, equals(code.ranges[index].range));
901+
}
902+
903+
// Selecting on the assignment of `test = 1`
904+
res = await getDefinitionAsLocationLinks(
905+
mainFileUri,
906+
code.positions.last.position,
907+
);
908+
expect(res, hasLength(2));
909+
for (var (index, loc) in res.indexed) {
910+
expect(loc.originSelectionRange, equals(code.ranges.last.range));
911+
expect(loc.targetRange, equals(code.ranges[index].range));
912+
expect(loc.targetSelectionRange, equals(code.ranges[index].range));
913+
}
914+
}
915+
877916
Future<void> test_sameLine() async {
878917
var contents = '''
879918
int plusOne(int [!value!]) => 1 + val^ue;

pkg/analyzer_plugin/lib/src/utilities/navigation/navigation_dart.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,13 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {
376376

377377
@override
378378
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
379-
computer._addRegionForElement(node.name, node.declaredElement2);
379+
if (node.declaredElement2 case BindPatternVariableElement2(:var join2?)) {
380+
for (var variable in join2.variables2) {
381+
computer._addRegionForElement(node.name, variable);
382+
}
383+
} else {
384+
computer._addRegionForElement(node.name, node.declaredElement2);
385+
}
380386
super.visitDeclaredVariablePattern(node);
381387
}
382388

@@ -581,6 +587,10 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {
581587
fragment,
582588
);
583589
}
590+
} else if (element case JoinPatternVariableElement2(:var variables2)) {
591+
for (var variable in variables2) {
592+
computer._addRegionForElement(node, variable);
593+
}
584594
} else {
585595
computer._addRegionForElement(node, element);
586596
}

0 commit comments

Comments
 (0)