diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java index 90a539e97c2a..42a79640177f 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java @@ -5,12 +5,8 @@ package com.intellij.codeInsight.generation.surroundWith; import com.intellij.codeInsight.CodeInsightActionHandler; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.codeInsight.hint.HintManager; -import com.intellij.codeInsight.template.CustomLiveTemplate; import com.intellij.codeInsight.template.TemplateManager; -import com.intellij.codeInsight.template.impl.InvokeTemplateAction; -import com.intellij.codeInsight.template.impl.TemplateImpl; -import com.intellij.codeInsight.template.impl.TemplateManagerImpl; -import com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction; +import com.intellij.codeInsight.template.impl.SurroundWithTemplateHandler; import com.intellij.ide.DataManager; import com.intellij.lang.Language; import com.intellij.lang.LanguageSurrounders; @@ -200,8 +196,7 @@ public class SurroundWithHandler implements CodeInsightActionHandler { Editor editor, PsiFile file, Map surrounders) { - final List applicable = new ArrayList<>(); - boolean hasEnabledSurrounders = false; + List applicable = new ArrayList<>(); Set usedMnemonicsSet = new HashSet<>(); @@ -223,32 +218,17 @@ public class SurroundWithHandler implements CodeInsightActionHandler { index++; usedMnemonicsSet.add(Character.toUpperCase(mnemonic)); applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic)); - hasEnabledSurrounders = true; } } - List customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true); - List templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true); - - if (!templates.isEmpty() || !customTemplates.isEmpty()) { + List templateGroup = SurroundWithTemplateHandler.createActionGroup(editor, file, usedMnemonicsSet); + if (!templateGroup.isEmpty()) { applicable.add(new Separator("Live templates")); - } - - for (TemplateImpl template : templates) { - applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet)); - hasEnabledSurrounders = true; - } - - for (CustomLiveTemplate customTemplate : customTemplates) { - applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet)); - hasEnabledSurrounders = true; - } - - if (!templates.isEmpty() || !customTemplates.isEmpty()) { + applicable.addAll(templateGroup); applicable.add(Separator.getInstance()); applicable.add(new ConfigureTemplatesAction()); } - return hasEnabledSurrounders ? applicable : null; + return applicable.isEmpty() ? null : applicable; } private static class InvokeSurrounderAction extends AnAction { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java index ba3403cef15b..246069c47ef7 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java @@ -22,6 +22,7 @@ import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler; import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.template.CustomLiveTemplate; import com.intellij.ide.DataManager; +import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.DefaultActionGroup; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; @@ -30,11 +31,8 @@ import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.ListPopup; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** * @author mike @@ -43,35 +41,38 @@ public class SurroundWithTemplateHandler implements CodeInsightActionHandler { @Override public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) { if (!EditorModificationUtil.checkModificationAllowed(editor)) return; - DefaultActionGroup group = createActionGroup(project, editor, file); - if (group == null) return; + if (!editor.getSelectionModel().hasSelection()) { + SurroundWithHandler.selectLogicalLineContentsAtCaret(editor); + if (!editor.getSelectionModel().hasSelection()) return; + } - final ListPopup popup = JBPopupFactory.getInstance() - .createActionGroupPopup(CodeInsightBundle.message("templates.select.template.chooser.title"), group, + List group = createActionGroup(editor, file, new HashSet<>()); + if (group.isEmpty()) { + HintManager.getInstance().showErrorHint(editor, CodeInsightBundle.message("templates.surround.no.defined")); + return; + } + + + ListPopup popup = JBPopupFactory.getInstance() + .createActionGroupPopup(CodeInsightBundle.message("templates.select.template.chooser.title"), new DefaultActionGroup(group), DataManager.getInstance().getDataContext(editor.getContentComponent()), JBPopupFactory.ActionSelectionAid.MNEMONICS, false); popup.showInBestPositionFor(editor); } - @Nullable - public static DefaultActionGroup createActionGroup(Project project, Editor editor, PsiFile file) { - if (!editor.getSelectionModel().hasSelection()) { - SurroundWithHandler.selectLogicalLineContentsAtCaret(editor); - if (!editor.getSelectionModel().hasSelection()) return null; - } + @NotNull + public static List createActionGroup(@NotNull Editor editor, @NotNull PsiFile file, @NotNull Set usedMnemonicsSet) { List customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true); List templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true); if (templates.isEmpty() && customTemplates.isEmpty()) { - HintManager.getInstance().showErrorHint(editor, CodeInsightBundle.message("templates.surround.no.defined")); - return null; + return Collections.emptyList(); } - Set usedMnemonicsSet = new HashSet<>(); - DefaultActionGroup group = new DefaultActionGroup(); + List group = new ArrayList<>(); for (TemplateImpl template : templates) { - group.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet)); + group.add(new InvokeTemplateAction(template, editor, file.getProject(), usedMnemonicsSet)); } for (CustomLiveTemplate customTemplate : customTemplates) {