Skip to content

Commit d563bbb

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Fix for wrong occurrences when different expression with the same prefix.
R=brianwilkerson@google.com Change-Id: I6bc00e2c30a8bf7b2e761e86310ad69e22ca956c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107195 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 2c40c72 commit d563bbb

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

pkg/analysis_server/lib/src/services/refactoring/extract_local.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
381381
hashCode: (Token t) => t.lexeme.hashCode);
382382
expr.accept(new _TokenLocalElementVisitor(map));
383383
// map and join tokens
384-
return tokens.map((Token token) {
384+
var result = tokens.map((Token token) {
385385
String tokenString = token.lexeme;
386386
// append token's Element id
387387
Element element = map[token];
@@ -394,6 +394,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
394394
// done
395395
return tokenString;
396396
}).join(_TOKEN_SEPARATOR);
397+
return result + _TOKEN_SEPARATOR;
397398
}
398399

399400
/**
@@ -693,7 +694,7 @@ class _OccurrencesVisitor extends GeneralizingAstVisitor<void> {
693694
int endTokenIndex =
694695
countMatches(nodeSource.substring(0, lastIndex), _TOKEN_SEPARATOR);
695696
Token startToken = nodeTokens[startTokenIndex];
696-
Token endToken = nodeTokens[endTokenIndex];
697+
Token endToken = nodeTokens[endTokenIndex - 1];
697698
// add occurrence range
698699
int start = nodeOffset + startToken.offset;
699700
int end = nodeOffset + endToken.end;

pkg/analysis_server/test/services/refactoring/extract_local_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,36 @@ main() {
734734
expect(refactoring.isAvailable(), isTrue);
735735
}
736736

737+
test_occurrences_differentName_samePrefix() async {
738+
await indexTestUnit('''
739+
void main(A a) {
740+
if (a.foo != 1) {
741+
} else if (a.foo2 != 2) {
742+
}
743+
}
744+
745+
class A {
746+
int foo;
747+
int foo2;
748+
}
749+
''');
750+
_createRefactoringWithSuffix('a.foo', ' != 1');
751+
// apply refactoring
752+
await _assertSuccessfulRefactoring('''
753+
void main(A a) {
754+
var res = a.foo;
755+
if (res != 1) {
756+
} else if (a.foo2 != 2) {
757+
}
758+
}
759+
760+
class A {
761+
int foo;
762+
int foo2;
763+
}
764+
''');
765+
}
766+
737767
test_occurrences_differentVariable() async {
738768
await indexTestUnit('''
739769
main() {

0 commit comments

Comments
 (0)