From 035a4cb296a71d7377e58de80a379506cefc3077 Mon Sep 17 00:00:00 2001 From: Vojtech Balik Date: Thu, 23 Nov 2023 21:28:27 +0100 Subject: [PATCH] [formatter] IDEA-338921 fix inconsistent behaviour of undo/redo with "reindent file" action The "reindent file" action is now also a named command. GitOrigin-RevId: fc497084f3b09b3896f82d036e6dea9f5dcec790 --- .../DetectableIndentOptionsProvider.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/DetectableIndentOptionsProvider.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/DetectableIndentOptionsProvider.java index a6ab3eb9b4ef..b222bfe61b4f 100644 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/DetectableIndentOptionsProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/codeStyle/DetectableIndentOptionsProvider.java @@ -15,6 +15,8 @@ import com.intellij.openapi.actionSystem.Separator; import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; +import com.intellij.openapi.command.undo.BasicUndoableAction; +import com.intellij.openapi.command.undo.UndoManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileTypes.FileType; @@ -215,8 +217,9 @@ public class DetectableIndentOptionsProvider extends FileIndentOptionsProvider { disableForFile(virtualFile, indentOptions); notifyIndentOptionsChanged(project, virtualFile); })); + final var reindentActionText = ApplicationBundle.message("code.style.indent.detector.reindent", projectOptionsTip); actions.add( - DumbAwareAction.create(ApplicationBundle.message("code.style.indent.detector.reindent", projectOptionsTip), + DumbAwareAction.create(reindentActionText, e -> { disableForFile(virtualFile, indentOptions); final var document = FileDocumentManager.getInstance().getCachedDocument(virtualFile); @@ -228,9 +231,25 @@ public class DetectableIndentOptionsProvider extends FileIndentOptionsProvider { indentOptsWithoutDetected.associateWithDocument(document); } notifyIndentOptionsChanged(project, virtualFile); - CommandProcessor.getInstance().runUndoTransparentAction( + CommandProcessor.getInstance().executeCommand( + project, () -> ApplicationManager.getApplication().runWriteAction( - () -> CodeStyleManager.getInstance(project).adjustLineIndent(file, file.getTextRange())) + () -> { + CodeStyleManager.getInstance(project).adjustLineIndent(file, file.getTextRange()); + UndoManager.getInstance(project).undoableActionPerformed(new BasicUndoableAction() { + @Override + public void undo() { + notifyIndentOptionsChanged(project, virtualFile); + } + + @Override + public void redo() { + notifyIndentOptionsChanged(project, virtualFile); + } + }); + }), + reindentActionText, + null ); myDiscardedOptions.remove(virtualFile); }));