Search in sources :

Example 6 with ExpressionLookupItem

use of com.intellij.codeInsight.lookup.ExpressionLookupItem in project intellij-community by JetBrains.

the class FromArrayConversion method addConversions.

static void addConversions(final PsiElement element, final String prefix, final PsiType itemType, final Consumer<LookupElement> result, @Nullable PsiElement qualifier, final PsiType expectedType) throws IncorrectOperationException {
    final String methodName = getArraysConversionMethod(itemType, expectedType);
    if (methodName == null)
        return;
    final String qualifierText = ReferenceExpressionCompletionContributor.getQualifierText(qualifier);
    final PsiExpression conversion = createExpression("java.util.Arrays." + methodName + "(" + qualifierText + prefix + ")", element);
    final String presentable = "Arrays." + methodName + "(" + qualifierText + prefix + ")";
    String[] lookupStrings = { StringUtil.isEmpty(qualifierText) ? presentable : prefix, prefix, presentable, methodName + "(" + prefix + ")" };
    result.consume(new ExpressionLookupItem(conversion, PlatformIcons.METHOD_ICON, presentable, lookupStrings) {

        @Override
        public void handleInsert(InsertionContext context) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.SECOND_SMART_COMPLETION_ASLIST);
            int startOffset = context.getStartOffset() - qualifierText.length();
            final Project project = element.getProject();
            final String callSpace = getSpace(CodeStyleSettingsManager.getSettings(project).SPACE_WITHIN_METHOD_CALL_PARENTHESES);
            final String newText = "java.util.Arrays." + methodName + "(" + callSpace + qualifierText + prefix + callSpace + ")";
            context.getDocument().replaceString(startOffset, context.getTailOffset(), newText);
            context.commitDocument();
            JavaCodeStyleManager.getInstance(project).shortenClassReferences(context.getFile(), startOffset, startOffset + CommonClassNames.JAVA_UTIL_ARRAYS.length());
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem)

Example 7 with ExpressionLookupItem

use of com.intellij.codeInsight.lookup.ExpressionLookupItem in project intellij-community by JetBrains.

the class ReferenceExpressionCompletionContributor method fillCompletionVariants.

@Nullable
public static Runnable fillCompletionVariants(final JavaSmartCompletionParameters parameters, final Consumer<LookupElement> result) {
    final PsiElement element = parameters.getPosition();
    if (JavaSmartCompletionContributor.INSIDE_TYPECAST_EXPRESSION.accepts(element))
        return null;
    if (JavaKeywordCompletion.isAfterPrimitiveOrArrayType(element))
        return null;
    final int offset = parameters.getParameters().getOffset();
    final PsiJavaCodeReferenceElement reference = PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), offset, PsiJavaCodeReferenceElement.class, false);
    if (reference != null) {
        ElementFilter filter = getReferenceFilter(element, false);
        if (CheckInitialized.isInsideConstructorCall(element)) {
            filter = new AndFilter(filter, new CheckInitialized(element));
        }
        for (final LookupElement item : completeFinalReference(element, reference, filter, parameters)) {
            result.consume(item);
        }
        final boolean secondTime = parameters.getParameters().getInvocationCount() >= 2;
        final Set<LookupElement> base = JavaSmartCompletionContributor.completeReference(element, reference, filter, false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE);
        for (final LookupElement item : new LinkedHashSet<>(base)) {
            ExpressionLookupItem access = ArrayMemberAccess.accessFirstElement(element, item);
            if (access != null) {
                base.add(access);
                PsiType type = access.getType();
                if (type != null && parameters.getExpectedType().isAssignableFrom(type)) {
                    result.consume(access);
                }
            }
        }
        if (secondTime) {
            return new SlowerTypeConversions(base, element, reference, parameters, result);
        }
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ExpressionLookupItem (com.intellij.codeInsight.lookup.ExpressionLookupItem)7 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 NotNull (org.jetbrains.annotations.NotNull)2 VariableLookupItem (com.intellij.codeInsight.lookup.VariableLookupItem)1 SmartCompletionContextType (com.intellij.codeInsight.template.SmartCompletionContextType)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 Project (com.intellij.openapi.project.Project)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 SmartList (com.intellij.util.SmartList)1 LinkedHashSet (java.util.LinkedHashSet)1 Nullable (org.jetbrains.annotations.Nullable)1