Search in sources :

Example 6 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-plugins by JetBrains.

the class FlexCssReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(CssString.class).and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            CssFunction fun = PsiTreeUtil.getParentOfType((PsiElement) element, CssFunction.class);
            String funName;
            return fun != null && (FlexReferenceContributor.CLASS_REFERENCE.equals(funName = fun.getName()) || "Embed".equals(funName));
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
            CssFunction fun = PsiTreeUtil.getParentOfType(element, CssFunction.class);
            if (fun != null && "Embed".equals(fun.getName())) {
                // TODO: remove this stuff once css function will have proper psi
                PsiElement prev = PsiTreeUtil.prevLeaf(element);
                if (prev instanceof PsiWhiteSpace)
                    prev = PsiTreeUtil.prevLeaf(prev);
                if (prev != null)
                    prev = PsiTreeUtil.prevLeaf(prev);
                if (prev instanceof PsiWhiteSpace)
                    prev = PsiTreeUtil.prevLeaf(prev);
                // prev.getText() == Embed if element is the first parameter and the name not specified
                if (prev != null && !FlexReferenceContributor.SOURCE_ATTR_NAME.equals(prev.getText()) && !"Embed".equals(prev.getText())) {
                    return PsiReference.EMPTY_ARRAY;
                }
                return ReferenceSupport.getFileRefs(element, element, 1, ReferenceSupport.LookupOptions.EMBEDDED_ASSET);
            }
            final String value = StringUtil.unquoteString(element.getText());
            JSReferenceSet refSet = new JSReferenceSet(element, value, 1, false, true);
            if (fun != null && element instanceof CssString) {
                assert FlexReferenceContributor.CLASS_REFERENCE.equals(fun.getName());
                refSet.setLocalQuickFixProvider(new LocalQuickFixProvider() {

                    @Nullable
                    @Override
                    public LocalQuickFix[] getQuickFixes() {
                        if (!JSUtils.isValidClassName(value, true)) {
                            return LocalQuickFix.EMPTY_ARRAY;
                        }
                        ActionScriptCreateClassOrInterfaceFix[] fixes = new ActionScriptCreateClassOrInterfaceFix[] { new ActionScriptCreateClassOrInterfaceFix(value, null, element), new CreateFlexComponentFix(value, element) };
                        for (ActionScriptCreateClassOrInterfaceFix fix : fixes) {
                            fix.setCreatedClassFqnConsumer(newFqn -> ElementManipulators.getManipulator(element).handleContentChange(element, newFqn));
                        }
                        return fixes;
                    }
                });
            }
            return refSet.getReferences();
        }
    });
    registrar.registerReferenceProvider(PlatformPatterns.psiElement().and(new FilterPattern(new ElementFilter() {

        public boolean isAcceptable(Object element, PsiElement context) {
            if (element instanceof CssTokenImpl || element instanceof CssString) {
                CssTermList cssTermList = PsiTreeUtil.getParentOfType((PsiElement) element, CssTermList.class);
                if (cssTermList != null) {
                    CssDeclaration cssDeclaration = PsiTreeUtil.getParentOfType(cssTermList, CssDeclaration.class);
                    if (cssDeclaration != null && cssDeclaration.getValue() == cssTermList) {
                        if (FlexCssUtil.isStyleNameProperty(cssDeclaration.getPropertyName())) {
                            PsiFile file = cssDeclaration.getContainingFile();
                            if (file != null) {
                                if (file.getFileType() == CssFileType.INSTANCE) {
                                    Module module = findModuleForPsiElement(cssDeclaration);
                                    return module != null && ModuleType.get(module) == FlexModuleType.getInstance();
                                }
                                return JavaScriptSupportLoader.isFlexMxmFile(file);
                            }
                        }
                    }
                }
            }
            return false;
        }

        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String styleName = CssClassValueReference.getValue(element);
            if (styleName.length() > 0) {
                return new PsiReference[] { new CssClassValueReference(element) };
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) CssTokenImpl(com.intellij.psi.css.impl.CssTokenImpl) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFixProvider(com.intellij.codeInspection.LocalQuickFixProvider) FilterPattern(com.intellij.psi.filters.position.FilterPattern) ActionScriptCreateClassOrInterfaceFix(com.intellij.lang.javascript.validation.fixes.ActionScriptCreateClassOrInterfaceFix) ModuleUtilCore.findModuleForPsiElement(com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement) CreateFlexComponentFix(com.intellij.lang.javascript.flex.actions.newfile.CreateFlexComponentFix) CssClassValueReference(com.intellij.javascript.flex.css.CssClassValueReference) JSReferenceSet(com.intellij.lang.javascript.psi.impl.JSReferenceSet) ElementFilter(com.intellij.psi.filters.ElementFilter) Module(com.intellij.openapi.module.Module)

Example 7 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-community by JetBrains.

the class JavaNoVariantsDelegator method suggestChainedCalls.

private static void suggestChainedCalls(CompletionParameters parameters, CompletionResultSet result, PsiElement position) {
    PsiElement parent = position.getParent();
    if (!(parent instanceof PsiJavaCodeReferenceElement) || parent.getParent() instanceof PsiImportStatementBase) {
        return;
    }
    PsiElement qualifier = ((PsiJavaCodeReferenceElement) parent).getQualifier();
    if (!(qualifier instanceof PsiJavaCodeReferenceElement) || ((PsiJavaCodeReferenceElement) qualifier).isQualified()) {
        return;
    }
    PsiElement target = ((PsiJavaCodeReferenceElement) qualifier).resolve();
    if (target != null && !(target instanceof PsiPackage)) {
        return;
    }
    PsiFile file = position.getContainingFile();
    if (file instanceof PsiJavaCodeReferenceCodeFragment) {
        return;
    }
    String fullPrefix = parent.getText().substring(0, parameters.getOffset() - parent.getTextRange().getStartOffset());
    CompletionResultSet qualifiedCollector = result.withPrefixMatcher(fullPrefix);
    ElementFilter filter = JavaCompletionContributor.getReferenceFilter(position);
    for (LookupElement base : suggestQualifierItems(parameters, (PsiJavaCodeReferenceElement) qualifier, filter)) {
        PsiType type = JavaCompletionUtil.getLookupElementType(base);
        if (type != null && !PsiType.VOID.equals(type)) {
            PsiReferenceExpression ref = ReferenceExpressionCompletionContributor.createMockReference(position, type, base);
            if (ref != null) {
                for (final LookupElement item : JavaSmartCompletionContributor.completeReference(position, ref, filter, true, true, parameters, result.getPrefixMatcher())) {
                    qualifiedCollector.addElement(JavaCompletionUtil.highlightIfNeeded(null, new JavaChainLookupElement(base, item), item.getObject(), position));
                }
            }
        }
    }
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 8 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-community by JetBrains.

the class CompletionVariant method addReferenceCompletions.

void addReferenceCompletions(PsiReference reference, PsiElement position, Set<LookupElement> set, final PsiFile file, final CompletionData completionData) {
    for (final CompletionVariantItem ce : myCompletionsList) {
        if (ce.myCompletion instanceof ElementFilter) {
            final ElementFilter filter = (ElementFilter) ce.myCompletion;
            completionData.completeReference(reference, position, set, ce.myTailType, filter, this);
        }
    }
}
Also used : ElementFilter(com.intellij.psi.filters.ElementFilter)

Example 9 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-community by JetBrains.

the class ChainedCallCompletion method addChains.

static void addChains(final PsiElement place, LookupElement qualifierItem, final Consumer<LookupElement> result, PsiType qualifierType, final PsiType expectedType, JavaSmartCompletionParameters parameters) throws IncorrectOperationException {
    final Object object = qualifierItem.getObject();
    if (OBJECT_METHOD_PATTERN.accepts(object) && !allowGetClass(object, parameters)) {
        return;
    }
    if (parameters.getParameters().getInvocationCount() < 3 && qualifierType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
        return;
    }
    if (object instanceof PsiMethod && ((PsiMethod) object).getParameterList().getParametersCount() > 0) {
        return;
    }
    final PsiReferenceExpression mockRef = ReferenceExpressionCompletionContributor.createMockReference(place, qualifierType, qualifierItem);
    if (mockRef == null) {
        return;
    }
    final ElementFilter filter = ReferenceExpressionCompletionContributor.getReferenceFilter(place, true);
    for (final LookupElement item : ReferenceExpressionCompletionContributor.completeFinalReference(place, mockRef, filter, parameters)) {
        if (shouldChain(place, qualifierType, expectedType, item)) {
            result.consume(new JavaChainLookupElement(qualifierItem, item) {

                @Override
                public void handleInsert(InsertionContext context) {
                    FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.SECOND_SMART_COMPLETION_CHAIN);
                    super.handleInsert(context);
                }
            });
        }
    }
}
Also used : ElementFilter(com.intellij.psi.filters.ElementFilter) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 10 with ElementFilter

use of com.intellij.psi.filters.ElementFilter in project intellij-community by JetBrains.

the class MetaRegistryTest method testChangingMetaData.

public void testChangingMetaData() throws Throwable {
    final boolean[] flag = { false };
    MetaRegistry.addMetadataBinding(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, PsiElement context) {
            return flag[0];
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    }, MyTrueMetaData.class, getTestRootDisposable());
    MetaRegistry.addMetadataBinding(new ElementFilter() {

        @Override
        public boolean isAcceptable(Object element, PsiElement context) {
            return !flag[0];
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    }, MyFalseMetaData.class, getTestRootDisposable());
    final XmlTag tag = ((XmlFile) LightPlatformTestCase.createFile("a.xml", "<a/>")).getDocument().getRootTag();
    UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyFalseMetaData.class);
    flag[0] = true;
    new WriteCommandAction(LightPlatformTestCase.getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            tag.setName("b");
        }
    }.execute();
    UsefulTestCase.assertInstanceOf(tag.getMetaData(), MyTrueMetaData.class);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ElementFilter(com.intellij.psi.filters.ElementFilter) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Result(com.intellij.openapi.application.Result)

Aggregations

ElementFilter (com.intellij.psi.filters.ElementFilter)15 FilterPattern (com.intellij.psi.filters.position.FilterPattern)6 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 CssClassValueReference (com.intellij.javascript.flex.css.CssClassValueReference)2 Module (com.intellij.openapi.module.Module)2 ModuleUtilCore.findModuleForPsiElement (com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)2 PsiElement (com.intellij.psi.PsiElement)2 ClassFilter (com.intellij.psi.filters.ClassFilter)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ProcessingContext (com.intellij.util.ProcessingContext)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 JavaCompletionProcessor (com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)1 CssPropertyValueReference (com.intellij.javascript.flex.css.CssPropertyValueReference)1 CreateFlexComponentFix (com.intellij.lang.javascript.flex.actions.newfile.CreateFlexComponentFix)1