From 55d06009aa331186ca71e9c8c9f3e17f18e8f60b Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Mon, 2 Jun 2025 12:22:46 +0100 Subject: [PATCH] fix: internal IDE errors --- .../form/details/AbstractEditorPromptPanel.kt | 3 ++- .../chat/editor/diff/DiffAcceptedPanel.kt | 3 ++- .../chat/editor/diff/DiffEditorManager.kt | 11 ++++++----- .../chat/editor/diff/DiffSyncManager.kt | 18 ++++++++++++++---- .../chat/editor/header/DefaultHeaderPanel.kt | 6 +++++- .../chat/editor/header/HeaderPanel.kt | 3 ++- .../chat/editor/state/FailedDiffEditorState.kt | 3 ++- .../chat/editor/state/RegularEditorState.kt | 3 ++- 8 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/ee/carlrobert/codegpt/settings/prompts/form/details/AbstractEditorPromptPanel.kt b/src/main/kotlin/ee/carlrobert/codegpt/settings/prompts/form/details/AbstractEditorPromptPanel.kt index 5c59abda..c9ad8b2b 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/settings/prompts/form/details/AbstractEditorPromptPanel.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/settings/prompts/form/details/AbstractEditorPromptPanel.kt @@ -12,6 +12,7 @@ import com.intellij.openapi.editor.markup.HighlighterTargetArea import com.intellij.openapi.editor.markup.RangeHighlighter import com.intellij.openapi.editor.markup.TextAttributes import com.intellij.openapi.util.TextRange +import com.intellij.openapi.util.text.StringUtil import com.intellij.ui.JBColor import java.awt.Dimension import javax.swing.JPanel @@ -94,7 +95,7 @@ abstract class AbstractEditorPromptPanel( protected fun updateEditorText(text: String?) { runWriteAction { - editor.document.setText(text ?: "") + editor.document.setText(StringUtil.convertLineSeparators(text ?: "")) } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffAcceptedPanel.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffAcceptedPanel.kt index c0db8af1..b12e2a34 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffAcceptedPanel.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffAcceptedPanel.kt @@ -9,6 +9,7 @@ import com.intellij.icons.AllIcons import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project +import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.writeText @@ -82,7 +83,7 @@ class DiffAcceptedPanel( val revertAllButton = DiffEditorState.createContextActionButton("Revert All", AllIcons.Actions.Redo) { runWriteAction { - virtualFile.writeText(before) + virtualFile.writeText(StringUtil.convertLineSeparators(before)) } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffEditorManager.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffEditorManager.kt index b40e115a..0ca5ae1f 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffEditorManager.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffEditorManager.kt @@ -5,11 +5,13 @@ 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 import com.intellij.openapi.editor.ScrollType 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 @@ -21,8 +23,8 @@ class DiffEditorManager( private val virtualFile: VirtualFile? ) { - fun updateDiffContent(searchContent: String, replaceContent: String): Boolean { - val currentText = virtualFile?.readText() ?: return false + fun updateDiffContent(searchContent: String, replaceContent: String) { + val currentText = virtualFile?.readText() ?: return val document = diffViewer.getDocument(Side.RIGHT) runInEdt { @@ -31,10 +33,9 @@ class DiffEditorManager( currentText.replaceLast(searchContent.trim(), replaceContent.trim()) ) - diffViewer.rediff(true) + diffViewer.rediff() scrollToLastChange(diffViewer) } - return true } fun String.replaceLast(search: String, replacement: String): String { @@ -88,7 +89,7 @@ object DiffManagerUtil { fun Document.replaceContent(project: Project, replaceContent: String) { ensureDocumentWritable(project, this) DiffUtil.executeWriteCommand(this, project, "Updating document") { - setText(replaceContent) + setText(StringUtil.convertLineSeparators(replaceContent)) } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffSyncManager.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffSyncManager.kt index 732eb7b8..fec971d4 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffSyncManager.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/diff/DiffSyncManager.kt @@ -2,11 +2,13 @@ package ee.carlrobert.codegpt.toolwindow.chat.editor.diff import com.intellij.diff.util.Side import com.intellij.openapi.application.runInEdt +import com.intellij.openapi.application.runReadAction import com.intellij.openapi.application.runUndoTransparentWriteAction import com.intellij.openapi.editor.event.DocumentEvent import com.intellij.openapi.editor.event.DocumentListener import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.util.application import ee.carlrobert.codegpt.toolwindow.chat.editor.ResponseEditorPanel.Companion.RESPONSE_EDITOR_DIFF_VIEWER_KEY @@ -25,7 +27,9 @@ object DiffSyncManager { if (!fileToListener.containsKey(filePath)) { val virtualFile = LocalFileSystem.getInstance().findFileByPath(filePath) ?: return - val document = FileDocumentManager.getInstance().getDocument(virtualFile) ?: return + val document = + runReadAction { FileDocumentManager.getInstance().getDocument(virtualFile) } + ?: return val listener = object : DocumentListener { override fun documentChanged(event: DocumentEvent) { @@ -34,8 +38,10 @@ object DiffSyncManager { for (editor in affectedEditors) { val diffViewer = RESPONSE_EDITOR_DIFF_VIEWER_KEY.get(editor) if (diffViewer != null) { - val leftSideDoc = diffViewer.getDocument(Side.LEFT) - val rightSideDoc = diffViewer.getDocument(Side.RIGHT) + val leftSideDoc = + runReadAction { diffViewer.getDocument(Side.LEFT) } + val rightSideDoc = + runReadAction { diffViewer.getDocument(Side.RIGHT) } if (leftSideDoc.text == rightSideDoc.text) { continue @@ -51,7 +57,11 @@ object DiffSyncManager { runInEdt { if (replacedText.length != newText.length) { runUndoTransparentWriteAction { - rightSideDoc.setText(replacedText) + rightSideDoc.setText( + StringUtil.convertLineSeparators( + replacedText + ) + ) diffViewer.scheduleRediff() } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/DefaultHeaderPanel.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/DefaultHeaderPanel.kt index 6591a2f6..1c2ac1b6 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/DefaultHeaderPanel.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/DefaultHeaderPanel.kt @@ -85,7 +85,11 @@ class DefaultHeaderPanel(config: HeaderConfig) : HeaderPanel(config) { val coefficient = StringUtil.getDiceCoefficient(editor.document.text, file.readText()) if (coefficient > directApplyThreshold) { runUndoTransparentWriteAction { - file.writeText(editor.document.text) + file.writeText( + com.intellij.openapi.util.text.StringUtil.convertLineSeparators( + editor.document.text + ) + ) } val balloon = JBPopupFactory.getInstance() .createHtmlTextBalloonBuilder( diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt index 1b4a982d..05dfb976 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt @@ -9,6 +9,7 @@ import com.intellij.openapi.application.runUndoTransparentWriteAction import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.project.Project +import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.writeText @@ -171,7 +172,7 @@ abstract class HeaderPanel(protected val config: HeaderConfig) : BorderLayoutPan LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)?.let { newFile -> runInEdt { runUndoTransparentWriteAction { - newFile.writeText(content) + newFile.writeText(StringUtil.convertLineSeparators(content)) } remove(actionLink) diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/FailedDiffEditorState.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/FailedDiffEditorState.kt index d2b66297..9743f5bd 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/FailedDiffEditorState.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/FailedDiffEditorState.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.application.runInEdt import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.project.Project +import com.intellij.openapi.util.text.StringUtil import ee.carlrobert.codegpt.CodeGPTBundle import ee.carlrobert.codegpt.toolwindow.chat.editor.header.DefaultHeaderPanel import ee.carlrobert.codegpt.toolwindow.chat.editor.header.HeaderConfig @@ -21,7 +22,7 @@ class FailedDiffEditorState( override fun updateContent(segment: Segment) { runInEdt { runWriteAction { - editor.document.setText(segment.content) + editor.document.setText(StringUtil.convertLineSeparators(segment.content)) } } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/RegularEditorState.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/RegularEditorState.kt index 20271b53..0bee9fd2 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/RegularEditorState.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/state/RegularEditorState.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.application.runInEdt import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.editor.ex.EditorEx import com.intellij.openapi.project.Project +import com.intellij.openapi.util.text.StringUtil import ee.carlrobert.codegpt.toolwindow.chat.editor.header.DefaultHeaderPanel import ee.carlrobert.codegpt.toolwindow.chat.editor.header.HeaderConfig import ee.carlrobert.codegpt.toolwindow.chat.parser.Segment @@ -19,7 +20,7 @@ class RegularEditorState( override fun updateContent(segment: Segment) { runInEdt { runWriteAction { - editor.document.setText(segment.content) + editor.document.setText(StringUtil.convertLineSeparators(segment.content)) } } }