Search in sources :

Example 1 with ExpressionLookupItem

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

the class ToArrayConversion method addToArrayConversion.

private static void addToArrayConversion(final PsiElement element, final String prefix, @NonNls final String expressionString, @NonNls String presentableString, final Consumer<LookupElement> result, PsiElement qualifier) {
    final boolean callSpace = CodeStyleSettingsManager.getSettings(element.getProject()).SPACE_WITHIN_METHOD_CALL_PARENTHESES;
    final PsiExpression conversion;
    try {
        conversion = createExpression(getQualifierText(qualifier) + prefix + ".toArray(" + getSpace(callSpace) + expressionString + getSpace(callSpace) + ")", element);
    } catch (IncorrectOperationException e) {
        return;
    }
    String[] lookupStrings = { prefix + ".toArray(" + getSpace(callSpace) + expressionString + getSpace(callSpace) + ")", presentableString };
    result.consume(new ExpressionLookupItem(conversion, PlatformIcons.METHOD_ICON, prefix + ".toArray(" + presentableString + ")", lookupStrings) {

        @Override
        public void handleInsert(InsertionContext context) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.SECOND_SMART_COMPLETION_TOAR);
            context.commitDocument();
            JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(context.getFile(), context.getStartOffset(), context.getTailOffset());
        }
    });
}
Also used : ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with ExpressionLookupItem

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

the class BasicExpressionCompletionContributor method fillCompletionVariants.

public static void fillCompletionVariants(JavaSmartCompletionParameters parameters, final Consumer<LookupElement> result, PrefixMatcher matcher) {
    final PsiElement element = parameters.getPosition();
    if (JavaKeywordCompletion.isAfterTypeDot(element)) {
        addKeyword(result, element, PsiKeyword.CLASS);
        addKeyword(result, element, PsiKeyword.THIS);
    }
    if (!JavaKeywordCompletion.AFTER_DOT.accepts(element)) {
        if (parameters.getParameters().getInvocationCount() <= 1) {
            new CollectionsUtilityMethodsProvider(parameters.getPosition(), parameters.getExpectedType(), parameters.getDefaultType(), result).addCompletions(StringUtil.isNotEmpty(matcher.getPrefix()));
        }
        ClassLiteralGetter.addCompletions(parameters, result, matcher);
        final PsiElement position = parameters.getPosition();
        final PsiType expectedType = parameters.getExpectedType();
        for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) {
            if (!template.isDeactivated() && template.getTemplateContext().isEnabled(new SmartCompletionContextType())) {
                result.consume(new SmartCompletionTemplateItem(template, position));
            }
        }
        addKeyword(result, position, PsiKeyword.TRUE);
        addKeyword(result, position, PsiKeyword.FALSE);
        final PsiElement parent = position.getParent();
        if (parent != null && !(parent.getParent() instanceof PsiSwitchLabelStatement)) {
            for (final PsiExpression expression : ThisGetter.getThisExpressionVariants(position)) {
                result.consume(new ExpressionLookupItem(expression));
            }
        }
        processDataflowExpressionTypes(position, expectedType, matcher, result);
    }
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) SmartCompletionContextType(com.intellij.codeInsight.template.SmartCompletionContextType) ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem)

Example 3 with ExpressionLookupItem

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

the class AddVariableInitializerFix method suggestInitializer.

@NotNull
public static LookupElement[] suggestInitializer(final PsiVariable variable) {
    PsiType type = variable.getType();
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(variable.getProject());
    final List<LookupElement> result = new SmartList<>();
    final String defaultValue = PsiTypesUtil.getDefaultValueOfType(type);
    final ExpressionLookupItem defaultExpression = new ExpressionLookupItem(elementFactory.createExpressionFromText(defaultValue, variable));
    result.add(defaultExpression);
    if (type instanceof PsiClassType) {
        final PsiClass aClass = PsiTypesUtil.getPsiClass(type);
        if (aClass != null && PsiUtil.hasDefaultConstructor(aClass)) {
            final String expressionText = PsiKeyword.NEW + " " + type.getCanonicalText(false) + "()";
            ExpressionLookupItem newExpression = new ExpressionLookupItem(elementFactory.createExpressionFromText(expressionText, variable));
            result.add(newExpression);
        }
    }
    return result.toArray(new LookupElement[result.size()]);
}
Also used : ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem) SmartList(com.intellij.util.SmartList) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 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 5 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