mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Merge branch 'AT-457.add_typing_check_nb'
GitOrigin-RevId: 8fdc8de5e7147721c3e4fa163beb8299803d849e
This commit is contained in:
+27
-6
@@ -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<Object> _execute(@NotNull PlaybackContext context) {
|
||||
final AsyncPromise<Object> 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 = "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user