Skip to content

Commit

Permalink
refactor(variable): move and consolidate variable-related models #183
Browse files Browse the repository at this point in the history
- Move `ContextVariable` to core module and extend `Variable` interface.
- Add companion object with `from` method to `ConditionPsiVariable` and `ContextVariable`.
- Rename `ShireVariablePanel` to `ShireVariableViewPanel` and update its table model to include descriptions.
- Update imports and references to reflect the new structure.
  • Loading branch information
phodal committed Jan 6, 2025
1 parent 5c9b0b7 commit 82750d6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ enum class ConditionPsiVariable(
FILE_NAME("fileName", "The name of the file"),
FILE_EXTENSION("fileExtension", "The extension of the file"),
FILE_CONTENT("fileContent", "The content of the file")
;

companion object {
fun from(variableName: String): ConditionPsiVariable? {
return values().find { it.variableName == variableName }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.phodal.shirelang.compiler.variable
package com.phodal.shirecore.provider.variable.model

enum class ContextVariable(val variableName: String, val description: String) {
enum class ContextVariable(
override val variableName: String,
override val description: String,
override var value: Any? = "",
) :
Variable {
SELECTION("selection", "User selection code/element's in text"),
SELECTION_WITH_NUM("selectionWithNum", "User selection code/element's in text with line number"),
BEFORE_CURSOR("beforeCursor", "All the text before the cursor"),
Expand All @@ -12,4 +17,10 @@ enum class ContextVariable(val variableName: String, val description: String) {
COMMENT_SYMBOL("commentSymbol", "The comment symbol of the language, for example, `//` in Java"),
ALL("all", "All the text")
;

companion object {
fun from(variableName: String): ContextVariable? {
return values().find { it.variableName == variableName }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.phodal.shirecore.provider.variable.model

import com.phodal.shirecore.provider.variable.model.toolchain.*

interface Variable {
val variableName: String
val description: String
Expand All @@ -10,4 +12,20 @@ data class DebugValue(
override val variableName: String,
override var value: Any?,
override val description: String,
) :Variable
) : Variable {
companion object {
fun description(key: String): String {
return PsiContextVariable.from(key)?.description
?: ContextVariable.from(key)?.description
?: SystemInfoVariable.from(key)?.description
?: ConditionPsiVariable.from(key)?.description
/// ...
?: DatabaseToolchainVariable.from(key)?.description
?: TerminalToolchainVariable.from(key)?.description
?: VcsToolchainVariable.from(key)?.description
?: GradleToolchainVariable.from(key)?.description
?: SonarqubeVariable.from(key)?.description
?: "Unknown"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.phodal.shirelang.compiler.variable

import com.phodal.shirecore.provider.variable.model.ContextVariable
import com.phodal.shirecore.provider.variable.model.PsiContextVariable
import com.phodal.shirecore.provider.variable.model.ToolchainVariable
import com.phodal.shirecore.provider.variable.model.toolchain.VcsToolchainVariable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.intellij.openapi.editor.CaretModel
import com.intellij.psi.PsiNameIdentifierOwner
import com.phodal.shirelang.compiler.variable.resolver.base.VariableResolver
import com.phodal.shirelang.compiler.variable.resolver.base.VariableResolverContext
import com.phodal.shirelang.compiler.variable.ContextVariable
import com.phodal.shirelang.compiler.variable.ContextVariable.*
import com.phodal.shirecore.provider.variable.model.ContextVariable
import com.phodal.shirecore.provider.variable.model.ContextVariable.*

class ContextVariableResolver(
private val context: VariableResolverContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ 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.application.smartReadAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorState
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.UserDataHolder
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.RoundedLineBorder
Expand Down Expand Up @@ -57,7 +56,7 @@ open class ShirePreviewEditor(
)

private var shireRunnerContext: ShireRunnerContext? = null
private val variablePanel = ShireVariablePanel()
private val variablePanel = ShireVariableViewPanel()

private var highlightSketch: CodeHighlightSketch? = null
private var sampleEditor: Editor? = null
Expand Down Expand Up @@ -169,28 +168,26 @@ open class ShirePreviewEditor(

fun updateDisplayedContent() {
ApplicationManager.getApplication().invokeLater {
DumbService.getInstance(project).smartInvokeLater {
ShireCoroutineScope.scope(project).launch {
try {
val psiFile = org.jetbrains.kotlin.asJava.classes.runReadAction {
PsiManager.getInstance(project).findFile(virtualFile) as? ShireFile
} ?: return@launch

shireRunnerContext = ShireRunner.compileOnly(project, psiFile, mapOf(), sampleEditor)

val variables = shireRunnerContext?.compiledVariables
if (variables != null) {
variablePanel.updateVariables(variables)
}
ShireCoroutineScope.scope(project).launch {
try {
val psiFile = smartReadAction(project) {
PsiManager.getInstance(project).findFile(virtualFile) as? ShireFile
} ?: return@launch

highlightSketch?.updateViewText(shireRunnerContext!!.finalPrompt)
highlightSketch?.repaint()
shireRunnerContext = ShireRunner.compileOnly(project, psiFile, mapOf(), sampleEditor)

mainPanel.revalidate()
mainPanel.repaint()
} catch (e: Exception) {
e.printStackTrace()
val variables = shireRunnerContext?.compiledVariables
if (variables != null) {
variablePanel.updateVariables(variables)
}

highlightSketch?.updateViewText(shireRunnerContext!!.finalPrompt)
highlightSketch?.repaint()

mainPanel.revalidate()
mainPanel.repaint()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import com.intellij.ui.components.JBPanel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.table.JBTable
import com.intellij.util.ui.JBUI
import com.phodal.shirecore.provider.variable.model.ConditionPsiVariable
import com.phodal.shirecore.provider.variable.model.DebugValue
import com.phodal.shirecore.provider.variable.model.PsiContextVariable
import com.phodal.shirecore.provider.variable.model.toolchain.DatabaseToolchainVariable
import java.awt.BorderLayout
import javax.swing.JPanel
import javax.swing.ScrollPaneConstants
import javax.swing.table.DefaultTableModel

class ShireVariablePanel : JPanel(BorderLayout()) {
class ShireVariableViewPanel : JPanel(BorderLayout()) {
private val contentPanel = JBPanel<JBPanel<*>>(BorderLayout())
private val tableModel = DefaultTableModel(arrayOf("Name", "Value"), 0).apply {
private val tableModel = DefaultTableModel(arrayOf("Name", "Description", "Value"), 0).apply {
background = JBColor.WHITE
}

Expand All @@ -24,7 +28,7 @@ class ShireVariablePanel : JPanel(BorderLayout()) {
setShowGrid(true)
gridColor = JBColor.PanelBackground
intercellSpacing = JBUI.size(0, 0)

val columnModel = columnModel
columnModel.getColumn(0).preferredWidth = 150
columnModel.getColumn(1).preferredWidth = 450
Expand Down Expand Up @@ -61,7 +65,13 @@ class ShireVariablePanel : JPanel(BorderLayout()) {

variables.forEach { (key, value) ->
val valueStr = value.toString()
tableModel.addRow(arrayOf(key, valueStr))
val description = DebugValue.description(key)

tableModel.addRow(java.util.Vector<String>().apply {
add(key)
add(description)
add(valueStr)
})
}

revalidate()
Expand Down

0 comments on commit 82750d6

Please sign in to comment.