Fix KNPE from "Create" quick fix
#KT-27289 Fixed
This commit is contained in:
+3
-2
@@ -122,14 +122,15 @@ open class CreateClassFromUsageFix<E : KtElement> protected constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (classInfo.kind != ClassKind.ENUM_ENTRY) {
|
|
||||||
|
if (classInfo.kind != ClassKind.ENUM_ENTRY && parents.find { it is PsiPackage } == null) {
|
||||||
parents += SeparateFileWrapper(PsiManager.getInstance(project))
|
parents += SeparateFileWrapper(PsiManager.getInstance(project))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ApplicationManager.getApplication().isUnitTestMode) {
|
if (ApplicationManager.getApplication().isUnitTestMode) {
|
||||||
val targetParent = applicableParents.firstOrNull { element ->
|
val targetParent = applicableParents.firstOrNull { element ->
|
||||||
element.allChildren.any { it is PsiComment && it.text == "// TARGET_PARENT:" }
|
if (element is PsiPackage) false else element.allChildren.any { it is PsiComment && it.text == "// TARGET_PARENT:" }
|
||||||
} ?: classInfo.applicableParents.last()
|
} ?: classInfo.applicableParents.last()
|
||||||
return doInvoke(targetParent, editor, file)
|
return doInvoke(targetParent, editor, file)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import com.intellij.openapi.util.text.StringUtil
|
|||||||
import com.intellij.openapi.vfs.LocalFileSystem
|
import com.intellij.openapi.vfs.LocalFileSystem
|
||||||
import com.intellij.openapi.vfs.VirtualFile
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
|
import com.intellij.psi.impl.file.PsiPackageBase
|
||||||
import com.intellij.psi.impl.light.LightElement
|
import com.intellij.psi.impl.light.LightElement
|
||||||
import com.intellij.psi.presentation.java.SymbolPresentationUtil
|
import com.intellij.psi.presentation.java.SymbolPresentationUtil
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
@@ -406,9 +407,13 @@ fun <T> chooseContainerElement(
|
|||||||
return "$name$params"
|
return "$name$params"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun PsiElement.renderText(): String {
|
private fun PsiElement.renderText(): String = when (this) {
|
||||||
if (this is SeparateFileWrapper) return "Extract to separate file"
|
is SeparateFileWrapper -> "Extract to separate file"
|
||||||
return StringUtil.shortenTextWithEllipsis(text!!.collapseSpaces(), 53, 0)
|
is PsiPackageBase -> qualifiedName
|
||||||
|
else -> {
|
||||||
|
val text = text ?: "<invalid text>"
|
||||||
|
StringUtil.shortenTextWithEllipsis(text.collapseSpaces(), 53, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun PsiElement.getRepresentativeElement(): PsiElement = when (this) {
|
private fun PsiElement.getRepresentativeElement(): PsiElement = when (this) {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
package a.b
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package a.b
|
||||||
|
|
||||||
|
class ClassG {
|
||||||
|
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// "Create class 'ClassG'" "true"
|
||||||
|
|
||||||
|
package a
|
||||||
|
|
||||||
|
import a.b.ClassG<caret>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Create class 'ClassG'" "true"
|
||||||
|
// ERROR: Unresolved reference: ClassG
|
||||||
|
|
||||||
|
package a
|
||||||
|
|
||||||
|
import a.b.ClassG<caret>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package a.b
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package a.b
|
||||||
|
|
||||||
|
class ClassG {
|
||||||
|
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// "Create class 'ClassG'" "true"
|
||||||
|
|
||||||
|
package a
|
||||||
|
|
||||||
|
import a.b.ClassG
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
ClassG()
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Create class 'ClassG'" "true"
|
||||||
|
// ERROR: Unresolved reference: ClassG
|
||||||
|
|
||||||
|
package a
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
a.b.ClassG<caret>()
|
||||||
|
}
|
||||||
+10
@@ -199,6 +199,16 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
|
|||||||
runTest("idea/testData/multiModuleQuickFix/convertPropertyGetterToInitializer/");
|
runTest("idea/testData/multiModuleQuickFix/convertPropertyGetterToInitializer/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("createClassFromUsageImport")
|
||||||
|
public void testCreateClassFromUsageImport() throws Exception {
|
||||||
|
runTest("idea/testData/multiModuleQuickFix/createClassFromUsageImport/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("createClassFromUsageRef")
|
||||||
|
public void testCreateClassFromUsageRef() throws Exception {
|
||||||
|
runTest("idea/testData/multiModuleQuickFix/createClassFromUsageRef/");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("createFunInExpectClass")
|
@TestMetadata("createFunInExpectClass")
|
||||||
public void testCreateFunInExpectClass() throws Exception {
|
public void testCreateFunInExpectClass() throws Exception {
|
||||||
runTest("idea/testData/multiModuleQuickFix/createFunInExpectClass/");
|
runTest("idea/testData/multiModuleQuickFix/createFunInExpectClass/");
|
||||||
|
|||||||
Reference in New Issue
Block a user