Fix KNPE from "Create" quick fix

#KT-27289 Fixed
This commit is contained in:
Dmitry Gridin
2019-03-01 19:02:42 +03:00
parent 78bee70946
commit dce2139eb0
11 changed files with 61 additions and 5 deletions
@@ -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))
}
}
if (ApplicationManager.getApplication().isUnitTestMode) {
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()
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.VirtualFile
import com.intellij.psi.*
import com.intellij.psi.impl.file.PsiPackageBase
import com.intellij.psi.impl.light.LightElement
import com.intellij.psi.presentation.java.SymbolPresentationUtil
import com.intellij.psi.util.PsiTreeUtil
@@ -406,9 +407,13 @@ fun <T> chooseContainerElement(
return "$name$params"
}
private fun PsiElement.renderText(): String {
if (this is SeparateFileWrapper) return "Extract to separate file"
return StringUtil.shortenTextWithEllipsis(text!!.collapseSpaces(), 53, 0)
private fun PsiElement.renderText(): String = when (this) {
is SeparateFileWrapper -> "Extract to separate file"
is PsiPackageBase -> qualifiedName
else -> {
val text = text ?: "<invalid text>"
StringUtil.shortenTextWithEllipsis(text.collapseSpaces(), 53, 0)
}
}
private fun PsiElement.getRepresentativeElement(): PsiElement = when (this) {
@@ -0,0 +1 @@
package a.b
@@ -0,0 +1,5 @@
package a.b
class ClassG {
}
@@ -0,0 +1,5 @@
// "Create class 'ClassG'" "true"
package a
import a.b.ClassG<caret>
@@ -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
@@ -0,0 +1,5 @@
package a.b
class ClassG {
}
@@ -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>()
}
@@ -199,6 +199,16 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
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")
public void testCreateFunInExpectClass() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createFunInExpectClass/");