mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-15 02:59:33 +07:00
[live-templates] Action to edit template settings (IDEA-201181)
GitOrigin-RevId: 0711d85c39e94ee2855f9991bf30eb8098307ca6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
546a704590
commit
e05e91581d
@@ -18,9 +18,10 @@ import com.intellij.codeInsight.template.macro.CompleteMacro
|
||||
import com.intellij.codeInsight.template.macro.ConcatMacro
|
||||
import com.intellij.codeInsight.template.macro.FilePathMacroBase
|
||||
import com.intellij.codeInsight.template.macro.SplitWordsMacro
|
||||
import com.intellij.ide.DataManager
|
||||
import com.intellij.internal.statistic.FUCollectorTestCase
|
||||
import com.intellij.lang.java.JavaLanguage
|
||||
import com.intellij.openapi.actionSystem.IdeActions
|
||||
import com.intellij.openapi.actionSystem.*
|
||||
import com.intellij.openapi.command.WriteCommandAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.DumbServiceImpl
|
||||
@@ -38,7 +39,6 @@ import org.jdom.Element
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
import static com.intellij.testFramework.EdtTestUtil.runInEdtAndWait
|
||||
|
||||
/**
|
||||
* @author spleaner
|
||||
*/
|
||||
@@ -1297,4 +1297,30 @@ class Foo {
|
||||
assert logEvent
|
||||
assert logEvent.event.id == "started"
|
||||
}
|
||||
|
||||
void "test additional actions"() {
|
||||
TemplateManager manager = TemplateManager.getInstance(getProject())
|
||||
TemplateImpl template = (TemplateImpl)manager.createTemplate('doubleparen', 'user', '(($END$$SELECTION$))')
|
||||
TemplateContextType contextType = contextType(JavaCodeContextType.class)
|
||||
template.getTemplateContext().setEnabled(contextType, true)
|
||||
CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable())
|
||||
myFixture.configureByText("a.java", "class X {{<selection>hello</selection>}}")
|
||||
def group = SurroundWithTemplateHandler.createActionGroup(editor, myFixture.file, new HashSet<Character>())
|
||||
def action = group.find { it.templateText == "doubleparen" }
|
||||
assert action instanceof ActionGroup
|
||||
final Presentation presentation = new Presentation()
|
||||
final DataContext context = DataManager.getInstance().getDataContext()
|
||||
final AnActionEvent event = new AnActionEvent(null, context, "", presentation, ActionManager.getInstance(), 0);
|
||||
assert !presentation.performGroup
|
||||
assert !presentation.popupGroup
|
||||
action.update(event)
|
||||
assert presentation.performGroup
|
||||
assert presentation.popupGroup
|
||||
def children = (action as ActionGroup).getChildren(event)
|
||||
assert children[0].templateText == "Edit live template settings"
|
||||
assert children[1].templateText == "Disable 'doubleparen' template"
|
||||
assert !template.isDeactivated()
|
||||
children[1].actionPerformed(event)
|
||||
assert template.isDeactivated()
|
||||
}
|
||||
}
|
||||
@@ -3,28 +3,34 @@ package com.intellij.codeInsight.template.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.template.TemplateManager;
|
||||
import com.intellij.icons.AllIcons;
|
||||
import com.intellij.openapi.actionSystem.ActionUpdateThread;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DefaultActionGroup;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.options.ShowSettingsUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.NlsActions.ActionText;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class InvokeTemplateAction extends AnAction {
|
||||
public class InvokeTemplateAction extends DefaultActionGroup {
|
||||
private final TemplateImpl myTemplate;
|
||||
private final Editor myEditor;
|
||||
private final Project myProject;
|
||||
@@ -43,13 +49,26 @@ public class InvokeTemplateAction extends AnAction {
|
||||
Set<Character> usedMnemonicsSet,
|
||||
@Nullable Runnable afterInvocationCallback) {
|
||||
super(extractMnemonic(template.getKey(), usedMnemonicsSet) +
|
||||
(StringUtil.isEmptyOrSpaces(template.getDescription()) ? "" : ". " + template.getDescription()));
|
||||
(StringUtil.isEmptyOrSpaces(template.getDescription()) ? "" : ". " + template.getDescription()),
|
||||
List.of(new EditTemplateSettingsAction(project, template),
|
||||
new DisableTemplateSettingsAction(template)));
|
||||
myTemplate = template;
|
||||
myProject = project;
|
||||
myEditor = editor;
|
||||
myCallback = afterInvocationCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(@NotNull AnActionEvent e) {
|
||||
e.getPresentation().setPopupGroup(true);
|
||||
e.getPresentation().setPerformGroup(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ActionUpdateThread getActionUpdateThread() {
|
||||
return ActionUpdateThread.EDT;
|
||||
}
|
||||
|
||||
public static @ActionText String extractMnemonic(@ActionText String caption, Set<? super Character> usedMnemonics) {
|
||||
if (StringUtil.isEmpty(caption)) return "";
|
||||
|
||||
@@ -111,4 +130,38 @@ public class InvokeTemplateAction extends AnAction {
|
||||
myCallback.run();
|
||||
}
|
||||
}
|
||||
|
||||
private static class EditTemplateSettingsAction extends AnAction {
|
||||
private final Project myProject;
|
||||
private final TemplateImpl myTemplate;
|
||||
|
||||
private EditTemplateSettingsAction(Project project, TemplateImpl template) {
|
||||
//noinspection DialogTitleCapitalization
|
||||
super(CodeInsightBundle.message("action.text.edit.live.template.settings"), null, PlatformIcons.EDIT);
|
||||
myProject = project;
|
||||
myTemplate = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
final LiveTemplatesConfigurable configurable = new LiveTemplatesConfigurable();
|
||||
ShowSettingsUtil.getInstance().editConfigurable(myProject, configurable, () -> configurable.getTemplateListPanel().editTemplate(
|
||||
myTemplate));
|
||||
}
|
||||
}
|
||||
|
||||
private static class DisableTemplateSettingsAction extends AnAction {
|
||||
private final TemplateImpl myTemplate;
|
||||
|
||||
private DisableTemplateSettingsAction(TemplateImpl template) {
|
||||
//noinspection DialogTitleCapitalization
|
||||
super(CodeInsightBundle.message("action.text.disable.live.template", template.getKey()), null, AllIcons.Actions.Cancel);
|
||||
myTemplate = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
myTemplate.setDeactivated(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user