|
| 1 | +package cc.unitmesh.go.provider |
| 2 | + |
| 3 | +import cc.unitmesh.devti.sketch.ui.ExtensionLangSketch |
| 4 | +import cc.unitmesh.devti.sketch.ui.LanguageSketchProvider |
| 5 | +import cc.unitmesh.devti.sketch.ui.code.CodeHighlightSketch |
| 6 | +import cc.unitmesh.devti.sketch.ui.preview.FileEditorPreviewSketch |
| 7 | +import com.goide.GoLanguage |
| 8 | +import com.goide.playground.ui.GoPlaygroundFileEditorWithPreview |
| 9 | +import com.intellij.openapi.Disposable |
| 10 | +import com.intellij.openapi.editor.EditorFactory |
| 11 | +import com.intellij.openapi.editor.EditorKind |
| 12 | +import com.intellij.openapi.fileEditor.FileEditor |
| 13 | +import com.intellij.openapi.fileEditor.TextEditor |
| 14 | +import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider |
| 15 | +import com.intellij.openapi.project.Project |
| 16 | +import com.intellij.openapi.util.Disposer |
| 17 | +import com.intellij.openapi.vfs.VirtualFile |
| 18 | +import com.intellij.testFramework.LightVirtualFile |
| 19 | +import javax.swing.JComponent |
| 20 | + |
| 21 | +class GoLangPlaygroundSketchProvider : LanguageSketchProvider { |
| 22 | + override fun isSupported(lang: String): Boolean = lang == "go" |
| 23 | + |
| 24 | + override fun create(project: Project, content: String): ExtensionLangSketch { |
| 25 | + if (content.contains("package main")) { |
| 26 | + val file = LightVirtualFile("main.go", content) |
| 27 | + return GoLangPlaygroundSketch(project, content, file) |
| 28 | + } |
| 29 | + |
| 30 | + return object : CodeHighlightSketch(project, content, GoLanguage.INSTANCE), ExtensionLangSketch { |
| 31 | + override fun getExtensionName(): String = "GoHighlight" |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class GoLangPlaygroundSketch(val myProject: Project, val content: String, val file: LightVirtualFile) : |
| 37 | + FileEditorPreviewSketch(myProject, file, "GoPlaygroundFileEditorWithPreview") { |
| 38 | + override fun getExtensionName(): String = "GoMain" |
| 39 | + |
| 40 | + val editorWithPreview = createEditorWithPreview(myProject, file) as? GoPlaygroundFileEditorWithPreview |
| 41 | + |
| 42 | + override val mainPanel: JComponent |
| 43 | + get() { |
| 44 | + return editorWithPreview?.component ?: editor.component |
| 45 | + } |
| 46 | + |
| 47 | + override fun getComponent(): JComponent { |
| 48 | + return editorWithPreview?.component ?: editor.component |
| 49 | + } |
| 50 | + |
| 51 | + override fun getViewText(): String { |
| 52 | + return editorWithPreview?.editor?.document?.text ?: editor.file.readText() |
| 53 | + } |
| 54 | + |
| 55 | + companion object { |
| 56 | + fun createEditorWithPreview(project: Project, file: VirtualFile): FileEditor { |
| 57 | + val textEditorProvider = TextEditorProvider.getInstance() |
| 58 | + val editorFactory = EditorFactory.getInstance() |
| 59 | + val editor = textEditorProvider.createEditor(project, file) |
| 60 | + |
| 61 | + return if (editor is TextEditor) { |
| 62 | + val viewer = editorFactory.createViewer( |
| 63 | + editorFactory.createDocument("" as CharSequence), |
| 64 | + project, |
| 65 | + EditorKind.PREVIEW |
| 66 | + ) |
| 67 | + val previewEditor = textEditorProvider.getTextEditor(viewer) |
| 68 | + |
| 69 | + Disposer.register(editor as Disposable, Disposable { |
| 70 | + EditorFactory.getInstance().releaseEditor(viewer) |
| 71 | + }) |
| 72 | + |
| 73 | + GoPlaygroundFileEditorWithPreview(editor, previewEditor) |
| 74 | + } else { |
| 75 | + editor |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments