From e5f4377ee44871b70dfa972d7f137c1b0419aeb4 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 25 Jan 2012 14:58:28 +0400 Subject: [PATCH] - KT-1068 Formatter: no indent after for - KT-1098 After entering "if (lock == null)" and pressing enter indent is wrong - Move tests for formatter --- .../lang/parsing/JetExpressionParsing.java | 7 +- .../jet/plugin/formatter/JetBlock.java | 75 ++++++++----------- .../IndentationOnNewline/DoInFun.after.kt | 4 + .../formatter/IndentationOnNewline/DoInFun.kt | 3 + .../IndentationOnNewline/For.after.kt | 3 + .../formatter/IndentationOnNewline/For.kt | 2 + .../IndentationOnNewline/If.after.kt | 4 + .../formatter/IndentationOnNewline/If.kt | 3 + .../IndentationOnNewline/While.after.kt | 4 + .../formatter/IndentationOnNewline/While.kt | 3 + .../formatter/AbstractJetFormatterTest.java} | 6 +- .../jet/formatter/JetFormatterTest.java} | 2 +- .../formatter/JetTypingIndentationTest.java | 40 ++++++++++ 13 files changed, 106 insertions(+), 50 deletions(-) create mode 100644 idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/DoInFun.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/For.after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/For.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/If.after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/If.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/While.after.kt create mode 100644 idea/testData/formatter/IndentationOnNewline/While.kt rename idea/tests/{org.jetbrains.jet.formatter/AbstractKotlinFormatterTest.java => org/jetbrains/jet/formatter/AbstractJetFormatterTest.java} (96%) rename idea/tests/{org.jetbrains.jet.formatter/KotlinFormatterTest.java => org/jetbrains/jet/formatter/JetFormatterTest.java} (88%) create mode 100644 idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java index 4e7db15ef0e..1e67a337309 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java @@ -20,6 +20,7 @@ public class JetExpressionParsing extends AbstractJetParsing { private static final TokenSet WHEN_CONDITION_RECOVERY_SET = TokenSet.create(RBRACE, IN_KEYWORD, NOT_IN, IS_KEYWORD, NOT_IS, ELSE_KEYWORD); private static final TokenSet WHEN_CONDITION_RECOVERY_SET_WITH_ARROW = TokenSet.create(RBRACE, IN_KEYWORD, NOT_IN, IS_KEYWORD, NOT_IS, ELSE_KEYWORD, ARROW, DOT); + private static final ImmutableMap KEYWORD_TEXTS = tokenSetToMap(KEYWORDS); private static ImmutableMap tokenSetToMap(TokenSet tokens) { @@ -1406,9 +1407,9 @@ public class JetExpressionParsing extends AbstractJetParsing { parseControlStructureBody(); } - expect(WHILE_KEYWORD, "Expecting 'while' followed by a post-condition"); - - parseCondition(); + if (expect(WHILE_KEYWORD, "Expecting 'while' followed by a post-condition")) { + parseCondition(); + } loop.done(DO_WHILE); } diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index 2fd443a39c9..9bfa22ee50d 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -2,9 +2,9 @@ package org.jetbrains.jet.plugin.formatter; import com.intellij.formatting.*; import com.intellij.lang.ASTNode; -import com.intellij.openapi.util.TextRange; import com.intellij.psi.TokenType; import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.formatter.common.AbstractBlock; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; import org.jetbrains.annotations.NotNull; @@ -16,15 +16,14 @@ import java.util.Collections; import java.util.List; /** + * @see Block for good JavaDoc documentation * @author yole */ -public class JetBlock implements ASTBlock { - private ASTNode myNode; - private Alignment myAlignment; +public class JetBlock extends AbstractBlock { private Indent myIndent; - private Wrap myWrap; private CodeStyleSettings mySettings; private final SpacingBuilder mySpacingBuilder; + private List mySubBlocks; private static final TokenSet CODE_BLOCKS = TokenSet.create( @@ -35,52 +34,35 @@ public class JetBlock implements ASTBlock { private static final TokenSet STATEMENT_PARTS = TokenSet.create( JetNodeTypes.THEN, JetNodeTypes.ELSE); + + // private static final List - public JetBlock(ASTNode node, Alignment alignment, Indent indent, Wrap wrap, CodeStyleSettings settings, - SpacingBuilder spacingBuilder) { - myNode = node; - myAlignment = alignment; + public JetBlock(@NotNull ASTNode node, + Alignment alignment, + Indent indent, + Wrap wrap, + CodeStyleSettings settings, + SpacingBuilder spacingBuilder) { + + super(node, wrap, alignment); myIndent = indent; - myWrap = wrap; mySettings = settings; mySpacingBuilder = spacingBuilder; } - @Override - public ASTNode getNode() { - return myNode; - } - - @NotNull - @Override - public TextRange getTextRange() { - return myNode.getTextRange(); - } - - @Override - public Wrap getWrap() { - return myWrap; - } - @Override public Indent getIndent() { return myIndent; } @Override - public Alignment getAlignment() { - return myAlignment; - } - - @NotNull - @Override - public List getSubBlocks() { + protected List buildChildren() { if (mySubBlocks == null) { mySubBlocks = buildSubBlocks(); } return new ArrayList(mySubBlocks); } - + private List buildSubBlocks() { List blocks = new ArrayList(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { @@ -97,7 +79,8 @@ public class JetBlock implements ASTBlock { return Collections.unmodifiableList(blocks); } - private Block buildSubBlock(ASTNode child) { + @NotNull + private Block buildSubBlock(@NotNull ASTNode child) { Wrap wrap = null; Indent childIndent = Indent.getNoneIndent(); Alignment childAlignment = null; @@ -128,7 +111,7 @@ public class JetBlock implements ASTBlock { return new JetBlock(child, childAlignment, childIndent, wrap, mySettings, mySpacingBuilder); } - private static Indent indentIfNotBrace(ASTNode child) { + private static Indent indentIfNotBrace(@NotNull ASTNode child) { return child.getElementType() == JetTokens.RBRACE || child.getElementType() == JetTokens.LBRACE ? Indent.getNoneIndent() : Indent.getNormalIndent(); @@ -151,16 +134,18 @@ public class JetBlock implements ASTBlock { @NotNull @Override public ChildAttributes getChildAttributes(int newChildIndex) { - Indent childIndent = Indent.getNoneIndent(); - if (CODE_BLOCKS.contains(myNode.getElementType()) || myNode.getElementType() == JetNodeTypes.WHEN) { - childIndent = Indent.getNormalIndent(); - } - return new ChildAttributes(childIndent, null); - } + final IElementType type = getNode().getElementType(); + if (CODE_BLOCKS.contains(type) || + type == JetNodeTypes.WHEN || + type == JetNodeTypes.IF || + type == JetNodeTypes.FOR || + type == JetNodeTypes.WHILE || + type == JetNodeTypes.DO_WHILE) { - @Override - public boolean isIncomplete() { - return false; + return new ChildAttributes(Indent.getNormalIndent(), null); + } + + return new ChildAttributes(Indent.getNoneIndent(), null); } @Override diff --git a/idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt b/idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt new file mode 100644 index 00000000000..e168ec583e2 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/DoInFun.after.kt @@ -0,0 +1,4 @@ +fun some() { + do + +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/DoInFun.kt b/idea/testData/formatter/IndentationOnNewline/DoInFun.kt new file mode 100644 index 00000000000..4c17791e4e2 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/DoInFun.kt @@ -0,0 +1,3 @@ +fun some() { + do +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/For.after.kt b/idea/testData/formatter/IndentationOnNewline/For.after.kt new file mode 100644 index 00000000000..0fc22bb9341 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/For.after.kt @@ -0,0 +1,3 @@ +fun some() { + for (var i in 1..10) + \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/For.kt b/idea/testData/formatter/IndentationOnNewline/For.kt new file mode 100644 index 00000000000..e0306a79eb4 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/For.kt @@ -0,0 +1,2 @@ +fun some() { + for (var i in 1..10) \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/If.after.kt b/idea/testData/formatter/IndentationOnNewline/If.after.kt new file mode 100644 index 00000000000..ac818828a71 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/If.after.kt @@ -0,0 +1,4 @@ +fun some() { + if (3 > 5) + +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/If.kt b/idea/testData/formatter/IndentationOnNewline/If.kt new file mode 100644 index 00000000000..8f5f367e3b4 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/If.kt @@ -0,0 +1,3 @@ +fun some() { + if (3 > 5) +} \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/While.after.kt b/idea/testData/formatter/IndentationOnNewline/While.after.kt new file mode 100644 index 00000000000..568a7d9a336 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/While.after.kt @@ -0,0 +1,4 @@ +fun some() { + var t = 12 + while (t > 0) + \ No newline at end of file diff --git a/idea/testData/formatter/IndentationOnNewline/While.kt b/idea/testData/formatter/IndentationOnNewline/While.kt new file mode 100644 index 00000000000..93cb6c3c351 --- /dev/null +++ b/idea/testData/formatter/IndentationOnNewline/While.kt @@ -0,0 +1,3 @@ +fun some() { + var t = 12 + while (t > 0) \ No newline at end of file diff --git a/idea/tests/org.jetbrains.jet.formatter/AbstractKotlinFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java similarity index 96% rename from idea/tests/org.jetbrains.jet.formatter/AbstractKotlinFormatterTest.java rename to idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java index e4d1709d83c..0bc2dd41f47 100644 --- a/idea/tests/org.jetbrains.jet.formatter/AbstractKotlinFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/AbstractJetFormatterTest.java @@ -39,7 +39,7 @@ import java.util.Map; // Based on from com.intellij.psi.formatter.java.AbstractJavaFormatterTest @SuppressWarnings("UnusedDeclaration") -public abstract class AbstractKotlinFormatterTest extends LightIdeaTestCase { +public abstract class AbstractJetFormatterTest extends LightIdeaTestCase { protected enum Action {REFORMAT, INDENT} @@ -96,6 +96,10 @@ public abstract class AbstractKotlinFormatterTest extends LightIdeaTestCase { doTextTest(Action.REFORMAT, text, textAfter); } + public void doIndentTextTest(@NonNls final String text, @NonNls String textAfter) throws IncorrectOperationException { + doTextTest(Action.INDENT, text, textAfter); + } + public void doTextTest(final Action action, final String text, String textAfter) throws IncorrectOperationException { final PsiFile file = createFile("A.kt", text); diff --git a/idea/tests/org.jetbrains.jet.formatter/KotlinFormatterTest.java b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java similarity index 88% rename from idea/tests/org.jetbrains.jet.formatter/KotlinFormatterTest.java rename to idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java index 0230cda7c12..5f6ca551d5d 100644 --- a/idea/tests/org.jetbrains.jet.formatter/KotlinFormatterTest.java +++ b/idea/tests/org/jetbrains/jet/formatter/JetFormatterTest.java @@ -3,7 +3,7 @@ package org.jetbrains.jet.formatter; /** * Based on com.intellij.psi.formatter.java.JavaFormatterTest */ -public class KotlinFormatterTest extends AbstractKotlinFormatterTest { +public class JetFormatterTest extends AbstractJetFormatterTest { public void testBlockFor() throws Exception { doTest(); } diff --git a/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java new file mode 100644 index 00000000000..524c6d60f0a --- /dev/null +++ b/idea/tests/org/jetbrains/jet/formatter/JetTypingIndentationTest.java @@ -0,0 +1,40 @@ +package org.jetbrains.jet.formatter; + +import com.intellij.testFramework.LightCodeInsightTestCase; +import org.jetbrains.jet.plugin.PluginTestCaseBase; + +import java.io.File; + +/** + * @author Nikolay Krasko + */ +public class JetTypingIndentationTest extends LightCodeInsightTestCase { + public void testWhile() { + doFileNewlineTest(); + } + + public void testFor() { + doFileNewlineTest(); + } + + public void testIf() { + doFileNewlineTest(); + } + + public void testDoInFun() { + doFileNewlineTest(); + } + + public void doFileNewlineTest() { + configureByFile(getTestName(false) + ".kt"); + type('\n'); + checkResultByFile(getTestName(false) + ".after.kt"); + } + + @Override + protected String getTestDataPath() { + final String testRelativeDir = "formatter/IndentationOnNewline"; + return new File(PluginTestCaseBase.getTestDataPathBase(), testRelativeDir).getPath() + + File.separator; + } +}