diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt index d7cc74ca8ed8..75016f4d56f9 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt @@ -11,7 +11,9 @@ import com.intellij.ide.highlighter.JavaFileType import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.impl.NonBlockingReadActionImpl +import com.intellij.openapi.command.undo.UndoManager import com.intellij.openapi.editor.Editor +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.util.registry.Registry import com.intellij.platform.backend.documentation.AsyncDocumentation import com.intellij.platform.backend.documentation.DocumentationData @@ -68,6 +70,38 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { """.trimIndent()) } + + fun testUndo() { + Registry.get("ide.completion.command.force.enabled").setValue(true, getTestRootDisposable()) + myFixture.configureByText(JavaFileType.INSTANCE, """ + class A { + void foo() { + int y = 10; + int x = y.format; + } + } + """.trimIndent()) + val elements = myFixture.completeBasic() + selectItem(elements.first { element -> element.lookupString.contains("format", ignoreCase = true) }) + myFixture.checkResult(""" + class A { + void foo() { + int y = 10; + int x = y; + } + } + """.trimIndent()) + val editors = FileEditorManager.getInstance(project).getEditors(myFixture.file.virtualFile) + UndoManager.getInstance(project).undo(editors[0]) + myFixture.checkResult(""" + class A { + void foo() { + int y = 10; + int x = y.format; + } + } + """.trimIndent()) + } fun testFormatOutside() { Registry.get("ide.completion.command.force.enabled").setValue(true, getTestRootDisposable()) myFixture.configureByText(JavaFileType.INSTANCE, """ diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/command/CommandInsertHandler.kt b/platform/lang-impl/src/com/intellij/codeInsight/completion/command/CommandInsertHandler.kt index 8c47d72f6d6e..9cbb57444c92 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/command/CommandInsertHandler.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/command/CommandInsertHandler.kt @@ -26,8 +26,9 @@ internal class CommandInsertHandler(private val completionCommand: CompletionCom override fun handleInsert(context: InsertionContext, item: LookupElement) { var editor = context.editor val originalEditor = editor.getUserData(ORIGINAL_EDITOR) - var startOffset: Int + var startOffset: Int = -1 var psiFile = context.file + val commandProcessor = CommandProcessor.getInstance() if (completionCommand.customPrefixMatcher("") == null) { if (originalEditor != null) { startOffset = originalEditor.second @@ -37,14 +38,17 @@ internal class CommandInsertHandler(private val completionCommand: CompletionCom Disposer.dispose(installedEditor) } else { - // Remove the dots and command text from the document - startOffset = removeCommandText(context) + commandProcessor.executeCommand(context.project, { + // Remove the dots and command text from the document + startOffset = removeCommandText(context) + }, commandProcessor.currentCommandName, commandProcessor.currentCommandGroupId) } } else { startOffset = context.tailOffset } + if (startOffset == -1) return // Execute the command val injectedLanguageManager = InjectedLanguageManager.getInstance(context.project) @@ -55,9 +59,9 @@ internal class CommandInsertHandler(private val completionCommand: CompletionCom } ApplicationManager.getApplication().invokeLater { - CommandProcessor.getInstance().executeCommand(context.project, { + commandProcessor.runUndoTransparentAction( { completionCommand.execute(startOffset, psiFile, editor) - }, completionCommand.presentableName, completionCommand) + }) } }