[command-completion] IDEA-374653 Don't return the text of command completion after 2 x ctrl+z

GitOrigin-RevId: b95df5fdf71ae8af1e3f431918b00ad07b0b7d43
This commit is contained in:
Mikhail Pyltsin
2025-07-08 22:10:37 +00:00
committed by intellij-monorepo-bot
parent ff9b72a769
commit 4cb2980dea
2 changed files with 43 additions and 5 deletions
@@ -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<caret>;
}
}
""".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, """
@@ -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)
})
}
}