com.jetbrains.php.codeInsight.PhpCodeInsightUtil Java Examples

The following examples show how to use com.jetbrains.php.codeInsight.PhpCodeInsightUtil. 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 Thinkphp5-Plugin with MIT License 6 votes vote down vote up
@NotNull
public static Map<String, String> getUseImports(@NotNull PsiElement element) {
    // search for use alias in local file
    final Map<String, String> useImports = new HashMap<>();

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(element);
    if(scope == null) {
        return useImports;
    }

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for (PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #2
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 #3
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Map<String, String> getUseImportMap(@NotNull PsiElement phpDocComment) {
    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(phpDocComment);
    if(scope == null) {
        return Collections.emptyMap();
    }

    Map<String, String> useImports = new HashMap<>();

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for(PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #4
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 #5
Source File: AnnotationInspectionUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Nullable
public String getNamespace() {
    if (hasNamespace) {
        return this.namespace;
    }

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);

    if (scope instanceof PhpNamespace) {
        String namespaceFqn = ((PhpNamespace) scope).getFQN();
        if (PhpLangUtil.isFqn(namespaceFqn) && !PhpLangUtil.isGlobalNamespaceFQN(namespaceFqn)) {
            hasNamespace = true;
            return this.namespace = namespaceFqn;
        }
    }

    return this.namespace = null;
}
 
Example #6
Source File: DoctrineUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public static void importOrmUseAliasIfNotExists(@NotNull PhpClassMember field) {

        // check for already imported class aliases
        String qualifiedName = PhpDocUtil.getQualifiedName(field, DOCTRINE_ORM_MAPPING);
        if(qualifiedName == null || !qualifiedName.equals(DOCTRINE_ORM_MAPPING.substring(1))) {
            return;
        }

        // try to import:
        // use Doctrine\ORM\Mapping as ORM;
        PhpClass phpClass = field.getContainingClass();
        if(phpClass == null) {
            return;
        }

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

        PhpElementsUtil.insertUseIfNecessary(scopeForUseOperator, DOCTRINE_ORM_MAPPING, "ORM");
    }
 
Example #7
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static String insertUseIfNecessary(@NotNull PsiElement phpClass, @NotNull String fqnClasName) {
    if(!fqnClasName.startsWith("\\")) {
        fqnClasName = "\\" + fqnClasName;
    }

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

    if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(fqnClasName)) {
        PhpAliasImporter.insertUseStatement(fqnClasName, scopeForUseOperator);
    }

    for (Map.Entry<String, String> entry : PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).entrySet()) {
        if(fqnClasName.equals(entry.getValue())) {
            return entry.getKey();
        }
    }

    return null;
}
 
Example #8
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void replaceElementWithClassConstant(@NotNull PhpClass phpClass, @NotNull PsiElement originElement) throws Exception{
    String fqn = phpClass.getFQN();
    if(!fqn.startsWith("\\")) {
        fqn = "\\" + fqn;
    }

    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(originElement);
    if(scopeForUseOperator == null) {
        throw new Exception("Class fqn error");
    }

    if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(fqn)) {
        PhpAliasImporter.insertUseStatement(fqn, scopeForUseOperator);
    }

    originElement.replace(PhpPsiElementFactory.createPhpPsiFromText(
        originElement.getProject(),
        ClassConstantReference.class,
        "<?php " + phpClass.getName() + "::class"
    ));
}
 
Example #9
Source File: AnnotationUseImporter.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void insertUse(InsertionContext context, String fqnAnnotation) {
    PsiElement element = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset());
    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(element);

    if(null == scopeForUseOperator) {
        return;
    }

    // PhpCodeInsightUtil.canImport:
    // copied from PhpReferenceInsertHandler; throws an error on PhpContractUtil because of "fully qualified names only"
    // but that is catch on phpstorm side already; looks fixed now so use fqn

    if(!fqnAnnotation.startsWith("\\")) {
        fqnAnnotation = "\\" + fqnAnnotation;
    }

    // this looks suitable! :)
    if(PhpCodeInsightUtil.alreadyImported(scopeForUseOperator, fqnAnnotation) == null) {
        PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
        PhpAliasImporter.insertUseStatement(fqnAnnotation, scopeForUseOperator);
        PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
    }
}
 
Example #10
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 #11
Source File: AnnotationBackportUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Map<String, String> getUseImportMap(@NotNull PhpDocComment phpDocComment) {

    // search for use alias in local file
    final Map<String, String> useImports = new HashMap<>();

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(phpDocComment);
    if(scope == null) {
        return useImports;
    }

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for (PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #12
Source File: PhpElementsUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Map<String, String> getUseImports(@NotNull PsiElement element) {
    // search for use alias in local file
    final Map<String, String> useImports = new HashMap<>();

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(element);
    if(scope == null) {
        return useImports;
    }

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for (PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #13
Source File: GenericsUtil.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
@NotNull
private static Map<String, String> getUseImportMap(@Nullable PhpDocComment phpDocComment) {
    if(phpDocComment == null) {
        return Collections.emptyMap();
    }

    PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(phpDocComment);
    if(scope == null) {
        return Collections.emptyMap();
    }

    Map<String, String> useImports = new HashMap<>();

    for (PhpUseList phpUseList : PhpCodeInsightUtil.collectImports(scope)) {
        for(PhpUse phpUse : phpUseList.getDeclarations()) {
            String alias = phpUse.getAliasName();
            if (alias != null) {
                useImports.put(alias, phpUse.getFQN());
            } else {
                useImports.put(phpUse.getName(), phpUse.getFQN());
            }
        }
    }

    return useImports;
}
 
Example #14
Source File: DoctrineClassOrmAnnotationGenerateAction.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
protected void execute(@NotNull Editor editor, @NotNull PhpClass phpClass, @NotNull PsiFile psiFile) {
    // insert ORM alias
    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(phpClass.getFirstChild());
    if(scopeForUseOperator != null) {
        PhpElementsUtil.insertUseIfNecessary(scopeForUseOperator, DoctrineUtil.DOCTRINE_ORM_MAPPING, "ORM");
        PsiDocumentManager.getInstance(psiFile.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    }

    PhpDocUtil.addClassOrmDocs(phpClass, editor.getDocument(), psiFile);
}
 
Example #15
Source File: DoctrineEmbeddedClassAnnotationGenerateAction.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
protected void execute(@NotNull Editor editor, @NotNull PhpClass phpClass, @NotNull PsiFile psiFile) {
    // insert ORM alias
    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(phpClass.getFirstChild());
    if(scopeForUseOperator != null) {
        PhpElementsUtil.insertUseIfNecessary(scopeForUseOperator, DoctrineUtil.DOCTRINE_ORM_MAPPING, "ORM");
        PsiDocumentManager.getInstance(psiFile.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    }

    PhpDocUtil.addClassEmbeddedDocs(phpClass, editor.getDocument(), psiFile);
}
 
Example #16
Source File: ImportUseForAnnotationQuickFix.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
private void invoke(@NotNull PsiElement psiElement, @NotNull Pair<String, String> pair) {
    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement);
    if(scopeForUseOperator != null) {
        if (pair.getSecond() == null) {
            PhpAliasImporter.insertUseStatement(pair.getFirst(), scopeForUseOperator);
        } else {
            PhpAliasImporter.insertUseStatement(pair.getFirst(), pair.getSecond(), scopeForUseOperator);
        }
    }
}
 
Example #17
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Adds class as alias
 *
 * @param scopeForUseOperator Any element that is inside a namespace statement
 * @param nsClass \Class\Foo
 * @param alias Class\Foo as Bar
 */
public static void insertUseIfNecessary(@NotNull PhpPsiElement scopeForUseOperator, @NotNull String nsClass, @NotNull String alias) {
    // we need absolute class, else we get duplicate imports
    if(!nsClass.startsWith("\\")) {
        nsClass = "\\" + nsClass;
    }

    if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(nsClass)) {
        PhpAliasImporter.insertUseStatement(nsClass, alias, scopeForUseOperator);
    }
}
 
Example #18
Source File: PhpInjectFileReferenceCollector.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@Nullable
private static String getFQN(FunctionReference targetFunctionReference) {
    if (targetFunctionReference instanceof MethodReference) {
        PhpReference classReference = ObjectUtils.tryCast(((MethodReference)targetFunctionReference).getClassReference(), PhpReference.class);
        if (classReference != null) {
            return PhpCodeInsightUtil.getImmediateFQN(classReference) + "." + targetFunctionReference.getName();
        }
    }

    return PhpCodeInsightUtil.getImmediateFQN(targetFunctionReference);
}
 
Example #19
Source File: PhpBundleFileFactory.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
private static void insertUseIfNecessary(PhpPsiElement scopeForUseOperator, String nsClass) {
    if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(nsClass)) {
        PhpAliasImporter.insertUseStatement(nsClass, scopeForUseOperator);
    }
}
 
Example #20
Source File: AnnotationTagInsertHandler.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
/**
 * Insert class alias before PhpStorm tries to import a new use statement "\Foo\Bar as Car"
 */
private void preAliasInsertion(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    Collection<UseAliasOption> importsAliases = AnnotationUtil.getActiveImportsAliasesFromSettings();
    if(importsAliases.size() == 0) {
        return;
    }

    Object object = lookupElement.getObject();
    if(!(object instanceof PhpClass)) {
        return;
    }

    final String fqn = StringUtils.stripStart(((PhpClass) object).getFQN(), "\\");

    UseAliasOption useAliasOption = ContainerUtil.find(importsAliases, useAliasOption1 ->
        useAliasOption1.getAlias() != null &&
        useAliasOption1.getClassName() != null &&
        fqn.startsWith(StringUtils.stripStart(useAliasOption1.getClassName(), "\\"))
    );

    if(useAliasOption == null || useAliasOption.getClassName() == null || useAliasOption.getAlias() == null) {
        return;
    }

    PsiElement elementAt = context.getFile().findElementAt(context.getEditor().getCaretModel().getOffset());
    if(elementAt == null) {
        return;
    }


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

    String className = useAliasOption.getClassName();
    if(!className.startsWith("\\")) {
        className = "\\" + className;
    }

    PhpElementsUtil.insertUseIfNecessary(scopeForUseOperator, className, useAliasOption.getAlias());
    PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
}