lambda: spaces around arrow

This commit is contained in:
Anna Kozlova
2012-08-27 18:45:23 +04:00
parent 85903981fc
commit 0a0c7c5f63
9 changed files with 46 additions and 2 deletions
@@ -188,7 +188,8 @@ public class JavaLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
"public class Foo<T, U> {\n" +
" int[] X = new int[]{1, 3, 5, 6, 7, 87, 1213, 2};\n" +
"\n" +
" public void foo(int x, int y) {\n" +
" public void foo(int x, int y) {" +
" Runnable r = () -> {};\n" +
" for (int i = 0; i < x; i++) {\n" +
" y += (y ^ 0x123) << 2;\n" +
" }\n" +
@@ -1168,6 +1168,14 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
}
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
if ((myRole1 == ChildRole.ARROW && (myRole2 == ChildRole.LBRACE || myRole2 == ChildRole.EXPRESSION)) ||
(myRole1 == ChildRole.PARAMETER_LIST && myRole2 == ChildRole.ARROW)) {
createSpaceInCode(mySettings.SPACE_AROUND_LAMBDA_ARROW);
}
}
@Override public void visitForStatement(PsiForStatement statement) {
if (myRole2 == ChildRole.LPARENTH) {
createSpaceInCode(mySettings.SPACE_BEFORE_FOR_PARENTHESES);
@@ -156,6 +156,7 @@ public class ChildRole {
public static final int ANNOTATION_VALUE = 249; // in NAME_VALUE_PAIR, ANNOTATION_ARRAY_INITIALIZER
public static final int ANNOTATION = 250; // in MODIFIER_LIST
public static final int CATCH_SECTION = 251; // in TRY_STATEMENT
public static final int ARROW = 252; // in LAMBDA STATEMENT
private ChildRole() {
}
@@ -15,11 +15,14 @@
*/
package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -40,6 +43,20 @@ public class PsiLambdaExpressionImpl extends ExpressionPsiElement implements Psi
return PsiTreeUtil.getRequiredChildOfType(this, PsiParameterList.class);
}
@Override
public int getChildRole(ASTNode child) {
final IElementType elType = child.getElementType();
if (elType == JavaTokenType.ARROW) {
return ChildRole.ARROW;
} else if (elType == JavaElementType.PARAMETER_LIST) {
return ChildRole.PARAMETER_LIST;
} else if (elType == JavaElementType.CODE_BLOCK) {
return ChildRole.LBRACE;
} else {
return ChildRole.EXPRESSION;
}
}
@Override
public PsiElement getBody() {
final PsiElement element = getLastChild();
@@ -301,6 +301,18 @@ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest {
"try { } catch (E1|E2 e) { }");
}
public void testSpacesInsideLambda() throws Exception {
getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
getSettings().SPACE_AROUND_LAMBDA_ARROW = true;
doMethodTest("()->{}",
"() -> {}");
getSettings().SPACE_AROUND_LAMBDA_ARROW = false;
doMethodTest("() -> {}",
"()->{}");
}
public void testSpacesBeforeResourceList() throws Exception {
getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
getSettings().BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
@@ -32,6 +32,7 @@ public interface CodeStyleSettingsCustomizable {
SPACE_AROUND_MULTIPLICATIVE_OPERATORS,
SPACE_AROUND_SHIFT_OPERATORS,
SPACE_AROUND_UNARY_OPERATOR,
SPACE_AROUND_LAMBDA_ARROW,
SPACE_AFTER_COMMA,
SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS,
SPACE_BEFORE_COMMA,
@@ -517,6 +517,8 @@ public class CommonCodeStyleSettings {
public boolean SPACE_AROUND_UNARY_OPERATOR = false;
public boolean SPACE_AROUND_LAMBDA_ARROW = true;
public boolean SPACE_AFTER_COMMA = true;
public boolean SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS = true;
public boolean SPACE_BEFORE_COMMA = false;
@@ -53,6 +53,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_AROUND_MULTIPLICATIVE_OPERATORS", ApplicationBundle.message("checkbox.spaces.multiplicative.operators"), SPACES_AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_SHIFT_OPERATORS", ApplicationBundle.message("checkbox.spaces.shift.operators"), SPACES_AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_UNARY_OPERATOR", ApplicationBundle.message("checkbox.spaces.around.unary.operator"), SPACES_AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_LAMBDA_ARROW", ApplicationBundle.message("checkbox.spaces.around.lambda.arrow"), SPACES_AROUND_OPERATORS);
initCustomOptions(SPACES_AROUND_OPERATORS);
initBooleanField("SPACE_BEFORE_CLASS_LBRACE", ApplicationBundle.message("checkbox.spaces.class.left.brace"), SPACES_BEFORE_LEFT_BRACE);
@@ -558,4 +558,5 @@ arrangement.text.negate=Negate
arrangement.action.rule.add.text=Add rule
arrangement.action.rule.add.description=Add new arrangement rule
arrangement.action.rule.remove.text=Remove rule
arrangement.action.rule.remove.description=Remove selected arrangement rule
arrangement.action.rule.remove.description=Remove selected arrangement rule
checkbox.spaces.around.lambda.arrow=Lambda arrow