Auto-import fix should suggest declarations that cannot be used in this position
This also should fix KT-9122 Assertion error when psi modified from IDE #KT-9122 Fixed
This commit is contained in:
@@ -44,6 +44,7 @@ public sealed class CallType<TReceiver : JetElement?>(val descriptorKindFilter:
|
||||
|
||||
object SAFE : CallType<JetExpression>(DescriptorKindFilter.ALL)
|
||||
|
||||
// TODO: split into INFIX and OPERATOR (+ support other convention calls like [] etc)
|
||||
object INFIX : CallType<JetExpression>(DescriptorKindFilter.FUNCTIONS exclude NonInfixExclude)
|
||||
|
||||
object UNARY : CallType<JetExpression>(DescriptorKindFilter.FUNCTIONS exclude NonUnaryExclude)
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
|
||||
import org.jetbrains.kotlin.idea.core.getResolutionScope
|
||||
import org.jetbrains.kotlin.idea.core.isVisible
|
||||
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
|
||||
import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetPsiUtil
|
||||
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
|
||||
@@ -85,7 +86,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
|
||||
override fun getFamilyName() = JetBundle.message("import.fix")
|
||||
|
||||
override fun isAvailable(project: Project, editor: Editor, file: PsiFile)
|
||||
= (super<JetHintAction>.isAvailable(project, editor, file)) && (anySuggestionFound ?: !suggestions.isEmpty())
|
||||
= (super.isAvailable(project, editor, file)) && (anySuggestionFound ?: !suggestions.isEmpty())
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
|
||||
CommandProcessor.getInstance().runUndoTransparentAction {
|
||||
@@ -135,10 +136,14 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
|
||||
val diagnostics = bindingContext.getDiagnostics().forElement(element)
|
||||
if (!diagnostics.any { it.getFactory() in ERRORS }) return listOf()
|
||||
|
||||
val resolutionScope = element.getResolutionScope(bindingContext, file.getResolutionFacade())
|
||||
val containingDescriptor = resolutionScope.ownerDescriptor
|
||||
val resolutionScope = element.getResolutionScope(bindingContext, file.getResolutionFacade())
|
||||
val containingDescriptor = resolutionScope.ownerDescriptor
|
||||
|
||||
val callType = CallTypeAndReceiver.detect(element).callType
|
||||
|
||||
fun isVisible(descriptor: DeclarationDescriptor): Boolean {
|
||||
if (!callType.descriptorKindFilter.accepts(descriptor)) return false
|
||||
|
||||
if (descriptor is DeclarationDescriptorWithVisibility) {
|
||||
return descriptor.isVisible(containingDescriptor, bindingContext, element)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ERROR: Unresolved reference: infix
|
||||
package x
|
||||
|
||||
object infix {
|
||||
fun invoke() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun x() {
|
||||
"" <caret>infix ""
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Import" "true"
|
||||
// ERROR: Unresolved reference: infix
|
||||
package x
|
||||
|
||||
object infix {
|
||||
fun invoke() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun x() {
|
||||
"" <caret>infix ""
|
||||
}
|
||||
@@ -406,6 +406,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("infixCallAndObject.kt")
|
||||
public void testInfixCallAndObject() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/infixCallAndObject.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("libraryPropertyJsRuntime.kt")
|
||||
public void testLibraryPropertyJsRuntime() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/libraryPropertyJsRuntime.kt");
|
||||
|
||||
Reference in New Issue
Block a user