use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class FunctionalExpressionCompletionProvider method addFunctionalVariants.
static void addFunctionalVariants(@NotNull CompletionParameters parameters, boolean smart, boolean addInheritors, CompletionResultSet result) {
if (!PsiUtil.isLanguageLevel8OrHigher(parameters.getOriginalFile()) || !isLambdaContext(parameters.getPosition()))
return;
ExpectedTypeInfo[] expectedTypes = JavaSmartCompletionContributor.getExpectedTypes(parameters);
for (ExpectedTypeInfo expectedType : expectedTypes) {
final PsiType defaultType = expectedType.getDefaultType();
if (LambdaUtil.isFunctionalType(defaultType)) {
final PsiType functionalInterfaceType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(defaultType);
final PsiMethod functionalInterfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
if (functionalInterfaceMethod != null) {
PsiParameter[] params = PsiParameter.EMPTY_ARRAY;
final PsiElement originalPosition = parameters.getPosition();
final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(functionalInterfaceMethod, PsiUtil.resolveGenericsClassInType(functionalInterfaceType));
if (!functionalInterfaceMethod.hasTypeParameters()) {
params = functionalInterfaceMethod.getParameterList().getParameters();
final Project project = functionalInterfaceMethod.getProject();
final JVMElementFactory jvmElementFactory = JVMElementFactories.getFactory(originalPosition.getLanguage(), project);
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
if (jvmElementFactory != null) {
params = GenerateMembersUtil.overriddenParameters(params, jvmElementFactory, javaCodeStyleManager, substitutor, originalPosition);
}
String paramsString = params.length == 1 ? getParamName(params[0], originalPosition) : "(" + StringUtil.join(params, parameter -> getParamName(parameter, originalPosition), ",") + ")";
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiLambdaExpression lambdaExpression = (PsiLambdaExpression) JavaPsiFacade.getElementFactory(project).createExpressionFromText(paramsString + " -> {}", null);
lambdaExpression = (PsiLambdaExpression) codeStyleManager.reformat(lambdaExpression);
paramsString = lambdaExpression.getParameterList().getText();
final LookupElementBuilder builder = LookupElementBuilder.create(functionalInterfaceMethod, paramsString + " -> ").withPresentableText(paramsString + " -> {}").withTypeText(functionalInterfaceType.getPresentableText()).withIcon(AllIcons.Nodes.Function);
LookupElement lambdaElement = builder.withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE);
result.addElement(smart ? lambdaElement : PrioritizedLookupElement.withPriority(lambdaElement, 1));
}
addMethodReferenceVariants(smart, addInheritors, parameters, result.getPrefixMatcher(), functionalInterfaceType, functionalInterfaceMethod, params, originalPosition, substitutor, element -> result.addElement(smart ? JavaSmartCompletionContributor.decorate(element, Arrays.asList(expectedTypes)) : element));
}
}
}
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class SameSignatureCallParametersProvider method createParametersLookupElement.
private static LookupElement createParametersLookupElement(final PsiMethod takeParametersFrom, PsiElement call, PsiMethod invoked) {
final PsiParameter[] parameters = takeParametersFrom.getParameterList().getParameters();
final String lookupString = StringUtil.join(parameters, psiParameter -> psiParameter.getName(), ", ");
final int w = PlatformIcons.PARAMETER_ICON.getIconWidth();
LayeredIcon icon = new LayeredIcon(2);
icon.setIcon(PlatformIcons.PARAMETER_ICON, 0, 2 * w / 5, 0);
icon.setIcon(PlatformIcons.PARAMETER_ICON, 1);
LookupElementBuilder element = LookupElementBuilder.create(lookupString).withIcon(icon);
if (PsiTreeUtil.isAncestor(takeParametersFrom, call, true)) {
element = element.withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
context.commitDocument();
for (PsiParameter parameter : CompletionUtil.getOriginalOrSelf(takeParametersFrom).getParameterList().getParameters()) {
VariableLookupItem.makeFinalIfNeeded(context, parameter);
}
}
});
}
element.putUserData(JavaCompletionUtil.SUPER_METHOD_PARAMETERS, Boolean.TRUE);
return TailTypeDecorator.withTail(element, ExpectedTypesProvider.getFinalCallParameterTailType(call, invoked.getReturnType(), invoked));
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class ListTemplateActionTest method doTest.
private void doTest(@NotNull String lookupText) {
myFixture.configureByFile(getTestName(false) + ".java");
new ListTemplatesAction().actionPerformedImpl(myFixture.getProject(), myFixture.getEditor());
LookupElement[] elements = myFixture.getLookupElements();
assertNotNull(elements);
for (LookupElement element : elements) {
if (lookupText.equals(element.getLookupString())) {
myFixture.getLookup().setCurrentItem(element);
myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR);
myFixture.checkResultByFile(getTestName(false) + "_after.java");
return;
}
}
//noinspection ConstantConditions
fail("Lookup element with text '" + lookupText + "' not found:\n" + StringUtil.join(myFixture.getLookupElementStrings(), "\n"));
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class TemplatesCompletionTest method doAutoPopupTest.
private void doAutoPopupTest(@NotNull String textToType, @Nullable Class<? extends PostfixTemplate> expectedClass) {
configureByFile();
type(textToType);
LookupImpl lookup = getLookup();
if (expectedClass != null) {
assertNotNull(lookup);
LookupElement item = lookup.getCurrentItem();
assertNotNull(item);
assertInstanceOf(item, PostfixTemplateLookupElement.class);
assertInstanceOf(((PostfixTemplateLookupElement) item).getPostfixTemplate(), expectedClass);
} else {
assertNull(lookup);
}
}
use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.
the class TemplatesCompletionTest method testRestartCompletionForExactMatch.
public void testRestartCompletionForExactMatch() {
configureByFile();
type("not");
LookupElement currentItem = getLookup().getCurrentItem();
assertNotNull(currentItem);
assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
assertEquals(".not", currentItem.getLookupString());
type("null");
currentItem = getLookup().getCurrentItem();
assertNotNull(currentItem);
assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
assertEquals(".notnull", currentItem.getLookupString());
}
Aggregations