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);
}
}
}
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;
}
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);
}
});
}
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();
}
}
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);
}
Aggregations