diff --git a/ChangeLog.md b/ChangeLog.md index 11ef3ae69fa..5d72381eb7f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -235,6 +235,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - [`KT-14004`](https://youtrack.jetbrains.com/issue/KT-14004) Introduce Variable: Fix exception on trying to extract variable of functional type - [`KT-13726`](https://youtrack.jetbrains.com/issue/KT-13726) Move: Fix bogus conflicts due to references resolving to wrong library version - [`KT-14114`](https://youtrack.jetbrains.com/issue/KT-14114) Move: Fix exception on moving Kotlin file without declarations +- [`KT-14157`](https://youtrack.jetbrains.com/issue/KT-14157) Rename: Rename do-while loop variables in the loop condition ##### New features diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtNamedDeclarationStub.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtNamedDeclarationStub.java index 8f0cad2fd78..c79d7caf83f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtNamedDeclarationStub.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtNamedDeclarationStub.java @@ -103,7 +103,18 @@ abstract class KtNamedDeclarationStub> extends @Override public SearchScope getUseScope() { KtElement enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration(this, false); - if (enclosingBlock != null) return new LocalSearchScope(enclosingBlock); + if (enclosingBlock != null) { + PsiElement enclosingParent = enclosingBlock.getParent(); + if (enclosingParent instanceof KtContainerNode) { + enclosingParent = enclosingParent.getParent(); + } + if (enclosingBlock instanceof KtBlockExpression && enclosingParent instanceof KtDoWhileExpression) { + KtExpression condition = ((KtDoWhileExpression) enclosingParent).getCondition(); + if (condition != null) return new LocalSearchScope(new PsiElement[] { enclosingBlock, condition }); + } + + return new LocalSearchScope(enclosingBlock); + } if (hasModifier(KtTokens.PRIVATE_KEYWORD)) { KtElement containingClass = PsiTreeUtil.getParentOfType(this, KtClassOrObject.class); diff --git a/idea/testData/findUsages/kotlin/variable/varInDoWhile.0.kt b/idea/testData/findUsages/kotlin/variable/varInDoWhile.0.kt new file mode 100644 index 00000000000..0e6e9327ebe --- /dev/null +++ b/idea/testData/findUsages/kotlin/variable/varInDoWhile.0.kt @@ -0,0 +1,8 @@ +// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtProperty +// OPTIONS: usages +fun test() { + do { + val message = "test" + println(message) + } while (message.isEmpty()) +} \ No newline at end of file diff --git a/idea/testData/findUsages/kotlin/variable/varInDoWhile.results.txt b/idea/testData/findUsages/kotlin/variable/varInDoWhile.results.txt new file mode 100644 index 00000000000..7f537d668d5 --- /dev/null +++ b/idea/testData/findUsages/kotlin/variable/varInDoWhile.results.txt @@ -0,0 +1,2 @@ +Receiver 7 } while (message.isEmpty()) +Value read 6 println(message) \ No newline at end of file diff --git a/idea/testData/refactoring/rename/varInDoWhile/after/test.kt b/idea/testData/refactoring/rename/varInDoWhile/after/test.kt new file mode 100644 index 00000000000..998854e26f3 --- /dev/null +++ b/idea/testData/refactoring/rename/varInDoWhile/after/test.kt @@ -0,0 +1,6 @@ +fun test() { + do { + val message2 = "test" + println(message2) + } while (message2.isEmpty()) +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/varInDoWhile/before/test.kt b/idea/testData/refactoring/rename/varInDoWhile/before/test.kt new file mode 100644 index 00000000000..683de8944d2 --- /dev/null +++ b/idea/testData/refactoring/rename/varInDoWhile/before/test.kt @@ -0,0 +1,6 @@ +fun test() { + do { + val /*rename*/message = "test" + println(message) + } while (message.isEmpty()) +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/varInDoWhile/varInDoWhile.test b/idea/testData/refactoring/rename/varInDoWhile/varInDoWhile.test new file mode 100644 index 00000000000..c44a37b9cbd --- /dev/null +++ b/idea/testData/refactoring/rename/varInDoWhile/varInDoWhile.test @@ -0,0 +1,6 @@ +{ + "type": "MARKED_ELEMENT", + "mainFile": "test.kt", + "newName": "message2", + "withRuntime": "true" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/findUsages/FindUsagesTestGenerated.java b/idea/tests/org/jetbrains/kotlin/findUsages/FindUsagesTestGenerated.java index f8a79e255d9..133b6a93380 100644 --- a/idea/tests/org/jetbrains/kotlin/findUsages/FindUsagesTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/findUsages/FindUsagesTestGenerated.java @@ -1347,6 +1347,12 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest { doTest(fileName); } + @TestMetadata("varInDoWhile.0.kt") + public void testVarInDoWhile() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/variable/varInDoWhile.0.kt"); + doTest(fileName); + } + @TestMetadata("writeAccess.0.kt") public void testWriteAccess() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/variable/writeAccess.0.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java index c3dc56515fd..492c5e3082b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -1102,4 +1102,10 @@ public class RenameTestGenerated extends AbstractRenameTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/topLevelPropertyRedeclaration/topLevelPropertyRedeclaration.test"); doTest(fileName); } + + @TestMetadata("varInDoWhile/varInDoWhile.test") + public void testVarInDoWhile_VarInDoWhile() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/varInDoWhile/varInDoWhile.test"); + doTest(fileName); + } }