Merge pull request #302 from sayon/indents

Better autoformatting
This commit is contained in:
Nikolay Krasko
2013-09-03 23:58:18 -07:00
17 changed files with 143 additions and 15 deletions
@@ -34,6 +34,11 @@ public class JetCodeStyleSettings extends CustomCodeStyleSettings {
public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true; public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true;
public boolean ALIGN_IN_COLUMNS_CASE_BRANCH = false; public boolean ALIGN_IN_COLUMNS_CASE_BRANCH = false;
public boolean SPACE_AROUND_FUNCTION_TYPE_ARROW = true;
public boolean SPACE_AROUND_WHEN_ARROW = true;
public boolean SPACE_BEFORE_LAMBDA_ARROW = true;
public static JetCodeStyleSettings getInstance(Project project) { public static JetCodeStyleSettings getInstance(Project project) {
return CodeStyleSettingsManager.getSettings(project).getCustomSettings(JetCodeStyleSettings.class); return CodeStyleSettingsManager.getSettings(project).getCustomSettings(JetCodeStyleSettings.class);
} }
@@ -36,11 +36,11 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE); PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE);
JetBlock block = new JetBlock( JetBlock block = new JetBlock(
containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,
createSpacingBuilder(settings)); createSpacingBuilder(settings));
return FormattingModelProvider.createFormattingModelForPsiFile( return FormattingModelProvider.createFormattingModelForPsiFile(
element.getContainingFile(), block, settings); element.getContainingFile(), block, settings);
} }
private static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) { private static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
@@ -106,7 +106,15 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
.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) .between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1)
.aroundInside(ARROW, WHEN_ENTRY).spaces(1) .beforeInside(ARROW, FUNCTION_LITERAL).spaceIf(jetSettings.SPACE_BEFORE_LAMBDA_ARROW)
//when
.aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW)
.beforeInside(LBRACE, WHEN).spacing(1, 1, 0, true, 0) //omit blank lines before '{' in 'when' statement
.aroundInside(ARROW, FUNCTION_TYPE).spaceIf(jetSettings.SPACE_AROUND_FUNCTION_TYPE_ARROW)
.betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1)
; ;
} }
@@ -57,17 +57,25 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
"}"; "}";
default: default:
return return
"open class Some {\n" + "open class Some {\n"+
" private val f = {(a: Int)->a*2}\n" + " private val f: (Int)->Int = { (a: Int) -> a * 2 }\n"+
" fun foo() : Int {\n" + " fun foo(): Int {\n"+
" val test : Int = 12\n" + " val test: Int = 12\n"+
" return test\n" + " for (i in 10..42) {\n"+
" }\n" + " println (when {\n"+
" private fun <T>foo2():Int where T : List<T> {\n" + " i < test -> -1\n"+
" return 0\n" + " i > test -> 1\n"+
" }\n" + " else -> 0\n"+
"}\n\n" + " })\n"+
"class AnotherClass<T:Any>: Some"; " }\n"+
" return test\n"+
" }\n"+
" private fun <T>foo2(): Int where T : List<T> {\n"+
" return 0\n"+
" }\n"+
"}\n"+
"class AnotherClass<T : Any> : Some()\n";
} }
} }
@@ -115,6 +123,18 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
"Insert whitespaces in simple one line methods", "Insert whitespaces in simple one line methods",
CodeStyleSettingsCustomizable.SPACES_OTHER); CodeStyleSettingsCustomizable.SPACES_OTHER);
consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_AROUND_FUNCTION_TYPE_ARROW",
"Surround arrow in function types with spaces",
CodeStyleSettingsCustomizable.SPACES_OTHER);
consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_AROUND_WHEN_ARROW",
"Surround arrow in \"when\" clause with spaces",
CodeStyleSettingsCustomizable.SPACES_OTHER);
consumer.showCustomOption(JetCodeStyleSettings.class, "SPACE_BEFORE_LAMBDA_ARROW",
"Before lambda arrow",
CodeStyleSettingsCustomizable.SPACES_OTHER);
break; break;
case WRAPPING_AND_BRACES_SETTINGS: case WRAPPING_AND_BRACES_SETTINGS:
consumer.showStandardOptions( consumer.showStandardOptions(
@@ -0,0 +1,4 @@
fun test(some: ((Int)->Int) ->Int) {
}
// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW
@@ -0,0 +1,4 @@
fun test(some: ((Int) -> Int) -> Int) {
}
// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW
@@ -0,0 +1,4 @@
fun test(some: ((Int)->Int)->Int) {
}
// SET_TRUE: SPACE_AROUND_FUNCTION_TYPE_ARROW
+3
View File
@@ -0,0 +1,3 @@
val f: (Int, Int, Int) -> Int = { x, y, z -> x + y + z }
// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW
@@ -0,0 +1,3 @@
val f: (Int, Int, Int) -> Int = { x, y, z-> x + y + z }
// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW
@@ -0,0 +1,3 @@
val f: (Int, Int, Int) -> Int = { x, y, z -> x + y + z }
// SET_FALSE: SPACE_BEFORE_LAMBDA_ARROW
@@ -0,0 +1,3 @@
fun f() {
array(1, 2, 3).map{ v -> v }
}
@@ -0,0 +1,3 @@
fun f() {
array(1, 2, 3).map { v -> v }
}
+8
View File
@@ -0,0 +1,8 @@
fun f(x: Any): Int {
return when (x) {
is Int ->1
else->0
}
}
// SET_FALSE: SPACE_AROUND_WHEN_ARROW
@@ -0,0 +1,8 @@
fun f(x: Any): Int {
return when (x) {
is Int->1
else->0
}
}
// SET_FALSE: SPACE_AROUND_WHEN_ARROW
@@ -0,0 +1,8 @@
fun f(x: Any): Int {
return when (x) {
is Int -> 1
else -> 0
}
}
// SET_FALSE: SPACE_AROUND_WHEN_ARROW
@@ -0,0 +1,17 @@
fun f(x: Any): Int {
return when (x)
{
is Int->1
else->0
}
}
@@ -0,0 +1,7 @@
fun f(x: Any): Int {
return when (x)
{
is Int -> 1
else -> 0
}
}
@@ -131,6 +131,26 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
doTest(); doTest();
} }
public void testWhenArrow() throws Exception {
doTestWithInvert();
}
public void testWhenLinesBeforeLbrace() throws Exception {
doTest();
}
public void testFunctionalType() throws Exception {
doTestWithInvert();
}
public void testLambdaArrow() throws Exception {
doTestWithInvert();
}
public void testReferenceExpressionFunctionLiteral() throws Exception {
doTest();
}
@Override @Override
public void doTest() throws Exception { public void doTest() throws Exception {
String originalFileText = AbstractJetFormatterTest.loadFile(getTestName(false) + ".kt"); String originalFileText = AbstractJetFormatterTest.loadFile(getTestName(false) + ".kt");