1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-07-30 09:59:08 +02:00

Clean up ex command processing

This commit is contained in:
Alex Plate 2019-07-31 16:47:19 +03:00
parent 90079c9dfe
commit 978d95b351
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
2 changed files with 14 additions and 29 deletions
src/com/maddyhome/idea/vim

View File

@ -41,10 +41,7 @@ import java.util.regex.Pattern;
*/
public class CommandParser {
private static final int MAX_RECURSION = 100;
public static final int RES_EMPTY = 1;
public static final int RES_ERROR = 1;
public static final int RES_READONLY = 1;
public static final Pattern TRIM_WHITESPACE = Pattern.compile("[ \\t]*(.*)[ \\t\\n\\r]+", Pattern.DOTALL);
private static final Pattern TRIM_WHITESPACE = Pattern.compile("[ \\t]*(.*)[ \\t\\n\\r]+", Pattern.DOTALL);
private final CommandHandler[] myHandlers = new CommandHandler[] {
new ActionListHandler(),
new AsciiHandler(),
@ -168,31 +165,20 @@ public class CommandParser {
* @param context The data context
* @param cmd The text entered by the user
* @param count The count entered before the colon
* @return A bitwise collection of flags, if any, from the result of running the command.
* @throws ExException if any part of the command is invalid or unknown
*/
public int processCommand(@NotNull Editor editor, @NotNull DataContext context, @NotNull String cmd,
public void processCommand(@NotNull Editor editor, @NotNull DataContext context, @NotNull String cmd,
int count) throws ExException {
return processCommand(editor, context, cmd, count, MAX_RECURSION);
processCommand(editor, context, cmd, count, MAX_RECURSION);
}
/**
* Parse and execute an Ex command entered by the user
*
* @param editor The editor to run the command in
* @param context The data context
* @param cmd The text entered by the user
* @param count The count entered before the colon
* @param aliasCountdown A countdown for the depth of alias recursion that is allowed
* @return A bitwise collection of flags, if any, from the result of running the command.
* @throws ExException if any part of the command is invalid or unknown
*/
private int processCommand(@NotNull Editor editor, @NotNull DataContext context, @NotNull String cmd,
private void processCommand(@NotNull Editor editor, @NotNull DataContext context, @NotNull String cmd,
int count, int aliasCountdown) throws ExException {
// Nothing entered
int result = 0;
if (cmd.length() == 0) {
return result | RES_EMPTY;
logger.warn("CMD is empty");
return;
}
// Only save the command to the history if it is at the top of the stack.
@ -209,14 +195,16 @@ public class CommandParser {
if (aliasCountdown > 0) {
String commandAlias = VimPlugin.getCommand().getAliasCommand(cmd, count);
if (commandAlias.isEmpty()) {
return result |= RES_ERROR;
logger.warn("Command alias is empty");
return;
}
return processCommand(editor, context, commandAlias, count, aliasCountdown - 1);
processCommand(editor, context, commandAlias, count, aliasCountdown - 1);
} else {
VimPlugin.showMessage("Recursion detected, maximum alias depth reached.");
VimPlugin.indicateError();
return result |= RES_ERROR;
logger.warn("Recursion detected, maximum alias depth reached. ");
}
return;
}
// Parse the command
@ -230,7 +218,8 @@ public class CommandParser {
if (handler.getArgFlags().getFlags().contains(CommandHandler.Flag.WRITABLE) && !editor.getDocument().isWritable()) {
VimPlugin.indicateError();
return result | RES_READONLY;
logger.info("Trying to modify readonly document");
return;
}
// Run the command
@ -239,8 +228,6 @@ public class CommandParser {
VimPlugin.getRegister().storeTextInternal(editor, new TextRange(-1, -1), cmd,
SelectionType.CHARACTER_WISE, ':', false);
}
return result;
}
@Nullable

View File

@ -104,7 +104,6 @@ public class ProcessGroup {
ExEntryPanel panel = ExEntryPanel.getInstance();
panel.deactivate(true);
boolean res = true;
int flags;
try {
CommandState.getInstance(editor).popState();
logger.debug("processing command");
@ -112,8 +111,7 @@ public class ProcessGroup {
record(editor, text);
if (logger.isDebugEnabled()) logger.debug("swing=" + SwingUtilities.isEventDispatchThread());
if (panel.getLabel().equals(":")) {
flags = CommandParser.getInstance().processCommand(editor, context, text, 1);
if (logger.isDebugEnabled()) logger.debug("flags=" + flags);
CommandParser.getInstance().processCommand(editor, context, text, 1);
}
else {
int pos = VimPlugin.getSearch().search(editor, text, panel.getCount(),