diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index ddb3b781955..1e61cfee504 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -30,6 +30,8 @@ import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.kdoc.lexer.KDocTokens; +import org.jetbrains.jet.lang.psi.JetDeclaration; +import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.plugin.JetLanguage; import java.util.ArrayList; @@ -156,6 +158,15 @@ public class JetBlock extends AbstractBlock { return node; } + private static ASTNode getPrevWithoutWhitespaceAndComments(ASTNode node) { + node = node.getTreePrev(); + while (node != null && (node.getElementType() == TokenType.WHITE_SPACE || JetTokens.COMMENTS.contains(node.getElementType()))) { + node = node.getTreePrev(); + } + + return node; + } + @Override public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) { return mySpacingBuilder.getSpacing(this, child1, child2); @@ -390,6 +401,14 @@ public class JetBlock extends AbstractBlock { } } + // do not indent child after heading comments inside declaration + if (childParent != null && childParent.getPsi() instanceof JetDeclaration) { + ASTNode prev = getPrevWithoutWhitespace(child); + if (prev != null && JetTokens.COMMENTS.contains(prev.getElementType()) && getPrevWithoutWhitespaceAndComments(prev) == null) { + return Indent.getNoneIndent(); + } + } + for (NodeIndentStrategy strategy : INDENT_RULES) { Indent indent = strategy.getIndent(child); if (indent != null) { diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt b/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt index a179621eb8b..6fbf965bff7 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt +++ b/idea/src/org/jetbrains/jet/plugin/formatter/kotlinSpacingRules.kt @@ -91,6 +91,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { before(FUN).lineBreakInCode() before(PROPERTY).lineBreakInCode() + + after(DOC_COMMENT).lineBreakInCode() + // =============== Spacing ================ betweenInside(LBRACE, RBRACE, CLASS_BODY).spaces(0) @@ -304,4 +307,4 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW) } } -} \ No newline at end of file +} diff --git a/idea/testData/formatter/DocComments.after.kt b/idea/testData/formatter/DocComments.after.kt new file mode 100644 index 00000000000..3bb3d07c557 --- /dev/null +++ b/idea/testData/formatter/DocComments.after.kt @@ -0,0 +1,11 @@ +/** Doc comment for A */ +class A { + /** Doc comment for function */ + fun foo() { + } + + /** + * Doc comment for property + */ + val prop = 1 +} \ No newline at end of file diff --git a/idea/testData/formatter/DocComments.kt b/idea/testData/formatter/DocComments.kt new file mode 100644 index 00000000000..54f337ffd62 --- /dev/null +++ b/idea/testData/formatter/DocComments.kt @@ -0,0 +1,6 @@ +/** Doc comment for A */class A { /** Doc comment for function */ + fun foo(){} + /** + * Doc comment for property + */ val prop = 1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index a7379f60759..2c50cdaeff6 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -136,6 +136,12 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest(fileName); } + @TestMetadata("DocComments.after.kt") + public void testDocComments() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/DocComments.after.kt"); + doTest(fileName); + } + @TestMetadata("ElseOnNewLine.after.kt") public void testElseOnNewLine() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/ElseOnNewLine.after.kt");