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