Search in sources :

Example 16 with ProcessingContext

use of com.intellij.util.ProcessingContext in project intellij-community by JetBrains.

the class StandardPatternsTest method testSave.

public void testSave() {
    Key<String> key = Key.create("abc");
    final ProcessingContext context = new ProcessingContext();
    assertFalse(string().contains("abc").save(key).accepts(null));
    assertNull(context.get(key));
    assertFalse(string().contains("abc").save(key).accepts("def"));
    assertNull(context.get(key));
    final String s = "defabcdef";
    assertTrue(string().contains("abc").save(key).accepts(s, context));
    assertSame(s, context.get(key));
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext)

Example 17 with ProcessingContext

use of com.intellij.util.ProcessingContext in project intellij-community by JetBrains.

the class IconsReferencesContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    final PsiJavaElementPattern.Capture<PsiLiteralExpression> presentationAnno = literalExpression().annotationParam("com.intellij.ide.presentation.Presentation", "icon");
    registrar.registerReferenceProvider(presentationAnno, new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
            if (!PsiUtil.isPluginProject(element.getProject()))
                return PsiReference.EMPTY_ARRAY;
            return new PsiReference[] { new PsiReferenceBase<PsiElement>(element, true) {

                @Override
                public PsiElement resolve() {
                    String value = (String) ((PsiLiteralExpression) element).getValue();
                    if (value != null) {
                        List<String> path = StringUtil.split(value, ".");
                        if (path.size() > 1 && path.get(0).endsWith("Icons")) {
                            Project project = element.getProject();
                            PsiClass cur = findIconClass(project, path.get(0));
                            if (cur == null) {
                                return null;
                            }
                            for (int i = 1; i < path.size() - 1; i++) {
                                cur = cur.findInnerClassByName(path.get(i), false);
                                if (cur == null) {
                                    return null;
                                }
                            }
                            return cur.findFieldByName(path.get(path.size() - 1), false);
                        }
                    }
                    return null;
                }

                @Override
                public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
                    PsiElement field = resolve();
                    if (field instanceof PsiField) {
                        String fqn = ((PsiField) field).getContainingClass().getQualifiedName();
                        if (fqn.startsWith("com.intellij.icons.")) {
                            return replace(newElementName, fqn, "com.intellij.icons.", element);
                        }
                        if (fqn.startsWith("icons.")) {
                            return replace(newElementName, fqn, "icons.", element);
                        }
                    }
                    return super.handleElementRename(newElementName);
                }

                @Override
                public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
                    if (element instanceof PsiField) {
                        String fqn = ((PsiField) element).getContainingClass().getQualifiedName();
                        String newElementName = ((PsiField) element).getName();
                        if (fqn.startsWith("com.intellij.icons.")) {
                            return replace(newElementName, fqn, "com.intellij.icons.", getElement());
                        }
                        if (fqn.startsWith("icons.")) {
                            return replace(newElementName, fqn, "icons.", getElement());
                        }
                    }
                    return super.bindToElement(element);
                }

                private PsiElement replace(String newElementName, String fqn, String pckg, PsiElement container) {
                    String newValue = "\"" + fqn.substring(pckg.length()) + "." + newElementName + "\"";
                    return getElement().replace(JavaPsiFacade.getElementFactory(container.getProject()).createExpressionFromText(newValue, container.getParent()));
                }

                @NotNull
                @Override
                public Object[] getVariants() {
                    return EMPTY_ARRAY;
                }
            } };
        }
    });
    final PsiMethodPattern method = psiMethod().withName("findIcon", "getIcon").definedInClass(IconLoader.class.getName());
    final PsiJavaElementPattern.Capture<PsiLiteralExpression> findGetIconPattern = literalExpression().and(psiExpression().methodCallParameter(0, method));
    registrar.registerReferenceProvider(findGetIconPattern, new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
            if (!PsiUtil.isIdeaProject(element.getProject()))
                return PsiReference.EMPTY_ARRAY;
            return new FileReferenceSet(element) {

                @Override
                protected Collection<PsiFileSystemItem> getExtraContexts() {
                    final Module icons = ModuleManager.getInstance(element.getProject()).findModuleByName("icons");
                    if (icons != null) {
                        final ArrayList<PsiFileSystemItem> result = new ArrayList<>();
                        final VirtualFile[] roots = ModuleRootManager.getInstance(icons).getSourceRoots();
                        final PsiManager psiManager = element.getManager();
                        for (VirtualFile root : roots) {
                            final PsiDirectory directory = psiManager.findDirectory(root);
                            if (directory != null) {
                                result.add(directory);
                            }
                        }
                        return result;
                    }
                    return super.getExtraContexts();
                }
            }.getAllReferences();
        }
    });
    registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withLocalName("icon"), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
            if (!PsiUtil.isPluginProject(element.getProject()) || !DescriptorUtil.isPluginXml(element.getContainingFile())) {
                return PsiReference.EMPTY_ARRAY;
            }
            return new PsiReference[] { new PsiReferenceBase<PsiElement>(element, true) {

                @Override
                public PsiElement resolve() {
                    String value = ((XmlAttributeValue) element).getValue();
                    if (value.startsWith("/")) {
                        FileReference lastRef = new FileReferenceSet(element).getLastReference();
                        return lastRef != null ? lastRef.resolve() : null;
                    }
                    List<String> path = StringUtil.split(value, ".");
                    if (path.size() > 1 && path.get(0).endsWith("Icons")) {
                        Project project = element.getProject();
                        PsiClass cur = findIconClass(project, path.get(0));
                        if (cur == null)
                            return null;
                        for (int i = 1; i < path.size() - 1; i++) {
                            cur = cur.findInnerClassByName(path.get(i), false);
                            if (cur == null)
                                return null;
                        }
                        return cur.findFieldByName(path.get(path.size() - 1), false);
                    }
                    return null;
                }

                @Override
                public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
                    PsiElement element = resolve();
                    if (element instanceof PsiFile) {
                        FileReference lastRef = new FileReferenceSet(element).getLastReference();
                        return lastRef.handleElementRename(newElementName);
                    }
                    if (element instanceof PsiField) {
                        String fqn = ((PsiField) element).getContainingClass().getQualifiedName();
                        if (fqn.startsWith("com.intellij.icons.")) {
                            return replace(fqn, newElementName, "com.intellij.icons.");
                        }
                        if (fqn.startsWith("icons.")) {
                            return replace(fqn, newElementName, "icons.");
                        }
                    }
                    return super.handleElementRename(newElementName);
                }

                @Override
                public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
                    if (element instanceof PsiFile) {
                        FileReference lastRef = new FileReferenceSet(element).getLastReference();
                        return lastRef.bindToElement(element);
                    }
                    if (element instanceof PsiField) {
                        String fqn = ((PsiField) element).getContainingClass().getQualifiedName();
                        String newName = ((PsiField) element).getName();
                        if (fqn.startsWith("com.intellij.icons.")) {
                            return replace(fqn, newName, "com.intellij.icons.");
                        }
                        if (fqn.startsWith("icons.")) {
                            return replace(fqn, newName, "icons.");
                        }
                    }
                    return super.bindToElement(element);
                }

                private PsiElement replace(String fqn, String newName, String pckg) {
                    XmlAttribute parent = (XmlAttribute) getElement().getParent();
                    parent.setValue(fqn.substring(pckg.length()) + "." + newName);
                    return parent.getValueElement();
                }

                @NotNull
                @Override
                public Object[] getVariants() {
                    return EMPTY_ARRAY;
                }
            } };
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessingContext(com.intellij.util.ProcessingContext) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) ArrayList(java.util.ArrayList) List(java.util.List) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) PsiMethodPattern(com.intellij.patterns.PsiMethodPattern) Project(com.intellij.openapi.project.Project) PsiJavaElementPattern(com.intellij.patterns.PsiJavaElementPattern) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) PsiFileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.PsiFileReference) IconLoader(com.intellij.openapi.util.IconLoader)

Example 18 with ProcessingContext

use of com.intellij.util.ProcessingContext in project intellij-community by JetBrains.

the class GroovyDslScript method addGdslMembers.

private CustomMembersHolder addGdslMembers(GroovyClassDescriptor descriptor, final PsiType psiType) {
    final ProcessingContext ctx = new ProcessingContext();
    ctx.put(GdslUtil.INITIAL_CONTEXT, descriptor);
    try {
        if (!isApplicable(executor, descriptor, ctx)) {
            return CustomMembersHolder.EMPTY;
        }
        return executor.processVariants(descriptor, ctx, psiType);
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ProcessCanceledException) {
            throw (ProcessCanceledException) cause;
        }
        if (cause instanceof OutOfMemoryError) {
            throw (OutOfMemoryError) cause;
        }
        handleDslError(e);
    } catch (ProcessCanceledException | OutOfMemoryError e) {
        throw e;
    } catch (Throwable e) {
        // To handle exceptions in definition script
        handleDslError(e);
    }
    return CustomMembersHolder.EMPTY;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 19 with ProcessingContext

use of com.intellij.util.ProcessingContext in project intellij-plugins by JetBrains.

the class JstdConfigFileReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(JstdConfigFileUtils.CONFIG_FILE_ELEMENT_PATTERN, new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            final YAMLKeyValue keyValue = ObjectUtils.tryCast(element, YAMLKeyValue.class);
            if (keyValue == null) {
                return PsiReference.EMPTY_ARRAY;
            }
            final YAMLDocument yamlDocument = ObjectUtils.tryCast(keyValue.getParent(), YAMLDocument.class);
            if (yamlDocument == null) {
                return PsiReference.EMPTY_ARRAY;
            }
            final BasePathInfo basePathInfo = new BasePathInfo(yamlDocument);
            if (BasePathInfo.isBasePathKey(keyValue)) {
                PsiReference basePathRef = createBasePathRef(basePathInfo);
                if (basePathRef != null) {
                    return new PsiReference[] { basePathRef };
                }
            } else if (JstdConfigFileUtils.isTopLevelKeyWithInnerFileSequence(keyValue)) {
                VirtualFile basePath = basePathInfo.getBasePath();
                if (basePath != null) {
                    List<PsiReference> references = Lists.newArrayList();
                    addReferencesForKeyValueWithInnerFileSequence(basePathInfo, keyValue, references);
                    return references.toArray(new PsiReference[references.size()]);
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProcessingContext(com.intellij.util.ProcessingContext) YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument) YAMLKeyValue(org.jetbrains.yaml.psi.YAMLKeyValue) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with ProcessingContext

use of com.intellij.util.ProcessingContext in project intellij-plugins by JetBrains.

the class FlexConfigXmlReferenceContributor method registerReferenceProviders.

public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    XmlUtil.registerXmlTagReferenceProvider(registrar, new String[] { "path-element", "class", "classname", "symbol" }, new NamespaceFilter(FlexApplicationComponent.HTTP_WWW_ADOBE_COM_2006_FLEX_CONFIG), true, new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) {
            TextRange myRange = ElementManipulators.getValueTextRange(element);
            if (myRange.getStartOffset() == 0)
                return PsiReference.EMPTY_ARRAY;
            XmlTag tag = (XmlTag) element;
            final String trimmed = tag.getValue().getTrimmedText();
            if (trimmed.indexOf('{') != -1)
                return PsiReference.EMPTY_ARRAY;
            if ("path-element".equals(tag.getLocalName())) {
                return ReferenceSupport.getFileRefs(element, myRange.getStartOffset(), trimmed, ReferenceSupport.LookupOptions.FLEX_COMPILER_CONFIG_PATH_ELEMENT);
            }
            return new FlexConfigXmlReferenceSet(element, trimmed, myRange.getStartOffset()).getReferences();
        }
    });
    registrar.registerReferenceProvider(xmlAttributeValue(xmlAttribute("class").withParent(xmlTag().withName("component").withParent(xmlTag().withName("componentPackage")))), new PsiReferenceProvider() {

        @NotNull
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) {
            TextRange myRange = ElementManipulators.getValueTextRange(element);
            if (myRange.getStartOffset() == 0)
                return PsiReference.EMPTY_ARRAY;
            final String attrValue = ((XmlAttributeValue) element).getValue();
            return new FlexConfigXmlReferenceSet(element, attrValue, myRange.getStartOffset()).getReferences();
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) NamespaceFilter(com.intellij.psi.filters.position.NamespaceFilter) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

ProcessingContext (com.intellij.util.ProcessingContext)33 NotNull (org.jetbrains.annotations.NotNull)20 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 CompletionSorterImpl (com.intellij.codeInsight.completion.impl.CompletionSorterImpl)3 Module (com.intellij.openapi.module.Module)3 Project (com.intellij.openapi.project.Project)3 Collection (java.util.Collection)3 LiveTemplateLookupElement (com.intellij.codeInsight.template.impl.LiveTemplateLookupElement)2 CssClassValueReference (com.intellij.javascript.flex.css.CssClassValueReference)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ModuleUtilCore.findModuleForPsiElement (com.intellij.openapi.module.ModuleUtilCore.findModuleForPsiElement)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 JavaClassReferenceProvider (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider)2 XmlElement (com.intellij.psi.xml.XmlElement)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Configuration (org.intellij.plugins.intelliLang.Configuration)2