diff --git a/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java b/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java index ea278516ee4b..097b68769057 100644 --- a/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java +++ b/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java @@ -7,6 +7,8 @@ import com.intellij.openapi.editor.impl.EditorComponentImpl; import com.intellij.openapi.ui.TypingTarget; import com.intellij.openapi.ui.playback.PlaybackContext; import com.intellij.openapi.ui.playback.commands.AbstractCommand; +import com.sampullara.cli.Args; +import com.sampullara.cli.Argument; import org.jetbrains.annotations.NotNull; import org.jetbrains.concurrency.AsyncPromise; import org.jetbrains.concurrency.Promise; @@ -15,9 +17,15 @@ import java.util.concurrent.TimeUnit; import static com.intellij.openapi.ui.playback.commands.AlphaNumericTypeCommand.findTarget; +/** + * Command replace text from startPosition (0 by default) to endPosition (end of document by default) by newText ("" by default) + * Syntax: %replaceText [-startPosition start] [-endPosition end] [-newText text] + * Example: %replaceText -startPosition 0 -endPosition 10 -text "/" - replace text from 0 to 10 position by "/" + * Example: %replaceText -newText "newText" - replace all text in document by "newText" + * Example: %replaceText -startPosition 0 -endPosition 50 - replace text form 0 to 50 position by "" + */ public class ReplaceTextCommand extends AbstractCommand { public static final String PREFIX = CMD_PREFIX + "replaceText"; - public static final char NEWLINE = '\u32E1'; public ReplaceTextCommand(@NotNull String command, int line) { super(command, line); @@ -26,8 +34,8 @@ public class ReplaceTextCommand extends AbstractCommand { @Override protected @NotNull Promise _execute(@NotNull PlaybackContext context) { final AsyncPromise result = new AsyncPromise<>(); - - String text = getText().substring(PREFIX.length() + 1).replace(NEWLINE, '\n'); + Options options = new Options(); + Args.parse(options, extractCommandArgument(PREFIX).split(" ")); ApplicationManager.getApplication().executeOnPooledThread(() -> { try { @@ -41,11 +49,13 @@ public class ReplaceTextCommand extends AbstractCommand { TypingTarget target = findTarget(context); if (target instanceof EditorComponentImpl) { DocumentEx document = ((EditorComponentImpl)target).getEditor().getDocument(); - //noinspection TestOnlyProblems + int startPosition = options.startPosition == null ? 0 : options.startPosition; + int endPosition = options.endPosition == null ? document.getTextLength() : options.endPosition; WriteCommandAction.runWriteCommandAction(context.getProject(), - () -> document.replaceText(text, document.getModificationStamp() + 1)); + () -> document.replaceString(startPosition, endPosition, options.newText)); result.setResult(null); - } else { + } + else { result.setError("Cannot replace text on non-Editor component"); } }); @@ -53,4 +63,15 @@ public class ReplaceTextCommand extends AbstractCommand { return result; } + + public static class Options { + @Argument + public Integer startPosition; + + @Argument + public Integer endPosition; + + @Argument + public String newText = ""; + } }