More proper fix for KT-8137/EA-69470: avoiding failed assertion when searching usages of parameter of local class constructor
KT-8137 AE at JetSourceNavigationHelper.convertNamedClassOrObject() on function local class with several constructor parameters #KT-8137 fixed #EA-69470 fixed
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.navigation.ItemPresentationProviders
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
@@ -89,4 +90,8 @@ public abstract class JetConstructor<T : JetConstructor<T>> : JetDeclarationStub
|
||||
?: getValueParameterList()?.getTextOffset()
|
||||
?: super<JetDeclarationStub>.getTextOffset()
|
||||
}
|
||||
|
||||
override fun getUseScope(): SearchScope {
|
||||
return getContainingClassOrObject().getUseScope()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ public class JetParameter extends JetNamedDeclarationStub<KotlinParameterStub> i
|
||||
PsiElement parent = getParent();
|
||||
if (parent != null) {
|
||||
PsiElement grandparent = parent.getParent();
|
||||
if (grandparent instanceof JetNamedFunction) {
|
||||
if (grandparent instanceof JetFunction && !(grandparent instanceof JetFunctionLiteral)) {
|
||||
return grandparent.getUseScope();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -775,9 +775,9 @@ public class JetPsiUtil {
|
||||
else if (declaration instanceof JetParameter) {
|
||||
PsiElement parent = declaration.getParent();
|
||||
|
||||
// val/var parameter of primary constructor should not be considered as local
|
||||
// val/var parameter of primary constructor should be considered as local according to containing class
|
||||
if (((JetParameter) declaration).hasValOrVar() && parent != null && parent.getParent() instanceof JetPrimaryConstructor) {
|
||||
return null;
|
||||
return getEnclosingElementForLocalDeclaration(((JetPrimaryConstructor) parent.getParent()).getContainingClassOrObject(), skipParameters);
|
||||
}
|
||||
|
||||
else if (skipParameters && parent != null && parent.getParent() instanceof JetNamedFunction) {
|
||||
|
||||
+3
-2
@@ -6,7 +6,8 @@ public open class Server(private val <caret>foo: String = "foo") {
|
||||
open fun processRequest() = foo
|
||||
}
|
||||
|
||||
public class ServerEx(): Server() {
|
||||
override fun processRequest() = "foo" + foo
|
||||
public class ServerEx(): Server(foo = "!") {
|
||||
override fun processRequest() = "foo" + foo // this reference is found as a side effect of big use scope of constructor parameter:
|
||||
// if it was simple property, it wouldn't be found
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ import server.*;
|
||||
|
||||
class Client {
|
||||
public fun foo() {
|
||||
println(Server().foo)
|
||||
println(Server(foo = "!").foo)
|
||||
ServerEx().processRequest()
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+5
-1
@@ -1 +1,5 @@
|
||||
Value read (6: 33) open fun processRequest() = foo
|
||||
[kotlinPrivatePropertyUsages2.0.kt] Named argument (9: 33) public class ServerEx(): Server(foo = "!") {
|
||||
[kotlinPrivatePropertyUsages2.0.kt] Value read (10: 45) override fun processRequest() = "foo" + foo
|
||||
[kotlinPrivatePropertyUsages2.0.kt] Value read (6: 33) open fun processRequest() = foo
|
||||
[kotlinPrivatePropertyUsages2.1.kt] Named argument (5: 24) println(Server(foo = "!").foo)
|
||||
[kotlinPrivatePropertyUsages2.1.kt] Value read (5: 35) println(Server(foo = "!").foo)
|
||||
@@ -64,4 +64,9 @@ fun test() {
|
||||
val tt = A.T()
|
||||
t.bar(2)
|
||||
val fff = A.T::bar
|
||||
|
||||
class LocalClass(val localClassProperty: Int) {
|
||||
}
|
||||
|
||||
LocalClass(localClassProperty = 1).localClassProperty
|
||||
}
|
||||
@@ -358,11 +358,11 @@ public abstract class AbstractJetFindUsagesTest extends JetLightCodeInsightFixtu
|
||||
}
|
||||
|
||||
private <T extends PsiElement> void findUsagesAndCheckResults(
|
||||
String mainFileText,
|
||||
String prefix,
|
||||
String rootPath,
|
||||
T caretElement,
|
||||
FindUsagesOptions options
|
||||
@NotNull String mainFileText,
|
||||
@NotNull String prefix,
|
||||
@NotNull String rootPath,
|
||||
@NotNull T caretElement,
|
||||
@Nullable FindUsagesOptions options
|
||||
) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
Collection<UsageInfo> usageInfos = findUsages(caretElement, options);
|
||||
|
||||
@@ -419,7 +419,7 @@ public abstract class AbstractJetFindUsagesTest extends JetLightCodeInsightFixtu
|
||||
JetTestUtils.assertEqualsToFile(new File(rootPath, prefix + "results.txt"), StringUtil.join(finalUsages, "\n"));
|
||||
}
|
||||
|
||||
private Collection<UsageInfo> findUsages(@NotNull PsiElement targetElement, @Nullable FindUsagesOptions options) {
|
||||
protected Collection<UsageInfo> findUsages(@NotNull PsiElement targetElement, @Nullable FindUsagesOptions options) {
|
||||
Project project = getProject();
|
||||
FindUsagesHandler handler =
|
||||
((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager().getFindUsagesHandler(targetElement, false);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.findUsages
|
||||
|
||||
import com.intellij.psi.search.FilenameIndex
|
||||
import org.jetbrains.kotlin.psi.JetParameter
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
public class KotlinFindUsagesWithLibraryCustomTest : AbstractKotlinFindUsagesWithLibraryTest() {
|
||||
public fun testFindUsagesForLocalClassProperty() {
|
||||
val libraryFile = FilenameIndex.getFilesByName(getProject(), "library.kt", myFixture.getModule().getModuleWithLibrariesScope()).first()
|
||||
val indexOf = libraryFile.getText().indexOf("localClassProperty")
|
||||
val jetParameter = libraryFile.findElementAt(indexOf)!!.getStrictParentOfType<JetParameter>()!!
|
||||
val usages = findUsages(jetParameter.getOriginalElement(), null)
|
||||
assertEquals(2, usages.size())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user