KT-2242 formatting problems: function literal passed outside the parentheses - option for spaces in single line function literal

#KT-2242 Fixed
This commit is contained in:
Nikolay Krasko
2012-08-21 16:10:45 +04:00
parent e7287ba8f4
commit 9c91acac74
10 changed files with 52 additions and 9 deletions
@@ -143,8 +143,13 @@ public class JetBlock extends AbstractBlock {
IElementType child1Type = ((ASTBlock) child1).getNode().getElementType();
IElementType child2Type = ((ASTBlock) child2).getNode().getElementType();
JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class);
int spacesInSimpleMethod = jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD ? 1 : 0;
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);
return Spacing.createDependentLFSpacing(
spacesInSimpleMethod, spacesInSimpleMethod, this.getTextRange(),
mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
if (parentType == FUNCTION_LITERAL && child1Type == ARROW && child2Type == BLOCK) {
@@ -152,11 +157,14 @@ public class JetBlock extends AbstractBlock {
}
if (parentType == FUNCTION_LITERAL && child2Type == RBRACE) {
return Spacing.createDependentLFSpacing(1, 1, this.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
return Spacing.createDependentLFSpacing(
spacesInSimpleMethod, spacesInSimpleMethod, 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 Spacing.createSpacing(spacesInSimpleMethod, spacesInSimpleMethod, 0,
mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
return null;
@@ -31,6 +31,8 @@ public class JetCodeStyleSettings extends CustomCodeStyleSettings {
public boolean SPACE_BEFORE_EXTEND_COLON = false;
public boolean SPACE_AFTER_EXTEND_COLON = true;
public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true;
public static JetCodeStyleSettings getInstance(Project project) {
return CodeStyleSettingsManager.getSettings(project).getCustomSettings(JetCodeStyleSettings.class);
}
@@ -52,10 +52,12 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
" 14\n" +
" )\n" +
" }\n" +
" private val f = {(a: Int)->a*2}\n" +
"}";
default:
return
"class Some {\n" +
" private val f = {(a: Int)->a*2}\n" +
" fun foo() : Int {\n" +
" val test : Int = 12\n" +
" return test\n" +
@@ -104,6 +106,11 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_BEFORE_EXTEND_COLON",
"Space before colon in new type definition",
CodeStyleSettingsCustomizable.SPACES_OTHER);
consumer.showCustomOption(JetCodeStyleSettings.class, "INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD",
"Insert whitespaces in simple one line methods",
CodeStyleSettingsCustomizable.SPACES_OTHER);
break;
case WRAPPING_AND_BRACES_SETTINGS:
consumer.showStandardOptions(
@@ -1,4 +1,6 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {a -> if (true) { a } else { 1 }}
fun foo() = test() {a -> if (true) { a } else { 1 }}
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -7,4 +7,6 @@ fun foo() = test() { a ->
} else {
1
}
}
}
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -0,0 +1,12 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {a ->
if (true) {
a
} else {
1
}
}
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -1,4 +1,6 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() { it }
fun foo() = test() { it }
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -1,4 +1,6 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() { it }
fun foo() = test() { it }
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -0,0 +1,6 @@
fun test(some: (Int) -> Int) {
}
fun foo() = test() {it}
// SET_TRUE: INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD
@@ -73,7 +73,7 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
}
public void testMultilineFunctionLiteralWithParams() throws Exception {
doTest();
doTestWithInvert();
}
public void testParameters() throws Exception {
@@ -93,7 +93,7 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
}
public void testSingleLineFunctionLiteral() throws Exception {
doTest();
doTestWithInvert();
}
public void testSpaceAroundExtendColon() throws Exception {