diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyAssignmentExpressionPattern.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyAssignmentExpressionPattern.java new file mode 100644 index 000000000000..7a95db57cd35 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyAssignmentExpressionPattern.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.psi.patterns; + +import com.intellij.patterns.ElementPattern; +import com.intellij.patterns.PatternCondition; +import com.intellij.util.ProcessingContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression; + +public class GroovyAssignmentExpressionPattern extends GroovyExpressionPattern { + protected GroovyAssignmentExpressionPattern() { + super(GrAssignmentExpression.class); + } + + public GroovyAssignmentExpressionPattern left(@NotNull final ElementPattern pattern) { + return with(new PatternCondition("left") { + public boolean accepts(@NotNull final GrAssignmentExpression psiBinaryExpression, final ProcessingContext context) { + return pattern.getCondition().accepts(psiBinaryExpression.getLValue(), context); + } + }); + } + + public GroovyAssignmentExpressionPattern right(@NotNull final ElementPattern pattern) { + return with(new PatternCondition("right") { + public boolean accepts(@NotNull final GrAssignmentExpression psiBinaryExpression, final ProcessingContext context) { + return pattern.getCondition().accepts(psiBinaryExpression.getRValue(), context); + } + }); + } + + public GroovyAssignmentExpressionPattern operation(final ElementPattern pattern) { + return with(new PatternCondition("operation") { + public boolean accepts(@NotNull final GrAssignmentExpression psiBinaryExpression, final ProcessingContext context) { + return pattern.getCondition().accepts(psiBinaryExpression.getOperationToken(), context); + } + }); + } + +} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyPatterns.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyPatterns.java index 13864a087d40..992876369a78 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyPatterns.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/patterns/GroovyPatterns.java @@ -36,10 +36,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression; import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil; @@ -58,6 +55,10 @@ public class GroovyPatterns extends PsiJavaPatterns { return new GroovyBinaryExpressionPattern(); } + public static GroovyAssignmentExpressionPattern groovyAssignmentExpression() { + return new GroovyAssignmentExpressionPattern(); + } + public static GroovyElementPattern.Capture groovyLiteralExpression() { return groovyLiteralExpression(null); } @@ -71,6 +72,23 @@ public class GroovyPatterns extends PsiJavaPatterns { }); } + public static GroovyElementPattern.Capture rightOfAssignment(final ElementPattern value, + final GroovyAssignmentExpressionPattern assignment) { + return new GroovyElementPattern.Capture(new InitialPatternCondition(GroovyPsiElement.class) { + @Override + public boolean accepts(@Nullable Object o, ProcessingContext context) { + if (!(o instanceof GroovyPsiElement)) return false; + + PsiElement parent = ((GroovyPsiElement)o).getParent(); + if (!(parent instanceof GrAssignmentExpression)) return false; + + if (((GrAssignmentExpression)parent).getRValue() != o) return false; + + return assignment.getCondition().accepts(parent, context) && value.getCondition().accepts(o, context); + } + }); + } + public static GroovyElementPattern.Capture stringLiteral() { return new GroovyElementPattern.Capture(new InitialPatternCondition(GrLiteralImpl.class) { public boolean accepts(@Nullable final Object o, final ProcessingContext context) { @@ -91,7 +109,8 @@ public class GroovyPatterns extends PsiJavaPatterns { PsiElement nameElement = ((GrArgumentLabel)o).getNameElement(); if (nameElement instanceof LeafPsiElement) { IElementType elementType = ((LeafPsiElement)nameElement).getElementType(); - if (elementType == GroovyElementTypes.mIDENT || CommonClassNames.JAVA_LANG_STRING.equals(TypesUtil.getPsiTypeName(elementType))) { + if (elementType == GroovyElementTypes.mIDENT || + CommonClassNames.JAVA_LANG_STRING.equals(TypesUtil.getPsiTypeName(elementType))) { return namePattern.accepts(((GrArgumentLabel)o).getName()); } }