Skip to content

Commit 26bf2ed

Browse files
committed
refactor(devti): extract editor creation logic and update UI components
- Extract createCodeViewerEditor and configEditor functions into EditorUtil object - Update AutoDevLanguageLabelAction to use JBUI for border - Replace AllIcons with AutoDevIcons in SingleFileDiffSketch - Add border to toolbar in LangSketch
1 parent deab5dc commit 26bf2ed

File tree

5 files changed

+157
-122
lines changed

5 files changed

+157
-122
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/snippet/AutoDevLanguageLabelAction.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.DumbAwareAction
1313
import com.intellij.openapi.util.Key
1414
import com.intellij.testFramework.LightVirtualFile
1515
import com.intellij.ui.components.JBLabel
16+
import com.intellij.util.ui.JBUI
1617
import com.intellij.util.ui.UIUtil
1718
import javax.swing.JComponent
1819

@@ -21,10 +22,11 @@ class AutoDevLanguageLabelAction : DumbAwareAction(), CustomComponentAction {
2122

2223
override fun createCustomComponent(presentation: Presentation, place: String): JComponent {
2324
val languageId = presentation.getClientProperty(LANGUAGE_PRESENTATION_KEY) ?: ""
24-
val jBLabel: JComponent = JBLabel(languageId)
25-
jBLabel.setOpaque(false)
26-
jBLabel.setForeground(UIUtil.getLabelInfoForeground())
27-
return jBLabel
25+
val label: JComponent = JBLabel(languageId)
26+
label.setOpaque(false)
27+
label.setForeground(UIUtil.getLabelInfoForeground())
28+
label.border = JBUI.Borders.empty(0, 8)
29+
return label
2830
}
2931

3032
override fun updateCustomComponent(component: JComponent, presentation: Presentation) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import com.intellij.openapi.editor.colors.EditorColorsManager
1212
import com.intellij.openapi.editor.ex.EditorEx
1313
import com.intellij.openapi.project.Project
1414
import com.intellij.util.messages.Topic
15+
import com.intellij.util.ui.JBUI
16+
import com.intellij.util.ui.UIUtil
1517
import javax.swing.JComponent
1618

1719
interface LangSketch : Disposable {
@@ -39,6 +41,8 @@ interface LangSketch : Disposable {
3941
toolbar.targetComponent = editor.contentComponent
4042
editor.headerComponent = toolbar.component
4143

44+
toolbar.component.border = JBUI.Borders.customLine(UIUtil.getBoundsColor(), 0, 0, 1, 0)
45+
4246
val connect = project.messageBus.connect(this)
4347
val topic: Topic<EditorColorsListener> = EditorColorsManager.TOPIC
4448
connect.subscribe(topic, EditorColorsListener {

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

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class CodeHighlightSketch(
8282
if (hasSetupAction) return
8383
hasSetupAction = true
8484

85-
val editor = createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
85+
val editor = EditorUtil.createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
8686

8787
border = if (withLeftRightBorder) {
8888
JBEmptyBorder(8, 8, 8, 8)
@@ -198,121 +198,6 @@ open class CodeHighlightSketch(
198198

199199
override fun getData(dataId: String): Any? = null
200200

201-
companion object {
202-
private val LINE_NO_REGEX = Regex("^\\d+:")
203-
204-
fun createCodeViewerEditor(
205-
project: Project,
206-
text: String,
207-
ideaLanguage: Language?,
208-
fileName: String?,
209-
disposable: Disposable,
210-
): EditorEx {
211-
var editorText = text
212-
val language = ideaLanguage ?: CodeFence.findLanguage("Plain text")
213-
val ext = if (language.displayName == "Plain text") {
214-
CodeFence.lookupFileExt(language.displayName)
215-
} else {
216-
language.associatedFileType?.defaultExtension ?: "Unknown"
217-
}
218-
/// check text easyline starts with Lineno and :, for example: 1:
219-
var isShowLineNo = true
220-
editorText.lines().forEach {
221-
if (!it.matches(LINE_NO_REGEX)) {
222-
isShowLineNo = false
223-
return@forEach
224-
}
225-
}
226-
227-
if (isShowLineNo) {
228-
val newLines = text.lines().map { it.replace(LINE_NO_REGEX, "") }
229-
editorText = newLines.joinToString("\n")
230-
}
231-
232-
val file: VirtualFile = if (fileName != null) {
233-
LightVirtualFile(fileName, language, editorText)
234-
} else {
235-
LightVirtualFile(AutoDevSnippetFile.naming(ext), language, editorText)
236-
}
237-
238-
val document: Document = file.findDocument() ?: throw IllegalStateException("Document not found")
239-
return createCodeViewerEditor(project, file, document, disposable, isShowLineNo)
240-
}
241-
242-
private fun createCodeViewerEditor(
243-
project: Project,
244-
file: VirtualFile,
245-
document: Document,
246-
disposable: Disposable,
247-
isShowLineNo: Boolean? = false,
248-
): EditorEx {
249-
val editor: EditorEx = ReadAction.compute<EditorEx, Throwable> {
250-
if (project.isDisposed) return@compute throw IllegalStateException("Project is disposed")
251-
252-
try {
253-
EditorFactory.getInstance().createViewer(document, project, EditorKind.PREVIEW) as EditorEx
254-
} catch (e: Throwable) {
255-
throw e
256-
}
257-
}
258-
259-
disposable.whenDisposed {
260-
EditorFactory.getInstance().releaseEditor(editor)
261-
}
262-
263-
editor.setFile(file)
264-
return configEditor(editor, project, file, isShowLineNo)
265-
}
266-
267-
fun configEditor(
268-
editor: EditorEx,
269-
project: Project,
270-
file: VirtualFile,
271-
isShowLineNo: Boolean?
272-
): EditorEx {
273-
editor.setCaretEnabled(true)
274-
275-
val highlighter = ApplicationManager.getApplication()
276-
.getService(EditorHighlighterFactory::class.java)
277-
.createEditorHighlighter(project, file)
278-
279-
editor.highlighter = highlighter
280-
281-
val markupModel: MarkupModelEx = editor.markupModel
282-
(markupModel as EditorMarkupModel).isErrorStripeVisible = false
283-
284-
val settings = editor.settings.also {
285-
it.isDndEnabled = false
286-
it.isLineNumbersShown = isShowLineNo == true
287-
it.additionalLinesCount = 0
288-
it.isLineMarkerAreaShown = false
289-
it.isFoldingOutlineShown = false
290-
it.isRightMarginShown = false
291-
it.isShowIntentionBulb = false
292-
it.isUseSoftWraps = true
293-
it.isRefrainFromScrolling = true
294-
it.isAdditionalPageAtBottom = false
295-
it.isCaretRowShown = false
296-
}
297-
298-
editor.addFocusListener(object : FocusChangeListener {
299-
override fun focusGained(focusEditor: Editor) {
300-
settings.isCaretRowShown = true
301-
}
302-
303-
override fun focusLost(focusEditor: Editor) {
304-
if (focusEditor.isDisposed) return
305-
if (editor.isDisposed) return
306-
307-
settings.isCaretRowShown = false
308-
editor.markupModel.removeAllHighlighters()
309-
}
310-
})
311-
312-
return editor
313-
}
314-
}
315-
316201
override fun dispose() {
317202
editorFragment?.editor?.let {
318203
EditorFactory.getInstance().releaseEditor(it)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package cc.unitmesh.devti.sketch.ui.code
2+
3+
import cc.unitmesh.devti.AutoDevSnippetFile
4+
import cc.unitmesh.devti.util.parser.CodeFence
5+
import cc.unitmesh.devti.util.whenDisposed
6+
import com.intellij.lang.Language
7+
import com.intellij.openapi.Disposable
8+
import com.intellij.openapi.application.ApplicationManager
9+
import com.intellij.openapi.application.ReadAction
10+
import com.intellij.openapi.editor.Document
11+
import com.intellij.openapi.editor.Editor
12+
import com.intellij.openapi.editor.EditorFactory
13+
import com.intellij.openapi.editor.EditorKind
14+
import com.intellij.openapi.editor.ex.EditorEx
15+
import com.intellij.openapi.editor.ex.EditorMarkupModel
16+
import com.intellij.openapi.editor.ex.FocusChangeListener
17+
import com.intellij.openapi.editor.ex.MarkupModelEx
18+
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory
19+
import com.intellij.openapi.project.Project
20+
import com.intellij.openapi.vfs.VirtualFile
21+
import com.intellij.testFramework.LightVirtualFile
22+
import java.awt.Color
23+
24+
object EditorUtil {
25+
private val LINE_NO_REGEX = Regex("^\\d+:")
26+
27+
fun createCodeViewerEditor(
28+
project: Project,
29+
text: String,
30+
ideaLanguage: Language?,
31+
fileName: String?,
32+
disposable: Disposable,
33+
): EditorEx {
34+
var editorText = text
35+
val language = ideaLanguage ?: CodeFence.findLanguage("Plain text")
36+
val ext = if (language.displayName == "Plain text") {
37+
CodeFence.lookupFileExt(language.displayName)
38+
} else {
39+
language.associatedFileType?.defaultExtension ?: "Unknown"
40+
}
41+
/// check text easyline starts with Lineno and :, for example: 1:
42+
var isShowLineNo = true
43+
editorText.lines().forEach {
44+
if (!it.matches(LINE_NO_REGEX)) {
45+
isShowLineNo = false
46+
return@forEach
47+
}
48+
}
49+
50+
if (isShowLineNo) {
51+
val newLines = text.lines().map { it.replace(LINE_NO_REGEX, "") }
52+
editorText = newLines.joinToString("\n")
53+
}
54+
55+
val file: VirtualFile = if (fileName != null) {
56+
LightVirtualFile(fileName, language, editorText)
57+
} else {
58+
LightVirtualFile(AutoDevSnippetFile.naming(ext), language, editorText)
59+
}
60+
61+
val document: Document = file.findDocument() ?: throw IllegalStateException("Document not found")
62+
return createCodeViewerEditor(project, file, document, disposable, isShowLineNo)
63+
}
64+
65+
private fun createCodeViewerEditor(
66+
project: Project,
67+
file: VirtualFile,
68+
document: Document,
69+
disposable: Disposable,
70+
isShowLineNo: Boolean? = false,
71+
): EditorEx {
72+
val editor: EditorEx = ReadAction.compute<EditorEx, Throwable> {
73+
if (project.isDisposed) return@compute throw IllegalStateException("Project is disposed")
74+
75+
try {
76+
EditorFactory.getInstance().createViewer(document, project, EditorKind.PREVIEW) as? EditorEx
77+
} catch (e: Throwable) {
78+
throw e
79+
}
80+
}
81+
82+
disposable.whenDisposed {
83+
EditorFactory.getInstance().releaseEditor(editor)
84+
}
85+
86+
editor.setFile(file)
87+
return configEditor(editor, project, file, isShowLineNo)
88+
}
89+
90+
fun configEditor(
91+
editor: EditorEx,
92+
project: Project,
93+
file: VirtualFile,
94+
isShowLineNo: Boolean?
95+
): EditorEx {
96+
editor.setCaretEnabled(true)
97+
98+
99+
val highlighter = ApplicationManager.getApplication()
100+
.getService(EditorHighlighterFactory::class.java)
101+
.createEditorHighlighter(project, file)
102+
103+
editor.highlighter = highlighter
104+
105+
val markupModel: MarkupModelEx = editor.markupModel
106+
(markupModel as EditorMarkupModel).isErrorStripeVisible = false
107+
108+
val settings = editor.settings.also {
109+
it.isDndEnabled = false
110+
it.isLineNumbersShown = isShowLineNo == true
111+
it.additionalLinesCount = 0
112+
it.isLineMarkerAreaShown = false
113+
it.isFoldingOutlineShown = false
114+
it.isRightMarginShown = false
115+
it.isShowIntentionBulb = false
116+
it.isUseSoftWraps = true
117+
it.isRefrainFromScrolling = true
118+
it.isAdditionalPageAtBottom = false
119+
it.isCaretRowShown = false
120+
}
121+
122+
// Hide scrollbars when they're not needed
123+
editor.scrollPane.verticalScrollBar.isVisible = false
124+
editor.scrollPane.horizontalScrollBar.isVisible = false
125+
126+
editor.scrollPane.background = editor.backgroundColor
127+
128+
editor.addFocusListener(object : FocusChangeListener {
129+
override fun focusGained(focusEditor: Editor) {
130+
settings.isCaretRowShown = true
131+
}
132+
133+
override fun focusLost(focusEditor: Editor) {
134+
if (focusEditor.isDisposed) return
135+
if (editor.isDisposed) return
136+
137+
settings.isCaretRowShown = false
138+
editor.markupModel.removeAllHighlighters()
139+
}
140+
})
141+
142+
return editor
143+
}
144+
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffSketch.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class SingleFileDiffSketch(
149149
file: VirtualFile, appliedPatch: GenericPatchApplier.AppliedPatch?, filePatch: TextFilePatch, isRepaired: Boolean = false
150150
): List<JButton> {
151151
val viewButton = JButton(AutoDevBundle.message("sketch.patch.view")).apply {
152-
icon = AllIcons.Actions.ListChanges
152+
icon = AutoDevIcons.View
153153
toolTipText = AutoDevBundle.message("sketch.patch.action.viewDiff.tooltip")
154154

155155
addMouseListener(object : MouseAdapter() {
@@ -160,7 +160,7 @@ class SingleFileDiffSketch(
160160
}
161161

162162
val applyButton = JButton(AutoDevBundle.message("sketch.patch.apply")).apply {
163-
icon = AllIcons.Actions.RunAll
163+
icon = AutoDevIcons.Run
164164
toolTipText = AutoDevBundle.message("sketch.patch.action.applyDiff.tooltip")
165165
isEnabled = appliedPatch?.status == ApplyPatchStatus.SUCCESS
166166

0 commit comments

Comments
 (0)