Java Code Examples for com.jetbrains.php.codeInsight.PhpCodeInsightUtil#findScopeForUseOperator()

The following examples show how to use com.jetbrains.php.codeInsight.PhpCodeInsightUtil#findScopeForUseOperator() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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());
}