From 169609fef65945324c50accd9158612b618a5c62 Mon Sep 17 00:00:00 2001 From: Nikita Barkov Date: Sun, 30 Jul 2023 21:52:51 +0200 Subject: [PATCH 1/2] [perform_tests] AT-457. Added dsl for replace text command. Made this command more flexible, to replace part of the text GitOrigin-RevId: 70b8fb86125694f3b151ab2244e7f37ded7cbb8f --- .../commands/ReplaceTextCommand.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java b/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java index ea278516ee4b..3d2bcae17d2d 100644 --- a/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java +++ b/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java @@ -17,17 +17,25 @@ import static com.intellij.openapi.ui.playback.commands.AlphaNumericTypeCommand. public class ReplaceTextCommand extends AbstractCommand { public static final String PREFIX = CMD_PREFIX + "replaceText"; - public static final char NEWLINE = '\u32E1'; + private static final String DEFAULT_POSITION_SYMBOL = "/"; public ReplaceTextCommand(@NotNull String command, int line) { super(command, line); } + private static String getArgValue(String arg) { + String[] splitArg = arg.split("="); + return splitArg.length == 2 ? splitArg[1] : ""; + } + @Override protected @NotNull Promise _execute(@NotNull PlaybackContext context) { final AsyncPromise result = new AsyncPromise<>(); + String[] args = extractCommandArgument(PREFIX).split(" "); + String potentialStart = getArgValue(args[0]); + String potentialEnd = getArgValue(args[1]); + String text = getArgValue(args[2]); - String text = getText().substring(PREFIX.length() + 1).replace(NEWLINE, '\n'); 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 = potentialStart.equals(DEFAULT_POSITION_SYMBOL) ? 0 : Integer.parseInt(potentialStart); + int endPosition = potentialEnd.equals(DEFAULT_POSITION_SYMBOL) ? document.getTextLength() : Integer.parseInt(potentialEnd); WriteCommandAction.runWriteCommandAction(context.getProject(), - () -> document.replaceText(text, document.getModificationStamp() + 1)); + () -> document.replaceString(startPosition, endPosition, text)); result.setResult(null); - } else { + } + else { result.setError("Cannot replace text on non-Editor component"); } }); From 45518adc7177f75fbe48dcb886f4af39c37e966f Mon Sep 17 00:00:00 2001 From: Nikita Barkov Date: Mon, 31 Jul 2023 21:01:50 +0200 Subject: [PATCH 2/2] [perform_tests] AT-457. Changed the way to get arguments. Added javadoc GitOrigin-RevId: e7d16d1ff0cc0566d591522bf9aa4b9355d661e4 --- .../commands/ReplaceTextCommand.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java b/plugins/performanceTesting/src/com/jetbrains/performancePlugin/commands/ReplaceTextCommand.java index 3d2bcae17d2d..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,27 +17,25 @@ 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"; - private static final String DEFAULT_POSITION_SYMBOL = "/"; public ReplaceTextCommand(@NotNull String command, int line) { super(command, line); } - private static String getArgValue(String arg) { - String[] splitArg = arg.split("="); - return splitArg.length == 2 ? splitArg[1] : ""; - } - @Override protected @NotNull Promise _execute(@NotNull PlaybackContext context) { final AsyncPromise result = new AsyncPromise<>(); - String[] args = extractCommandArgument(PREFIX).split(" "); - String potentialStart = getArgValue(args[0]); - String potentialEnd = getArgValue(args[1]); - String text = getArgValue(args[2]); - + Options options = new Options(); + Args.parse(options, extractCommandArgument(PREFIX).split(" ")); ApplicationManager.getApplication().executeOnPooledThread(() -> { try { @@ -49,10 +49,10 @@ public class ReplaceTextCommand extends AbstractCommand { TypingTarget target = findTarget(context); if (target instanceof EditorComponentImpl) { DocumentEx document = ((EditorComponentImpl)target).getEditor().getDocument(); - int startPosition = potentialStart.equals(DEFAULT_POSITION_SYMBOL) ? 0 : Integer.parseInt(potentialStart); - int endPosition = potentialEnd.equals(DEFAULT_POSITION_SYMBOL) ? document.getTextLength() : Integer.parseInt(potentialEnd); + int startPosition = options.startPosition == null ? 0 : options.startPosition; + int endPosition = options.endPosition == null ? document.getTextLength() : options.endPosition; WriteCommandAction.runWriteCommandAction(context.getProject(), - () -> document.replaceString(startPosition, endPosition, text)); + () -> document.replaceString(startPosition, endPosition, options.newText)); result.setResult(null); } else { @@ -63,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 = ""; + } }