1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-08-08 18:40:37 +02:00

Merge branch 'action-command'

This commit is contained in:
Andrey Vlasovskikh 2014-10-24 16:02:21 +04:00
commit f95f5e8901
3 changed files with 152 additions and 0 deletions

View File

@ -70,6 +70,7 @@ public class CommandParser {
public void registerHandlers() {
if (registered) return;
new ActionListHandler();
new AsciiHandler();
new CmdFilterHandler();
new CopyTextHandler();
@ -77,6 +78,7 @@ public class CommandParser {
new DigraphHandler();
new DumpLineHandler();
new EditFileHandler();
new ActionHandler();
new ExitHandler();
new FindClassHandler();
new FindFileHandler();

View File

@ -0,0 +1,65 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2014 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.ex.handler;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.ex.CommandHandler;
import com.maddyhome.idea.vim.ex.ExCommand;
import com.maddyhome.idea.vim.ex.ExException;
import org.jetbrains.annotations.NotNull;
/**
* @author smartbomb
*/
public class ActionHandler extends CommandHandler {
public ActionHandler() {
super("action", "", RANGE_FORBIDDEN | DONT_REOPEN);
}
public boolean execute(@NotNull Editor editor, @NotNull final DataContext context,
@NotNull ExCommand cmd) throws ExException {
final String actionName = cmd.getArgument().trim();
final AnAction action = ActionManager.getInstance().getAction(actionName);
if (action == null) {
VimPlugin.showMessage("Action not found: " + actionName);
return false;
}
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
try {
KeyHandler.executeAction(action, context);
}
catch (RuntimeException e) {
// TODO: Find out if any runtime exceptions may happen here
assert false : "Error while executing :action " + actionName + " (" + action + "): " + e;
}
}
});
return true;
}
}

View File

@ -0,0 +1,85 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2014 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.ex.handler;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.text.StringUtil;
import com.maddyhome.idea.vim.ex.CommandHandler;
import com.maddyhome.idea.vim.ex.ExCommand;
import com.maddyhome.idea.vim.ex.ExException;
import com.maddyhome.idea.vim.ex.ExOutputModel;
import com.maddyhome.idea.vim.helper.StringHelper;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author smartbomb
*/
public class ActionListHandler extends CommandHandler {
public ActionListHandler() {
super("actionlist", "", RANGE_FORBIDDEN | DONT_REOPEN | ARGUMENT_OPTIONAL);
}
public boolean execute(@NotNull Editor editor, @NotNull final DataContext context,
@NotNull ExCommand cmd) throws ExException {
final String arg = cmd.getArgument().trim().toLowerCase();
final List<String> args = StringUtil.split(arg, "*");
final ActionManager actionManager = ActionManager.getInstance();
final List<String> actionNames = Arrays.asList(actionManager.getActionIds(""));
Collections.sort(actionNames, String.CASE_INSENSITIVE_ORDER);
final StringBuilder builder = new StringBuilder();
builder.append("--- Actions ---\n");
for (String actionName : actionNames) {
if (match(actionName, args)) {
builder.append(StringHelper.leftJustify(actionName, 50, ' '));
final AnAction action = actionManager.getAction(actionName);
final Shortcut[] shortcuts = action.getShortcutSet().getShortcuts();
for (Shortcut shortcut : shortcuts) {
builder.append(" ");
if (shortcut instanceof KeyboardShortcut) {
final KeyboardShortcut keyboardShortcut = (KeyboardShortcut)shortcut;
builder.append(StringHelper.toKeyNotation(keyboardShortcut.getFirstKeyStroke()));
}
else {
builder.append(shortcut.toString());
}
}
builder.append("\n");
}
}
ExOutputModel.getInstance(editor).output(builder.toString());
return true;
}
private boolean match(@NotNull String actionName, @NotNull List<String> args) {
for (String argChunk : args) {
if (!actionName.toLowerCase().contains(argChunk)) {
return false;
}
}
return true;
}
}