Skip to content

Commit

Permalink
Use transparent undo when save (#192 ?)
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud committed Mar 15, 2023
1 parent d811db2 commit c09beb0
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 204 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/reason/comp/ocaml/OcamlFormatProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.*;

import static jpsplugin.com.reason.Platform.*;

public class OcamlFormatProcess {
private static final Log LOG = Log.create("ocamlformat.process");
private static final Log LOG = Log.create("format.ocaml.process");

private final Project myProject;

Expand Down
92 changes: 43 additions & 49 deletions src/main/java/com/reason/ide/actions/ConvertAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.*;
import com.intellij.openapi.command.*;
import com.intellij.openapi.command.undo.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileTypes.*;
Expand Down Expand Up @@ -57,55 +56,50 @@ protected void apply(@NotNull Project project, @NotNull PsiFile file, boolean is
convertedText = null;
}

if (convertedText != null) {
UndoUtil.forceUndoIn(sourceFile, () ->
CommandProcessor.getInstance()
.executeCommand(
project,
() -> WriteAction.run(
() -> {
String newFilename = sourceFile.getNameWithoutExtension() + "." + toFormat;
if (isNewFile) {
try {
VirtualFile newSourceFile =
VfsUtilCore.copyFile(this, sourceFile, sourceFile.getParent(), newFilename);
PsiFile newPsiFile =
PsiManager.getInstance(project).findFile(newSourceFile);
Document newDocument =
newPsiFile == null
? null
: PsiDocumentManager.getInstance(project)
.getDocument(newPsiFile);
if (newDocument != null) {
newDocument.setText(convertedText);
FileDocumentManager.getInstance().saveDocument(newDocument);
}
VirtualFileManager.getInstance().syncRefresh();
FileEditorManager.getInstance(project).openFile(newSourceFile, true);
} catch (IOException ex) {
Notifications.Bus.notify(
new ORNotification(
"Convert",
"File creation failed\n" + ex.getMessage(),
NotificationType.ERROR));
}
} else {
document.setText(convertedText);
FileDocumentManager.getInstance().saveDocument(document);
try {
sourceFile.rename(this, newFilename);
} catch (IOException ex) {
Notifications.Bus.notify(
new ORNotification(
"Convert",
"File renaming failed\n" + ex.getMessage(),
NotificationType.ERROR));
}
if (sourceFile != null && convertedText != null) {
CommandProcessor.getInstance()
.runUndoTransparentAction(
() -> WriteAction.run(
() -> {
String newFilename = sourceFile.getNameWithoutExtension() + "." + toFormat;
if (isNewFile) {
try {
VirtualFile newSourceFile =
VfsUtilCore.copyFile(this, sourceFile, sourceFile.getParent(), newFilename);
PsiFile newPsiFile =
PsiManager.getInstance(project).findFile(newSourceFile);
Document newDocument =
newPsiFile == null
? null
: PsiDocumentManager.getInstance(project)
.getDocument(newPsiFile);
if (newDocument != null) {
newDocument.setText(convertedText);
FileDocumentManager.getInstance().saveDocument(newDocument);
}
}),
"Convert File",
"EditMenu",
document));
VirtualFileManager.getInstance().syncRefresh();
FileEditorManager.getInstance(project).openFile(newSourceFile, true);
} catch (IOException ex) {
Notifications.Bus.notify(
new ORNotification(
"Convert",
"File creation failed\n" + ex.getMessage(),
NotificationType.ERROR));
}
} else {
document.setText(convertedText);
FileDocumentManager.getInstance().saveDocument(document);
try {
sourceFile.rename(this, newFilename);
} catch (IOException ex) {
Notifications.Bus.notify(
new ORNotification(
"Convert",
"File renaming failed\n" + ex.getMessage(),
NotificationType.ERROR));
}
}
}));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import org.jetbrains.annotations.*;

public interface FormatterProcessor {
@Nullable String apply(@NotNull String textToFormat);
@Nullable String apply(@NotNull String textToFormat);
}
12 changes: 4 additions & 8 deletions src/main/java/com/reason/ide/format/ReformatOnSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.intellij.openapi.application.*;
import com.intellij.openapi.application.ex.*;
import com.intellij.openapi.command.*;
import com.intellij.openapi.command.undo.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.project.*;
Expand Down Expand Up @@ -73,13 +72,10 @@ public static void apply(@NotNull Project project, @NotNull Document document) {
newFile.putUserData(REFORMAT_COUNT, 1);
} else {
//noinspection DialogTitleCapitalization
CommandProcessor.getInstance().executeCommand(project, () -> {
LOG.debug(" -> Applying text formatting");
UndoUtil.forceUndoIn(virtualFile, () -> document.setText(newText));
},
"or.reformat",
"CodeFormatGroup",
document);
CommandProcessor.getInstance().runUndoTransparentAction(() -> {
LOG.debug(" -> Applying text formatting");
document.setText(newText);
});

if (count == 1) {
// Only re-save first time, to avoid infinite loop
Expand Down
Loading

0 comments on commit c09beb0

Please sign in to comment.