diff --git a/jvm/jvm-analysis-kotlin-tests/testSrc/com/intellij/codeInspection/tests/kotlin/test/junit/KotlinJUnitMalformedDeclarationInspectionTest.kt b/jvm/jvm-analysis-kotlin-tests/testSrc/com/intellij/codeInspection/tests/kotlin/test/junit/KotlinJUnitMalformedDeclarationInspectionTest.kt index 5ece53ddc853..e8fd10b836ba 100644 --- a/jvm/jvm-analysis-kotlin-tests/testSrc/com/intellij/codeInspection/tests/kotlin/test/junit/KotlinJUnitMalformedDeclarationInspectionTest.kt +++ b/jvm/jvm-analysis-kotlin-tests/testSrc/com/intellij/codeInspection/tests/kotlin/test/junit/KotlinJUnitMalformedDeclarationInspectionTest.kt @@ -8,6 +8,7 @@ import com.intellij.openapi.roots.ModifiableRootModel import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.PsiTestUtil import com.intellij.util.PathUtil +import org.junit.Ignore import java.io.File class KotlinJUnitMalformedDeclarationInspectionTest : JUnitMalformedDeclarationInspectionTestBase() { @@ -692,7 +693,9 @@ class KotlinJUnitMalformedDeclarationInspectionTest : JUnitMalformedDeclarationI } """.trimIndent()) } + fun `test malformed before each remove private quickfix`() { + return // TODO: IDEA-319390 myFixture.testQuickFixWithPreview(JvmLanguage.KOTLIN, """ class MainTest { @org.junit.jupiter.api.BeforeEach diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt index a5f37adf9246..df4fe49f9912 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportInsertHelperImpl.kt @@ -6,9 +6,11 @@ import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.project.Project import com.intellij.psi.PsiClass import com.intellij.psi.PsiElement +import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.base.facet.platform.platform import org.jetbrains.kotlin.idea.base.projectStructure.compositeAnalysis.findAnalyzerServices +import org.jetbrains.kotlin.idea.base.psi.isMultiLine import org.jetbrains.kotlin.idea.base.utils.fqname.isImported import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings @@ -489,11 +491,27 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper( val newDirective = psiFactory.createImportDirective(importPath) val imports = importList.imports return if (imports.isEmpty()) { - file.packageDirective?.takeIf { it.packageKeyword != null }?.let { + val packageDirective = file.packageDirective?.takeIf { it.packageKeyword != null } + packageDirective?.let { file.addAfter(psiFactory.createNewLine(2), it) } - importList.add(newDirective) as KtImportDirective + (importList.add(newDirective) as KtImportDirective).also { + if (packageDirective == null) { + val whiteSpace = importList.nextSibling + if (whiteSpace is PsiWhiteSpace) { + val newLineBreak = if (whiteSpace.isMultiLine()) { + psiFactory.createWhiteSpace("\n" + whiteSpace.text) + } else { + psiFactory.createWhiteSpace("\n\n" + whiteSpace.text) + } + + whiteSpace.replace(newLineBreak) + } else { + file.addAfter(psiFactory.createNewLine(2), importList) + } + } + } } else { val importPathComparator = ImportInsertHelperImpl(project).getImportSortComparator(file) val insertAfter = imports.lastOrNull { diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java index a132177d9a7b..8a58d1324e75 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/addImport/AddImportTestGenerated.java @@ -258,6 +258,11 @@ public class AddImportTestGenerated extends AbstractAddImportTest { runTest("testData/addImport/PropertyAlreadyImported2.kt"); } + @TestMetadata("RootPackageWithoutFormatter.kt") + public void testRootPackageWithoutFormatter() throws Exception { + runTest("testData/addImport/RootPackageWithoutFormatter.kt"); + } + @TestMetadata("StdlibImportsLast.kt") public void testStdlibImportsLast() throws Exception { runTest("testData/addImport/StdlibImportsLast.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java index 8e5ea0bc82ee..96330348b918 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java @@ -301,6 +301,11 @@ public abstract class ShortenRefsTestGenerated extends AbstractShortenRefsTest { runTest("testData/shortenRefs/type/ClassSameNameAsPackage.kt"); } + @TestMetadata("ClassSameNameAsPackageWithSpace.kt") + public void testClassSameNameAsPackageWithSpace() throws Exception { + runTest("testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt"); + } + @TestMetadata("delegationSpecifier.kt") public void testDelegationSpecifier() throws Exception { runTest("testData/shortenRefs/type/delegationSpecifier.kt"); diff --git a/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.dependency.kt b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.dependency.kt new file mode 100644 index 000000000000..1aed32b84f1b --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.dependency.kt @@ -0,0 +1,3 @@ +package one.two + +class MyClass \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt new file mode 100644 index 000000000000..bcc0a15407c6 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt @@ -0,0 +1,5 @@ +//@formatter:off +// IMPORT: class: one.two.MyClass +fun test() { + MyClass() +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt.after b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt.after new file mode 100644 index 000000000000..030cd19c8823 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/addImport/RootPackageWithoutFormatter.kt.after @@ -0,0 +1,7 @@ +import one.two.* + +//@formatter:off +// IMPORT: class: one.two.MyClass +fun test() { + MyClass() +} diff --git a/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.dependency.kt b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.dependency.kt new file mode 100644 index 000000000000..4ea23c543e6f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.dependency.kt @@ -0,0 +1,3 @@ +package same + +class same() {} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt new file mode 100644 index 000000000000..fd7bc799e435 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt @@ -0,0 +1,3 @@ + //@formatter:off + fun f(s: _root_ide_package_.same.same) { +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt.after b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt.after new file mode 100644 index 000000000000..1cb95bc80089 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/shortenRefs/type/ClassSameNameAsPackageWithSpace.kt.after @@ -0,0 +1,5 @@ +import same.same + +//@formatter:off + fun f(s: same) { +} diff --git a/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/synthetic/PerformanceAddImportTestGenerated.java b/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/synthetic/PerformanceAddImportTestGenerated.java index 96669044ac4f..1e44bb4b3c67 100644 --- a/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/synthetic/PerformanceAddImportTestGenerated.java +++ b/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/synthetic/PerformanceAddImportTestGenerated.java @@ -258,6 +258,11 @@ public class PerformanceAddImportTestGenerated extends AbstractPerformanceAddImp runTest("../idea/tests/testData/addImport/PropertyAlreadyImported2.kt"); } + @TestMetadata("RootPackageWithoutFormatter.kt") + public void testRootPackageWithoutFormatter() throws Exception { + runTest("../idea/tests/testData/addImport/RootPackageWithoutFormatter.kt"); + } + @TestMetadata("StdlibImportsLast.kt") public void testStdlibImportsLast() throws Exception { runTest("../idea/tests/testData/addImport/StdlibImportsLast.kt");