com.jetbrains.php.lang.patterns.PhpPatterns Java Examples

The following examples show how to use com.jetbrains.php.lang.patterns.PhpPatterns. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * array('foo' => FOO.class, 'foo1' => 'bar', 1 => 'foo')
 */
@NotNull
static public Map<String, PsiElement> getArrayKeyValueMapWithValueAsPsiElement(@NotNull ArrayCreationExpression arrayCreationExpression) {
    HashMap<String, PsiElement> keys = new HashMap<>();

    for(ArrayHashElement arrayHashElement: arrayCreationExpression.getHashElements()) {
        PhpPsiElement child = arrayHashElement.getKey();
        if(child != null && ((child instanceof StringLiteralExpression) || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) {

            String key;
            if(child instanceof StringLiteralExpression) {
                key = ((StringLiteralExpression) child).getContents();
            } else {
                key = child.getText();
            }

            if(key == null || StringUtils.isBlank(key)) {
                continue;
            }

            keys.put(key, arrayHashElement.getValue());
        }
    }

    return keys;
}
 
Example #2
Source File: YiiContibutorHelper.java    From yiistorm with MIT License 6 votes vote down vote up
public static PsiElementPattern.Capture<PsiElement> methodParamsList(String methodName,
                                                                     StringPattern className) {
    return PlatformPatterns.psiElement().withElementType(PhpElementTypes.PARAMETER_LIST)
            .withParent(
                    PlatformPatterns.psiElement()
                            .withElementType(PhpElementTypes.METHOD_REFERENCE)
                            .referencing(
                                    PhpPatterns.psiElement().withElementType(
                                            PhpElementTypes.CLASS_METHOD
                                    ).withName(methodName)
                                            .withParent(
                                                    PhpPatterns.psiElement().withName(
                                                            className
                                                    ))
                            )

            );
}
 
Example #3
Source File: YiiAppCompletionContributor.java    From yiistorm with MIT License 6 votes vote down vote up
public static PsiElementPattern.Capture appFieldPattern() {
    return PlatformPatterns.psiElement()
            .withParent(PlatformPatterns.psiElement().withElementType(PhpElementTypes.FIELD_REFERENCE)
                    .withChild(
                            PlatformPatterns.psiElement().withElementType(PhpElementTypes.METHOD_REFERENCE)
                                    .referencing(
                                            PhpPatterns.psiElement().withElementType(PhpElementTypes.CLASS_METHOD)
                                                    .withName("app").withParent(
                                                    PhpPatterns.psiElement()
                                                            .withElementType(PhpElementTypes.CLASS)
                                                            .withName(StandardPatterns.string().oneOf("Yii", "YiiBase"))
                                            )
                                    )
                    )
            ).withLanguage(PhpLanguage.INSTANCE);

}
 
Example #4
Source File: PhpElementsUtil.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
public static PsiElementPattern.Capture<PsiElement> isStringArrayValue() {

    return PhpPatterns.psiElement()
            .andOr(
                    PhpPatterns.psiElement().withParent(
                            PlatformPatterns.psiElement(StringLiteralExpression.class)
                                    .withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE))
                    ),
                    PlatformPatterns.psiElement(StringLiteralExpression.class)
                            .withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE))
            );
}
 
Example #5
Source File: ArrayKeyValueSignatureRegistrarMatcher.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
private ArrayCreationExpression findArrayCreationExpression(@NotNull StringLiteralExpression psiElement, @NotNull String key) {

    // value inside array
    // $menu->addChild(array(
    //   'route' => 'foo',
    // ));
    if(PhpPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).accepts(psiElement.getContext())) {
        PsiElement arrayValue = psiElement.getContext();
        if(arrayValue != null) {
            PsiElement arrayHashElement = arrayValue.getContext();
            if(arrayHashElement instanceof ArrayHashElement) {
                PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
                if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(key)) {
                    PsiElement arrayCreationExpression = arrayHashElement.getContext();
                    if(arrayCreationExpression instanceof ArrayCreationExpression) {
                        if(!(arrayCreationExpression.getParent() instanceof ParameterList)) {
                            return null;
                        }

                        return (ArrayCreationExpression) arrayCreationExpression;
                    }
                }
            }
        }
    }

    return null;
}
 
Example #6
Source File: ArrayKeyValueSignatureRegistrarMatcher.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
private ArrayCreationExpression findArrayCreationExpression(@NotNull StringLiteralExpression psiElement, @NotNull String key) {

    // value inside array
    // $menu->addChild(array(
    //   'route' => 'foo',
    // ));
    if(PhpPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).accepts(psiElement.getContext())) {
        PsiElement arrayValue = psiElement.getContext();
        if(arrayValue != null) {
            PsiElement arrayHashElement = arrayValue.getContext();
            if(arrayHashElement instanceof ArrayHashElement) {
                PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
                if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(key)) {
                    PsiElement arrayCreationExpression = arrayHashElement.getContext();
                    if(arrayCreationExpression instanceof ArrayCreationExpression) {
                        if(!(arrayCreationExpression.getParent() instanceof ParameterList)) {
                            return null;
                        }

                        return (ArrayCreationExpression) arrayCreationExpression;
                    }
                }
            }
        }
    }

    return null;
}
 
Example #7
Source File: DefaultReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public boolean isContributedElement(PsiElement psiElement, MethodParameterSetting config) {

            // value inside array
            // $menu->addChild(array(
            //   'route' => 'foo',
            // ));
            if(PhpPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).accepts(psiElement.getContext())) {
                PsiElement arrayValue = psiElement.getContext();
                if(arrayValue != null) {
                    PsiElement arrayHashElement = arrayValue.getContext();
                    if(arrayHashElement instanceof ArrayHashElement) {
                        PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
                        if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(config.getContributorData())) {
                            PsiElement arrayCreationExpression = arrayHashElement.getContext();
                            if(arrayCreationExpression instanceof ArrayCreationExpression) {
                                if(arrayCreationExpression.getParent() instanceof ParameterList) {
                                    return true;
                                }
                            }
                        }

                    }
                }

            }

            return false;
        }
 
Example #8
Source File: ArrayValueWithKeyAndNewExpressionMatcher.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * $menu->addChild([
 *   'route' => '<caret>',
 * ]);
 */
@NotNull
public static PsiElementPattern.Capture<PsiElement> pattern() {
    return PhpPatterns.psiElement().withParent(
        PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).inside(ParameterList.class)
        )
    );
}
 
Example #9
Source File: ArrayValueWithKeyAndMethodMatcher.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * $menu->addChild([
 *   'route' => '<caret>',
 * ]);
 */
@NotNull
public static PsiElementPattern.Capture<PsiElement> pattern() {
    return PhpPatterns.psiElement().withParent(
        PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).inside(ParameterList.class)
        )
    );
}
 
Example #10
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * array('foo' => 'bar', 'foo1' => 'bar', 1 => 'foo')
 */
@NotNull
static public HashMap<String, String> getArrayKeyValueMap(@NotNull ArrayCreationExpression arrayCreationExpression) {
    HashMap<String, String> keys = new HashMap<>();

    for(ArrayHashElement arrayHashElement: arrayCreationExpression.getHashElements()) {
        PhpPsiElement child = arrayHashElement.getKey();
        if(child != null && ((child instanceof StringLiteralExpression) || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) {

            String key;
            if(child instanceof StringLiteralExpression) {
                key = ((StringLiteralExpression) child).getContents();
            } else {
                key = child.getText();
            }

            if(key == null || StringUtils.isBlank(key)) {
                continue;
            }

            String value = null;

            if(arrayHashElement.getValue() instanceof StringLiteralExpression) {
                value = ((StringLiteralExpression) arrayHashElement.getValue()).getContents();
            }

            if(value == null || StringUtils.isBlank(value)) {
                continue;
            }

            keys.put(key, value);
        }
    }

    return keys;
}
 
Example #11
Source File: YiiContibutorHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static PhpElementPattern.Capture<StringLiteralExpression> methodLiteralExpression(String methodName,
                                                                                         StringPattern className) {
    return PhpPatterns.phpLiteralExpression()
            .withParent(
                    methodParamsList(methodName, className)
            );
}
 
Example #12
Source File: YiiContibutorHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static PsiElementPattern.Capture<PsiElement> paramListInMethodWithName(String methodName) {
    return PlatformPatterns.psiElement().withElementType(PhpElementTypes.PARAMETER_LIST)
            .withParent(
                    PlatformPatterns.psiElement().withElementType(PhpElementTypes.METHOD_REFERENCE)
                            .referencing(PhpPatterns.psiElement().withElementType(PhpElementTypes.CLASS_METHOD)
                                    .withName(methodName)
                            )
            );
}
 
Example #13
Source File: YiiContibutorHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static PsiElementPattern.Capture<PsiElement> arrayInMethodWithName(String methodName) {
    return PlatformPatterns.psiElement().withElementType(PhpElementTypes.ARRAY_CREATION_EXPRESSION)
            .withParent(
                    PlatformPatterns.psiElement().withElementType(PhpElementTypes.METHOD_REFERENCE)
                            .referencing(PhpPatterns.psiElement().withElementType(PhpElementTypes.CLASS_METHOD)
                                    .withName(methodName)
                            )
            );
}
 
Example #14
Source File: PhpAnnotationCompletionConfidence.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {

    if(!(psiFile instanceof PhpFile)) {
        return ThreeState.UNSURE;
    }

    PsiElement context = contextElement.getContext();
    if(context instanceof StringLiteralExpression) {

        // foo="<|>"
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocString).accepts(context)) {
            return ThreeState.NO;
        }

    } else if(context instanceof PhpDocComment) {

        // * <|>
        if(PhpPatterns.psiElement().afterLeafSkipping(
            PhpPatterns.psiElement(PsiWhiteSpace.class),
            PhpPatterns.psiElement(PhpDocTokenTypes.DOC_LEADING_ASTERISK)
        ).accepts(contextElement)) {
            return ThreeState.NO;
        }

    } else if(context instanceof PhpPsiElementImpl) {

        // @Foo(<|>)
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList).accepts(context)) {
            return ThreeState.NO;
        }

        // @<|>
        if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocTag).accepts(context)) {
            return ThreeState.NO;
        }

    }

    return ThreeState.UNSURE;
}
 
Example #15
Source File: YiiApplicationReferenceContributor.java    From yiistorm with MIT License 3 votes vote down vote up
@Override
public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar) {

    psiReferenceRegistrar.registerReferenceProvider(PhpPatterns.psiElement(PhpElementTypes.PARAMETER),
            new PhpStringLiteralExpressionReference(ParameterReference.class)
                    .addCall("Yii", "getParameter")
    );


}