@@ -12,8 +12,10 @@ import com.intellij.openapi.command.UndoConfirmationPolicy
12
12
import com.intellij.openapi.command.undo.UndoManager
13
13
import com.intellij.openapi.diff.impl.patch.PatchReader
14
14
import com.intellij.openapi.diff.impl.patch.TextFilePatch
15
+ import com.intellij.openapi.editor.Editor
15
16
import com.intellij.openapi.fileEditor.FileEditorManager
16
17
import com.intellij.openapi.fileEditor.FileEditorProvider
18
+ import com.intellij.openapi.fileEditor.TextEditor
17
19
import com.intellij.openapi.project.Project
18
20
import com.intellij.openapi.ui.DialogWrapper
19
21
import com.intellij.openapi.vcs.changes.patch.AbstractFilePatchInProgress
@@ -22,6 +24,7 @@ import com.intellij.openapi.vcs.changes.patch.MatchPatchPaths
22
24
import com.intellij.openapi.vfs.VirtualFile
23
25
import com.intellij.psi.PsiDocumentManager
24
26
import com.intellij.testFramework.LightVirtualFile
27
+ import com.intellij.ui.components.panels.HorizontalLayout
25
28
import com.intellij.ui.components.panels.VerticalLayout
26
29
import com.intellij.util.containers.MultiMap
27
30
import com.intellij.util.ui.JBUI
@@ -57,14 +60,25 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
57
60
if (filePatches.isEmpty()) {
58
61
val msg = " PatchProcessor: no valid patches found, please check the patch content"
59
62
AutoDevNotifications .error(myProject, msg)
60
- val editor = FileEditorManager .getInstance(myProject).selectedTextEditor ? : return @invokeLater
61
- val repairButton = JButton (" Repair Patch" ).apply {
63
+ val repairButton = JButton (" Repair" ).apply {
64
+ icon = AllIcons .Actions .IntentionBulb
65
+ toolTipText = " Try to repair the patch content"
66
+
67
+ val editor = tryGetEditor()
68
+ if (editor == null ) {
69
+ AutoDevNotifications .error(myProject, " Failed to get editor" )
70
+ return @apply
71
+ }
72
+
62
73
addActionListener {
63
74
applyDiffRepairSuggestion(myProject, editor, editor.document.text, patchContent)
64
75
}
65
76
}
66
77
67
- mainPanel.add(repairButton)
78
+ val actionPanel = JPanel (HorizontalLayout (4 ))
79
+ actionPanel.add(repairButton)
80
+
81
+ mainPanel.add(actionPanel)
68
82
69
83
return @invokeLater
70
84
}
@@ -94,6 +108,18 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
94
108
}
95
109
}
96
110
111
+ private fun tryGetEditor (): Editor ? {
112
+ var defaultEditor = FileEditorManager .getInstance(myProject).selectedTextEditor ? : return null
113
+
114
+ val fileRegex = Regex (" /patch:(.*)" )
115
+ val matchResult = fileRegex.find(patchContent)
116
+ val filePath = matchResult?.groupValues?.get(1 ) ? : " "
117
+ val virtualFile = myProject.findFile(filePath) ? : return defaultEditor
118
+ val fileEditor = FileEditorManager .getInstance(myProject).getSelectedEditor(virtualFile) as ? TextEditor
119
+
120
+ return fileEditor?.editor ? : defaultEditor
121
+ }
122
+
97
123
private fun createHeaderAction (): JComponent {
98
124
val acceptButton = JButton (AllIcons .Actions .SetDefault ).apply {
99
125
toolTipText = AutoDevBundle .message(" sketch.patch.action.accept.tooltip" )
0 commit comments