Skip to content

Commit

Permalink
feat(ui): implement LangSketch and add PlantUML support #153
Browse files Browse the repository at this point in the history
Improve the UI by implementing the LangSketch interface and integrating PlantUML support. This includes:

- Enhancing the SingleFileDiffView class to implement LangSketch.
- Introducing LanguageSketchProvider for dynamic language sketch provision.
- Adding a PlantUmlSketchProvider and PlantUmlSketch class for PlantUML file rendering within the IDE.
  • Loading branch information
phodal committed Dec 10, 2024
1 parent 55e33d0 commit 6aa5b75
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.phodal.shirecore.provider.sketch

import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.project.Project
import com.phodal.shirecore.ui.viewer.LangSketch

interface LanguageSketchProvider {
fun isSupported(lang: String): Boolean

fun createSketch(project: Project, content: String): LangSketch

companion object {
private val EP_NAME: ExtensionPointName<LanguageSketchProvider> =
ExtensionPointName("com.phodal.shireLangSketchProvider")

fun provide(language: String): LanguageSketchProvider? {
return EP_NAME.extensionList.firstOrNull {
it.isSupported(language)
}
}
}
}
76 changes: 53 additions & 23 deletions core/src/main/kotlin/com/phodal/shirecore/ui/ShirePanelView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.phodal.shirecore.ui.viewer.DiffLangSketch
import com.phodal.shirecore.ui.viewer.LangSketch
import com.phodal.shirecore.utils.markdown.CodeFence
import com.phodal.shirecore.utils.markdown.CodeFenceLanguage
import com.phodal.shirecore.provider.sketch.LanguageSketchProvider
import java.awt.BorderLayout
import javax.swing.*

Expand Down Expand Up @@ -91,36 +92,65 @@ class ShirePanelView(val project: Project) : SimpleToolWindowPanel(true, true),

fun onUpdate(text: String) {
val codeFenceList = CodeFence.parseAll(text)
codeFenceList.forEachIndexed { index, codeFence ->
if (index < blockViews.size) {
when (codeFence.originLanguage) {
"diff" -> {
if(codeFence.isComplete && blockViews[index] !is DiffLangSketch) {
blockViews[index] = DiffLangSketch(project, codeFence.text)
myList.remove(index)
myList.add(blockViews[index].getComponent(), index)
myList.revalidate() // 刷新布局
myList.repaint() // 重绘界面
} else {
blockViews[index].updateLanguage(codeFence.ideaLanguage)
blockViews[index].updateViewText(codeFence.text)

runInEdt {
codeFenceList.forEachIndexed { index, codeFence ->
if (index < blockViews.size) {
when (codeFence.originLanguage) {
"diff" -> {
if (codeFence.isComplete && blockViews[index] !is DiffLangSketch) {
val diffLangSketch = DiffLangSketch(project, codeFence.text)
val oldComponent = blockViews[index]

blockViews[index] = diffLangSketch
myList.remove(index)
myList.add(diffLangSketch.getComponent(), index)

oldComponent.dispose()
} else {
blockViews[index].apply {
updateLanguage(codeFence.ideaLanguage)
updateViewText(codeFence.text)
}
}
}
else -> {
var langSketch: LangSketch? = null
if (codeFence.originLanguage != null) {
langSketch = LanguageSketchProvider.provide(codeFence.originLanguage)
?.createSketch(project, codeFence.text)
}

if (langSketch != null) {
blockViews[index] = langSketch
myList.remove(index)
myList.add(langSketch.getComponent(), index)
} else {
blockViews[index].apply {
updateLanguage(codeFence.ideaLanguage)
updateViewText(codeFence.text)
}
}
}
}
else -> {
blockViews[index].updateLanguage(codeFence.ideaLanguage)
blockViews[index].updateViewText(codeFence.text)
}
}
} else {
runInEdt {
} else {
val codeBlockViewer = CodeHighlightSketch(project, codeFence.text, PlainTextLanguage.INSTANCE)
blockViews.add(codeBlockViewer)
myList.add(codeBlockViewer)
myList.add(codeBlockViewer.getComponent())
}
}
}

scrollToBottom()
while (blockViews.size > codeFenceList.size) {
val lastIndex = blockViews.lastIndex
blockViews.removeAt(lastIndex)
myList.remove(lastIndex)
}

myList.revalidate()
myList.repaint()

scrollToBottom()
}
}

fun onFinish(text: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.phodal.shirecore.ui.viewer.patch

import com.intellij.icons.AllIcons
import com.intellij.lang.Language
import com.intellij.openapi.command.undo.UndoManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
Expand All @@ -12,13 +13,14 @@ import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.JBUI
import com.phodal.shirecore.ShireCoreBundle
import com.phodal.shirecore.ui.viewer.LangSketch
import java.awt.BorderLayout
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.*


class SingleFileDiffView(private val myProject: Project, private val filepath: VirtualFile) {
class SingleFileDiffView(private val myProject: Project, private val filepath: VirtualFile) : LangSketch {
private val mainPanel: JPanel = JPanel(VerticalLayout(5))
private val myHeaderPanel: JPanel = JPanel(BorderLayout())

Expand Down Expand Up @@ -99,7 +101,23 @@ class SingleFileDiffView(private val myProject: Project, private val filepath: V
return listOf(rollback)
}

fun getComponent(): JComponent {
override fun getViewText(): String {
return ""
}

override fun updateViewText(text: String) {
// do nothing
}

override fun getComponent(): JComponent {
return mainPanel
}

override fun updateLanguage(language: Language?) {

}

override fun dispose() {

}
}
6 changes: 6 additions & 0 deletions core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
<with attribute="implementationClass"
implements="com.phodal.shirecore.provider.complexity.ComplexityProvider"/>
</extensionPoint>

<!-- Lang Sketch Provider -->
<extensionPoint qualifiedName="com.phodal.shireLangSketchProvider"
interface="com.phodal.shirecore.provider.sketch.LanguageSketchProvider"
dynamic="true">
</extensionPoint>
</extensionPoints>

<extensions defaultExtensionNs="JavaScript.JsonSchema">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.phodal.shire.plantuml

import com.intellij.lang.Language
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.dsl.builder.panel
import com.phodal.shirecore.provider.sketch.LanguageSketchProvider
import com.phodal.shirecore.ui.viewer.LangSketch
import org.plantuml.idea.preview.editor.PlantUmlPreviewEditor
import org.plantuml.idea.preview.PlantUmlPreviewPanel
import javax.swing.JComponent

class PlantUmlSketchProvider : LanguageSketchProvider {
override fun isSupported(lang: String): Boolean {
return lang == "plantuml" || lang == "puml" || lang == "uml"
}

override fun createSketch(project: Project, content: String): LangSketch {
val virtualFile = LightVirtualFile("plantuml.puml", content)
return PlantUmlSketch(project, virtualFile)
}
}

class PlantUmlSketch(private val project: Project, private val virtualFile: VirtualFile) : LangSketch {
private var mainPanel: JComponent

init {
val editor = PlantUmlPreviewEditor(virtualFile, project)
val plantUmlPreviewPanel = PlantUmlPreviewPanel(project, editor)

mainPanel = panel {
row {
plantUmlPreviewPanel.also {
it.isVisible = true
it.setSize(800, 600)
}
}
}
}

override fun getViewText(): String {
return virtualFile.inputStream.bufferedReader().use { it.readText() }
}

override fun updateViewText(text: String) {
virtualFile.setBinaryContent(text.toByteArray())
}

override fun getComponent(): JComponent {
return mainPanel!!
}

override fun updateLanguage(language: Language?) {
}

override fun dispose() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<shireLanguageToolchainProvider
language="PUML"
implementationClass="com.phodal.shire.plantuml.PlantUmlToolchainProvider"/>

<shireLangSketchProvider implementation="com.phodal.shire.plantuml.PlantUmlSketchProvider"/>
</extensions>
</idea-plugin>

0 comments on commit 6aa5b75

Please sign in to comment.