Skip to content

Commit 6bc690f

Browse files
committed
feat(preview): set default layout for file editor preview
Initialize `DEFAULT_LAYOUT_FOR_FILE` to `Layout.SHOW_EDITOR_AND_PREVIEW` in `FileEditorPreviewSketch`. Refactor provider method and adjust related classes accordingly.
1 parent 1b9a5e6 commit 6bc690f

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cc.unitmesh.devti.sketch.ui.openapi
22

33
import cc.unitmesh.devti.sketch.ui.ExtensionLangSketch
4-
import cc.unitmesh.devti.sketch.ui.preview.FileEditorPreviewSketch
54
import cc.unitmesh.devti.sketch.ui.LanguageSketchProvider
65
import cc.unitmesh.devti.sketch.ui.code.CodeHighlightSketch
6+
import cc.unitmesh.devti.sketch.ui.preview.FileEditorPreviewSketch
77
import cc.unitmesh.devti.util.parser.CodeFence.Companion.findLanguage
8+
import cc.unitmesh.devti.util.parser.CodeFence.Companion.findLanguageByExt
9+
import com.intellij.ide.scratch.ScratchRootType
810
import com.intellij.openapi.project.Project
11+
import com.intellij.openapi.vfs.VirtualFile
912
import com.intellij.testFramework.LightVirtualFile
10-
import javax.swing.JComponent
1113

1214
class OpenAPISketchProvider : LanguageSketchProvider {
1315
override fun isSupported(lang: String) = lang == "yaml" || lang == "yml"
@@ -24,17 +26,21 @@ class OpenAPISketchProvider : LanguageSketchProvider {
2426
}
2527
}
2628

27-
return OpenAPISketch(project, content)
29+
val language = findLanguageByExt("yaml")
30+
val virtualFile = ScratchRootType.getInstance()
31+
.createScratchFile(project, createFileNameWithTime(), language, content)
32+
?: LightVirtualFile(createFileNameWithTime(), content)
33+
34+
return OpenAPISketch(project, content, virtualFile)
2835
}
2936
}
3037

31-
class OpenAPISketch(val myProject: Project, private val content: String) : FileEditorPreviewSketch(
32-
myProject,
33-
LightVirtualFile(createFileNameWithTime(), content),
34-
"SwaggerEditorWithPreview"
35-
) {
36-
override var mainPanel: JComponent = editor.component
37-
38+
class OpenAPISketch(val myProject: Project, private val content: String, virtualFile: VirtualFile) :
39+
FileEditorPreviewSketch(
40+
myProject,
41+
virtualFile,
42+
"SwaggerUIEditorProvider"
43+
) {
3844
override fun getExtensionName(): String = "OpenAPI"
3945
}
4046

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ import cc.unitmesh.devti.sketch.ui.LangSketch
55
import cc.unitmesh.devti.sketch.ui.code.CodeHighlightSketch
66
import cc.unitmesh.devti.util.parser.CodeFence
77
import com.intellij.lang.Language
8+
import com.intellij.openapi.actionSystem.*
89
import com.intellij.openapi.fileEditor.FileEditor
910
import com.intellij.openapi.fileEditor.FileEditorProvider
11+
import com.intellij.openapi.fileEditor.TextEditorWithPreview
12+
import com.intellij.openapi.fileEditor.TextEditorWithPreview.*
1013
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
1114
import com.intellij.openapi.project.Project
1215
import com.intellij.openapi.vfs.VfsUtilCore
1316
import com.intellij.openapi.vfs.VirtualFile
17+
import com.intellij.ui.dsl.builder.panel
1418
import javax.swing.JComponent
1519

1620
val editorWithPreviews: List<FileEditorProvider> =
1721
FileEditorProvider.EP_FILE_EDITOR_PROVIDER.extensionList.filter {
1822
it.javaClass.simpleName.contains("Preview")
1923
}
24+
2025
/**
2126
* @param withPreviewEditorId means a editor extends from [com.intellij.openapi.fileEditor.TextEditorWithPreview]
2227
*/
@@ -25,11 +30,16 @@ abstract class FileEditorPreviewSketch(
2530
val virtualFile: VirtualFile,
2631
val withPreviewEditorId: String
2732
) : ExtensionLangSketch {
28-
open val editor: FileEditor = getEditorProvider().createEditor(project, virtualFile)
33+
init {
34+
virtualFile.putUserData(DEFAULT_LAYOUT_FOR_FILE, Layout.SHOW_EDITOR_AND_PREVIEW)
35+
}
36+
37+
val editorProvider = buildEditorProvider()
38+
open val editor: FileEditor = editorProvider.createEditor(project, virtualFile)
2939

3040
open val mainPanel = editor.component
3141

32-
fun getEditorProvider(): FileEditorProvider =
42+
fun buildEditorProvider(): FileEditorProvider =
3343
FileEditorProvider.EP_FILE_EDITOR_PROVIDER.extensionList.firstOrNull {
3444
it.javaClass.simpleName == withPreviewEditorId
3545
} ?: TextEditorProvider.getInstance()
@@ -44,6 +54,24 @@ abstract class FileEditorPreviewSketch(
4454
return VfsUtilCore.loadText(this)
4555
}
4656

57+
// protected fun createRightToolbar(target: JComponent): ActionToolbar {
58+
// val rightToolbar = ActionManager.getInstance()
59+
// .createActionToolbar(ActionPlaces.TEXT_EDITOR_WITH_PREVIEW, createViewActionGroup(), true)
60+
// rightToolbar.targetComponent = target
61+
// rightToolbar.isReservePlaceAutoPopupIcon = false
62+
//
63+
// return rightToolbar
64+
// }
65+
//
66+
// protected open fun createViewActionGroup(): ActionGroup {
67+
// val actionManager = ActionManager.getInstance()
68+
// return DefaultActionGroup(
69+
// actionManager.getAction("TextEditorWithPreview.Layout.EditorOnly"),
70+
// actionManager.getAction("TextEditorWithPreview.Layout.EditorAndPreview"),
71+
// actionManager.getAction("TextEditorWithPreview.Layout.PreviewOnly")
72+
// )
73+
// }
74+
4775
override fun updateLanguage(language: Language?, originLanguage: String?) {}
4876
override fun dispose() {}
4977

core/src/main/resources/icons/insert-code.svg

Lines changed: 1 addition & 1 deletion
Loading

exts/ext-http-client/src/main/kotlin/cc/unitmesh/httpclient/IntellijHttpClientExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class IntellijHttpClientExecutor : HttpClientProvider {
2222

2323
// create temporary file for http request
2424
ScratchFileService.getInstance()
25+
2526
val scratchFile = ScratchRootType.getInstance()
2627
.createScratchFile(project, "autodev-http-request.http", originFile.language, text) ?: return
27-
2828
val psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: return
2929

3030
val runner: RunnerAndConfigurationSettings = ConfigurationContext(psiFile)

goland/src/main/kotlin/cc/unitmesh/go/provider/GoLangPlaygroundSketchProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ class GoLangPlaygroundSketch(val myProject: Project, val content: String, val fi
6060

6161
return if (editor is TextEditor) {
6262
val viewer = editorFactory.createViewer(
63-
editorFactory.createDocument("" as CharSequence),
63+
editorFactory.createDocument(""),
6464
project,
6565
EditorKind.PREVIEW
6666
)
6767
val previewEditor = textEditorProvider.getTextEditor(viewer)
6868

69-
Disposer.register(editor as Disposable, Disposable {
69+
Disposer.register(editor, Disposable {
7070
EditorFactory.getInstance().releaseEditor(viewer)
7171
})
7272

0 commit comments

Comments
 (0)