diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index 5a238432116..969cec7cc12 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -53,9 +53,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing return rules(kotlinCommonSettings, builderUtil) { simple { before(FILE_ANNOTATION_LIST).lineBreakInCode() - after(FILE_ANNOTATION_LIST).blankLines(1) - - after(PACKAGE_DIRECTIVE).blankLines(1) between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode() after(IMPORT_LIST).blankLines(1) } @@ -98,7 +95,14 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing else -> null } } + } + simple { + after(FILE_ANNOTATION_LIST).blankLines(1) + after(PACKAGE_DIRECTIVE).blankLines(1) + } + + custom { inPosition(leftSet = DECLARATIONS, rightSet = DECLARATIONS).customRule(fun( _: ASTBlock, _: ASTBlock, diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/expectactual/AbstractCreateDeclarationFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/expectactual/AbstractCreateDeclarationFix.kt index b194a3e3af9..fbee60845e9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/expectactual/AbstractCreateDeclarationFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/expectactual/AbstractCreateDeclarationFix.kt @@ -5,12 +5,14 @@ package org.jetbrains.kotlin.idea.quickfix.expectactual +import com.intellij.codeInsight.actions.ReformatCodeAction import com.intellij.ide.util.EditorHelper import com.intellij.openapi.editor.Editor import com.intellij.openapi.module.Module import com.intellij.openapi.project.DumbService import com.intellij.openapi.project.Project import com.intellij.psi.codeStyle.CodeStyleManager +import com.intellij.psi.formatter.FormatterUtil import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction import org.jetbrains.kotlin.idea.quickfix.TypeAccessibilityChecker @@ -18,6 +20,7 @@ import org.jetbrains.kotlin.idea.refactoring.introduce.showErrorHint import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.startOffset +import java.util.* abstract class AbstractCreateDeclarationFix( declaration: D, @@ -71,6 +74,7 @@ abstract class AbstractCreateDeclarationFix( val oldPackageDirective = targetFile.packageDirective val newPackageDirective = packageDirective.copy() as KtPackageDirective if (oldPackageDirective != null) { + if (oldPackageDirective.text.isEmpty()) targetFile.addAfter(factory.createNewLine(2), oldPackageDirective) oldPackageDirective.replace(newPackageDirective) } else { targetFile.add(newPackageDirective) diff --git a/idea/testData/formatter/CommentAfterPackage.after.kt b/idea/testData/formatter/CommentAfterPackage.after.kt new file mode 100644 index 00000000000..4c1d0f30374 --- /dev/null +++ b/idea/testData/formatter/CommentAfterPackage.after.kt @@ -0,0 +1,3 @@ +package one // tooo + +val a = 42 diff --git a/idea/testData/formatter/CommentAfterPackage.kt b/idea/testData/formatter/CommentAfterPackage.kt new file mode 100644 index 00000000000..4c1d0f30374 --- /dev/null +++ b/idea/testData/formatter/CommentAfterPackage.kt @@ -0,0 +1,3 @@ +package one // tooo + +val a = 42 diff --git a/idea/testData/formatter/fileAnnotations/commentAfter.after.kt b/idea/testData/formatter/fileAnnotations/commentAfter.after.kt new file mode 100644 index 00000000000..3617b8b7534 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter.after.kt @@ -0,0 +1,2 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // for common tests +package foo \ No newline at end of file diff --git a/idea/testData/formatter/fileAnnotations/commentAfter.kt b/idea/testData/formatter/fileAnnotations/commentAfter.kt new file mode 100644 index 00000000000..3617b8b7534 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter.kt @@ -0,0 +1,2 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // for common tests +package foo \ No newline at end of file diff --git a/idea/testData/formatter/fileAnnotations/commentAfter2.after.kt b/idea/testData/formatter/fileAnnotations/commentAfter2.after.kt new file mode 100644 index 00000000000..50774de39a7 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter2.after.kt @@ -0,0 +1,3 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") +// for common tests +package foo \ No newline at end of file diff --git a/idea/testData/formatter/fileAnnotations/commentAfter2.kt b/idea/testData/formatter/fileAnnotations/commentAfter2.kt new file mode 100644 index 00000000000..50774de39a7 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter2.kt @@ -0,0 +1,3 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") +// for common tests +package foo \ No newline at end of file diff --git a/idea/testData/formatter/fileAnnotations/commentAfter3.after.kt b/idea/testData/formatter/fileAnnotations/commentAfter3.after.kt new file mode 100644 index 00000000000..4756dba4981 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter3.after.kt @@ -0,0 +1,2 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") /*for common tests*/ +package foo \ No newline at end of file diff --git a/idea/testData/formatter/fileAnnotations/commentAfter3.kt b/idea/testData/formatter/fileAnnotations/commentAfter3.kt new file mode 100644 index 00000000000..4756dba4981 --- /dev/null +++ b/idea/testData/formatter/fileAnnotations/commentAfter3.kt @@ -0,0 +1,2 @@ +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") /*for common tests*/ +package foo \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/annotationPosition/topLevelFunctionSuppressOnFile.kt.after b/idea/testData/quickfix/suppress/annotationPosition/topLevelFunctionSuppressOnFile.kt.after index 2cbd93d56c8..8f7a5b77861 100644 --- a/idea/testData/quickfix/suppress/annotationPosition/topLevelFunctionSuppressOnFile.kt.after +++ b/idea/testData/quickfix/suppress/annotationPosition/topLevelFunctionSuppressOnFile.kt.after @@ -1,5 +1,4 @@ @file:Suppress("REDUNDANT_NULLABLE") - // "Suppress 'REDUNDANT_NULLABLE' for file ${file}" "true" public fun foo(): String?? = null \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index dfabb785174..6e22f65d8c4 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -165,6 +165,11 @@ public class FormatterTestGenerated extends AbstractFormatterTest { runTest("idea/testData/formatter/ColonSpaces.after.kt"); } + @TestMetadata("CommentAfterPackage.after.kt") + public void testCommentAfterPackage() throws Exception { + runTest("idea/testData/formatter/CommentAfterPackage.after.kt"); + } + @TestMetadata("CommentInExpressionBodies.after.kt") public void testCommentInExpressionBodies() throws Exception { runTest("idea/testData/formatter/CommentInExpressionBodies.after.kt"); @@ -955,6 +960,21 @@ public class FormatterTestGenerated extends AbstractFormatterTest { runTest("idea/testData/formatter/fileAnnotations/beforePackage.after.kt"); } + @TestMetadata("commentAfter.after.kt") + public void testCommentAfter() throws Exception { + runTest("idea/testData/formatter/fileAnnotations/commentAfter.after.kt"); + } + + @TestMetadata("commentAfter2.after.kt") + public void testCommentAfter2() throws Exception { + runTest("idea/testData/formatter/fileAnnotations/commentAfter2.after.kt"); + } + + @TestMetadata("commentAfter3.after.kt") + public void testCommentAfter3() throws Exception { + runTest("idea/testData/formatter/fileAnnotations/commentAfter3.after.kt"); + } + @TestMetadata("inEmptyFile.after.kt") public void testInEmptyFile() throws Exception { runTest("idea/testData/formatter/fileAnnotations/inEmptyFile.after.kt"); diff --git a/j2k/testData/fileOrElement/comments/comments.kt b/j2k/testData/fileOrElement/comments/comments.kt index 2309f20c103..c1ff7a70b9e 100644 --- a/j2k/testData/fileOrElement/comments/comments.kt +++ b/j2k/testData/fileOrElement/comments/comments.kt @@ -1,7 +1,5 @@ // ERROR: Class 'A' is not abstract and does not implement abstract member public abstract fun run(): Unit defined in java.lang.Runnable -package foo - -// we use package 'foo' +package foo // we use package 'foo' // imports: import java.util.ArrayList // we need ArrayList