Search in sources :

Example 21 with ProcessingContext

use of com.intellij.util.ProcessingContext 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 22 with ProcessingContext

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

the class JavaCompletionContributor method addIdentifierVariants.

private static void addIdentifierVariants(@NotNull CompletionParameters parameters, PsiElement position, CompletionResultSet result, PrefixMatcher matcher, PsiElement parent, @NotNull JavaCompletionSession session) {
    if (TypeArgumentCompletionProvider.IN_TYPE_ARGS.accepts(position)) {
        new TypeArgumentCompletionProvider(false, session).addCompletions(parameters, new ProcessingContext(), result);
    }
    FunctionalExpressionCompletionProvider.addFunctionalVariants(parameters, false, false, result);
    if (JavaSmartCompletionContributor.AFTER_NEW.accepts(position)) {
        new JavaInheritorsGetter(ConstructorInsertHandler.BASIC_INSTANCE).generateVariants(parameters, matcher, session);
    }
    if (MethodReturnTypeProvider.IN_METHOD_RETURN_TYPE.accepts(position)) {
        MethodReturnTypeProvider.addProbableReturnTypes(parameters, element -> {
            registerClassFromTypeElement(element, session);
            result.addElement(element);
        });
    }
    if (SmartCastProvider.shouldSuggestCast(parameters)) {
        SmartCastProvider.addCastVariants(parameters, result.getPrefixMatcher(), element -> {
            registerClassFromTypeElement(element, session);
            result.addElement(PrioritizedLookupElement.withPriority(element, 1));
        });
    }
    if (parent instanceof PsiReferenceExpression) {
        final List<ExpectedTypeInfo> expected = Arrays.asList(ExpectedTypesProvider.getExpectedTypes((PsiExpression) parent, true));
        CollectConversion.addCollectConversion((PsiReferenceExpression) parent, expected, JavaSmartCompletionContributor.decorateWithoutTypeCheck(result, expected));
    }
    if (IMPORT_REFERENCE.accepts(position)) {
        result.addElement(LookupElementBuilder.create("*"));
    }
    addKeywords(parameters, result, session);
    addExpressionVariants(parameters, position, result);
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Example 23 with ProcessingContext

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

the class InlineToAnonymousConstructorProcessor method analyzeConstructor.

private void analyzeConstructor(final PsiCodeBlock initializerBlock) throws IncorrectOperationException {
    PsiCodeBlock body = myConstructor.getBody();
    assert body != null;
    for (PsiElement child : body.getChildren()) {
        if (child instanceof PsiStatement) {
            PsiStatement stmt = (PsiStatement) child;
            ProcessingContext context = new ProcessingContext();
            if (ourAssignmentPattern.accepts(stmt, context)) {
                PsiAssignmentExpression expression = context.get(ourAssignmentKey);
                if (processAssignmentInConstructor(expression)) {
                    initializerBlock.addBefore(replaceParameterReferences(stmt, null, false), initializerBlock.getRBrace());
                }
            } else if (!ourSuperCallPattern.accepts(stmt) && !ourThisCallPattern.accepts(stmt)) {
                replaceParameterReferences(stmt, new ArrayList<>(), false);
                initializerBlock.addBefore(stmt, initializerBlock.getRBrace());
            }
        } else if (child instanceof PsiComment) {
            if (child.getPrevSibling() instanceof PsiWhiteSpace) {
                initializerBlock.addBefore(child.getPrevSibling(), initializerBlock.getRBrace());
            }
            initializerBlock.addBefore(child, initializerBlock.getRBrace());
        }
    }
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) ArrayList(java.util.ArrayList)

Example 24 with ProcessingContext

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

the class I18nReferenceContributor method registerBundleNameProviders.

private static void registerBundleNameProviders(PsiReferenceRegistrar registrar) {
    final PsiReferenceProvider bundleReferenceProvider = new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            return new PsiReference[] { new MyResourceBundleReference(element) };
        }
    };
    final XmlTagPattern.Capture resourceBundleTagPattern = XmlPatterns.xmlTag().withName("resource-bundle").withParent(XmlPatterns.xmlTag().withName("idea-plugin"));
    registrar.registerReferenceProvider(resourceBundleTagPattern, bundleReferenceProvider);
    ElementPattern bundlePattern = createPattern(EXTENSION_TAG_NAMES, "bundle", "groupBundle");
    registrar.registerReferenceProvider(bundlePattern, bundleReferenceProvider, PsiReferenceRegistrar.DEFAULT_PRIORITY);
    ElementPattern typeNameBundlePattern = createPattern(TYPE_NAME_TAG, "resourceBundle");
    registrar.registerReferenceProvider(typeNameBundlePattern, bundleReferenceProvider, PsiReferenceRegistrar.DEFAULT_PRIORITY);
    final XmlTagPattern.Capture intentionActionBundleTagPattern = XmlPatterns.xmlTag().withName(INTENTION_ACTION_BUNDLE_TAG).withParent(XmlPatterns.xmlTag().withName(INTENTION_ACTION_TAG).withSuperParent(2, XmlPatterns.xmlTag().withName("idea-plugin")));
    registrar.registerReferenceProvider(intentionActionBundleTagPattern, bundleReferenceProvider, PsiReferenceRegistrar.DEFAULT_PRIORITY);
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) XmlTagPattern(com.intellij.patterns.XmlTagPattern) ElementPattern(com.intellij.patterns.ElementPattern) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with ProcessingContext

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

the class LanguageReferenceProvider method registerReferenceProviders.

public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    final Configuration configuration = Configuration.getInstance();
    registrar.registerReferenceProvider(literalExpression().annotationParam(StandardPatterns.string().with(new PatternCondition<String>("isLanguageAnnotation") {

        @Override
        public boolean accepts(@NotNull final String s, final ProcessingContext context) {
            return Comparing.equal(configuration.getAdvancedConfiguration().getLanguageAnnotationClass(), s);
        }
    }), "value").and(literalExpression().with(new PatternCondition<PsiLiteralExpression>("isStringLiteral") {

        @Override
        public boolean accepts(@NotNull final PsiLiteralExpression expression, final ProcessingContext context) {
            return PsiUtilEx.isStringOrCharacterLiteral(expression);
        }
    })), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) {
            return new PsiReference[] { new LanguageReference((PsiLiteralExpression) element) };
        }
    });
    registrar.registerReferenceProvider(literalExpression().with(new PatternCondition<PsiLiteralExpression>("isStringLiteral") {

        @Override
        public boolean accepts(@NotNull final PsiLiteralExpression expression, final ProcessingContext context) {
            return PsiUtilEx.isStringOrCharacterLiteral(expression);
        }
    }), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext context) {
            final PsiLiteralExpression expression = (PsiLiteralExpression) psiElement;
            final PsiModifierListOwner owner = AnnotationUtilEx.getAnnotatedElementFor(expression, AnnotationUtilEx.LookupType.PREFER_DECLARATION);
            if (owner != null && PsiUtilEx.isLanguageAnnotationTarget(owner)) {
                final PsiAnnotation[] annotations = AnnotationUtilEx.getAnnotationFrom(owner, configuration.getAdvancedConfiguration().getPatternAnnotationPair(), true);
                if (annotations.length > 0) {
                    final String pattern = AnnotationUtilEx.calcAnnotationValue(annotations, "value");
                    if (pattern != null) {
                        return new PsiReference[] { new RegExpEnumReference(expression, pattern) };
                    }
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) Configuration(org.intellij.plugins.intelliLang.Configuration) PatternCondition(com.intellij.patterns.PatternCondition) NotNull(org.jetbrains.annotations.NotNull)

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