diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AsListToListPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AsListToListPostfixTemplate.java index 5dc8fad6245f..c75b160971be 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AsListToListPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/AsListToListPostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,21 +9,19 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class AsListToListPostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class AsListToListPostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public AsListToListPostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("Arrays.asList(exp)", - "asList", + super("Arrays.asList", + "toList", "java.util.Arrays.asList($EXPR$)$END$", - "Arrays.asList($EXPR$)", + "Arrays.asList(exp)", Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateArrayReferenceExpressionCondition()), - LanguageLevel.JDK_1_3, false, new String[]{".toList"}, provider); + LanguageLevel.JDK_1_3, false, provider); } - @Override +@Override public boolean isBuiltin() { return true; } - - } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaEditableTaggedPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaEditableTaggedPostfixTemplate.java deleted file mode 100644 index d0caac931fcc..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaEditableTaggedPostfixTemplate.java +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.codeInsight.template.postfix.templates; - -import com.intellij.codeInsight.completion.CompletionType; -import com.intellij.codeInsight.completion.JavaContributorCollectors; -import com.intellij.codeInsight.lookup.LookupElementPresentation; -import com.intellij.codeInsight.template.impl.TemplateImpl; -import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; -import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; -import com.intellij.java.JavaBundle; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.util.TextRange; -import com.intellij.pom.java.LanguageLevel; -import com.intellij.psi.PsiElement; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.*; - -import static com.intellij.codeInsight.completion.JavaContributorCollectors.POSTFIX_TAG; - -@SuppressWarnings("PostfixTemplateDescriptionNotFound") -public class JavaEditableTaggedPostfixTemplate extends JavaEditablePostfixTemplate implements CustomizableLookupElementTemplate { - - @SuppressWarnings("RegExpUnexpectedAnchor") private static final String EXPR_$ = "$EXPR$"; - - @Nullable - private String myText; - - @NotNull - private final String myExample; - - public @NotNull String @NotNull [] getTags() { - return myTags; - } - - private final @NotNull String @NotNull [] myTags; - - public JavaEditableTaggedPostfixTemplate(@NotNull String templateId, - @NotNull String templateName, - @NotNull String templateText, - @NotNull String example, - @NotNull Set expressionConditions, - @NotNull LanguageLevel minimumLanguageLevel, - boolean useTopmostExpression, - @NotNull String @NotNull [] tags, - @NotNull PostfixTemplateProvider provider) { - this(templateId, templateName, createTemplate(templateText), example, expressionConditions, minimumLanguageLevel, useTopmostExpression, - tags, provider); - } - - public JavaEditableTaggedPostfixTemplate(@NotNull String templateId, - @NotNull String templateName, - @NotNull TemplateImpl liveTemplate, - @NotNull String example, - @NotNull Set expressionConditions, - @NotNull LanguageLevel minimumLanguageLevel, - boolean useTopmostExpression, - @NotNull String @NotNull [] tags, - @NotNull PostfixTemplateProvider provider) { - super(templateId, templateName, liveTemplate, example.replace(EXPR_$, "expr"), expressionConditions, minimumLanguageLevel, - useTopmostExpression, provider); - myTags = tags; - myExample = example; - } - - - @Override - public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - List expressions = getExpressions(context, copyDocument, newOffset); - if (expressions.size() != 1) return false; - PsiElement element = expressions.get(0); - myText = element.getText(); //not ideal, because the state is changed, but it needs for nice rendering - return true; - } - - @Override - public void renderElement(@NotNull LookupElementPresentation presentation) { - String exp = myText; - String templateText = myExample; - if (exp == null || templateText.length() + exp.length() >= 100 || !templateText.contains(EXPR_$)) { - return; - } - String withExp = templateText.replace(EXPR_$, exp); - if (withExp.contains("$")) { - return; - } - ArrayList allTags = new ArrayList<>(Arrays.asList(myTags)); - allTags.add(getKey()); - String message = JavaBundle.message("java.completion.tag", allTags.size()); - String withTags = withExp + " " + message + String.join(", ", allTags); - presentation.setItemText(withTags); - int startOffset = templateText.indexOf(EXPR_$); - presentation.decorateItemTextRange(new TextRange(startOffset, startOffset + exp.length()), - LookupElementPresentation.LookupItemDecoration.GRAY); - startOffset = withExp.length(); - presentation.decorateItemTextRange(new TextRange(startOffset, startOffset + 1 + message.length()), - LookupElementPresentation.LookupItemDecoration.GRAY); - - presentation.setTypeText(""); - } - - @Override - public Collection getAllLookupStrings() { - return Arrays.asList(myTags); - } - - @Override - protected @NotNull TextRange getRangeToRemove(@NotNull PsiElement element) { - JavaContributorCollectors.logInsertHandle(element.getProject(), POSTFIX_TAG, CompletionType.BASIC); - return super.getRangeToRemove(element); - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaUtilDateToLocalDatePostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaUtilDateToLocalDatePostfixTemplate.java index 48a07f5f5eaa..1ee3d42fa5bf 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaUtilDateToLocalDatePostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/JavaUtilDateToLocalDatePostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,15 +9,14 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class JavaUtilDateToLocalDatePostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class JavaUtilDateToLocalDatePostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public JavaUtilDateToLocalDatePostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("LocalDate.ofInstant(exp.toInstant(), zoneId) java.sql.Date.valueOf(exp)", - "toLocalDate", + super("toLocalDate", "java.time.LocalDate.ofInstant($EXPR$.toInstant(), $END$)", - "LocalDate.ofInstant($EXPR$.toInstant(), zoneId)", + "LocalDate.ofInstant(exp.toInstant(), zoneId)", Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateExpressionFqnCondition("java.util.Date")), - LanguageLevel.JDK_1_8, false, new String[]{".asLocalDate"}, provider); + LanguageLevel.JDK_1_8, false, provider); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ListOfToListPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ListOfToListPostfixTemplate.java index 994bebfff32c..f67dc1e62235 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ListOfToListPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ListOfToListPostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,17 +9,27 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class ListOfToListPostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class ListOfToListPostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public ListOfToListPostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("List.of(exp)", - "listOf", + super("List.of", + "toList", "java.util.List.of($EXPR$)$END$", - "List.of($EXPR$)", + "List.of(exp)", Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateArrayReferenceExpressionCondition()), - LanguageLevel.JDK_1_9, false, new String[]{".asList", ".toList"}, provider); + LanguageLevel.JDK_1_9, false, provider); } + + // + // super("format", + // "String.format($EXPR$, $END$)", + // "String.format(expr)", + // Collections.singleton( + // new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateExpressionFqnCondition(CommonClassNames.JAVA_LANG_STRING)), + // LanguageLevel.JDK_1_3, false, provider); + //} + @Override public boolean isBuiltin() { return true; diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/LocalDateToJavaSqlDatePostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/LocalDateToJavaSqlDatePostfixTemplate.java index 6e823e7c63c3..4ef20518b881 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/LocalDateToJavaSqlDatePostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/LocalDateToJavaSqlDatePostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,15 +9,14 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class LocalDateToJavaSqlDatePostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class LocalDateToJavaSqlDatePostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public LocalDateToJavaSqlDatePostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("java.sql.Date.valueOf(exp)", - "sqlValueOf", + super("toSqlDate", "java.sql.Date.valueOf($EXPR$)$END$", - "java.sql.Date.valueOf($EXPR$)", + "java.sql.Date.valueOf(exp)", Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateExpressionFqnCondition("java.time.LocalDate")), - LanguageLevel.JDK_1_8, false, new String[]{".toSqlDate"}, provider); + LanguageLevel.JDK_1_8, false, provider); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplate.java index 2aba6ce1aea2..a018e8f461b5 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,21 +9,19 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class NewArrayListToListPostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class NewArrayListToListPostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public NewArrayListToListPostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("new ArrayList<>(exp)", + super("new ArrayList", "toList", "new java.util.ArrayList<>($EXPR$)$END$", - "new ArrayList<>($EXPR$)", + "new ArrayList<>(exp)" , Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateExpressionFqnCondition("java.util.Collection")), - LanguageLevel.JDK_1_7, false, new String[]{".asList"}, provider); + LanguageLevel.JDK_1_7, false, provider); } @Override public boolean isBuiltin() { return true; } - - } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplate.java index 8a5e620e3744..7e55fe41610f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplate.java @@ -1,6 +1,7 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; import com.intellij.openapi.project.DumbAware; import com.intellij.pom.java.LanguageLevel; @@ -8,21 +9,18 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; -public class NewHashSetToSetPostfixTemplate extends JavaEditableTaggedPostfixTemplate implements DumbAware { +public class NewHashSetToSetPostfixTemplate extends JavaEditablePostfixTemplate implements DumbAware { public NewHashSetToSetPostfixTemplate(@NotNull JavaPostfixTemplateProvider provider) { - super("new HashSet<>(exp)", - "toSet", + super("toSet", "new java.util.HashSet<>($EXPR$)$END$", - "new HashSet<>($EXPR$)", + "new HashSet<>(exp)", Collections.singleton( new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateExpressionFqnCondition("java.util.Collection")), - LanguageLevel.JDK_1_7, false, new String[]{".asSet"}, provider); + LanguageLevel.JDK_1_7, false, provider); } @Override public boolean isBuiltin() { return true; } - - } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaEditablePostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaEditablePostfixTemplate.java index 76cee6d0cc9a..5affa7cf1375 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaEditablePostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaEditablePostfixTemplate.java @@ -1,6 +1,8 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.template.postfix.templates.editable; +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.codeInsight.completion.JavaContributorCollectors; import com.intellij.codeInsight.template.impl.TemplateImpl; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateProvider; import com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils; @@ -20,6 +22,8 @@ import org.jetbrains.annotations.NotNull; import java.util.*; +import static com.intellij.codeInsight.completion.JavaContributorCollectors.POSTFIX_TAG; + @SuppressWarnings("PostfixTemplateDescriptionNotFound") public class JavaEditablePostfixTemplate extends EditablePostfixTemplateWithMultipleExpressions { @@ -101,6 +105,7 @@ public class JavaEditablePostfixTemplate @Override protected @NotNull TextRange getRangeToRemove(@NotNull PsiElement element) { + JavaContributorCollectors.logInsertHandle(element.getProject(), POSTFIX_TAG, CompletionType.BASIC); PsiElement toRemove = getElementToRemove(element); if (toRemove instanceof PsiExpressionStatement) { PsiElement lastChild = toRemove.getLastChild(); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaPostfixTemplateEditor.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaPostfixTemplateEditor.java index 007451a9cc00..326d8d993140 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaPostfixTemplateEditor.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/editable/JavaPostfixTemplateEditor.java @@ -4,7 +4,6 @@ package com.intellij.codeInsight.template.postfix.templates.editable; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; import com.intellij.codeInsight.template.Template; import com.intellij.codeInsight.template.postfix.settings.PostfixTemplateEditorBase; -import com.intellij.codeInsight.template.postfix.templates.JavaEditableTaggedPostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateProvider; import com.intellij.ide.util.TreeClassChooser; @@ -20,7 +19,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.text.Strings; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.JavaCodeFragment; import com.intellij.psi.JavaCodeFragmentFactory; @@ -28,7 +26,6 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiDocumentManager; import com.intellij.ui.SimpleListCellRenderer; import com.intellij.ui.components.JBCheckBox; -import com.intellij.ui.components.JBTextField; import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.FormBuilder; @@ -37,7 +34,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; @@ -46,19 +42,17 @@ public class JavaPostfixTemplateEditor extends PostfixTemplateEditorBase myLanguageLevelCombo; private final @NotNull JBCheckBox myStaticImportCheckBox; - private final @NotNull JBTextField myTagsField; public JavaPostfixTemplateEditor(@NotNull PostfixTemplateProvider provider) { super(provider, createEditor(), true); myStaticImportCheckBox = new JBCheckBox(JavaBundle.message("dialog.edit.template.checkbox.use.static.import")); myLanguageLevelCombo = new ComboBox<>(LanguageLevel.values()); myLanguageLevelCombo.setRenderer(SimpleListCellRenderer.create("", LanguageLevel::getPresentableText)); - myTagsField = new JBTextField(); + myPanel = FormBuilder.createFormBuilder() .addLabeledComponent(JavaBundle.message("postfix.template.language.level.title"), myLanguageLevelCombo) .addComponentFillVertically(myEditTemplateAndConditionsPanel, UIUtil.DEFAULT_VGAP) .addComponent(myStaticImportCheckBox) - .addLabeledComponent("Tags: ", myTagsField) .getPanel(); } @@ -75,17 +69,9 @@ public class JavaPostfixTemplateEditor extends PostfixTemplateEditorBase - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/asList/simpleAsList_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/asList/simpleAsList_after.java deleted file mode 100644 index 166d43a2f3c1..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/asList/simpleAsList_after.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.util.Arrays; - -public class Foo { - void m(Integer[] o) { - Arrays.asList(o) - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList.java b/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList.java deleted file mode 100644 index 462333a8a55c..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Foo { - void m(Integer[] o) { - o.asList - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList_after.java deleted file mode 100644 index 9765e651cd9f..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/listOfToList/simpleAsList_after.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.util.List; - -public class Foo { - void m(Integer[] o) { - List.of(o) - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList.java b/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList.java deleted file mode 100644 index bba36f25ef88..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.util.Set; - -public class Foo { - void m(Set o) { - o.asList - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList_after.java deleted file mode 100644 index 4b91a9763b22..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/toListNewArrayList/simpleAsList_after.java +++ /dev/null @@ -1,8 +0,0 @@ -import java.util.ArrayList; -import java.util.Set; - -public class Foo { - void m(Set o) { - new ArrayList<>(o) - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet.java b/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet.java deleted file mode 100644 index 8b80bf41c8c0..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.util.Set; - -public class Foo { - void m(Set o) { - o.asSet - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet_after.java deleted file mode 100644 index f8d398291c83..000000000000 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/toSetNewHashSet/simpleAsSet_after.java +++ /dev/null @@ -1,8 +0,0 @@ -import java.util.HashSet; -import java.util.Set; - -public class Foo { - void m(Set o) { - new HashSet<>(o) - } -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/AsListToListPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/AsListToListPostfixTemplateTest.java index 60cb60e3ddf7..99535e29cd6c 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/AsListToListPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/AsListToListPostfixTemplateTest.java @@ -21,8 +21,5 @@ public class AsListToListPostfixTemplateTest extends PostfixTemplateTestCase { doTestCompletion("asList"); } - public void testSimpleAsList() { - doTestCompletion("asList"); - } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/ListOfToListPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/ListOfToListPostfixTemplateTest.java index 57799c9977fa..03ac66b5d65e 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/ListOfToListPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/ListOfToListPostfixTemplateTest.java @@ -18,11 +18,7 @@ public class ListOfToListPostfixTemplateTest extends PostfixTemplateTestCase { } public void testSimple() { - doTestCompletion("listOf"); - } - - public void testSimpleAsList() { - doTestCompletion("listOf"); + doTestCompletion("List.of"); } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplateTest.java index 0899ad5adcda..dc01246b8c34 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewArrayListToListPostfixTemplateTest.java @@ -20,9 +20,5 @@ public class NewArrayListToListPostfixTemplateTest extends PostfixTemplateTestCa public void testSimple() { doTestCompletion(null); } - - public void testSimpleAsList() { - doTestCompletion(null); - } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplateTest.java index 16c124343fa9..0b50896af3dc 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewHashSetToSetPostfixTemplateTest.java @@ -20,9 +20,5 @@ public class NewHashSetToSetPostfixTemplateTest extends PostfixTemplateTestCase public void testSimple() { doTestCompletion(null); } - - public void testSimpleAsSet() { - doTestCompletion(null); - } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/PostfixTemplateTestCase.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/PostfixTemplateTestCase.java index b1e219c28f23..49fb40b004c8 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/PostfixTemplateTestCase.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/PostfixTemplateTestCase.java @@ -3,6 +3,7 @@ package com.intellij.java.codeInsight.template.postfix.templates; import com.intellij.JavaTestUtil; import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.LookupElementPresentation; import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; @@ -34,7 +35,10 @@ abstract public class PostfixTemplateTestCase extends LightJavaCodeInsightFixtur LookupElement[] elements = myFixture.completeBasic(); if (text != null) { for (LookupElement element : elements) { - if(element.getLookupString().toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT))) { + LookupElementPresentation presentation = new LookupElementPresentation(); + element.renderElement(presentation); + if (presentation.getItemText().toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT)) || + presentation.getTypeText().toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT))) { myFixture.getLookup().setCurrentItem(element); break; } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/RenderHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/RenderHighlightingTest.java new file mode 100644 index 000000000000..8147d1f5e753 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/RenderHighlightingTest.java @@ -0,0 +1,42 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.impl.LookupCellRenderer; +import com.intellij.codeInsight.lookup.impl.LookupImpl; +import com.intellij.openapi.application.ReadAction; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; + +import javax.swing.*; +import java.awt.*; + +import static com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor.setShowTemplatesInTests; + +public class RenderHighlightingTest extends LightJavaCodeInsightFixtureTestCase { + public void testSimple() { + setShowTemplatesInTests(true, getTestRootDisposable()); + myFixture.configureByText("a.java", """ + import java.util.List; + class A { + void m(List list) { + list.toS + } + }"""); + LookupElement[] elements = myFixture.completeBasic(); + ReadAction.run(() -> { + JList list = getLookup().getList(); + for (LookupElement item : getLookup().getItems()) { + if (!item.getLookupString().equals(".toSet")) continue; + if (list.getCellRenderer() instanceof LookupCellRenderer lookupCellRenderer) { + Component component = list.getCellRenderer().getListCellRendererComponent(list, item, 0, false, false); + System.out.println(component); + } + } + }); + } + + + private LookupImpl getLookup() { + return (LookupImpl)myFixture.getLookup(); + } +} diff --git a/platform/lang-impl/api-dump-unreviewed.txt b/platform/lang-impl/api-dump-unreviewed.txt index 6874ff2c73d1..53e336c61def 100644 --- a/platform/lang-impl/api-dump-unreviewed.txt +++ b/platform/lang-impl/api-dump-unreviewed.txt @@ -5624,7 +5624,6 @@ c:com.intellij.codeInsight.template.postfix.completion.PostfixTemplateLookupElem - com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement - (com.intellij.codeInsight.template.postfix.templates.PostfixLiveTemplate,com.intellij.codeInsight.template.postfix.templates.PostfixTemplate,java.lang.String,com.intellij.codeInsight.template.postfix.templates.PostfixTemplateProvider,Z):V - expandTemplate(com.intellij.openapi.editor.Editor,com.intellij.psi.PsiFile):V -- getAllLookupStrings():java.util.Set - getPostfixTemplate():com.intellij.codeInsight.template.postfix.templates.PostfixTemplate - getProvider():com.intellij.codeInsight.template.postfix.templates.PostfixTemplateProvider - renderElement(com.intellij.codeInsight.lookup.LookupElementPresentation):V diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.kt b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.kt index 78b3d4c0b96a..fa9ac9ba7549 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.kt @@ -425,8 +425,16 @@ class LookupCellRenderer(lookup: LookupImpl, editorComponent: JComponent) : List val prefix = if (item is EmptyLookupItem) "" else lookup.itemPattern(item) if (prefix.isNotEmpty()) { - val ranges = getMatchingFragments(prefix, name) - if (ranges != null) { + var ranges: List? = getMatchingFragments(prefix, name) + if (ranges == null) { + val startIndex = item.lookupString.indexOf(name) + if (startIndex != -1) { + ranges = getMatchingFragments(prefix, item.lookupString) + ?.map { TextRange((it.startOffset - startIndex).coerceIn(0, name.length), (it.endOffset - startIndex).coerceIn(0, name.length)) } + ?.filter { it.length != 0 } + } + } + if (ranges != null && ranges.isNotEmpty()) { val highlighted = SimpleTextAttributes(style, MATCHED_FOREGROUND_COLOR) SpeedSearchUtil.appendColoredFragments(nameComponent, name, ranges, base, highlighted) renderItemNameDecoration(nameComponent, itemNameDecorations) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/completion/PostfixTemplateLookupElement.java b/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/completion/PostfixTemplateLookupElement.java index 7b96d4f31cb6..8fd4d37e33ee 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/completion/PostfixTemplateLookupElement.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/completion/PostfixTemplateLookupElement.java @@ -4,7 +4,6 @@ package com.intellij.codeInsight.template.postfix.completion; import com.intellij.codeInsight.lookup.LookupElementPresentation; import com.intellij.codeInsight.template.CustomTemplateCallback; import com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement; -import com.intellij.codeInsight.template.postfix.templates.CustomizableLookupElementTemplate; import com.intellij.codeInsight.template.postfix.templates.PostfixLiveTemplate; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateProvider; @@ -13,9 +12,6 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; -import java.util.HashSet; -import java.util.Set; - public class PostfixTemplateLookupElement extends CustomLiveTemplateLookupElement { private final @NotNull PostfixTemplate myTemplate; private final @NotNull String myTemplateKey; @@ -41,24 +37,11 @@ public class PostfixTemplateLookupElement extends CustomLiveTemplateLookupElemen return myProvider; } - @Override - public Set getAllLookupStrings() { - Set allLookupStrings = super.getAllLookupStrings(); - if (myTemplate instanceof CustomizableLookupElementTemplate customizableRenderPresentation) { - allLookupStrings = new HashSet<>(allLookupStrings); - allLookupStrings.addAll(customizableRenderPresentation.getAllLookupStrings()); - } - return allLookupStrings; - } - @Override public void renderElement(@NotNull LookupElementPresentation presentation) { super.renderElement(presentation); presentation.setTypeText(myTemplate.getExample()); presentation.setTypeGrayed(true); - if (myTemplate instanceof CustomizableLookupElementTemplate customizableRenderPresentation) { - customizableRenderPresentation.renderElement(presentation); - } } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/CustomizableLookupElementTemplate.java b/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/CustomizableLookupElementTemplate.java deleted file mode 100644 index eb07bc659258..000000000000 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/CustomizableLookupElementTemplate.java +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.codeInsight.template.postfix.templates; - -import com.intellij.codeInsight.lookup.LookupElementPresentation; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; - -import java.util.Collection; - -@ApiStatus.Internal -@ApiStatus.Experimental -public interface CustomizableLookupElementTemplate { - void renderElement(@NotNull LookupElementPresentation presentation); - - Collection getAllLookupStrings(); -}