fix: replace line separators when replacing editor's content

This commit is contained in:
Carl-Robert Linnupuu 2025-06-04 15:42:35 +01:00
parent 520bfa728b
commit 1007d77f96
3 changed files with 25 additions and 28 deletions

View file

@ -137,7 +137,7 @@ public class ChatMessageResponseBody extends JPanel {
public ChatMessageResponseBody withResponse(@NotNull String response) {
try {
for (var item : new CompleteMessageParser().parse(response)) {
processResponse(item, false, false);
processResponse(item, false);
currentlyProcessedTextPane = null;
currentlyProcessedEditorPanel = null;
}
@ -158,7 +158,7 @@ public class ChatMessageResponseBody extends JPanel {
var parsedResponse = streamOutputParser.parse(partialMessage);
for (Segment item : parsedResponse) {
processResponse(item, true, true);
processResponse(item, true);
}
}
@ -287,7 +287,7 @@ public class ChatMessageResponseBody extends JPanel {
.orElse(null);
}
private void processResponse(Segment item, boolean caretVisible, boolean partialResponse) {
private void processResponse(Segment item, boolean caretVisible) {
if (item instanceof Thinking) {
processThinkingOutput(item.getContent());
return;

View file

@ -5,7 +5,6 @@ import com.intellij.diff.tools.fragmented.UnifiedDiffViewer
import com.intellij.diff.util.DiffUtil
import com.intellij.diff.util.Side
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diff.DiffBundle
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.LogicalPosition
@ -14,8 +13,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.readText
import com.intellij.util.concurrency.annotations.RequiresEdt
import ee.carlrobert.codegpt.toolwindow.chat.editor.diff.DiffManagerUtil.replaceContent
import com.intellij.util.application
class DiffEditorManager(
private val project: Project,
@ -27,14 +25,20 @@ class DiffEditorManager(
val currentText = virtualFile?.readText() ?: return
val document = diffViewer.getDocument(Side.RIGHT)
runInEdt {
document.replaceContent(
project,
currentText.replaceLast(searchContent.trim(), replaceContent.trim())
)
diffViewer.rediff()
scrollToLastChange(diffViewer)
application.executeOnPooledThread {
runInEdt {
ensureDocumentWritable(project, document)
if (DiffUtil.executeWriteCommand(document, project, "Updating document") {
document.setText(
StringUtil.convertLineSeparators(
currentText.replaceLast(searchContent.trim(), replaceContent.trim())
)
)
}) {
diffViewer.rediff()
scrollToLastChange(diffViewer)
}
}
}
}
@ -46,7 +50,7 @@ class DiffEditorManager(
fun applyAllChanges(): List<UnifiedDiffChange> {
val document = diffViewer.getDocument(Side.LEFT)
DiffManagerUtil.ensureDocumentWritable(project, document)
ensureDocumentWritable(project, document)
val allChanges = mutableListOf<UnifiedDiffChange>()
@ -81,19 +85,8 @@ class DiffEditorManager(
ScrollType.CENTER
)
}
}
object DiffManagerUtil {
@RequiresEdt
fun Document.replaceContent(project: Project, replaceContent: String) {
ensureDocumentWritable(project, this)
DiffUtil.executeWriteCommand(this, project, "Updating document") {
setText(StringUtil.convertLineSeparators(replaceContent))
}
}
fun ensureDocumentWritable(project: Project, document: Document) {
private fun ensureDocumentWritable(project: Project, document: Document) {
if (!document.isWritable) {
DiffUtil.makeWritable(project, document)
document.setReadOnly(false)

View file

@ -120,6 +120,10 @@ class SseMessageParser : MessageParser {
}
line.trimStart().startsWith(SEARCH_MARKER) -> {
// Emit accumulated code content before transitioning
if (state.content.isNotEmpty()) {
segments.add(Code(state.content, state.header.language, state.header.filePath))
}
segments.add(SearchWaiting("", state.header.language, state.header.filePath))
parserState = ParserState.InSearch(state.header, "")
true
@ -129,7 +133,7 @@ class SseMessageParser : MessageParser {
val newContent =
if (state.content.isEmpty()) line else state.content + NEWLINE + line
parserState = ParserState.InCode(state.header, newContent)
false
true
}
}
}