[mod-command] ModCommandExecutor: execute for file copy

GitOrigin-RevId: 99fa55ec8cbe0af1859f1c1f350c4b1ab103ef4d
This commit is contained in:
Tagir Valeev
2023-11-02 20:33:49 +00:00
committed by intellij-monorepo-bot
parent 6a62e6820c
commit be39e6323a
3 changed files with 24 additions and 23 deletions
@@ -5,7 +5,6 @@ import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.lang.jvm.JvmModifiersOwner
import com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModCommandExecutor
import com.intellij.modcommand.ModUpdateFileText
import com.intellij.modcommand.PsiUpdateModCommandQuickFix
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
@@ -39,17 +38,8 @@ abstract class CompositeModCommandQuickFix : PsiUpdateModCommandQuickFix() {
val manager = PsiDocumentManager.getInstance(project)
manager.doPostponedOperationsAndUnblockDocument(document)
if (modCommandAction != null) {
for (command in modCommandAction.perform(ActionContext.from(null, containingFile)).unpack()) {
if (command is ModUpdateFileText) {
if (command.file != containingFile.originalFile.virtualFile) {
throw UnsupportedOperationException("The command ${action.familyName} updates non-current file")
}
ModCommandExecutor.getInstance().updateText(project, document, command)
}
else {
throw UnsupportedOperationException("Unexpected command performed by ${action.familyName}: " + command)
}
}
ModCommandExecutor.getInstance()
.executeForFileCopy(modCommandAction.perform(ActionContext.from(null, containingFile)), containingFile)
}
else {
action.invoke(project, null, containingFile)
@@ -7,6 +7,7 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.psi.PsiFile;
import com.intellij.util.concurrency.annotations.RequiresEdt;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nls;
@@ -40,17 +41,13 @@ public interface ModCommandExecutor {
@NotNull BatchExecutionResult executeInBatch(@NotNull ActionContext context, @NotNull ModCommand command);
/**
* Apply {@link ModUpdateFileText} command to specific document. The caller must ensure that the document is writable.
* This API is not intended to be used outside the platform and may change without notice.
*
* @param project current project
* @param document document to apply changes to
* @param updateTextCommand command to apply
* @throws IllegalStateException if the current document text does not match the expected
* Apply a command for non-physical file copy.
*
* @param command command to apply
* @param file a non-physical file copy to apply the command to
* @throws UnsupportedOperationException if the command does something except modifying the specified file
*/
@ApiStatus.Internal
void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText updateTextCommand)
throws IllegalStateException;
void executeForFileCopy(@NotNull ModCommand command, @NotNull PsiFile file) throws UnsupportedOperationException;
/**
* @return an instance of this service
@@ -662,7 +662,21 @@ public class ModCommandExecutorImpl implements ModCommandExecutor {
}
@Override
public void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText upd)
public void executeForFileCopy(@NotNull ModCommand command, @NotNull PsiFile file) {
for (ModCommand cmd : command.unpack()) {
if (cmd instanceof ModUpdateFileText updateFileText) {
if (!updateFileText.file().equals(file.getOriginalFile().getVirtualFile())) {
throw new UnsupportedOperationException("The command updates non-current file");
}
updateText(file.getProject(), file.getViewProvider().getDocument(), updateFileText);
}
else if (!(cmd instanceof ModNavigate) && !(cmd instanceof ModHighlight)) {
throw new UnsupportedOperationException("Unexpected command: " + command);
}
}
}
private void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText upd)
throws IllegalStateException {
String oldText = upd.oldText();
if (!document.getText().equals(oldText)) {