Java Code Examples for com.intellij.patterns.PlatformPatterns

The following examples show how to use com.intellij.patterns.PlatformPatterns. These examples are extracted from open source projects. 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 Project: idea-php-symfony2-plugin   Source File: PsiElementUtils.java    License: MIT License 6 votes vote down vote up
@Nullable
public static MethodReference getMethodReferenceWithFirstStringParameter(PsiElement psiElement) {
    if(!PlatformPatterns.psiElement()
        .withParent(StringLiteralExpression.class).inside(ParameterList.class)
        .withLanguage(PhpLanguage.INSTANCE).accepts(psiElement)) {

        return null;
    }

    ParameterList parameterList = PsiTreeUtil.getParentOfType(psiElement, ParameterList.class);
    if (parameterList == null) {
        return null;
    }

    if (!(parameterList.getContext() instanceof MethodReference)) {
        return null;
    }

    return (MethodReference) parameterList.getContext();
}
 
Example 2
Source Project: idea-php-symfony2-plugin   Source File: TwigUtil.java    License: MIT License 6 votes vote down vote up
@Nullable
public static String getTwigMethodString(@Nullable PsiElement transPsiElement) {
    if (transPsiElement == null) return null;

    ElementPattern<PsiElement> pattern = PlatformPatterns.psiElement(TwigTokenTypes.RBRACE);

    String currentText = transPsiElement.getText();
    for (PsiElement child = transPsiElement.getNextSibling(); child != null; child = child.getNextSibling()) {
        currentText = currentText + child.getText();
        if (pattern.accepts(child)) {
            //noinspection unchecked
            return currentText;
        }
    }

    return null;
}
 
Example 3
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
/**
 * Provide a workaround for getting a FUNCTION scope as it not consistent in all Twig elements
 *
 * {% if asset('') %}
 * {{ asset('') }}
 */
@NotNull
private static ElementPattern<PsiElement> getFunctionCallScopePattern() {
    return PlatformPatterns.or(
        // old and inconsistently implementations of FUNCTION_CALL:
        // eg {% if asset('') %} does not provide a FUNCTION_CALL whereas a print block does
        PlatformPatterns.psiElement(TwigElementTypes.PRINT_BLOCK),
        PlatformPatterns.psiElement(TwigElementTypes.TAG),
        PlatformPatterns.psiElement(TwigElementTypes.IF_TAG),
        PlatformPatterns.psiElement(TwigElementTypes.SET_TAG),
        PlatformPatterns.psiElement(TwigElementTypes.ELSE_TAG),
        PlatformPatterns.psiElement(TwigElementTypes.ELSEIF_TAG),
        PlatformPatterns.psiElement(TwigElementTypes.FOR_TAG),

        // PhpStorm 2017.3.2: {{ asset('') }}
        PlatformPatterns.psiElement(TwigElementTypes.FUNCTION_CALL)
    );
}
 
Example 4
/**
 * @see fr.adrienbrault.idea.symfony2plugin.completion.command.ConsoleHelperGotoCompletionRegistrar
 */
public void testHelperSet() {
    assertCompletionContains(PhpFileType.INSTANCE, "<?php" +
            "/** @var \\Symfony\\Component\\Console\\Helper\\HelperSet $foo */\n" +
            "$foo->has('<caret>');",
        "foo"
    );

    assertNavigationMatch(PhpFileType.INSTANCE, "<?php" +
            "/** @var \\Symfony\\Component\\Console\\Helper\\HelperSet $foo */\n" +
            "$foo->has('fo<caret>o');",
        PlatformPatterns.psiElement(PhpClass.class)
    );

    assertCompletionContains(PhpFileType.INSTANCE, "<?php" +
            "/** @var \\Symfony\\Component\\Console\\Helper\\HelperSet $foo */\n" +
            "$foo->get('<caret>');",
        "foo"
    );

    assertNavigationMatch(PhpFileType.INSTANCE, "<?php" +
            "/** @var \\Symfony\\Component\\Console\\Helper\\HelperSet $foo */\n" +
            "$foo->get('fo<caret>o');",
        PlatformPatterns.psiElement(PhpClass.class)
    );
}
 
Example 5
Source Project: idea-php-symfony2-plugin   Source File: PhpElementsUtil.java    License: MIT License 6 votes vote down vote up
static public PsiElementPattern.Capture<StringLiteralExpression> getFunctionWithFirstStringPattern(@NotNull String... functionName) {
    return PlatformPatterns
        .psiElement(StringLiteralExpression.class)
        .withParent(
            PlatformPatterns.psiElement(ParameterList.class)
                .withFirstChild(
                    PlatformPatterns.psiElement(PhpElementTypes.STRING)
                )
                .withParent(
                    PlatformPatterns.psiElement(FunctionReference.class).with(new PatternCondition<FunctionReference>("function match") {
                        @Override
                        public boolean accepts(@NotNull FunctionReference functionReference, ProcessingContext processingContext) {
                            return ArrayUtils.contains(functionName, functionReference.getName());
                        }
                    })
                )
        )
        .withLanguage(PhpLanguage.INSTANCE);
}
 
Example 6
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
public static ElementPattern<PsiElement> getTemplateFileReferenceTagPattern(String... tagNames) {

        // {% include '<xxx>' with {'foo' : bar, 'bar' : 'foo'} %}

        //noinspection unchecked
        return PlatformPatterns
            .psiElement(TwigTokenTypes.STRING_TEXT)
            .afterLeafSkipping(
                PlatformPatterns.or(
                    PlatformPatterns.psiElement(TwigTokenTypes.LBRACE),
                    PlatformPatterns.psiElement(PsiWhiteSpace.class),
                    PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
                    PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
                    PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
                ),
                PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME).withText(PlatformPatterns.string().oneOf(tagNames))
            )
            .withLanguage(TwigLanguage.INSTANCE);
    }
 
Example 7
Source Project: idea-php-symfony2-plugin   Source File: TwigTypeResolveUtil.java    License: MIT License 6 votes vote down vote up
/**
 * Get items before foo.bar.car, foo.bar.car()
 *
 * ["foo", "bar"]
 */
@NotNull
public static Collection<String> formatPsiTypeName(@NotNull PsiElement psiElement) {
    String typeNames = PhpElementsUtil.getPrevSiblingAsTextUntil(psiElement, PlatformPatterns.or(
        PlatformPatterns.psiElement(TwigTokenTypes.LBRACE),
        PlatformPatterns.psiElement(PsiWhiteSpace.class
    )));

    if(typeNames.trim().length() == 0) {
        return Collections.emptyList();
    }

    if(typeNames.endsWith(".")) {
        typeNames = typeNames.substring(0, typeNames.length() -1);
    }

    Collection<String> possibleTypes = new ArrayList<>();
    if(typeNames.contains(".")) {
        possibleTypes.addAll(Arrays.asList(typeNames.split("\\.")));
    } else {
        possibleTypes.add(typeNames);
    }

    return possibleTypes;
}
 
Example 8
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
/**
 * Pattern to match given string before "filter" with given function name
 *
 * {{ 'foo<caret>bar'|trans }}
 * {{ 'foo<caret>bar'|trans() }}
 * {{ 'foo<caret>bar'|transchoice() }}
 */
@NotNull
public static ElementPattern<PsiElement> getTranslationKeyPattern(@NotNull String... type) {
    //noinspection unchecked
    return
        PlatformPatterns
            .psiElement(TwigTokenTypes.STRING_TEXT)
            .beforeLeafSkipping(
                PlatformPatterns.or(
                    PlatformPatterns.psiElement(PsiWhiteSpace.class),
                    PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
                    PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
                    PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
                ),
                PlatformPatterns.psiElement(TwigTokenTypes.FILTER).beforeLeafSkipping(
                    PlatformPatterns.or(
                        PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
                        PlatformPatterns.psiElement(PsiWhiteSpace.class)
                    ),
                    PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText(
                        PlatformPatterns.string().oneOf(type)
                    )
                )
            )
            .withLanguage(TwigLanguage.INSTANCE);
}
 
Example 9
Source Project: idea-php-toolbox   Source File: PhpMatcherTest.java    License: MIT License 6 votes vote down vote up
public void testVariadicSignatureRegistrarMatcher() {
    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n _variadic('<caret>')", "foo");
    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n _variadic(null, '<caret>')", "foo");
    assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n _variadic('foo<caret>')", PlatformPatterns.psiElement(Method.class).withName("format"));

    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n _variadic('<caret>')", "foo");
    assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n _variadic('foo<caret>')", PlatformPatterns.psiElement(Method.class).withName("format"));

    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n new \\Foo\\Variadic('<caret>')", "foo");
    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n new \\Foo\\Variadic(null, '<caret>')", "foo");
    assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n new \\Foo\\Variadic('foo<caret>')", PlatformPatterns.psiElement(Method.class).withName("format"));

    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n /** @var $f \\Foo\\Variadic */\n $f->getFoo('<caret>')", "foo");
    assertCompletionContains(PhpFileType.INSTANCE, "<?php\n /** @var $f \\Foo\\Variadic */\n $f->getFoo(null, '<caret>')", "foo");
    assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n /** @var $f \\Foo\\Variadic */\n $f->getFoo('foo<caret>')", PlatformPatterns.psiElement(Method.class).withName("format"));
}
 
Example 10
private void completeFieldArgumentTypeName() {
    CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
        @Override
        protected void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {

            final PsiElement completionElement = parameters.getPosition();
            final TypeDefinitionRegistry registry = GraphQLTypeDefinitionRegistryServiceImpl.getService(completionElement.getProject()).getRegistry(parameters.getOriginalFile());
            addInputTypeCompletions(result, registry);
        }
    };
    extend(CompletionType.BASIC,
            psiElement(GraphQLElementTypes.NAME).afterLeafSkipping(
                    // skip
                    PlatformPatterns.or(psiComment(), psiElement(TokenType.WHITE_SPACE), psiElement().withText("[")),
                    // until argument type colon occurs
                    psiElement().withText(":")
            ).inside(GraphQLInputValueDefinition.class), provider);
}
 
Example 11
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
public static ElementPattern<PsiElement> getAutocompletableRoutePattern() {
    //noinspection unchecked
    return PlatformPatterns
        .psiElement(TwigTokenTypes.STRING_TEXT)
        .afterLeafSkipping(
            PlatformPatterns.or(
                PlatformPatterns.psiElement(TwigTokenTypes.LBRACE),
                PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
                PlatformPatterns.psiElement(PsiWhiteSpace.class),
                PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
                PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
            ),
            PlatformPatterns.or(
                PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText("path"),
                PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText("url")
            )
        )
        .withLanguage(TwigLanguage.INSTANCE)
    ;
}
 
Example 12
@NotNull
private PsiElementPattern.Capture<ArrayIndex> getEmptyArrayKeyPattern() {
    return PlatformPatterns.psiElement(ArrayIndex.class).withParent(
        PlatformPatterns.psiElement(ArrayAccessExpression.class).withParent(
            PlatformPatterns.psiElement(ArrayAccessExpression.class).withParent(
                PlatformPatterns.psiElement(AssignmentExpression.class)
            )
        ).withChild(
            PlatformPatterns.psiElement(ArrayAccessExpression.class)
                .withChild(
                    PlatformPatterns.psiElement(ArrayIndex.class).withText(PlatformPatterns.string().oneOf("'namespaces'"))
                )
                .withChild(
                    PlatformPatterns.psiElement(ArrayAccessExpression.class).withChild(PlatformPatterns.psiElement(ArrayIndex.class).withText(PlatformPatterns.string().oneOf("'fluid'"))).withChild(
                        PlatformPatterns.psiElement(ArrayAccessExpression.class).withChild(PlatformPatterns.psiElement(ArrayIndex.class).withText(PlatformPatterns.string().oneOf("'SYS'"))).withChild(
                            PlatformPatterns.psiElement(ArrayAccessExpression.class).withChild(PlatformPatterns.psiElement(ArrayIndex.class).withText(PlatformPatterns.string().oneOf("'TYPO3_CONF_VARS'")))
                        )
                    )
                )
        )
    );
}
 
Example 13
Source Project: idea-php-typo3-plugin   Source File: RouteReferenceContributor.java    License: MIT License 6 votes vote down vote up
private boolean isGenerator(@NotNull PsiElement element) {
    if (!PlatformPatterns
            .psiElement(StringLiteralExpression.class).withParent(
                    PlatformPatterns.psiElement(ParameterList.class).withParent(
                            PlatformPatterns.psiElement(MethodReference.class))
            )
            .accepts(element)) {

        return false;
    }

    PsiElement methodRef = element.getParent().getParent();
    if (methodRef instanceof MethodReference) {
        Method method = (Method) ((MethodReference) methodRef).resolve();
        if (method != null) {
            return isClassMethodCombination(method, "\\TYPO3\\CMS\\Backend\\Routing\\UriBuilder", "buildUriFromRoutePath")
                    || isClassMethodCombination(method, "\\TYPO3\\CMS\\Backend\\Routing\\UriBuilder", "buildUriFromRoute")
                    || isClassMethodCombination(method, "\\TYPO3\\CMS\\Backend\\Routing\\UriBuilder", "buildUriFromAjaxId")
                    || isClassMethodCombination(method, "\\TYPO3\\CMS\\Backend\\Utility\\BackendUtility", "getAjaxUrl");
        }
    }

    return false;
}
 
Example 14
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
/**
 * Check for {% if foo is "foo" %}
 */
public static ElementPattern<PsiElement> getAfterIsTokenPattern() {
    //noinspection unchecked
    return PlatformPatterns
        .psiElement()
        .afterLeafSkipping(
            PlatformPatterns.or(
                PlatformPatterns.psiElement(PsiWhiteSpace.class),
                PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE)
            ),
            PlatformPatterns.or(
                PlatformPatterns.psiElement(TwigTokenTypes.IS),
                PlatformPatterns.psiElement(TwigTokenTypes.NOT)
            )
        )
        .withLanguage(TwigLanguage.INSTANCE);
}
 
Example 15
public void testRouteNameReferencesAsMethod() {
    Collection<String[]> providers = new ArrayList<String[]>() {{
        add(new String[] {"Illuminate\\Routing\\UrlGenerator", "route"});
        add(new String[] {"Illuminate\\Contracts\\Routing\\UrlGenerator", "route"});
        add(new String[] {"Collective\\Html\\HtmlBuilder", "linkRoute"});
        add(new String[] {"Tests\\Feature\\ExampleTest", "visitRoute"});
    }};

    for (String[] provider : providers) {
        assertCompletionContains(PhpFileType.INSTANCE, String.format("<?php\n" +
                "/** @var $r \\%s */\n" +
                "$r->%s('<caret>')"
                , provider[0], provider[1]),
            "profile"
        );

        assertNavigationMatch(PhpFileType.INSTANCE, String.format("<?php\n" +
                "/** @var $r \\%s */\n" +
                "$r->%s('profile<caret>')"
                , provider[0], provider[1]),
            PlatformPatterns.psiElement().inFile(PlatformPatterns.psiFile().withName("routes.php"))
        );
    }
}
 
Example 16
Source Project: idea-php-annotation-plugin   Source File: AnnotationPattern.java    License: MIT License 6 votes vote down vote up
/**
 * matches "@Callback(property="<value>")"
 */
public static ElementPattern<PsiElement> getTextIdentifier() {
    return PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_STRING)
        .withParent(PlatformPatterns.psiElement(StringLiteralExpression.class)
            .afterLeafSkipping(
                PlatformPatterns.or(
                    PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_TEXT).withText(PlatformPatterns.string().containsChars("=")),
                    PlatformPatterns.psiElement(PsiWhiteSpace.class)
                ),
                PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER)
            )
            .withParent(PlatformPatterns
                .psiElement(PhpDocElementTypes.phpDocAttributeList)
                .withParent(PlatformPatterns
                    .psiElement(PhpDocElementTypes.phpDocTag)
                )
            )
        );
}
 
Example 17
Source Project: idea-php-typo3-plugin   Source File: PhpElementsUtil.java    License: MIT License 6 votes vote down vote up
public static boolean isMethodWithFirstStringOrFieldReference(PsiElement psiElement, String... methodName) {
    if (!PlatformPatterns
            .psiElement(PhpElementTypes.METHOD_REFERENCE)
            .withChild(PlatformPatterns
                    .psiElement(PhpElementTypes.PARAMETER_LIST)
                    .withFirstChild(PlatformPatterns.or(
                            PlatformPatterns.psiElement(PhpElementTypes.STRING),
                            PlatformPatterns.psiElement(PhpElementTypes.FIELD_REFERENCE),
                            PlatformPatterns.psiElement(PhpElementTypes.CLASS_CONSTANT_REFERENCE)
                    ))
            ).accepts(psiElement)) {

        return false;
    }

    // cant we move it up to PlatformPatterns? withName condition dont looks working
    String methodRefName = ((MethodReference) psiElement).getName();

    return null != methodRefName && Arrays.asList(methodName).contains(methodRefName);
}
 
Example 18
Source Project: Intellij-Dust   Source File: DustCompletionContributor.java    License: MIT License 6 votes vote down vote up
public DustCompletionContributor() {
  extend(CompletionType.BASIC,
      PlatformPatterns.psiElement(DustTypes.IDENTIFIER).withLanguage(DustLanguage.INSTANCE),
      new CompletionProvider<CompletionParameters>() {
        public void addCompletions(@NotNull CompletionParameters parameters,
                                   ProcessingContext context,
                                   @NotNull CompletionResultSet resultSet) {
          resultSet.addElement(LookupElementBuilder.create("if"));
          resultSet.addElement(LookupElementBuilder.create("select"));
          resultSet.addElement(LookupElementBuilder.create("log"));
          resultSet.addElement(LookupElementBuilder.create("idx"));
          resultSet.addElement(LookupElementBuilder.create("jsControl"));
          resultSet.addElement(LookupElementBuilder.create("math"));
          resultSet.addElement(LookupElementBuilder.create("lt"));
          resultSet.addElement(LookupElementBuilder.create("gt"));
          resultSet.addElement(LookupElementBuilder.create("ne"));
          resultSet.addElement(LookupElementBuilder.create("eq"));
          resultSet.addElement(LookupElementBuilder.create("pre.scss"));
          resultSet.addElement(LookupElementBuilder.create("pre.layout"));
          resultSet.addElement(LookupElementBuilder.create("pre.js"));
          resultSet.addElement(LookupElementBuilder.create("pre.i18n"));
          // TODO add more keywords, think about writing a reference based autocompleter
        }
      }
  );
}
 
Example 19
Source Project: idea-php-toolbox   Source File: PhpArrayCallbackGotoCompletion.java    License: MIT License 6 votes vote down vote up
/**
 * [$this, '']
 * array($this, '')
 */
@NotNull
@Override
public PsiElementPattern.Capture<PsiElement> getPattern() {
    return PlatformPatterns.psiElement().withParent(
        PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
            PlatformPatterns.psiElement().withElementType(PhpElementTypes.ARRAY_VALUE).afterLeafSkipping(
                PlatformPatterns.psiElement(PsiWhiteSpace.class),
                PlatformPatterns.psiElement().withText(",")
            ).afterSiblingSkipping(
                PlatformPatterns.psiElement(PsiWhiteSpace.class),
                PlatformPatterns.psiElement().withElementType(PhpElementTypes.ARRAY_VALUE).withFirstNonWhitespaceChild(
                    PlatformPatterns.psiElement(Variable.class)
                ).afterLeafSkipping(
                    PlatformPatterns.psiElement(PsiWhiteSpace.class),
                    PlatformPatterns.psiElement().withText(PlatformPatterns.string().oneOf("[", "("))
                )
            )
        )
    );
}
 
Example 20
public void testFormDataFieldPropertyPathReferencesForMethod() {
    for (String s : new String[]{"foo_bar", "fooBar"}) {
        assertReferenceMatchOnParent(PhpFileType.INSTANCE, "<?php\n" +
            "\n" +
            "class FormType\n" +
            "{\n" +
            "    protected $foo = 'DateTime';\n" +
            "    public function buildForm(\\Symfony\\Component\\Form\\FormBuilderInterface $builder, array $options) {\n" +
            "        $builder->add('" + s + "<caret>');\n" +
            "    }\n" +
            "    public function setDefaultOptions(OptionsResolverInterface $resolver)\n" +
            "    {\n" +
            "        $resolver->setDefaults(array(\n" +
            "            'data_class' => \\Form\\DataClass\\Model::class,\n" +
            "        ));\n" +
            "    }\n" +
            "}", PlatformPatterns.psiElement(Method.class)
        );
    }
}
 
Example 21
Source Project: buck   Source File: BuckIdentifierReferenceContributor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {

  registrar.registerReferenceProvider(
      PlatformPatterns.psiElement(BuckIdentifier.class),
      new PsiReferenceProvider() {
        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(
            @NotNull PsiElement element, @NotNull ProcessingContext context) {
          if (element instanceof BuckIdentifier) {
            BuckIdentifier identifier = (BuckIdentifier) element;
            return new PsiReference[] {new BuckIdentifierReference(identifier)};
          } else {
            return new PsiReference[0];
          }
        }
      });
}
 
Example 22
Source Project: idea-php-symfony2-plugin   Source File: PhpElementsUtil.java    License: MIT License 6 votes vote down vote up
/**
 * $this->methodName('service_name')
 * $this->methodName(SERVICE::NAME)
 * $this->methodName($this->name)
 */
static public boolean isMethodWithFirstStringOrFieldReference(PsiElement psiElement, String... methodName) {

    if(!PlatformPatterns
        .psiElement(PhpElementTypes.METHOD_REFERENCE)
        .withChild(PlatformPatterns
            .psiElement(PhpElementTypes.PARAMETER_LIST)
            .withFirstChild(PlatformPatterns.or(
                PlatformPatterns.psiElement(PhpElementTypes.STRING),
                PlatformPatterns.psiElement(PhpElementTypes.FIELD_REFERENCE),
                PlatformPatterns.psiElement(PhpElementTypes.CLASS_CONSTANT_REFERENCE)
            ))
        ).accepts(psiElement)) {

        return false;
    }

    // cant we move it up to PlatformPatterns? withName condition dont looks working
    String methodRefName = ((MethodReference) psiElement).getName();

    return null != methodRefName && Arrays.asList(methodName).contains(methodRefName);
}
 
Example 23
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if(!(file instanceof PhpFile) || !DoctrineUtil.isDoctrineOrmInVendor(project)) {
        return false;
    }

    int offset = editor.getCaretModel().getOffset();
    if(offset <= 0) {
        return false;
    }

    PsiElement psiElement = file.findElementAt(offset);
    if(psiElement == null) {
        return false;
    }

    if(!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
        return false;
    }

    return true;
}
 
Example 24
Source Project: idea-php-symfony2-plugin   Source File: DoctrineUtil.java    License: MIT License 6 votes vote down vote up
/**
 * Extract text: @Entity(repositoryClass="foo")
 */
@Nullable
public static String getAnnotationRepositoryClass(@NotNull PhpDocTag phpDocTag, @NotNull PhpClass phpClass) {
    PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
    if(phpDocAttributeList == null) {
        return null;
    }

    // @TODO: use annotation plugin
    // repositoryClass="Foobar"
    String text = phpDocAttributeList.getText();

    String repositoryClass = EntityHelper.resolveDoctrineLikePropertyClass(
        phpClass,
        text,
        "repositoryClass",
        aVoid -> AnnotationUtil.getUseImportMap(phpDocTag)
    );

    if (repositoryClass == null) {
        return null;
    }

    return StringUtils.stripStart(repositoryClass, "\\");
}
 
Example 25
Source Project: idea-php-typo3-plugin   Source File: TemplateLineMarkerProvider.java    License: MIT License 6 votes vote down vote up
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
    if (!PlatformPatterns.psiElement(FluidFile.class).accepts(element)) {
        return;
    }

    Collection<Method> possibleMethodTargetsForControllerAction = FluidUtil.findPossibleMethodTargetsForControllerAction(
        element.getProject(),
        FluidUtil.inferControllerNameFromTemplateFile((FluidFile) element),
        FluidUtil.inferActionNameFromTemplateFile((FluidFile) element)
    );

    if (possibleMethodTargetsForControllerAction.size() > 0) {
        result.add(
            NavigationGutterIconBuilder
                .create(PhpIcons.METHOD)
                .setTargets(possibleMethodTargetsForControllerAction)
                .setTooltipText("Navigate to controller action")
                .createLineMarkerInfo(element)
        );
    }
}
 
Example 26
public void testNamedArgumentsNavigationForDefaultBinding() {
    assertNavigationMatch("services.yml", "" +
                    "services:\n" +
                    "   _defaults:\n" +
                    "       bind:\n" +
                    "           $<caret>i: ~\n"+
                    "   Foo\\Bar: ~" +
            PlatformPatterns.psiElement(Parameter.class)
    );

    assertNavigationMatch("services.yml", "" +
            "services:\n" +
            "   _defaults:\n" +
            "       bind:\n" +
            "           $<caret>i: ~\n"+
            "   foobar:\n" +
            "       class: Foo\\Bar\n" +
            PlatformPatterns.psiElement(Parameter.class)
    );
}
 
Example 27
Source Project: idea-php-symfony2-plugin   Source File: TwigPattern.java    License: MIT License 6 votes vote down vote up
public static ElementPattern<PsiElement> getForTagInVariablePattern() {

        // {% for key, user in "users" %}
        // {% for user in "users" %}
        // {% for user in "users"|slice(0, 10) %}

        //noinspection unchecked
        return PlatformPatterns
            .psiElement(TwigTokenTypes.IDENTIFIER)
            .afterLeafSkipping(
                PlatformPatterns.or(
                    PlatformPatterns.psiElement(PsiWhiteSpace.class),
                    PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE)
                ),
                PlatformPatterns.psiElement(TwigTokenTypes.IN)
            )
            .withLanguage(TwigLanguage.INSTANCE);
    }
 
Example 28
public void testNavigationForSnippetName() {
    assertNavigationMatch(
        SmartyFileType.INSTANCE,
        "{s name='swag-last-registr<caret>ations/customer' namespace='foobar/widgets'}{/s}",
        PlatformPatterns.psiFile()
    );

    assertNavigationMatch(
        SmartyFileType.INSTANCE,
        "{s name=\"swag-last-registr<caret>ations/customer\" namespace='foobar/widgets'}{/s}",
        PlatformPatterns.psiFile()
    );
}
 
Example 29
public void testThatArrayProvidesPsalmTypesNavigation() {
    assertNavigationMatch("test.php",
        "<?php\n" +
            "(new Bar())->foobar(['foo<caret>bar'])",
        PlatformPatterns.psiElement(PhpDocTag.class)
    );
}
 
Example 30
Source Project: idea-php-shopware-plugin   Source File: SmartyPattern.java    License: MIT License 5 votes vote down vote up
public static PsiElementPattern.Capture<PsiElement> getExtendPattern() {
    return PlatformPatterns.psiElement(SmartyTokenTypes.STRING_LITERAL).withParent(
        PlatformPatterns.psiElement(SmartyCompositeElementTypes.ATTRIBUTE_VALUE).withParent(
            PlatformPatterns.psiElement(SmartyCompositeElementTypes.ATTRIBUTE).withText(PlatformPatterns.string().contains("file=")).withParent(
                PlatformPatterns.psiElement(SmartyCompositeElementTypes.TAG).withText(PlatformPatterns.string().startsWith("{extends"))
            )
        )
    );
}