Java Code Examples for com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag#getFirstPsiChild()

The following examples show how to use com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag#getFirstPsiChild() . 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: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get the property value by given name
 *
 * "@Template(template="foobar.html.twig")"
 */
@Nullable
public static String getPropertyValue(@NotNull PhpDocTag phpDocTag, @NotNull String property) {
    PhpPsiElement attributeList = phpDocTag.getFirstPsiChild();
    if(attributeList == null || attributeList.getNode().getElementType() != PhpDocElementTypes.phpDocAttributeList) {
        return null;
    }

    PsiElement lParen = attributeList.getFirstChild();
    if(lParen == null) {
        return null;
    }

    StringLiteralExpression psiProperty = getPropertyValueAsPsiElement(phpDocTag, property);
    if(psiProperty == null) {
        return null;
    }

    String contents = psiProperty.getContents();
    if(StringUtils.isNotBlank(contents)) {
        return contents;
    }

    return null;
}
 
Example 2
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get the property value as string by given name
 *
 * "@Template(template="foobar.html.twig")"
 */
@Nullable
public static StringLiteralExpression getPropertyValueAsPsiElement(@NotNull PhpDocTag phpDocTag, @NotNull String property) {
    PhpPsiElement attributeList = phpDocTag.getFirstPsiChild();
    if(attributeList == null || attributeList.getNode().getElementType() != PhpDocElementTypes.phpDocAttributeList) {
        return null;
    }

    PsiElement lParen = attributeList.getFirstChild();
    if(lParen == null) {
        return null;
    }

    PsiElement psiProperty = Arrays.stream(attributeList.getChildren())
        .filter(psiElement1 -> getPropertyIdentifierValue(property).accepts(psiElement1))
        .findFirst()
        .orElse(null);

    return psiProperty instanceof StringLiteralExpression ?
        (StringLiteralExpression) psiProperty :
        null;
}
 
Example 3
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get the property value as PsiElement by given name
 *
 * "@Template(template=Foobar::class)"
 */
@Nullable
public static PsiElement getPropertyValueAsElement(@NotNull PhpDocTag phpDocTag, @NotNull String property) {
    PhpPsiElement attributeList = phpDocTag.getFirstPsiChild();
    if(attributeList == null || attributeList.getNode().getElementType() != PhpDocElementTypes.phpDocAttributeList) {
        return null;
    }

    PsiElement lParen = attributeList.getFirstChild();
    if(lParen == null) {
        return null;
    }

    return Arrays.stream(attributeList.getChildren())
        .filter(psiElement1 -> getPropertyIdentifierValueAsPsiElement(property).accepts(psiElement1))
        .findFirst()
        .orElse(null);
}
 
Example 4
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Get default property value from annotation "@Template("foo");
 *
 * @return Content of property value literal
 */
@Nullable
public static String getDefaultPropertyValue(@NotNull PhpDocTag phpDocTag) {
    PhpPsiElement phpDocAttrList = phpDocTag.getFirstPsiChild();

    if(phpDocAttrList != null) {
        if(phpDocAttrList.getNode().getElementType() == PhpDocElementTypes.phpDocAttributeList) {
            PhpPsiElement phpPsiElement = phpDocAttrList.getFirstPsiChild();
            if(phpPsiElement instanceof StringLiteralExpression) {
                return ((StringLiteralExpression) phpPsiElement).getContents();
            }
        }
    }

    return null;
}
 
Example 5
Source File: DoctrineOrmRepositoryIntention.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {

    PhpClass phpClass = getScopedPhpClass(element);
    if(phpClass == null) {
        return;
    }

    String fileName = phpClass.getName() + "Repository.php";
    String namespace = DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName());
    PsiDirectory dir = phpClass.getContainingFile().getContainingDirectory();

    PsiDirectory repositoryDir = dir;
    if (dir.getParentDirectory() != null) {
        PsiDirectory checkDir = dir.getParentDirectory().findSubdirectory("Repository");

        if (dir.findFile(fileName) == null && checkDir != null) {
            repositoryDir = checkDir;
        }
    }

    String repoClass = phpClass.getPresentableFQN() + "Repository";
    PhpClass repoPhpClass = PhpElementsUtil.getClass(project, repoClass);

    if (repoPhpClass == null && dir != repositoryDir) {
        // Entity/../Repository/
        namespace = phpClass.getNamespaceName();
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace.substring(0, namespace.lastIndexOf("\\"));
        namespace = namespace + "\\Repository";
        namespace = DoctrineUtil.trimBlackSlashes(namespace);

        repoClass = namespace + "\\" + phpClass.getName() + "Repository";
        repoClass = PhpLangUtil.toPresentableFQN(repoClass);

        repoPhpClass = PhpElementsUtil.getClass(project, repoClass);
    }

    if(repoPhpClass == null) {
        if(repositoryDir.findFile(fileName) == null) {
            final FileTemplate fileTemplate = FileTemplateManager.getInstance(project).getTemplate("Doctrine Entity Repository");
            final Properties defaultProperties = FileTemplateManager.getInstance(project).getDefaultProperties();

            Properties properties = new Properties(defaultProperties);

            properties.setProperty("NAMESPACE", namespace);
            properties.setProperty("NAME", phpClass.getName() + "Repository");

            properties.setProperty("ENTITY_NAMESPACE", DoctrineUtil.trimBlackSlashes(phpClass.getNamespaceName()));
            properties.setProperty("ENTITY_NAME", phpClass.getName());

            try {
                PsiElement newElement = FileTemplateUtil.createFromTemplate(fileTemplate, fileName, properties, repositoryDir);

                new OpenFileDescriptor(project, newElement.getContainingFile().getVirtualFile(), 0).navigate(true);
            } catch (Exception e) {
                return;
            }
        } else {
            if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
                HintManager.getInstance().showErrorHint(editor, "Repository already exists ");
            }
        }
    }

    PhpDocTagAnnotation ormEntityPhpDocBlock = DoctrineUtil.getOrmEntityPhpDocBlock(phpClass);
    if(ormEntityPhpDocBlock != null) {
        PhpDocTag phpDocTag = ormEntityPhpDocBlock.getPhpDocTag();
        PhpPsiElement firstPsiChild = phpDocTag.getFirstPsiChild();
        insertAttribute(editor, repoClass, phpDocTag, firstPsiChild);
    }

}
 
Example 6
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
/**
 * Extract property value or fallback on default annotation pattern
 *
 * "@Template("foobar.html.twig")"
 * "@Template(template="foobar.html.twig")"
 */
@Nullable
public static String getPropertyValueOrDefault(@NotNull PhpDocTag phpDocTag, @NotNull String property) {
    PhpPsiElement attributeList = phpDocTag.getFirstPsiChild();
    if(attributeList == null || attributeList.getNode().getElementType() != PhpDocElementTypes.phpDocAttributeList) {
        return null;
    }

    // @Template(template="foobar.html.twig")
    PsiElement psiProperty = Arrays.stream(attributeList.getChildren())
        .filter(psiElement1 -> getPropertyIdentifierValue(property).accepts(psiElement1))
        .findFirst()
        .orElse(null);

    String contents = null;

    // find property value: template="foobar.html.twig"
    if(psiProperty != null) {
        if(!(psiProperty instanceof StringLiteralExpression)) {
            return null;
        }

        contents = ((StringLiteralExpression) psiProperty).getContents();
    }

    // default value: @Template("foobar.html.twig")
    if(contents == null) {
        PsiElement defaultItem = attributeList.getFirstChild();
        if(defaultItem != null) {
            PsiElement defaultValue = defaultItem.getNextSibling();
            if(defaultValue instanceof StringLiteralExpression) {
                contents = ((StringLiteralExpression) defaultValue).getContents();
            }
        }
    }

    if(StringUtils.isNotBlank(contents)) {
        return contents;
    }

    return contents;
}