KT-1545, KT-3161 KDoc formatter

This commit is contained in:
Sergey Rostov
2013-05-24 13:42:15 +04:00
committed by Natalia.Ukhorskaya
parent c4bdf72054
commit 9dfe0f42a3
7 changed files with 37 additions and 6 deletions
@@ -27,9 +27,13 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet; import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.kdoc.lexer.KDocTokens;
import org.jetbrains.jet.plugin.JetLanguage; 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.JetNodeTypes.*;
import static org.jetbrains.jet.lexer.JetTokens.*; import static org.jetbrains.jet.lexer.JetTokens.*;
@@ -38,6 +42,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
* @see Block for good JavaDoc documentation * @see Block for good JavaDoc documentation
*/ */
public class JetBlock extends AbstractBlock { public class JetBlock extends AbstractBlock {
private static final int KDOC_COMMENT_INDENT = 1;
private final ASTAlignmentStrategy myAlignmentStrategy; private final ASTAlignmentStrategy myAlignmentStrategy;
private final Indent myIndent; private final Indent myIndent;
private final CodeStyleSettings mySettings; private final CodeStyleSettings mySettings;
@@ -208,6 +213,9 @@ public class JetBlock extends AbstractBlock {
} }
return new ChildAttributes(Indent.getContinuationIndent(), null); return new ChildAttributes(Indent.getContinuationIndent(), null);
} }
else if (type == DOC_COMMENT) {
return new ChildAttributes(Indent.getSpaceIndent(KDOC_COMMENT_INDENT), null);
}
if (isIncomplete()) { if (isIncomplete()) {
return super.getChildAttributes(newChildIndex); return super.getChildAttributes(newChildIndex);
@@ -321,6 +329,11 @@ public class JetBlock extends AbstractBlock {
.in(PROPERTY, FUN) .in(PROPERTY, FUN)
.notForType(BLOCK) .notForType(BLOCK)
.set(Indent.getContinuationWithoutFirstIndent()), .set(Indent.getContinuationWithoutFirstIndent()),
ASTIndentStrategy.forNode("KDoc comment indent")
.in(DOC_COMMENT)
.forType(KDocTokens.LEADING_ASTERISK, KDocTokens.END)
.set(Indent.getSpaceIndent(KDOC_COMMENT_INDENT)),
}; };
@Nullable @Nullable
@@ -25,6 +25,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.tree.TokenSet; import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.kdoc.lexer.KDocTokens;
import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.plugin.JetLanguage;
import static org.jetbrains.jet.JetNodeTypes.*; import static org.jetbrains.jet.JetNodeTypes.*;
@@ -34,8 +35,9 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
@NotNull @NotNull
@Override @Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE);
JetBlock block = new JetBlock( JetBlock block = new JetBlock(
element.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,
createSpacingBuilder(settings)); createSpacingBuilder(settings));
return FormattingModelProvider.createFormattingModelForPsiFile( return FormattingModelProvider.createFormattingModelForPsiFile(
@@ -53,6 +55,7 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
.between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode() .between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode()
.after(IMPORT_DIRECTIVE).blankLines(1) .after(IMPORT_DIRECTIVE).blankLines(1)
.before(DOC_COMMENT).lineBreakInCode()
.before(FUN).lineBreakInCode() .before(FUN).lineBreakInCode()
.before(PROPERTY).lineBreakInCode() .before(PROPERTY).lineBreakInCode()
.between(FUN, FUN).blankLines(1) .between(FUN, FUN).blankLines(1)
@@ -105,6 +108,9 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
.between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1) .between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1)
.aroundInside(ARROW, WHEN_ENTRY).spaces(1) .aroundInside(ARROW, WHEN_ENTRY).spaces(1)
// KDoc
.between(KDocTokens.LEADING_ASTERISK, KDocTokens.TEXT).spacing(1, 100, 0, true, 100)
; ;
} }
@@ -1,4 +1,3 @@
/** /**
* x<caret> * x<caret>
*/ */
fun test() = 0
@@ -1,5 +1,4 @@
/** /**
* x * x
* <caret> * <caret>
*/ */
fun test() = 0
+4
View File
@@ -0,0 +1,4 @@
class Test { /**
* do something
*/ fun doSomething() = 0
}
+6
View File
@@ -0,0 +1,6 @@
class Test {
/**
* do something
*/
fun doSomething() = 0
}
@@ -72,6 +72,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTest(); doTest();
} }
public void testKDoc() throws Exception {
doTest();
}
public void testMultilineFunctionLiteral() throws Exception { public void testMultilineFunctionLiteral() throws Exception {
doTest(); doTest();
} }