Search in sources :

Example 1 with ProcessingContext

use of com.intellij.util.ProcessingContext in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestFunctionCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    Project project = parameters.getPosition().getProject();
    PsiFile file = parameters.getOriginalFile();
    PsiDirectory containingDirectory = file.getContainingDirectory();
    if (file instanceof GoFile && containingDirectory != null) {
        CompletionResultSet resultSet = result.withPrefixMatcher(new CamelHumpMatcher(result.getPrefixMatcher().getPrefix(), false));
        Collection<String> allPackageFunctionNames = collectAllFunctionNames(containingDirectory);
        Set<String> allTestFunctionNames = collectAllTestNames(allPackageFunctionNames, project, (GoFile) file);
        String fileNameWithoutTestPrefix = StringUtil.trimEnd(file.getName(), GoConstants.TEST_SUFFIX_WITH_EXTENSION) + ".go";
        GlobalSearchScope packageScope = GoPackageUtil.packageScope(containingDirectory, ((GoFile) file).getCanonicalPackageName());
        GlobalSearchScope scope = new GoUtil.ExceptTestsScope(packageScope);
        IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
        for (String functionName : allPackageFunctionNames) {
            GoFunctionIndex.process(functionName, project, scope, idFilter, declaration -> {
                addVariants(declaration, functionName, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
                return false;
            });
        }
        Collection<String> methodKeys = ContainerUtil.newTroveSet();
        StubIndex.getInstance().processAllKeys(GoMethodIndex.KEY, new CancellableCollectProcessor<>(methodKeys), scope, idFilter);
        for (String key : methodKeys) {
            Processor<GoMethodDeclaration> processor = declaration -> {
                GoMethodDeclarationStubElementType.calcTypeText(declaration);
                String typeText = key.substring(Math.min(key.indexOf('.') + 1, key.length()));
                String methodName = declaration.getName();
                if (methodName != null) {
                    if (!declaration.isPublic() || declaration.isBlank()) {
                        return true;
                    }
                    String lookupString = !typeText.isEmpty() ? StringUtil.capitalize(typeText) + "_" + methodName : methodName;
                    addVariants(declaration, lookupString, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
                }
                return true;
            };
            GoMethodIndex.process(key, project, scope, idFilter, processor);
        }
    }
}
Also used : com.goide.psi(com.goide.psi) Document(com.intellij.openapi.editor.Document) GoMethodIndex(com.goide.stubs.index.GoMethodIndex) GotestGenerateAction(com.goide.runconfig.testing.frameworks.gotest.GotestGenerateAction) IdFilter(com.intellij.util.indexing.IdFilter) StubIndex(com.intellij.psi.stubs.StubIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) GoTestFunctionType(com.goide.runconfig.testing.GoTestFunctionType) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GoMethodDeclarationStubElementType(com.goide.stubs.types.GoMethodDeclarationStubElementType) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) GoElementFactory(com.goide.psi.impl.GoElementFactory) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ProcessingContext(com.intellij.util.ProcessingContext) GoFunctionIndex(com.goide.stubs.index.GoFunctionIndex) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GoUtil(com.goide.util.GoUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) GoIdFilter(com.goide.stubs.index.GoIdFilter) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Processor(com.intellij.util.Processor) GoConstants(com.goide.GoConstants) PsiDirectory(com.intellij.psi.PsiDirectory) GoPackageUtil(com.goide.sdk.GoPackageUtil) NotNull(org.jetbrains.annotations.NotNull) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 2 with ProcessingContext

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

the class DomCompletionContributor method domKnowsBetter.

private boolean domKnowsBetter(final CompletionParameters parameters, final CompletionResultSet result) {
    final XmlAttributeValue element = PsiTreeUtil.getParentOfType(parameters.getPosition(), XmlAttributeValue.class);
    if (element == null) {
        return false;
    }
    if (isSchemaEnumerated(element)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (parent instanceof XmlAttribute) {
        XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
        if (descriptor != null && descriptor.getDefaultValue() != null) {
            final PsiReference[] references = myProvider.getReferencesByElement(element, new ProcessingContext());
            if (references.length > 0) {
                return LegacyCompletionContributor.completeReference(parameters, result);
            }
        }
    }
    return false;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement)

Example 3 with ProcessingContext

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

the class PropertiesReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(literalExpression(), new PropertiesReferenceProvider(true));
    registrar.registerReferenceProvider(literalExpression().withParent(psiNameValuePair().withName(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER)), new ResourceBundleReferenceProvider());
    registrar.registerReferenceProvider(literalExpression(), new PsiReferenceProvider() {

        private final PsiReferenceProvider myUnderlying = new ResourceBundleReferenceProvider();

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            final PsiElement parent = element.getParent();
            if (!(parent instanceof PsiField)) {
                return PsiReference.EMPTY_ARRAY;
            }
            final PsiField field = (PsiField) parent;
            if (field.getInitializer() != element || !field.hasModifierProperty(PsiModifier.FINAL) || !field.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
                return PsiReference.EMPTY_ARRAY;
            }
            List<PsiReference> references = new ArrayList<>();
            final PsiClass propertyKeyAnnotation = JavaPsiFacade.getInstance(element.getProject()).findClass(AnnotationUtil.PROPERTY_KEY, element.getResolveScope());
            if (propertyKeyAnnotation != null) {
                AnnotatedElementsSearch.searchPsiParameters(propertyKeyAnnotation, new LocalSearchScope(element.getContainingFile())).forEach(parameter -> {
                    final PsiModifierList list = parameter.getModifierList();
                    LOG.assertTrue(list != null);
                    final PsiAnnotation annotation = list.findAnnotation(AnnotationUtil.PROPERTY_KEY);
                    LOG.assertTrue(annotation != null);
                    for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) {
                        if (AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER.equals(pair.getName())) {
                            final PsiAnnotationMemberValue value = pair.getValue();
                            if (value instanceof PsiReferenceExpression && ((PsiReferenceExpression) value).resolve() == field) {
                                Collections.addAll(references, myUnderlying.getReferencesByElement(element, context));
                                return false;
                            }
                        }
                    }
                    return true;
                });
            }
            return references.toArray(new PsiReference[references.size()]);
        }
    });
    registrar.registerReferenceProvider(PsiJavaPatterns.psiElement(PropertyValueImpl.class), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            String text = element.getText();
            String[] words = text.split("\\s");
            if (words.length != 1)
                return PsiReference.EMPTY_ARRAY;
            return CLASS_REFERENCE_PROVIDER.getReferencesByString(words[0], element, 0);
        }
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ProcessingContext(com.intellij.util.ProcessingContext) PsiJavaPatterns.psiNameValuePair(com.intellij.patterns.PsiJavaPatterns.psiNameValuePair) PsiJavaPatterns.literalExpression(com.intellij.patterns.PsiJavaPatterns.literalExpression) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ArrayList(java.util.ArrayList) PropertyValueImpl(com.intellij.lang.properties.psi.impl.PropertyValueImpl) List(java.util.List) AnnotatedElementsSearch(com.intellij.psi.search.searches.AnnotatedElementsSearch) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) com.intellij.psi(com.intellij.psi) JavaClassReferenceProvider(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider) Logger(com.intellij.openapi.diagnostic.Logger) PsiJavaPatterns(com.intellij.patterns.PsiJavaPatterns) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ProcessingContext(com.intellij.util.ProcessingContext) NotNull(org.jetbrains.annotations.NotNull) ArrayList(java.util.ArrayList) List(java.util.List) PropertyValueImpl(com.intellij.lang.properties.psi.impl.PropertyValueImpl)

Example 4 with ProcessingContext

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

the class PyQualifiedReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    PyExpression qualifier = myElement.getQualifier();
    if (qualifier != null) {
        qualifier = CompletionUtil.getOriginalOrSelf(qualifier);
    }
    if (qualifier == null) {
        return EMPTY_ARRAY;
    }
    final PyQualifiedExpression element = CompletionUtil.getOriginalOrSelf(myElement);
    PyType qualifierType = TypeEvalContext.codeCompletion(element.getProject(), element.getContainingFile()).getType(qualifier);
    ProcessingContext ctx = new ProcessingContext();
    final Set<String> namesAlready = new HashSet<>();
    ctx.put(PyType.CTX_NAMES, namesAlready);
    final Collection<Object> variants = new ArrayList<>();
    if (qualifierType != null) {
        Collections.addAll(variants, getVariantFromHasAttr(qualifier));
        if (qualifierType instanceof PyStructuralType && ((PyStructuralType) qualifierType).isInferredFromUsages()) {
            final PyClassType guessedType = guessClassTypeByName();
            if (guessedType != null) {
                Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
            }
        }
        if (qualifier instanceof PyQualifiedExpression) {
            final PyQualifiedExpression qualifierExpression = (PyQualifiedExpression) qualifier;
            final QualifiedName qualifiedName = qualifierExpression.asQualifiedName();
            if (qualifiedName == null) {
                return variants.toArray();
            }
            final Collection<PyExpression> attrs = collectAssignedAttributes(qualifiedName, qualifier);
            for (PyExpression ex : attrs) {
                final String name = ex.getName();
                if (name != null && name.endsWith(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
                    continue;
                }
                if (ex instanceof PsiNamedElement && qualifierType instanceof PyClassType && name != null) {
                    variants.add(LookupElementBuilder.createWithSmartPointer(name, ex).withTypeText(qualifierType.getName()).withIcon(PlatformIcons.FIELD_ICON));
                }
                if (ex instanceof PyReferenceExpression) {
                    PyReferenceExpression refExpr = (PyReferenceExpression) ex;
                    namesAlready.add(refExpr.getReferencedName());
                } else if (ex instanceof PyTargetExpression) {
                    PyTargetExpression targetExpr = (PyTargetExpression) ex;
                    namesAlready.add(targetExpr.getName());
                }
            }
            Collections.addAll(variants, qualifierType.getCompletionVariants(element.getName(), element, ctx));
            return variants.toArray();
        } else {
            return qualifierType.getCompletionVariants(element.getName(), element, ctx);
        }
    } else {
        final PyClassType guessedType = guessClassTypeByName();
        if (guessedType != null) {
            Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
        }
        if (qualifier instanceof PyReferenceExpression) {
            Collections.addAll(variants, collectSeenMembers(qualifier.getText()));
        }
        return variants.toArray();
    }
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ProcessingContext

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

the class CompletionLookupArranger method addElement.

@Override
public void addElement(LookupElement element, LookupElementPresentation presentation) {
    StatisticsWeigher.clearBaseStatisticsInfo(element);
    PresentationInvariant invariant = new PresentationInvariant(presentation.getItemText(), presentation.getTailText(), presentation.getTypeText());
    element.putUserData(PRESENTATION_INVARIANT, invariant);
    element.putUserData(GLOBAL_PRESENTATION_INVARIANT, invariant);
    CompletionSorterImpl sorter = obtainSorter(element);
    Classifier<LookupElement> classifier = myClassifiers.get(sorter);
    if (classifier == null) {
        myClassifiers.put(sorter, classifier = sorter.buildClassifier(new EmptyClassifier()));
    }
    ProcessingContext context = createContext(true);
    classifier.addElement(element, context);
    super.addElement(element, presentation);
    trimToLimit(context);
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) CompletionSorterImpl(com.intellij.codeInsight.completion.impl.CompletionSorterImpl) LiveTemplateLookupElement(com.intellij.codeInsight.template.impl.LiveTemplateLookupElement)

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