Skip to content

Commit

Permalink
DEV2-3995: show code diff on "insert"
Browse files Browse the repository at this point in the history
  • Loading branch information
yairco1990 committed Oct 16, 2023
1 parent 30ebe77 commit b003b56
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.diff.DiffContext
import com.intellij.diff.contents.DiffContent
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.diff.tools.simple.SimpleDiffViewer
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
Expand All @@ -18,9 +19,27 @@ class InsertAtCursorHandler(gson: Gson) : ChatMessageHandler<InsertPayload, Unit
val code = payload?.code ?: return
val editor = getEditorFromProject(project) ?: return

WriteCommandAction.runWriteCommandAction(project) {
val selectionModel = editor.selectionModel
editor.document.replaceString(selectionModel.selectionStart, selectionModel.selectionEnd, code)
ApplicationManager.getApplication().invokeLater {
val selectedText = editor.selectionModel.selectedText ?: ""

if (selectedText.isNotEmpty()) {
val shouldInsertText = InsertDiffDialog(
project,
selectedText,
code
).showAndGet()

if (!shouldInsertText) return@invokeLater
}

if (!ApplicationManager.getApplication().isDispatchThread) {
throw IllegalStateException("Trying to insert code on non-EDT thread")
}

WriteCommandAction.runWriteCommandAction(project) {
val selectionModel = editor.selectionModel
editor.document.replaceString(selectionModel.selectionStart, selectionModel.selectionEnd, code)
}
}
}

Expand Down

0 comments on commit b003b56

Please sign in to comment.