|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.util.psi.matcher; |
| 2 | + |
| 3 | +import com.intellij.patterns.PlatformPatterns; |
| 4 | +import com.intellij.patterns.PsiElementPattern; |
| 5 | +import com.intellij.psi.PsiElement; |
| 6 | +import com.intellij.psi.PsiWhiteSpace; |
| 7 | +import com.intellij.psi.util.PsiTreeUtil; |
| 8 | +import com.jetbrains.php.lang.lexer.PhpTokenTypes; |
| 9 | +import com.jetbrains.php.lang.patterns.PhpPatterns; |
| 10 | +import com.jetbrains.php.lang.psi.elements.NewExpression; |
| 11 | +import com.jetbrains.php.lang.psi.elements.ParameterList; |
| 12 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 19 | + */ |
| 20 | +public class NamedValueWithKeyAndNewExpressionMatcher { |
| 21 | + |
| 22 | + /** |
| 23 | + * new Foobar( |
| 24 | + * route: '<caret>', |
| 25 | + * ); |
| 26 | + */ |
| 27 | + @NotNull |
| 28 | + public static PsiElementPattern.Capture<PsiElement> pattern() { |
| 29 | + return PhpPatterns.psiElement().withParent( |
| 30 | + PlatformPatterns.psiElement(StringLiteralExpression.class).afterLeafSkipping(PlatformPatterns.or( |
| 31 | + PlatformPatterns.psiElement(PsiWhiteSpace.class), |
| 32 | + PlatformPatterns.psiElement(PhpTokenTypes.WHITE_SPACE), |
| 33 | + PlatformPatterns.psiElement(PhpTokenTypes.opCOLON) |
| 34 | + ), PlatformPatterns.psiElement(PhpTokenTypes.IDENTIFIER)).withParent(PlatformPatterns.psiElement(ParameterList.class)) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * new Foobar( |
| 40 | + * route: '<caret>', |
| 41 | + * ); |
| 42 | + */ |
| 43 | + @Nullable |
| 44 | + public static Result match(@NotNull PsiElement psiElement, @NotNull Matcher matcher) { |
| 45 | + PsiElement parameterList = psiElement.getParent(); |
| 46 | + if (parameterList instanceof ParameterList) { |
| 47 | + PsiElement newExpression = parameterList.getParent(); |
| 48 | + if (newExpression instanceof NewExpression) { |
| 49 | + for (NewExpressionCall newExpressionCall : matcher.getNewExpressionCalls()) { |
| 50 | + PsiElement colon = PsiTreeUtil.prevCodeLeaf(psiElement); |
| 51 | + if (colon == null || colon.getNode().getElementType() != PhpTokenTypes.opCOLON) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + PsiElement identifier = PsiTreeUtil.prevCodeLeaf(colon); |
| 56 | + if (identifier == null || identifier.getNode().getElementType() != PhpTokenTypes.IDENTIFIER || !newExpressionCall.getNamedArgument().equals(identifier.getText())) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (!PhpElementsUtil.isNewExpressionPhpClassWithInstance((NewExpression) newExpression, newExpressionCall.getClazz())) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + return new Result(newExpressionCall, (NewExpression) newExpression, identifier); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + public static class Matcher { |
| 73 | + @NotNull |
| 74 | + private final NewExpressionCall[] classes; |
| 75 | + |
| 76 | + public Matcher(@NotNull String namedArgument, @NotNull String clazz) { |
| 77 | + this.classes = new NewExpressionCall[] { new NewExpressionCall(clazz, namedArgument)}; |
| 78 | + } |
| 79 | + |
| 80 | + public Matcher(@NotNull NewExpressionCall... classes) { |
| 81 | + this.classes = classes; |
| 82 | + } |
| 83 | + |
| 84 | + @NotNull |
| 85 | + public NewExpressionCall[] getNewExpressionCalls() { |
| 86 | + return classes; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static class Result { |
| 91 | + @NotNull |
| 92 | + private final NewExpression newExpression; |
| 93 | + |
| 94 | + @NotNull |
| 95 | + private final NewExpressionCall expressionCall; |
| 96 | + |
| 97 | + @NotNull |
| 98 | + private final PsiElement arrayKey; |
| 99 | + |
| 100 | + public Result(@NotNull NewExpressionCall expressionCall, @NotNull NewExpression newExpression, @NotNull PsiElement arrayKey) { |
| 101 | + this.expressionCall = expressionCall; |
| 102 | + this.newExpression = newExpression; |
| 103 | + this.arrayKey = arrayKey; |
| 104 | + } |
| 105 | + |
| 106 | + @NotNull |
| 107 | + public NewExpression getNewExpression() { |
| 108 | + return newExpression; |
| 109 | + } |
| 110 | + |
| 111 | + @NotNull |
| 112 | + public NewExpressionCall getExpressionCall() { |
| 113 | + return expressionCall; |
| 114 | + } |
| 115 | + |
| 116 | + @NotNull |
| 117 | + public PsiElement getArrayKey() { |
| 118 | + return arrayKey; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static class NewExpressionCall { |
| 123 | + @NotNull |
| 124 | + private final String clazz; |
| 125 | + |
| 126 | + @NotNull |
| 127 | + private final String namedArgument; |
| 128 | + |
| 129 | + public NewExpressionCall(@NotNull String clazz, @NotNull String namedArgument) { |
| 130 | + this.clazz = clazz; |
| 131 | + this.namedArgument = namedArgument; |
| 132 | + } |
| 133 | + |
| 134 | + @NotNull |
| 135 | + public String getClazz() { |
| 136 | + return clazz; |
| 137 | + } |
| 138 | + |
| 139 | + @NotNull |
| 140 | + public String getNamedArgument() { |
| 141 | + return namedArgument; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments