Java Code Examples for com.jetbrains.php.lang.psi.PhpPsiElementFactory#createFromText()

The following examples show how to use com.jetbrains.php.lang.psi.PhpPsiElementFactory#createFromText() . 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: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public void testGetClassAnnotationAsUnknown() {
    Collection<Object[]> dataProvider = new ArrayList<Object[]>() {{
        add(new Object[] {"@Target()", AnnotationTarget.UNKNOWN});
        add(new Object[] {"@Target", AnnotationTarget.UNKNOWN});
        add(new Object[] {"", AnnotationTarget.UNDEFINED});
    }};

    for (Object[] objects: dataProvider) {
        PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
            "/**\n" +
            "* @Annotation\n" +
            "* "+ objects[0] +"\n" +
            "*/\n" +
            "class Foo {}\n"
        );

        PhpAnnotation classAnnotation = AnnotationUtil.getClassAnnotation(phpClass);
        List<AnnotationTarget> targets = classAnnotation.getTargets();

        assertContainsElements(targets, objects[1]);
    }
}
 
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: NewArrayValueLookupElement.java    From yiistorm with MIT License 5 votes vote down vote up
public void insertIntoArrayConfig(String string, String openFilePath) {

        String relpath = openFilePath.replace(project.getBasePath(), "").substring(1).replace("\\", "/");
        VirtualFile vf = project.getBaseDir().findFileByRelativePath(relpath);

        if (vf == null) {
            return;
        }
        PsiFile pf = PsiManager.getInstance(project).findFile(vf);

        String lineSeparator = " " + ProjectCodeStyleSettingsManager.getSettings(project).getLineSeparator();
        if (vf != null) {

            PsiElement groupStatement = pf.getFirstChild();
            if (groupStatement != null) {
                PsiDocumentManager.getInstance(project).commitDocument(pf.getViewProvider().getDocument());

                pf.getManager().reloadFromDisk(pf);
                for (PsiElement pl : groupStatement.getChildren()) {
                    if (pl.toString().equals("Return")) {
                        PsiElement[] pl2 = pl.getChildren();
                        if (pl2.length > 0 && pl2[0].toString().equals("Array creation expression")) {
                            ArrayCreationExpressionImpl ar = (ArrayCreationExpressionImpl) pl2[0];

                            ArrayHashElement p = (ArrayHashElement) PhpPsiElementFactory.createFromText(project,
                                    PhpElementTypes.HASH_ARRAY_ELEMENT,
                                    "array('" + string + "'=>'')");
                            PsiElement closingBrace = ar.getLastChild();
                            String preLast = closingBrace.getPrevSibling().toString();
                            if (!preLast.equals("Comma")) {
                                pf.getViewProvider().getDocument().insertString(
                                        closingBrace.getTextOffset(), "," + lineSeparator + p.getText());
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
 
Example 4
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetFormExtendedType() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
        "class Foobar\n" +
        "{\n" +
        "   public function getExtendedType()\n" +
        "   {\n" +
        "       return true ? 'foobar' : Foobar::class;" +
        "   }\n" +
        "}\n"
    );

    assertContainsElements(FormUtil.getFormExtendedType(phpClass), "foobar", "Foobar");

    phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
        "namespace Car {\n" +
        "   class Foo {}\n" +
        "}" +
        "" +
        "namespace Bar {\n" +
        "   use Car\n" +
        "   \n" +
        "   class Foobar\n" +
        "   {\n" +
        "       public function getExtendedType()\n" +
        "       {\n" +
        "           return Foo::class;" +
        "      }\n" +
        "   }\n" +
        "}"
    );

    assertContainsElements(FormUtil.getFormExtendedType(phpClass), "Bar\\Foo");
}
 
Example 5
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testThatAlreadyFqnNameMustNotSuggestAnyImport() {
    myFixture.copyFileToProject("doctrine.php");

    PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "/**\n" +
        "* @\\ORM\\Entity()\n" +
        "*/\n" +
        "class Foo {}\n"
    );

    Map<String, String> possibleImportClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
    assertEquals(0, possibleImportClasses.size());
}
 
Example 6
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testThatImportForClassIsSuggestedForAliasImportClass() {
    myFixture.copyFileToProject("doctrine.php");

    PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "/**\n" +
        "* @ORM\\Entity()\n" +
        "*/\n" +
        "class Foo {}\n"
    );

    Map<String, String> possibleImportClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
    assertEquals("ORM", possibleImportClasses.get("\\Doctrine\\ORM\\Mapping"));
}
 
Example 7
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testThatImportForClassIsSuggestedForImportedClass() {
    myFixture.copyFileToProject("doctrine.php");

    PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
        "/**\n" +
        "* @Entity()\n" +
        "*/\n" +
        "class Foo {}\n"
    );

    Map<String, String> possibleImportClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
    assertNull(possibleImportClasses.get("\\Doctrine\\ORM\\Mapping"));
}
 
Example 8
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetClassAnnotationAsArray() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Annotation\n" +
        "* @Target(\"PROPERTY\", \"METHOD\")\n" +
        "* @Target(\"ALL\")\n" +
        "*/\n" +
        "class Foo {}\n"
    );

    PhpAnnotation classAnnotation = AnnotationUtil.getClassAnnotation(phpClass);
    List<AnnotationTarget> targets = classAnnotation.getTargets();

    assertContainsElements(targets, AnnotationTarget.PROPERTY, AnnotationTarget.METHOD, AnnotationTarget.ALL);
}
 
Example 9
Source File: AnnotationUtilTest.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void testGetClassAnnotation() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
        "/**\n" +
        "* @Annotation\n" +
        "* @Target(\"PROPERTY\")\n" +
        "*/\n" +
        "class Foo {}\n"
    );

    PhpAnnotation classAnnotation = AnnotationUtil.getClassAnnotation(phpClass);
    List<AnnotationTarget> targets = classAnnotation.getTargets();

    assertContainsElements(targets, AnnotationTarget.PROPERTY);
}
 
Example 10
Source File: PhpElementsUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#getConstructorArgumentByName
 */
public void testGetConstructorArgumentByName() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php class Foobar { function __construct($foo, $foobar) {} };\n ");

    assertEquals(0, PhpElementsUtil.getConstructorArgumentByName(phpClass, "foo"));
    assertEquals(1, PhpElementsUtil.getConstructorArgumentByName(phpClass, "foobar"));
    assertEquals(-1, PhpElementsUtil.getConstructorArgumentByName(phpClass, "UNKNOWN"));
}
 
Example 11
Source File: TwigTypeResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Method createMethod(@NotNull String method) {
    return PhpPsiElementFactory.createFromText(
        getProject(),
        Method.class,
        "<?php interface F { function " + method + "(); }"
    );
}
 
Example 12
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetFormExtendedTypesAsYield() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
            "class Foobar\n" +
            "{\n" +
            "   public function getExtendedTypes()\n" +
            "   {\n" +
            "       yield Foobar::class;\n" +
            "       yield 'test';\n'" +
            "   }\n" +
            "}\n"
    );

    assertContainsElements(FormUtil.getFormExtendedType(phpClass), "Foobar", "test");
}
 
Example 13
Source File: FormUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void testGetFormExtendedTypesAsArray() {
    PhpClass phpClass = PhpPsiElementFactory.createFromText(getProject(), PhpClass.class, "<?php\n" +
            "class Foobar\n" +
            "{\n" +
            "   public function getExtendedTypes()\n" +
            "   {\n" +
            "       return [Foobar::class, 'test'];\n" +
            "   }\n" +
            "}\n"
    );

    assertContainsElements(FormUtil.getFormExtendedType(phpClass), "Foobar", "test");
}
 
Example 14
Source File: PhpDocUtil.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
private static void addPhpDocTag(@NotNull PhpNamedElement forElement, @NotNull Document document, @NotNull PsiFile file, @NotNull  PsiElement beforeElement, @NotNull String annotationClass, @Nullable String tagParameter) {

        String phpDocTagName = getQualifiedName(forElement, annotationClass);
        if(phpDocTagName == null) {
            return;
        }

        String tagString = "@" + phpDocTagName;
        if(tagParameter != null) {
            tagString += "(" + tagParameter + ")";
        }

        PhpDocComment docComment = forElement.getDocComment();

        if(docComment != null)  {

            PsiElement elementToInsert = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTag.class, "/** " + tagString + " */\\n");
            if(elementToInsert == null) {
                return;
            }

            PsiElement fromText = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocTokenTypes.DOC_LEADING_ASTERISK, "/** \n * @var */");
            docComment.addBefore(fromText, docComment.getLastChild());
            docComment.addBefore(elementToInsert, docComment.getLastChild());

            PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
            PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);

            return;
        }

        // new PhpDoc see PhpDocCommentGenerator
        docComment = PhpPsiElementFactory.createFromText(forElement.getProject(), PhpDocComment.class, "/**\n " + tagString + " \n */");
        if(docComment == null) {
            return;
        }

        PsiElement parent = beforeElement.getParent();
        int atOffset = beforeElement.getTextRange().getStartOffset() + 1;
        parent.addBefore(docComment, beforeElement);
        PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
        PsiElement atElement = file.findElementAt(atOffset);
        if (atElement != null)            {
            PsiElement docParent = PsiTreeUtil.findFirstParent(atElement, true, element -> ((element instanceof PhpDocComment)) || ((element instanceof PhpFile)));
            if ((docParent instanceof PhpDocComment)) {
                CodeStyleManager.getInstance(forElement.getProject()).reformatNewlyAddedElement(docParent.getParent().getNode(), docParent.getNode());
            }
        }

        PsiDocumentManager.getInstance(forElement.getProject()).doPostponedOperationsAndUnblockDocument(document);
        PsiDocumentManager.getInstance(forElement.getProject()).commitDocument(document);

    }
 
Example 15
Source File: FormOptionsUtilTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * @see FormOptionsUtil#getMethodReferenceStringParameter
 */
public void testGetMethodReferenceStringParameter() {
    MethodReference methodReference = PhpPsiElementFactory.createFromText(getProject(), MethodReference.class, "<?php class Foobar { function foo() { $this->bar('test', 'my_value'); } };\n");
    assertContainsElements(FormOptionsUtil.getMethodReferenceStringParameter(methodReference, new String[] {"foo"}, "bar", "test"), "my_value");
}
 
Example 16
Source File: FormOptionsUtilTest.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * @see FormOptionsUtil#getFormExtensionKeys
 */
public void testGetFormExtensionsKeysTargets() {
    StringLiteralExpression contents = PhpPsiElementFactory.createFromText(getProject(), StringLiteralExpression.class, "<?php 'BarType';");
    Collection<PsiElement> formExtensionsKeysTargets = FormOptionsUtil.getFormExtensionsKeysTargets(contents, "Options\\Bar\\Foobar");
    assertTrue(formExtensionsKeysTargets.size() > 0);
}
 
Example 17
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"));
}