From f9e8683db560a0d01f72891350c63eb3b0464003 Mon Sep 17 00:00:00 2001 From: Sergey Rostov Date: Fri, 24 May 2013 13:42:15 +0400 Subject: [PATCH] KT-1545, KT-3161 KDoc formatter --- .../jetbrains/jet/plugin/formatter/JetBlock.java | 15 ++++++++++++++- .../formatter/JetFormattingModelBuilder.java | 8 +++++++- .../testData/editor/commenter/newLineInComment.kt | 3 +-- .../editor/commenter/newLineInComment_after.kt | 3 +-- idea/testData/formatter/KDoc.kt | 4 ++++ idea/testData/formatter/KDoc_after.kt | 6 ++++++ .../jetbrains/jet/formatter/JetFormatterTest.java | 4 ++++ 7 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 idea/testData/formatter/KDoc.kt create mode 100644 idea/testData/formatter/KDoc_after.kt diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index 34f3616a936..cad36209958 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -27,9 +27,13 @@ import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.kdoc.lexer.KDocTokens; import org.jetbrains.jet.plugin.JetLanguage; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import static org.jetbrains.jet.JetNodeTypes.*; import static org.jetbrains.jet.lexer.JetTokens.*; @@ -38,6 +42,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*; * @see Block for good JavaDoc documentation */ public class JetBlock extends AbstractBlock { + private static final int KDOC_COMMENT_INDENT = 1; private final ASTAlignmentStrategy myAlignmentStrategy; private final Indent myIndent; private final CodeStyleSettings mySettings; @@ -208,6 +213,9 @@ public class JetBlock extends AbstractBlock { } return new ChildAttributes(Indent.getContinuationIndent(), null); } + else if (type == DOC_COMMENT) { + return new ChildAttributes(Indent.getSpaceIndent(KDOC_COMMENT_INDENT), null); + } if (isIncomplete()) { return super.getChildAttributes(newChildIndex); @@ -321,6 +329,11 @@ public class JetBlock extends AbstractBlock { .in(PROPERTY, FUN) .notForType(BLOCK) .set(Indent.getContinuationWithoutFirstIndent()), + + ASTIndentStrategy.forNode("KDoc comment indent") + .in(DOC_COMMENT) + .forType(KDocTokens.LEADING_ASTERISK, KDocTokens.END) + .set(Indent.getSpaceIndent(KDOC_COMMENT_INDENT)), }; @Nullable diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java index b30cd9184d5..13d4e7e8dc5 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java @@ -25,6 +25,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.tree.TokenSet; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.kdoc.lexer.KDocTokens; import org.jetbrains.jet.plugin.JetLanguage; import static org.jetbrains.jet.JetNodeTypes.*; @@ -34,8 +35,9 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { @NotNull @Override public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { + PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE); JetBlock block = new JetBlock( - element.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, + containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, createSpacingBuilder(settings)); return FormattingModelProvider.createFormattingModelForPsiFile( @@ -53,6 +55,7 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { .between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode() .after(IMPORT_DIRECTIVE).blankLines(1) + .before(DOC_COMMENT).lineBreakInCode() .before(FUN).lineBreakInCode() .before(PROPERTY).lineBreakInCode() .between(FUN, FUN).blankLines(1) @@ -105,6 +108,9 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder { .between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1) .aroundInside(ARROW, WHEN_ENTRY).spaces(1) + + // KDoc + .between(KDocTokens.LEADING_ASTERISK, KDocTokens.TEXT).spacing(1, 100, 0, true, 100) ; } diff --git a/idea/testData/editor/commenter/newLineInComment.kt b/idea/testData/editor/commenter/newLineInComment.kt index 5f9507de398..c933947394e 100644 --- a/idea/testData/editor/commenter/newLineInComment.kt +++ b/idea/testData/editor/commenter/newLineInComment.kt @@ -1,4 +1,3 @@ /** * x - */ -fun test() = 0 \ No newline at end of file + */ \ No newline at end of file diff --git a/idea/testData/editor/commenter/newLineInComment_after.kt b/idea/testData/editor/commenter/newLineInComment_after.kt index 130bcc93cb1..a15a2e7ab9e 100644 --- a/idea/testData/editor/commenter/newLineInComment_after.kt +++ b/idea/testData/editor/commenter/newLineInComment_after.kt @@ -1,5 +1,4 @@ /** * x * - */ -fun test() = 0 \ No newline at end of file + */ \ No newline at end of file diff --git a/idea/testData/formatter/KDoc.kt b/idea/testData/formatter/KDoc.kt new file mode 100644 index 00000000000..a764ec6cf31 --- /dev/null +++ b/idea/testData/formatter/KDoc.kt @@ -0,0 +1,4 @@ +class Test { /** +* do something + */ fun doSomething() = 0 +} \ No newline at end of file diff --git a/idea/testData/formatter/KDoc_after.kt b/idea/testData/formatter/KDoc_after.kt new file mode 100644 index 00000000000..58f3ca3aba3 --- /dev/null +++ b/idea/testData/formatter/KDoc_after.kt @@ -0,0 +1,6 @@ +class Test { + /** + * do something + */ + fun doSomething() = 0 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java index 958481189ac..85f31b0e319 100644 --- a/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -72,6 +72,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest { doTest(); } + public void testKDoc() throws Exception { + doTest(); + } + public void testMultilineFunctionLiteral() throws Exception { doTest(); }