Java Code Examples for com.jetbrains.php.lang.psi.PhpPsiUtil#getParentByCondition()

The following examples show how to use com.jetbrains.php.lang.psi.PhpPsiUtil#getParentByCondition() . 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: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Get class path on "use" path statement
 */
@Nullable
public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) {

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);
    if (scopeForUseOperator == null) {
        return null;
    }

    PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF);
    String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false);
    if (!PhpLangUtil.isFqn(qualifiedName)) {
        return qualifiedName;
    }

    // @TODO: remove full fqn fallback
    if(qualifiedName.startsWith("\\")) {
        qualifiedName = qualifiedName.substring(1);
    }

    return qualifiedName;
}
 
Example 2
Source File: PhpServiceIntention.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) {
    if(!Symfony2ProjectComponent.isEnabled(project)) {
        return false;
    }

    PsiElement parentByCondition = PhpPsiUtil.getParentByCondition(psiElement, Method.INSTANCEOF);
    if(parentByCondition == null) {
        return false;
    }

    PhpClass phpClass = PhpPsiUtil.getParentByCondition(psiElement, PhpClass.INSTANCEOF);
    if(phpClass == null) {
        return false;
    }

    return true;
}
 
Example 3
Source File: PhpDocUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get class path on "use" path statement
 */
@Nullable
public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) {

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);
    if (scopeForUseOperator == null) {
        return null;
    }

    PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF);
    String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false);
    if (!PhpLangUtil.isFqn(qualifiedName)) {
        return qualifiedName;
    }

    // @TODO: remove full fqn fallback
    if(qualifiedName.startsWith("\\")) {
        qualifiedName = qualifiedName.substring(1);
    }

    return qualifiedName;
}
 
Example 4
Source File: PhpHeaderDocumentationProvider.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
@Override
public @Nullable PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) {
    if (!isCallToHeaderFunc(contextElement)) {
        return null;
    }

    if (!(contextElement instanceof StringLiteralExpression)) {
        contextElement = PhpPsiUtil.getParentByCondition(contextElement, true, StringLiteralExpression.INSTANCEOF);
    }

    if (contextElement instanceof StringLiteralExpression) {
        String contents = ((StringLiteralExpression)contextElement).getContents();
        if (!contents.contains(":")) {
            return null;
        }
        String headerName = StringUtils.substringBefore(contents, ":");
        if (headerName.isEmpty() || !isHeaderName(headerName)) {
            return null;
        }
        return new HeaderDocElement(contextElement.getManager(), contextElement.getLanguage(), headerName);
    }
    return null;
}
 
Example 5
Source File: PhpElementsUtil.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
@Nullable
public static Function getFunction(PsiElement position) {
    FunctionReference functionReference = PhpPsiUtil.getParentByCondition(position, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF);
    if (functionReference != null) {
        return getFunction(functionReference);
    } else {
        NewExpression newExpression = PhpPsiUtil.getParentByCondition(position, true, NewExpression.INSTANCEOF, Statement.INSTANCEOF);
        if (newExpression != null) {
            ClassReference classReference = newExpression.getClassReference();
            if (classReference != null) {
                return getFunction(classReference);
            }
        }

        return null;
    }
}
 
Example 6
Source File: PhpHighlightPackParametersUsagesHandlerFactory.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
@Override
public @Nullable HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) {

    ParameterList parameterList = PhpPsiUtil.getParentByCondition(target, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF);
    if (parameterList == null) {
        return null;
    }

    FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class);
    String fqn = resolveFqn(functionCall);
    if (!"\\pack".equals(fqn)) {
        return null;
    }

    PsiElement[] parameters = parameterList.getParameters();
    PsiElement selectedParameter = StreamEx.of(parameters).findFirst((p) -> p.getTextRange().containsOffset(editor.getCaretModel().getOffset())).orElse(null);
    if (selectedParameter == null) {
        return null;
    }

    int selectedIndex = PhpCodeInsightUtil.getParameterIndex(selectedParameter);
    if (selectedIndex < 0 || selectedIndex >= parameters.length) {
        return null;
    }
    return new PhpHighlightPackParametersUsagesHandler(editor, file, 0, selectedIndex, parameters);
}
 
Example 7
Source File: PhpServiceIntention.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) throws IncorrectOperationException {

    PsiElement parentByCondition = PhpPsiUtil.getParentByCondition(psiElement, Method.INSTANCEOF);
    if(parentByCondition == null) {
        return;
    }

    PhpClass phpClass = PhpPsiUtil.getParentByCondition(psiElement, PhpClass.INSTANCEOF);
    if(phpClass == null) {
        return;
    }

    ServiceGenerateAction.invokeServiceGenerator(project, phpClass.getContainingFile(), phpClass, editor);
}
 
Example 8
Source File: PhpHeaderDocumentationProvider.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
private boolean isCallToHeaderFunc(PsiElement psiElement) {
    FunctionReference function = PhpPsiUtil.getParentByCondition(psiElement, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF);
    if (function == null) {
        return false;
    }
    return "header".equals(function.getName());
}
 
Example 9
Source File: PhpAutoPopupTypedHandler.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@NotNull
@Override
public Result checkAutoPopup(char charTyped, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if (!(file instanceof PhpFile)) {
        return Result.CONTINUE;
    }

    if (charTyped != '%') {
        return Result.CONTINUE;
    }

    int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);

    ParameterList parameterList = PhpPsiUtil.getParentByCondition(psiElement, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF);
    if (parameterList != null) {
        FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class);
        String fqn = PhpElementsUtil.resolveFqn(functionCall);

        if (/*charTyped == '%' &&*/ PhpElementsUtil.isFormatFunction(fqn)) {
            if (StringUtil.getPrecedingCharNum(editor.getDocument().getCharsSequence(), offset, '%') % 2 == 0) {
                PhpCompletionUtil.showCompletion(editor);
            }
        }
    }

    return Result.CONTINUE;
}
 
Example 10
Source File: PhpFormatDocumentationProvider.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
private String getCallToFormatFunc(PsiElement psiElement) {
    FunctionReference function = PhpPsiUtil.getParentByCondition(psiElement, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF);
    if (function == null) {
        return null;
    }
    return PhpElementsUtil.resolveFqn(function);
}
 
Example 11
Source File: PhpHighlightPackParametersUsagesHandler.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
private int resolveSpecificationIndexFromCaret(@NotNull HashMap<Integer, RelativeRange> specificationRelativeRanges) {
    int caretOffset = this.myEditor.getCaretModel().getOffset();
    StringLiteralExpression selectedLiteralExpression = PhpPsiUtil.getParentByCondition(this.myFile.findElementAt(caretOffset), false, StringLiteralExpression.INSTANCEOF);
    return StreamEx.of(specificationRelativeRanges.entrySet())
        .findFirst((e) -> specificationAtCaretOffsetExists(caretOffset, selectedLiteralExpression, e.getValue()))
        .map(Map.Entry::getKey).orElse(-1);
}