KT-2242 formatting problems: function literal passed outside the parentheses
#KT-2242 Fixed
This commit is contained in:
@@ -32,6 +32,10 @@ import org.jetbrains.jet.plugin.JetLanguage;
|
|||||||
|
|
||||||
import java.util.*;
|
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
|
* @author yole
|
||||||
* @see Block for good JavaDoc documentation
|
* @see Block for good JavaDoc documentation
|
||||||
@@ -46,7 +50,6 @@ public class JetBlock extends AbstractBlock {
|
|||||||
private static final TokenSet CODE_BLOCKS = TokenSet.create(
|
private static final TokenSet CODE_BLOCKS = TokenSet.create(
|
||||||
JetNodeTypes.BLOCK,
|
JetNodeTypes.BLOCK,
|
||||||
JetNodeTypes.CLASS_BODY,
|
JetNodeTypes.CLASS_BODY,
|
||||||
JetNodeTypes.FUNCTION_LITERAL_EXPRESSION,
|
|
||||||
JetNodeTypes.FUNCTION_LITERAL);
|
JetNodeTypes.FUNCTION_LITERAL);
|
||||||
|
|
||||||
// private static final List<IndentWhitespaceRule>
|
// private static final List<IndentWhitespaceRule>
|
||||||
@@ -129,7 +132,34 @@ public class JetBlock extends AbstractBlock {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Spacing getSpacing(Block child1, Block child2) {
|
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
|
@NotNull
|
||||||
@@ -248,13 +278,13 @@ public class JetBlock extends AbstractBlock {
|
|||||||
|
|
||||||
static ASTIndentStrategy[] INDENT_RULES = new ASTIndentStrategy[] {
|
static ASTIndentStrategy[] INDENT_RULES = new ASTIndentStrategy[] {
|
||||||
ASTIndentStrategy.forNode("No indent for braces in blocks")
|
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)
|
.forType(JetTokens.RBRACE, JetTokens.LBRACE)
|
||||||
.set(Indent.getNoneIndent()),
|
.set(Indent.getNoneIndent()),
|
||||||
|
|
||||||
ASTIndentStrategy.forNode("Indent for block content")
|
ASTIndentStrategy.forNode("Indent for block content")
|
||||||
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL_EXPRESSION)
|
.in(JetNodeTypes.BLOCK, JetNodeTypes.CLASS_BODY, JetNodeTypes.FUNCTION_LITERAL)
|
||||||
.notForType(JetTokens.RBRACE, JetTokens.LBRACE)
|
.notForType(JetTokens.RBRACE, JetTokens.LBRACE, JetNodeTypes.BLOCK)
|
||||||
.set(Indent.getNormalIndent()),
|
.set(Indent.getNormalIndent()),
|
||||||
|
|
||||||
ASTIndentStrategy.forNode("Indent for property accessors")
|
ASTIndentStrategy.forNode("Indent for property accessors")
|
||||||
|
|||||||
@@ -96,6 +96,8 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
|||||||
.afterInside(COLON, CLASS).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON)
|
.afterInside(COLON, CLASS).spaceIf(jetSettings.SPACE_AFTER_EXTEND_COLON)
|
||||||
.beforeInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON)
|
.beforeInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_BEFORE_EXTEND_COLON)
|
||||||
.afterInside(COLON, TYPE_PARAMETER).spaceIf(jetSettings.SPACE_AFTER_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.CodeStyleSettings;
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||||
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Based on com.intellij.psi.formatter.java.JavaFormatterTest
|
* Based on com.intellij.psi.formatter.java.JavaFormatterTest
|
||||||
@@ -33,6 +32,10 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCommentInFunctionLiteral() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void testConsecutiveCalls() throws Exception {
|
public void testConsecutiveCalls() throws Exception {
|
||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
@@ -65,6 +68,14 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMultilineFunctionLiteral() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMultilineFunctionLiteralWithParams() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void testParameters() throws Exception {
|
public void testParameters() throws Exception {
|
||||||
doTestWithInvert();
|
doTestWithInvert();
|
||||||
}
|
}
|
||||||
@@ -77,10 +88,18 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
|||||||
doTestWithInvert();
|
doTestWithInvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSingleLineFunctionLiteral() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void testSpaceAroundExtendColon() throws Exception {
|
public void testSpaceAroundExtendColon() throws Exception {
|
||||||
doTestWithInvert();
|
doTestWithInvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSpaceBeforeFunctionLiteral() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void testSpacesAroundOperations() throws Exception {
|
public void testSpacesAroundOperations() throws Exception {
|
||||||
doTestWithInvert();
|
doTestWithInvert();
|
||||||
}
|
}
|
||||||
@@ -97,10 +116,6 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JetCodeStyleSettings getJetSettings() {
|
|
||||||
return getSettings().getCustomSettings(JetCodeStyleSettings.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CodeStyleSettings getSettings() {
|
public static CodeStyleSettings getSettings() {
|
||||||
return CodeStyleSettingsManager.getSettings(getProject());
|
return CodeStyleSettingsManager.getSettings(getProject());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user