Skip to content

Commit

Permalink
feat(runner): add support for custom editor in ShireRunner #178
Browse files Browse the repository at this point in the history
- Expose `createCodeViewerEditor` as a public function.
- Add `sampleEditor` parameter to `ShireRunner` methods to allow custom editor usage.
- Update `ShirePreviewEditor` to include a sample editor for variable testing.
  • Loading branch information
phodal committed Jan 4, 2025
1 parent f3aa418 commit dd139d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CodeHighlightSketch(val project: Project, val text: String, private var id
companion object {
private val LINE_NO_REGEX = Regex("^\\d+:")

private fun createCodeViewerEditor(
fun createCodeViewerEditor(
project: Project,
text: String,
ideaLanguage: Language?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.phodal.shirelang.editor

import com.intellij.icons.AllIcons
import com.intellij.lang.Language
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ScrollType
Expand All @@ -11,22 +15,24 @@ import com.intellij.openapi.util.UserDataHolderBase
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.readText
import com.intellij.psi.PsiManager
import com.intellij.ui.JBColor
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import com.phodal.shirecore.sketch.highlight.CodeHighlightSketch
import com.phodal.shirecore.sketch.highlight.EditorFragment
import com.phodal.shirelang.psi.ShireFile
import com.phodal.shirelang.run.runner.ShireRunner
import com.phodal.shirelang.run.runner.ShireRunnerContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import org.intellij.plugins.markdown.lang.MarkdownLanguage
import java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.beans.PropertyChangeListener
import javax.swing.AbstractAction
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.ScrollPaneConstants
Expand All @@ -51,8 +57,18 @@ open class ShirePreviewEditor(
private val variablePanel = ShireVariablePanel()

private var highlightSketch: CodeHighlightSketch? = null
private var sampleEditor: Editor? = null

private val javaHelloWorld = """
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
""".trimIndent()

init {
val javaLanguage = Language.findLanguageByID("JAVA")
val corePanel = panel {
row {
val label = JBLabel("Shire Preview (Experimental)").apply {
Expand All @@ -64,6 +80,30 @@ open class ShirePreviewEditor(

cell(label).align(Align.FILL).resizableColumn()
}
if (javaLanguage != null) {
row {
cell(JBLabel("Sample File For Variable")).align(Align.FILL).resizableColumn()
cell(JBLabel("shire.java")).align(Align.FILL).resizableColumn()
// add refresh button for select
icon(AllIcons.Actions.Refresh)
button("Refresh", object : AnAction() {
override fun actionPerformed(p0: AnActionEvent) {
rerenderShire()
}
})
}
row {
val editor = CodeHighlightSketch.createCodeViewerEditor(
project,
javaHelloWorld,
javaLanguage,
this@ShirePreviewEditor
)
val editorFragment = EditorFragment(editor)
this@ShirePreviewEditor.sampleEditor = editor
cell(editorFragment.getContent()).align(Align.FILL).resizableColumn()
}
}
row {
cell(variablePanel).align(Align.FILL)
}
Expand All @@ -85,7 +125,7 @@ open class ShirePreviewEditor(
try {
val psiFile = PsiManager.getInstance(project).findFile(virtualFile) as? ShireFile ?: return@invokeLater
shireRunnerContext = runBlocking {
ShireRunner.compileOnly(project, psiFile, mapOf())
ShireRunner.compileOnly(project, psiFile, mapOf(), sampleEditor)
}

val variables = shireRunnerContext?.compiledVariables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ShireActionPromptBuilder : ShirePromptBuilder {

val initVariables = mapOf("chatPrompt" to originPrompt)
val finalPrompt = runBlocking {
ShireRunner.compileOnly(project, action.shireFile, initVariables)
ShireRunner.compileOnly(project, action.shireFile, initVariables, null)
}.finalPrompt

return finalPrompt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.phodal.shirelang.run.runner
import com.intellij.execution.console.ConsoleViewWrapperBase
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
Expand Down Expand Up @@ -41,7 +42,7 @@ class ShireRunner(
private val variableMap: Map<String, String>,
private val processHandler: ShireProcessHandler,
) {
private var compiledVariables: Map<String, Any> = mapOf()
private var `compiledVariables`: Map<String, Any> = mapOf()
private val terminalLocationExecutor = TerminalLocationExecutor.provide(project)

private var isCanceled: Boolean = false
Expand Down Expand Up @@ -212,21 +213,30 @@ class ShireRunner(
project: Project,
shireFile: ShireFile,
initVariables: Map<String, String>,
sampleEditor: Editor?,
): ShireRunnerContext {
val parsedResult = preAnalysisAndLocalExecute(shireFile, project)
prepareExecute(parsedResult, initVariables, project, null)
val parsedResult = preAnalysisAndLocalExecute(shireFile, project, sampleEditor)
prepareExecute(parsedResult, initVariables, project, null, userEditor = sampleEditor)

val variables = variableFromPostProcessorContext(initVariables)
val runnerContext = processTemplateCompile(parsedResult, variables, project, null, null)
val runnerContext = processTemplateCompile(parsedResult, variables, project, null, null,
userEditor = sampleEditor
)

val service = project.getService(OnStreamingService::class.java)
service?.onStart(project, runnerContext.finalPrompt)
return runnerContext

}

fun preAnalysisAndLocalExecute(shireFile: ShireFile, project: Project): ShireParsedResult {
val syntaxAnalyzer = ShireSyntaxAnalyzer(project, shireFile, ActionLocationEditor.defaultEditor(project))
fun preAnalysisAndLocalExecute(
shireFile: ShireFile,
project: Project,
editor: Editor? = null,
): ShireParsedResult {
val baseEditor = editor ?: ActionLocationEditor.defaultEditor(project)

val syntaxAnalyzer = ShireSyntaxAnalyzer(project, shireFile, baseEditor)
return syntaxAnalyzer.parseAndExecuteLocalCommand()
}

Expand All @@ -235,9 +245,10 @@ class ShireRunner(
variables: Map<String, Any>,
project: Project,
consoleView: ShireConsoleView?,
userEditor: Editor? = null,
): PostProcessorContext {
val hobbitHole = parsedResult.config
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val editor = userEditor ?: FileEditorManager.getInstance(project).selectedTextEditor
hobbitHole?.pickupElement(project, editor)

val file = runReadAction {
Expand Down Expand Up @@ -274,9 +285,10 @@ class ShireRunner(
project: Project,
shireConfiguration: ShireConfiguration?,
shireConsoleView: ShireConsoleView?,
userEditor: Editor? = null,
): ShireRunnerContext {
val hobbitHole = compileResult.config
val editor = ActionLocationEditor.provide(project, hobbitHole?.actionLocation)
val editor = userEditor ?: ActionLocationEditor.provide(project, hobbitHole?.actionLocation)

val templateCompiler =
ShireTemplateCompiler(
Expand Down

0 comments on commit dd139d2

Please sign in to comment.