Fixed formatter to work correctly with comments inside declaration nodes

This commit is contained in:
Valentin Kipyatkov
2014-10-03 19:56:42 +04:00
parent d6dc2531b0
commit 84ba96900a
5 changed files with 46 additions and 1 deletions
@@ -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) {
@@ -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)
}
}
}
}
@@ -0,0 +1,11 @@
/** Doc comment for A */
class A {
/** Doc comment for function */
fun foo() {
}
/**
* Doc comment for property
*/
val prop = 1
}
+6
View File
@@ -0,0 +1,6 @@
/** Doc comment for A */class A { /** Doc comment for function */
fun foo(){}
/**
* Doc comment for property
*/ val prop = 1
}
@@ -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");