KT-2242 formatting problems: function literal passed outside the parentheses

#KT-2242 Fixed
This commit is contained in:
Nikolay Krasko
2012-08-21 12:52:42 +04:00
parent d38b965bf1
commit 2e92594462
13 changed files with 123 additions and 10 deletions
@@ -32,6 +32,10 @@ import org.jetbrains.jet.plugin.JetLanguage;
import java.util.*;
import static org.jetbrains.jet.JetNodeTypes.BLOCK;
import static org.jetbrains.jet.JetNodeTypes.FUNCTION_LITERAL;
import static org.jetbrains.jet.lexer.JetTokens.*;
/**
* @author yole
* @see Block for good JavaDoc documentation
@@ -46,7 +50,6 @@ public class JetBlock extends AbstractBlock {
private static final TokenSet CODE_BLOCKS = TokenSet.create(
JetNodeTypes.BLOCK,
JetNodeTypes.CLASS_BODY,
JetNodeTypes.FUNCTION_LITERAL_EXPRESSION,
JetNodeTypes.FUNCTION_LITERAL);
// private static final List<IndentWhitespaceRule>
@@ -129,7 +132,34 @@ public class JetBlock extends AbstractBlock {
@Override
public Spacing getSpacing(Block child1, Block child2) {
return mySpacingBuilder.getSpacing(this, child1, child2);
Spacing spacing = mySpacingBuilder.getSpacing(this, child1, child2);
if (spacing != null) {
return spacing;
}
// TODO: extend SpacingBuilder API - afterInside(RBRACE, FUNCTION_LITERAL).spacing(...), beforeInside(RBRACE, FUNCTION_LITERAL).spacing(...)
IElementType parentType = this.getNode().getElementType();
IElementType child1Type = ((ASTBlock) child1).getNode().getElementType();
IElementType child2Type = ((ASTBlock) child2).getNode().getElementType();
if (parentType == FUNCTION_LITERAL && child1Type == LBRACE && child2Type == BLOCK) {
return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
if (parentType == FUNCTION_LITERAL && child1Type == ARROW && child2Type == BLOCK) {
return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
if (parentType == FUNCTION_LITERAL && child2Type == RBRACE) {
return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
if (parentType == FUNCTION_LITERAL && child1Type == LBRACE) {
return Spacing.createSpacing(1, 1, 0, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
return null;
}
@NotNull
@@ -248,13 +278,13 @@ public class JetBlock extends AbstractBlock {
static ASTIndentStrategy[] INDENT_RULES = new ASTIndentStrategy[] {
ASTIndentStrategy.forNode("No indent for braces in blocks")
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL_EXPRESSION)
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL)
.forType(JetTokens.RBRACE, JetTokens.LBRACE)
.set(Indent.getNoneIndent()),
ASTIndentStrategy.forNode("Indent for block content")
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL_EXPRESSION)
.notForType(JetTokens.RBRACE, JetTokens.LBRACE)
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL)
.notForType(JetTokens.RBRACE, JetTokens.LBRACE, JetNodeTypes.BLOCK)
.set(Indent.getNormalIndent()),
ASTIndentStrategy.forNode("Indent for property accessors")
@@ -96,6 +96,8 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
.afterInside(COLON, CLASS).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON)
.beforeInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON)
.afterInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON)
.between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1)
;
}
@@ -0,0 +1,9 @@
fun test(some: (Int) -> Int) {
}
fun foo() {
test() {
// Some comment
it
}
}
@@ -0,0 +1,9 @@
fun test(some: (Int) -> Int) {
}
fun foo() {
test() {
// Some comment
it
}
}
@@ -0,0 +1,4 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {if (true) { 0 } else { 1 }}
@@ -0,0 +1,4 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {a -> if (true) { a } else { 1 }}
@@ -0,0 +1,10 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() { a ->
if (true) {
a
} else {
1
}
}
@@ -0,0 +1,10 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {
if (true) {
0
} else {
1
}
}
@@ -0,0 +1,4 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() { it }
@@ -0,0 +1,4 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() { it }
@@ -0,0 +1,6 @@
fun test(some : (Int) -> Int) {
}
fun foo() {
test() { it }
}
@@ -0,0 +1,6 @@
fun test(some: (Int) -> Int) {
}
fun foo() {
test() { it }
}
@@ -18,7 +18,6 @@ package org.jetbrains.jet.formatter;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
/**
* Based on com.intellij.psi.formatter.java.JavaFormatterTest
@@ -33,6 +32,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTest();
}
public void testCommentInFunctionLiteral() throws Exception {
doTest();
}
public void testConsecutiveCalls() throws Exception {
doTest();
}
@@ -65,6 +68,14 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTest();
}
public void testMultilineFunctionLiteral() throws Exception {
doTest();
}
public void testMultilineFunctionLiteralWithParams() throws Exception {
doTest();
}
public void testParameters() throws Exception {
doTestWithInvert();
}
@@ -77,10 +88,18 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTestWithInvert();
}
public void testSingleLineFunctionLiteral() throws Exception {
doTest();
}
public void testSpaceAroundExtendColon() throws Exception {
doTestWithInvert();
}
public void testSpaceBeforeFunctionLiteral() throws Exception {
doTest();
}
public void testSpacesAroundOperations() throws Exception {
doTestWithInvert();
}
@@ -97,10 +116,6 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTest();
}
public static JetCodeStyleSettings getJetSettings() {
return getSettings().getCustomSettings(JetCodeStyleSettings.class);
}
public static CodeStyleSettings getSettings() {
return CodeStyleSettingsManager.getSettings(getProject());
}