com.intellij.patterns.PatternCondition Java Examples

The following examples show how to use com.intellij.patterns.PatternCondition. 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: BladePattern.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
/**
 * "@foobar"
 */
private static PsiElementPattern.Capture<BladePsiDirectiveParameter> getDirectiveNamePattern(@NotNull final String[] directives) {
    return PlatformPatterns.psiElement(BladePsiDirectiveParameter.class).withParent(
        PlatformPatterns.psiElement(BladePsiDirective.class).with(new PatternCondition<BladePsiDirective>("Directive Name") {
            @Override
            public boolean accepts(@NotNull BladePsiDirective bladePsiDirective, ProcessingContext processingContext) {
                for (String directive : directives) {
                    if(("@" + directive).equals(bladePsiDirective.getName())) {
                        return true;
                    }
                }

                return false;
            }
        })
    );
}
 
Example #2
Source File: YamlPermissionGotoCompletionTest.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
public void testThatRoutePermissionCompletesAndNavigates() {
    assertCompletionContains(YAMLFileType.YML, "" +
            "config.import_full:\n" +
            "  requirements:\n" +
            "    _permission: '<caret>'",
        "synchronize configuration"
    );

    assertNavigationMatch(YAMLFileType.YML, "" +
            "config.import_full:\n" +
            "  requirements:\n" +
            "    _permission: 'synchronize<caret> configuration'",
        PlatformPatterns.psiElement(YAMLKeyValue.class).with(new PatternCondition<YAMLKeyValue>("key") {
            @Override
            public boolean accepts(@NotNull YAMLKeyValue yamlKeyValue, ProcessingContext processingContext) {
                return "synchronize configuration".equals(yamlKeyValue.getKeyText());
            }
        })
    );
}
 
Example #3
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with 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 #4
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testYamlServiceLineMarker() {
    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  foo:\n" +
            "    class: Service\\YamlBar"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class YamlBar{}\n" +
        "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", PlatformPatterns.psiElement(YAMLKeyValue.class).with(new PatternCondition<YAMLKeyValue>("KeyText") {
        @Override
        public boolean accepts(@NotNull YAMLKeyValue yamlKeyValue, ProcessingContext processingContext) {
            return yamlKeyValue.getKeyText().equals("foo");
        }
    })));
}
 
Example #5
Source File: ServiceLineMarkerProviderTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testYamlServiceLineMarkerForClassName() {
    myFixture.configureByText(YAMLFileType.YML,
        "services:\n" +
            "  Service\\YamlBar: ~\n"
    );

    assertLineMarker(PhpPsiElementFactory.createPsiFileFromText(getProject(), "<?php\n" +
        "namespace Service{\n" +
        "    class YamlBar{}\n" +
        "}"
    ), new LineMarker.TargetAcceptsPattern("Navigate to definition", PlatformPatterns.psiElement(YAMLKeyValue.class).with(new PatternCondition<YAMLKeyValue>("KeyText") {
        @Override
        public boolean accepts(@NotNull YAMLKeyValue yamlKeyValue, ProcessingContext processingContext) {
            return yamlKeyValue.getKeyText().equals("Service\\YamlBar");
        }
    })));
}
 
Example #6
Source File: CamelJavaReferenceContributor.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public CamelJavaReferenceContributor() {
    addCompletionExtension(new BeanReferenceCompletionExtension());
    addCompletionExtension(new CamelEndpointNameCompletionExtension());
    addCompletionExtension(new CamelEndpointSmartCompletionExtension(false));
    extend(CompletionType.BASIC, psiElement().and(psiElement().inside(PsiFile.class).inFile(matchFileType("java"))),
            new EndpointCompletion(getCamelCompletionExtensions())
    );
    extend(CompletionType.BASIC, psiElement(PsiJavaToken.class).with(new PatternCondition<PsiJavaToken>("CamelJavaBeanReferenceSmartCompletion") {
        @Override
        public boolean accepts(@NotNull PsiJavaToken psiJavaToken, ProcessingContext processingContext) {
            return getCamelIdeaUtils().getBeanPsiElement(psiJavaToken) != null;
        }
    }), new CamelJavaBeanReferenceSmartCompletion());
}
 
Example #7
Source File: JavaClassQualifiedNameReference.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static PatternCondition<PsiElementBase> nameCondition(final ElementPattern pattern) {
  return new PatternConditionPlus<PsiElementBase, String>("_withPsiName", pattern) {
    @Override
    public boolean processValues(
        PsiElementBase t,
        ProcessingContext context,
        PairProcessor<String, ProcessingContext> processor) {
      return processor.process(t.getName(), context);
    }
  };
}
 
Example #8
Source File: BladePattern.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
/**
 * Pattern for @includeIf('auth.<caret>') or @includeIf("auth.<caret>")
 * @param directives "includeIf" without "@"
 */
@NotNull
public static PsiElementPattern.Capture<PsiElement> getDirectiveParameterPattern(@NotNull String... directives) {
    return PlatformPatterns.psiElement().withElementType(BladeTokenTypes.DIRECTIVE_PARAMETER_CONTENT)
        .withText(PlatformPatterns.string().with(new PatternCondition<String>("Directive Quoted Content") {
            @Override
            public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
                return (s.startsWith("'") || s.startsWith("\"")) && s.endsWith("'") || s.endsWith("\"");
            }
        }))
        .withParent(getDirectiveNamePattern(directives));
}
 
Example #9
Source File: BladePattern.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
/**
 * Pattern for @includeIf('auth.<caret>', [])
 * @param directives "includeIf" without "@"
 */
public static PsiElementPattern.Capture<PsiElement> getDirectiveWithAdditionalParameterPattern(@NotNull String... directives) {
    return PlatformPatterns.psiElement().withElementType(BladeTokenTypes.DIRECTIVE_PARAMETER_CONTENT)
        .withText(PlatformPatterns.string().with(new PatternCondition<String>("Directive Quoted Content") {
            @Override
            public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
                return s.matches("^\\s*['|\"]([^'\"]+)['|\"].*");
            }
        }))
        .withParent(getDirectiveNamePattern(directives));
}
 
Example #10
Source File: ShopwareJavaScriptCompletion.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
/**
 * String must start with "{s"
 */
private PsiElementPattern.Capture<PsiElement> getSnippetPattern() {
    return PlatformPatterns.psiElement()
        .withParent(PlatformPatterns.psiElement(JSLiteralExpression.class)
            .with(new PatternCondition<JSLiteralExpression>("Snippet Start") {
                @Override
                public boolean accepts(@NotNull JSLiteralExpression jsLiteralExpression, ProcessingContext processingContext) {
                    Object value = jsLiteralExpression.getValue();
                    return value instanceof String && (((String) value).startsWith("{s"));
                }
            }));
}
 
Example #11
Source File: SmartyPattern.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
/**
 * {tag}
 */
public static ElementPattern<PsiElement> getSmartyTagPattern(@NotNull String tag) {
    return PlatformPatterns.psiElement(SmartyTokenTypes.IDENTIFIER)
        .afterLeaf(PlatformPatterns.psiElement(SmartyTokenTypes.START_TAG_START))
        .withParent(
            PlatformPatterns.psiElement(SmartyTag.class).with(new PatternCondition<SmartyTag>("Smarty: Tag Name") {
                @Override
                public boolean accepts(@NotNull SmartyTag smartyTag, ProcessingContext processingContext) {
                    return tag.equalsIgnoreCase(smartyTag.getTagName());
                }
            })
        );
}
 
Example #12
Source File: CSharpPatterns.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static PsiElementPattern.Capture<PsiElement> fieldStart()
{
	return StandardPatterns.psiElement().withElementType(CSharpTokens.IDENTIFIER).withSuperParent(3, CSharpFieldDeclaration.class).with(new PatternCondition<PsiElement>("field-type-no-qualifier")
	{
		@Override
		@RequiredReadAction
		public boolean accepts(@Nonnull PsiElement element, ProcessingContext context)
		{
			CSharpFieldDeclaration declaration = PsiTreeUtil.getParentOfType(element, CSharpFieldDeclaration.class);
			if(declaration == null)
			{
				return false;
			}
			DotNetType type = declaration.getType();
			if(!(type instanceof CSharpUserType))
			{
				return false;
			}
			CSharpReferenceExpression referenceExpression = ((CSharpUserType) type).getReferenceExpression();
			if(referenceExpression.getQualifier() != null)
			{
				return false;
			}
			return true;
		}
	});
}
 
Example #13
Source File: ReferencePattern.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
public ReferencePattern withoutReference() {
    return with(new PatternCondition<PsiElement>("withoutReference") {
        @Override
        public boolean accepts(@NotNull final PsiElement t, final ProcessingContext context) {
            PsiReference ref = t.findReferenceAt(0);
            return ref == null;
        }
    });
}
 
Example #14
Source File: CSharpPatterns.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static PsiElementPattern.Capture<PsiElement> statementStart()
{
	return StandardPatterns.psiElement().withElementType(CSharpTokens.IDENTIFIER).with(new PatternCondition<PsiElement>("statement-validator")
	{
		@Override
		@RequiredReadAction
		public boolean accepts(@Nonnull PsiElement element, ProcessingContext processingContext)
		{
			PsiElement parent = element.getParent();
			PsiElement parent2 = parent == null ? null : parent.getParent();
			PsiElement parent3 = parent2 == null ? null : parent2.getParent();
			if(parent3 instanceof CSharpLocalVariable)
			{
				return validateLocalVariable((CSharpLocalVariable) parent3);
			}

			if(parent instanceof CSharpReferenceExpression && parent2 instanceof CSharpExpressionStatementImpl)
			{
				return validateReferenceExpression((CSharpReferenceExpression) parent);
			}
			return false;
		}

		@RequiredReadAction
		private boolean validateReferenceExpression(CSharpReferenceExpression expression)
		{
			if(expression.getQualifier() != null)
			{
				return false;
			}
			CSharpReferenceExpression.ResolveToKind kind = expression.kind();
			return kind == CSharpReferenceExpression.ResolveToKind.ANY_MEMBER;
		}

		@RequiredReadAction
		private boolean validateLocalVariable(CSharpLocalVariable localVariable)
		{
			// we cant use it when 'const <exp>'
			if(localVariable == null || localVariable.isConstant())
			{
				return false;
			}
			// disable it inside non local decl statement, like catch
			if(!(localVariable.getParent() instanceof CSharpLocalVariableDeclarationStatement))
			{
				return false;
			}
			DotNetType type = localVariable.getType();
			if(!(type instanceof CSharpUserType))
			{
				return false;
			}
			CSharpReferenceExpression referenceExpression = ((CSharpUserType) type).getReferenceExpression();
			if(referenceExpression.getQualifier() != null)
			{
				return false;
			}
			return CSharpPsiUtilImpl.isNullOrEmpty(localVariable);
		}
	});
	/*return StandardPatterns.psiElement().withElementType(CSharpTokens.IDENTIFIER).withSuperParent(3, CSharpLocalVariable.class).with(new PatternCondition<PsiElement>
	("null-identifier-local-var")

	{
		@Override
		@RequiredReadAction
		public boolean accepts(@NotNull PsiElement element, ProcessingContext context)
		{
			CSharpLocalVariable localVariable = PsiTreeUtil.getParentOfType(element, CSharpLocalVariable.class);
			// we cant use it when 'const <exp>'
			if(localVariable == null || localVariable.isConstant())
			{
				return false;
			}
			// disable it inside non local decl statement, like catch
			if(!(localVariable.getParent() instanceof CSharpLocalVariableDeclarationStatement))
			{
				return false;
			}
			DotNetType type = localVariable.getType();
			if(!(type instanceof CSharpUserType))
			{
				return false;
			}
			CSharpReferenceExpression referenceExpression = ((CSharpUserType) type).getReferenceExpression();
			if(referenceExpression.getQualifier() != null)
			{
				return false;
			}
			return CSharpPsiUtilImpl.isNullOrEmpty(localVariable);
		}
	}); */
}