Skip to content

Commit 825d224

Browse files
committed
refactor(ui): consolidate edit file execution logic
Extract common execution logic between auto and manual modes to eliminate code duplication. Move result handling to separate method and create unified button creation flow.
1 parent d717de6 commit 825d224

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/CodeHighlightSketch.kt

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -465,64 +465,54 @@ fun CodeHighlightSketch.processWriteCommand(currentText: String, fileName: Strin
465465
fun CodeHighlightSketch.processEditFileCommand(currentText: String) {
466466
val isAutoSketchMode = AutoSketchMode.getInstance(project).isEnable
467467

468-
if (isAutoSketchMode) {
469-
val button = JButton("Auto Executing...", AutoDevIcons.LOADING).apply {
468+
val button = if (isAutoSketchMode) {
469+
JButton("Auto Executing...", AutoDevIcons.LOADING).apply {
470470
isEnabled = false
471471
preferredSize = JBUI.size(150, 30)
472472
}
473+
} else {
474+
JButton("Execute Edit File", AllIcons.Actions.Execute).apply {
475+
preferredSize = JBUI.size(120, 30)
476+
}
477+
}
473478

474-
val panel = JPanel()
475-
panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
476-
panel.add(button)
477-
add(panel)
479+
val panel = JPanel().apply {
480+
layout = BoxLayout(this, BoxLayout.X_AXIS)
481+
add(button)
482+
}
483+
add(panel)
484+
485+
val executeCommand = {
486+
button.isEnabled = false
487+
button.text = if (isAutoSketchMode) "Auto Executing..." else "Executing..."
488+
button.icon = AutoDevIcons.LOADING
478489

479490
AutoDevCoroutineScope.scope(project).launch {
480491
executeEditFileCommand(project, currentText) { result ->
481492
runInEdt {
482-
if (result != null && !result.startsWith("DEVINS_ERROR")) {
483-
val diffSketch = DiffLangSketchProvider().create(project, result)
484-
add(diffSketch.getComponent())
485-
button.text = "Executed"
486-
button.icon = AllIcons.Actions.Checked
487-
} else {
488-
button.text = "Failed"
489-
button.icon = AllIcons.General.Error
490-
AutoDevNotifications.warn(project, result ?: "Unknown error occurred")
491-
}
493+
handleExecutionResult(result, button)
492494
}
493495
}
494496
}
497+
}
498+
499+
if (isAutoSketchMode) {
500+
executeCommand()
495501
} else {
496-
val executeButton = JButton("Execute Edit File", AllIcons.Actions.Execute).apply {
497-
preferredSize = JBUI.size(120, 30)
498-
addActionListener {
499-
this.isEnabled = false
500-
this.text = "Executing..."
501-
this.icon = AutoDevIcons.LOADING
502-
503-
AutoDevCoroutineScope.scope(project).launch {
504-
executeEditFileCommand(project, currentText) { result ->
505-
runInEdt {
506-
if (result != null && !result.startsWith("DEVINS_ERROR")) {
507-
val diffSketch = DiffLangSketchProvider().create(project, result)
508-
add(diffSketch.getComponent())
509-
this@apply.text = "Executed"
510-
this@apply.icon = AllIcons.Actions.Checked
511-
} else {
512-
this@apply.text = "Failed"
513-
this@apply.icon = AllIcons.General.Error
514-
AutoDevNotifications.warn(project, result ?: "Unknown error occurred")
515-
}
516-
}
517-
}
518-
}
519-
}
520-
}
502+
button.addActionListener { executeCommand() }
503+
}
504+
}
521505

522-
val panel = JPanel()
523-
panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
524-
panel.add(executeButton)
525-
add(panel)
506+
private fun CodeHighlightSketch.handleExecutionResult(result: String?, button: JButton) {
507+
if (result != null && !result.startsWith("DEVINS_ERROR")) {
508+
val diffSketch = DiffLangSketchProvider().create(project, result)
509+
add(diffSketch.getComponent())
510+
button.text = "Executed"
511+
button.icon = AllIcons.Actions.Checked
512+
} else {
513+
button.text = "Failed"
514+
button.icon = AllIcons.General.Error
515+
AutoDevNotifications.warn(project, result ?: "Unknown error occurred")
526516
}
527517
}
528518

0 commit comments

Comments
 (0)