fix: closing the 'Apply' view doesn't bring back the updated code

This commit is contained in:
Carl-Robert Linnupuu 2025-10-20 16:31:34 +01:00
parent a0f94f241a
commit 5a3a05e07a
3 changed files with 32 additions and 9 deletions

View file

@ -147,7 +147,7 @@ class ResponseEditorPanel(
}
if (!response.isNullOrBlank()) {
stateManager.transitionToDiffState(originalCode, response, params.destination)
stateManager.transitionToDiffState(originalCode, response, params.destination, params.source)
}
} catch (e: Exception) {
logger.error("Failed to apply changes", e)
@ -224,7 +224,8 @@ class ResponseEditorPanel(
val containsText = currentText.contains(segment.search.trim())
val newState = if (containsText) {
stateManager.createFromSegment(segment)
val finalSegment = createReplaceWaitingSegment(searchContent, replaceContent, virtualFile)
stateManager.createFromSegment(finalSegment, readOnly = false, eventSource = null, originalSuggestion = replaceContent)
} else {
stateManager.transitionToFailedDiffState(
segment.search,
@ -339,4 +340,4 @@ class ResponseEditorPanel(
content
}
}
}
}

View file

@ -44,12 +44,21 @@ class EditorStateManager(private val project: Project) {
}
fun transitionToDiffState(originalCode: String, updatedCode: String, virtualFile: VirtualFile) {
transitionToDiffState(originalCode, updatedCode, virtualFile, null)
}
fun transitionToDiffState(
originalCode: String,
updatedCode: String,
virtualFile: VirtualFile,
originalSuggestion: String?
) {
val oldState = currentState ?: return
val oldEditor = oldState.editor
val language = oldState.segment.language.ifBlank { virtualFile.extension ?: "Text" }
val segment = ReplaceWaiting(originalCode, updatedCode, language, virtualFile.path)
val editor = EditorFactory.createEditor(project, segment)
val state = createDiffState(editor, segment)
val state = createDiffState(editor, segment, null, originalSuggestion)
runInEdt {
val headerComponent = state.createHeaderComponent(false)

View file

@ -28,6 +28,11 @@ class StandardDiffEditorState(
eventSource: EventSource? = null,
private val originalSuggestion: String? = null
) : DiffEditorState(editor, segment, project, diffViewer, virtualFile, eventSource) {
private var latestSuggestion: String? = when (segment) {
is SearchReplace -> segment.replace.takeIf { it.isNotBlank() }
is ReplaceWaiting -> segment.replace.takeIf { it.isNotBlank() }
else -> null
} ?: originalSuggestion?.takeIf { it.isNotBlank() }
override fun applyAllChanges() {
val before = diffViewer?.getDocument(Side.LEFT)?.text ?: return
@ -50,6 +55,10 @@ class StandardDiffEditorState(
return
}
if (replace.isNotBlank()) {
latestSuggestion = replace
}
diffEditorManager.updateDiffContent(search, replace)
(editor.permanentHeaderComponent as? DiffHeaderPanel)
?.updateDiffStats(diffViewer?.diffChanges ?: emptyList())
@ -67,11 +76,15 @@ class StandardDiffEditorState(
override fun handleClose() {
runInEdt {
val responsePanel = editor.component.parent as? ResponseEditorPanel ?: return@runInEdt
val contentToKeep = originalSuggestion ?: when (segment) {
is SearchReplace -> segment.replace
is ReplaceWaiting -> segment.replace
else -> diffViewer?.getDocument(Side.RIGHT)?.text ?: ""
}
val contentToKeep =
originalSuggestion?.takeIf { it.isNotBlank() }
?: latestSuggestion?.takeIf { it.isNotBlank() }
?: when (segment) {
is SearchReplace -> segment.replace
is ReplaceWaiting -> segment.replace
else -> null
}?.takeIf { it.isNotBlank() }
?: (diffViewer?.getDocument(Side.RIGHT)?.text ?: "")
responsePanel.replaceEditorWithSegment(
Code(contentToKeep, segment.language, segment.filePath)
)