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

The following examples show how to use de.espend.idea.php.annotation.util.AnnotationUtil#visitAttributes() . 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: AnnotationGoToDeclarationHandler.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Add goto for property value which are Fields inside PhpClass
 *
 * @param psiElement origin DOC_IDENTIFIER psi element
 * @param targets Goto targets
 */
private void addPropertyGoto(PsiElement psiElement, List<PsiElement> targets) {
    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
    if(phpDocTag == null) {
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return;
    }

    String property = psiElement.getText();
    if(StringUtils.isBlank(property)) {
        return;
    }

    AnnotationUtil.visitAttributes(phpClass, (attributeName, type, target) -> {
        if(attributeName.equals(property)) {
            targets.add(target);
        }

        return null;
    });

    // extension point to provide virtual properties / fields targets
    AnnotationVirtualPropertyTargetsParameter parameter = null;
    for (PhpAnnotationVirtualProperties ep : AnnotationUtil.EP_VIRTUAL_PROPERTIES.getExtensions()) {
        if(parameter == null) {
            parameter = new AnnotationVirtualPropertyTargetsParameter(phpClass, psiElement, property);
        }

        ep.getTargets(parameter);
    }

    if(parameter != null) {
        targets.addAll(parameter.getTargets());
    }
}
 
Example 2
Source File: PhpAnnotationTypeCompletionProvider.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public void getPropertyValueCompletions(AnnotationPropertyParameter annotationPropertyParameter, AnnotationCompletionProviderParameter completionParameter) {
    String propertyName = annotationPropertyParameter.getPropertyName();
    if(!annotationPropertyParameter.getType().equals(AnnotationPropertyParameter.Type.PROPERTY_VALUE) && propertyName == null) {
        return;
    }

    Set<String> values = new HashSet<>();
    AnnotationUtil.visitAttributes(annotationPropertyParameter.getPhpClass(), (attributeName, type, target) -> {
        if(!attributeName.equals(propertyName)) {
            return null;
        }

        if (AnnotationPropertyEnum.fromString(type) == AnnotationPropertyEnum.BOOLEAN) {
            values.addAll(Arrays.asList("false", "true"));
        }

        // @Enum({"AUTO", "SEQUENCE"})
        if (target instanceof Field) {
            PhpDocComment docComment = ((Field) target).getDocComment();
            if(docComment != null) {
                PhpDocTag[] phpDocTags = docComment.getTagElementsByName("@Enum");
                for(PhpDocTag phpDocTag: phpDocTags) {
                    PhpPsiElement phpDocAttrList = phpDocTag.getFirstPsiChild();
                    if(phpDocAttrList != null) {
                        String enumArrayString = phpDocAttrList.getText();
                        Pattern targetPattern = Pattern.compile("\"(\\w+)\"");

                        Matcher matcher = targetPattern.matcher(enumArrayString);
                        while (matcher.find()) {
                            values.add(matcher.group(1));
                        }
                    }
                }
            }
        }

        return null;
    });

    for(String s: values) {
        completionParameter.getResult().addElement(LookupElementBuilder.create(s));
    }
}
 
Example 3
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, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement psiElement = completionParameters.getOriginalPosition();
    if (psiElement == null) {
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
    if (phpDocTag == null) {
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if (phpClass == null) {
        return;
    }

    AnnotationUtil.visitAttributes(phpClass, (attributeName, type, target) -> {
        completionResultSet.addElement(new PhpAnnotationPropertyLookupElement(new AnnotationProperty(attributeName, AnnotationPropertyEnum.fromString(type))));
        return null;
    });

    // extension point for virtual properties
    AnnotationVirtualPropertyCompletionParameter virtualPropertyParameter = null;
    AnnotationCompletionProviderParameter parameter = null;

    for (PhpAnnotationVirtualProperties ep : AnnotationUtil.EP_VIRTUAL_PROPERTIES.getExtensions()) {
        if (virtualPropertyParameter == null) {
            virtualPropertyParameter = new AnnotationVirtualPropertyCompletionParameter(phpClass);
        }

        if (parameter == null) {
            parameter = new AnnotationCompletionProviderParameter(completionParameters, processingContext, completionResultSet);
        }

        ep.addCompletions(virtualPropertyParameter, parameter);
    }

    if (virtualPropertyParameter != null) {
        for (Map.Entry<String, AnnotationPropertyEnum> pair : virtualPropertyParameter.getLookupElements().entrySet()) {
            completionResultSet.addElement(new PhpAnnotationPropertyLookupElement(
                new AnnotationProperty(pair.getKey(), pair.getValue()))
            );
        }
    }
}
 
Example 4
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
public void testAttributeVisitingForAnnotationClass() {
    myFixture.copyFileToProject("doctrine.php");

    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Attributes(\n" +
        "*    @Attribute(\"accessControl\", type=\"string\"),\n" +
        "*    @Attribute(\"accessControl2\", type=\"string\"),\n" +
        "*    @Attribute(\"typeNull\"),\n" +
        "* )\n" +
        "*\n" +
        "* @Attributes({\n" +
        "*    @Attribute(\"array\", type=\"array\"),\n" +
        "*    @Attribute(\"array2\", type=\"array\"),\n" +
        "* })\n" +
        "*/\n" +
        "class Foo\n" +
        "{" +
        "   public string $foo;\n" +
        "   \n" +
        "   /** @var boolean **/\n" +
        "   public $bool;\n" +
        "   public $myArray = [];\n" +
        "   public $myBool = false;\n" +
        "   public ?string $nullable;\n" +
        "}\n"
    );

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

    AnnotationUtil.visitAttributes(phpClass, (attribute, type, psiElement) -> {
        attributes.put(attribute, type);
        return null;
    });

    assertContainsElements(attributes.keySet(), "accessControl", "accessControl2", "array", "array2", "foo");

    assertEquals("array", attributes.get("array2"));
    assertEquals("string", attributes.get("accessControl"));
    assertEquals("bool", attributes.get("bool"));

    assertEquals("array", attributes.get("myArray"));
    assertEquals("bool", attributes.get("myBool"));
    assertEquals("string", attributes.get("nullable"));
    assertNull(attributes.get("typeNull"));
}