Java Code Examples for de.espend.idea.php.annotation.util.AnnotationUtil#getUseImportMap()

The following examples show how to use de.espend.idea.php.annotation.util.AnnotationUtil#getUseImportMap() . 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: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement psiElement = completionParameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(psiElement, PhpDocComment.class);
    if(parentOfType == null) {
        return;
    }

    AnnotationTarget annotationTarget = PhpElementsUtil.findAnnotationTarget(parentOfType);
    if(annotationTarget == null) {
        return;
    }

    Map<String, String> importMap = AnnotationUtil.getUseImportMap((PsiElement) parentOfType);

    Project project = completionParameters.getPosition().getProject();
    attachLookupElements(project, importMap , annotationTarget, completionResultSet);

}
 
Example 2
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public void testGetUseImportMap() {
    PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "use Foobar;\n" +
        "use Bar as MyFoo" +
        "\n" +
        "/**\n" +
        " * @Foo()\n" +
        " **/\n" +
        "class Foo() {}\n"
    );

    Map<String, String> propertyForEnum = AnnotationUtil.getUseImportMap((PsiElement) phpDocTag);

    assertEquals("\\Foobar", propertyForEnum.get("Foobar"));
    assertEquals("\\Bar", propertyForEnum.get("MyFoo"));
}
 
Example 3
Source File: TemplateAnnotationIndex.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
private void visitPhpClass(@NotNull PhpClass phpClass) {
    String fqn = phpClass.getFQN();
    if(!fqn.startsWith("\\")) {
        fqn = "\\" + fqn;
    }

    PhpDocComment phpDocComment = phpClass.getDocComment();
    if (phpDocComment != null) {
        Map<String, String> useImportMap = null;
        for (PhpDocTag phpDocTag : GenericsUtil.getTagElementsByNameForAllFrameworks(phpDocComment, "extends")) {
            String tagValue = phpDocTag.getTagValue();

            Matcher matcher = CLASS_EXTENDS_MATCHER.matcher(tagValue);
            if (!matcher.find()) {
                continue;
            }

            String extendsClass = matcher.group(1);
            String type = matcher.group(2);

            // init the imports scope; to be only loaded once
            if (useImportMap == null) {
                useImportMap = AnnotationUtil.getUseImportMap(phpDocComment);
            }

            // resolve the class name based on the scope of namespace and use statement
            // eg: "@template BarAlias\MyContainer<Bar\Foobar>" we need global namespaces starting with "\"
            extendsClass = GenericsUtil.getFqnClassNameFromScope(fqn, extendsClass, useImportMap);
            type = GenericsUtil.getFqnClassNameFromScope(fqn, type, useImportMap);

            map.put(fqn, new TemplateAnnotationUsage(
                fqn,
                TemplateAnnotationUsage.Type.EXTENDS,
                0,
                extendsClass + "::" + type
            ));
        }
    }

}
 
Example 4
Source File: AnnotationInspectionUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@NotNull
public Map<String, String> getImports() {
    if (imports != null) {
        return this.imports;
    }

    return this.imports = AnnotationUtil.getUseImportMap(psiElement);
}
 
Example 5
Source File: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement psiElement = completionParameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PsiElement phpDocTag = psiElement.getParent();
    if(!(phpDocTag instanceof PhpDocTag)) {
        return;
    }

    String name = ((PhpDocTag) phpDocTag).getName();
    if(!(name.startsWith("@"))) {
        return;
    }

    int start = name.indexOf("\\");
    if(start == -1) {
        return;
    }

    name = name.substring(1, start);

    Map<String, String> importMap = AnnotationUtil.getUseImportMap(phpDocTag);
    if(!importMap.containsKey(name)) {
        return;
    }

    // find annotation scope, to filter classes
    AnnotationTarget annotationTarget = PhpElementsUtil.findAnnotationTarget(PsiTreeUtil.getParentOfType(psiElement, PhpDocComment.class));
    if(annotationTarget == null) {
        annotationTarget = AnnotationTarget.UNKNOWN;
    }


    // force trailing backslash on namespace
    String namespace = importMap.get(name);
    if(!namespace.startsWith("\\")) {
        namespace = "\\" + namespace;
    }


    Map<String, PhpAnnotation> annotationMap = AnnotationUtil.getAnnotationsOnTargetMap(psiElement.getProject(), AnnotationTarget.ALL, AnnotationTarget.UNDEFINED, AnnotationTarget.UNKNOWN, annotationTarget);

    for(PhpClass phpClass: PhpIndexUtil.getPhpClassInsideNamespace(psiElement.getProject(), namespace)) {
        String fqnName = StringUtils.stripStart(phpClass.getFQN(), "\\");
        if(annotationMap.containsKey(fqnName)) {
            PhpAnnotation phpAnnotation = annotationMap.get(fqnName);
            if(phpAnnotation != null && phpAnnotation.matchOneOf(AnnotationTarget.ALL, AnnotationTarget.UNDEFINED, AnnotationTarget.UNKNOWN, annotationTarget)) {
                String subNamespace = fqnName.substring(namespace.length());
                String lookupString = name + "\\" + subNamespace;
                completionResultSet.addElement(LookupElementBuilder.create(lookupString).withTypeText(phpClass.getPresentableFQN(), true).withIcon(phpClass.getIcon()).withInsertHandler(AnnotationTagInsertHandler.getInstance()));
            }
        }

    }

}