use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class ReferenceExpressionCompletionContributor method completeFinalReference.
static Set<LookupElement> completeFinalReference(final PsiElement element, PsiJavaCodeReferenceElement reference, ElementFilter filter, final JavaSmartCompletionParameters parameters) {
final Set<PsiField> used = parameters.getParameters().getInvocationCount() < 2 ? findConstantsUsedInSwitch(element) : Collections.<PsiField>emptySet();
final Set<LookupElement> elements = JavaSmartCompletionContributor.completeReference(element, reference, new AndFilter(filter, new ElementFilter() {
@Override
public boolean isAcceptable(Object o, PsiElement context) {
if (o instanceof CandidateInfo) {
final CandidateInfo info = (CandidateInfo) o;
final PsiElement member = info.getElement();
final PsiType expectedType = parameters.getExpectedType();
if (expectedType.equals(PsiType.VOID)) {
return member instanceof PsiMethod;
}
//noinspection SuspiciousMethodCalls
if (member instanceof PsiEnumConstant && used.contains(CompletionUtil.getOriginalOrSelf(member))) {
return false;
}
return AssignableFromFilter.isAcceptable(member, element, expectedType, info.getSubstitutor());
}
return false;
}
@Override
public boolean isClassAcceptable(Class hintClass) {
return true;
}
}), false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE);
for (LookupElement lookupElement : elements) {
if (lookupElement.getObject() instanceof PsiMethod) {
final JavaMethodCallElement item = lookupElement.as(JavaMethodCallElement.CLASS_CONDITION_KEY);
if (item != null) {
final PsiMethod method = (PsiMethod) lookupElement.getObject();
if (SmartCompletionDecorator.hasUnboundTypeParams(method, parameters.getExpectedType())) {
item.setInferenceSubstitutor(SmartCompletionDecorator.calculateMethodReturnTypeSubstitutor(method, parameters.getExpectedType()), element);
}
}
}
}
return elements;
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class SlowerTypeConversions method run.
@Override
public void run() {
final Set<Pair<LookupElement, String>> processedChains = ContainerUtil.newHashSet();
for (final LookupElement item : myBase) {
addSecondCompletionVariants(myElement, myReference, item, myParameters, lookupElement -> {
ContainerUtil.addIfNotNull(processedChains, chainInfo(lookupElement));
myResult.consume(lookupElement);
});
}
if (!psiElement().afterLeaf(".").accepts(myElement)) {
BasicExpressionCompletionContributor.processDataflowExpressionTypes(myElement, null, TRUE_MATCHER, baseItem -> addSecondCompletionVariants(myElement, myReference, baseItem, myParameters, lookupElement -> {
if (!processedChains.contains(chainInfo(lookupElement))) {
myResult.consume(lookupElement);
}
}));
}
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class JavaCharFilter method acceptChar.
@Override
public Result acceptChar(char c, final int prefixLength, final Lookup lookup) {
if (!lookup.getPsiFile().getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
return null;
}
LookupElement item = lookup.getCurrentItem();
if (item == null || !item.isValid())
return null;
final Object o = item.getObject();
if (c == '!') {
if (o instanceof PsiVariable) {
if (PsiType.BOOLEAN.isAssignableFrom(((PsiVariable) o).getType()))
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
if (o instanceof PsiMethod) {
final PsiType type = ((PsiMethod) o).getReturnType();
if (type != null && PsiType.BOOLEAN.isAssignableFrom(type))
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
if (o instanceof PsiKeyword && ((PsiKeyword) o).textMatches(PsiKeyword.INSTANCEOF)) {
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
return null;
}
if (c == '.' && isWithinLiteral(lookup))
return Result.ADD_TO_PREFIX;
if (c == ':') {
PsiFile file = lookup.getPsiFile();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(lookup.getEditor().getDocument());
PsiElement leaf = file.findElementAt(lookup.getEditor().getCaretModel().getOffset() - 1);
if (PsiUtil.getLanguageLevel(file).isAtLeast(LanguageLevel.JDK_1_8)) {
PsiStatement statement = PsiTreeUtil.getParentOfType(leaf, PsiStatement.class);
if (statement == null || statement.getTextRange().getStartOffset() != leaf.getTextRange().getStartOffset()) {
// not typing a statement label
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
}
if (PsiTreeUtil.getParentOfType(leaf, PsiSwitchLabelStatement.class) != null || PsiTreeUtil.getParentOfType(leaf, PsiConditionalExpression.class) != null) {
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
return Result.HIDE_LOOKUP;
}
if (c == '[' || c == ']' || c == ')' || c == '>')
return CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP;
if (c == '<' && o instanceof PsiClass)
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
if (c == '(') {
if (o instanceof PsiClass) {
if (PsiJavaPatterns.psiElement().afterLeaf(PsiKeyword.NEW).accepts(lookup.getPsiElement())) {
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
return Result.HIDE_LOOKUP;
}
if (o instanceof PsiType) {
return Result.HIDE_LOOKUP;
}
}
if ((c == ',' || c == '=') && o instanceof PsiVariable) {
int lookupStart = lookup.getLookupStart();
String name = ((PsiVariable) o).getName();
if (lookupStart >= 0 && name != null && name.equals(lookup.itemPattern(item))) {
return Result.HIDE_LOOKUP;
}
}
if (c == '#' && PsiTreeUtil.getParentOfType(lookup.getPsiElement(), PsiDocComment.class) != null) {
if (o instanceof PsiClass) {
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
}
}
if (c == '(' && PsiKeyword.RETURN.equals(item.getLookupString())) {
return Result.HIDE_LOOKUP;
}
return null;
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class JavaMethodMergingContributor method handleAutoCompletionPossibility.
@Override
public AutoCompletionDecision handleAutoCompletionPossibility(@NotNull AutoCompletionContext context) {
final CompletionParameters parameters = context.getParameters();
if (parameters.getCompletionType() != CompletionType.SMART && parameters.getCompletionType() != CompletionType.BASIC) {
return null;
}
if (Registry.is("java.completion.argument.live.template")) {
return null;
}
final LookupElement[] items = context.getItems();
if (items.length > 1) {
String commonName = null;
final ArrayList<PsiMethod> allMethods = new ArrayList<>();
for (LookupElement item : items) {
Object o = item.getPsiElement();
if (item.getUserData(JavaCompletionUtil.FORCE_SHOW_SIGNATURE_ATTR) != null || !(o instanceof PsiMethod)) {
return AutoCompletionDecision.SHOW_LOOKUP;
}
final PsiMethod method = (PsiMethod) o;
final JavaChainLookupElement chain = item.as(JavaChainLookupElement.CLASS_CONDITION_KEY);
final String name = method.getName() + "#" + (chain == null ? "" : chain.getQualifier().getLookupString());
if (commonName != null && !commonName.equals(name)) {
return AutoCompletionDecision.SHOW_LOOKUP;
}
commonName = name;
allMethods.add(method);
}
for (LookupElement item : items) {
JavaCompletionUtil.putAllMethods(item, allMethods);
}
return AutoCompletionDecision.insertItem(findBestOverload(items));
}
return super.handleAutoCompletionPossibility(context);
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class JavaNoVariantsDelegator method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
if (JavaModuleCompletion.isModuleFile(parameters.getOriginalFile())) {
return;
}
final JavaCompletionSession session = new JavaCompletionSession(result);
ResultTracker tracker = new ResultTracker(result) {
@Override
public void consume(CompletionResult plainResult) {
super.consume(plainResult);
LookupElement element = plainResult.getLookupElement();
Object o = element.getObject();
if (o instanceof PsiClass) {
session.registerClass((PsiClass) o);
}
if (element instanceof TypeArgumentCompletionProvider.TypeArgsLookupElement) {
((TypeArgumentCompletionProvider.TypeArgsLookupElement) element).registerSingleClass(session);
}
}
};
result.runRemainingContributors(parameters, tracker);
final boolean empty = tracker.containsOnlyPackages || suggestAllAnnotations(parameters);
if (JavaCompletionContributor.isClassNamePossible(parameters) && !JavaCompletionContributor.mayStartClassName(result)) {
result.restartCompletionOnAnyPrefixChange();
}
if (empty) {
delegate(parameters, JavaCompletionSorting.addJavaSorting(parameters, result), session);
} else if (Registry.is("ide.completion.show.better.matching.classes")) {
if (parameters.getCompletionType() == CompletionType.BASIC && parameters.getInvocationCount() <= 1 && JavaCompletionContributor.mayStartClassName(result) && JavaCompletionContributor.isClassNamePossible(parameters) && !areNonImportedInheritorsAlreadySuggested(parameters)) {
suggestNonImportedClasses(parameters, JavaCompletionSorting.addJavaSorting(parameters, result.withPrefixMatcher(tracker.betterMatcher)), session);
}
}
}
Aggregations