remove duplication between Surround With and Surround With Live Template actions, cleanup

GitOrigin-RevId: 29cf62d01969baa8607139472f8b7459039c19ed
This commit is contained in:
peter
2019-05-13 20:08:19 +03:00
committed by intellij-monorepo-bot
parent 50accd1a16
commit 9fe7553e17
2 changed files with 26 additions and 45 deletions
@@ -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<Surrounder, PsiElement[]> surrounders) {
final List<AnAction> applicable = new ArrayList<>();
boolean hasEnabledSurrounders = false;
List<AnAction> applicable = new ArrayList<>();
Set<Character> 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<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
if (!templates.isEmpty() || !customTemplates.isEmpty()) {
List<AnAction> 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 {
@@ -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<AnAction> 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<AnAction> createActionGroup(@NotNull Editor editor, @NotNull PsiFile file, @NotNull Set<Character> usedMnemonicsSet) {
List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
List<TemplateImpl> 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<Character> usedMnemonicsSet = new HashSet<>();
DefaultActionGroup group = new DefaultActionGroup();
List<AnAction> 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) {