From dd843d4cff7631872f381457dd9921d799c6768c Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 9 Jul 2024 13:25:02 +0800 Subject: [PATCH] refactor(parser): replace FrontmatterParser with HobbitHoleParser #36 - Update references to FrontmatterParser with HobbitHoleParser for consistency and clarity in codebase. --- .shire/docs/hobbit-hole.shire | 19 +++++++++++++++++ .../shirelang/ShireActionStartupActivity.kt | 4 ++-- .../actions/base/DynamicShireActionConfig.kt | 4 ++-- .../shirelang/compiler/ShireSyntaxAnalyzer.kt | 6 +++--- .../shirelang/compiler/hobbit/HobbitHole.kt | 21 +++++++++++++++---- ...ontmatterParser.kt => HobbitHoleParser.kt} | 4 ++-- .../compiler/parser/ShireAstQLParser.kt | 4 ++-- .../com/phodal/shirelang/ShireCompileTest.kt | 4 ++-- 8 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 .shire/docs/hobbit-hole.shire rename shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/{FrontmatterParser.kt => HobbitHoleParser.kt} (99%) diff --git a/.shire/docs/hobbit-hole.shire b/.shire/docs/hobbit-hole.shire new file mode 100644 index 000000000..ee920b0bd --- /dev/null +++ b/.shire/docs/hobbit-hole.shire @@ -0,0 +1,19 @@ +--- +name: "Context Variable" +description: "Here is a description of the action." +interaction: RunPanel +variables: + "currentCode": /ShireCompileTest\.kt/ { cat } + "currentDoc": /shire-hobbit-hole.md/ { cat } +--- + +根据如下的代码用例,更新文档中的用例: + +代码用例如下: + +$currentCode + +现有文档如下: + +$currentDoc + diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/ShireActionStartupActivity.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/ShireActionStartupActivity.kt index b87d48b1d..171a43adf 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/ShireActionStartupActivity.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/ShireActionStartupActivity.kt @@ -12,7 +12,7 @@ import com.intellij.psi.search.FileTypeIndex import com.intellij.psi.search.GlobalSearchScope import com.phodal.shirelang.actions.base.DynamicShireActionConfig import com.phodal.shirelang.actions.base.DynamicShireActionService -import com.phodal.shirelang.compiler.parser.FrontmatterParser +import com.phodal.shirelang.compiler.parser.HobbitHoleParser import com.phodal.shirelang.psi.ShireFile @@ -27,7 +27,7 @@ class ShireActionStartupActivity : ProjectActivity { smartReadAction(project) { obtainShireFiles(project).forEach { val shireConfig = try { - FrontmatterParser.parse(it) + HobbitHoleParser.parse(it) } catch (e: Exception) { logger.warn("parse shire config error for file: ${it.virtualFile.path}", e) return@forEach diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/DynamicShireActionConfig.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/DynamicShireActionConfig.kt index f53a39ff0..c5a9e689b 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/DynamicShireActionConfig.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/DynamicShireActionConfig.kt @@ -1,7 +1,7 @@ package com.phodal.shirelang.actions.base import com.intellij.openapi.editor.Editor -import com.phodal.shirelang.compiler.parser.FrontmatterParser +import com.phodal.shirelang.compiler.parser.HobbitHoleParser import com.phodal.shirelang.compiler.hobbit.HobbitHole import com.phodal.shirelang.psi.ShireFile @@ -13,7 +13,7 @@ data class DynamicShireActionConfig( ) { companion object { fun from(file: ShireFile): DynamicShireActionConfig { - val hole = FrontmatterParser.parse(file) + val hole = HobbitHoleParser.parse(file) return DynamicShireActionConfig(file.name, hole, file) } } diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/ShireSyntaxAnalyzer.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/ShireSyntaxAnalyzer.kt index 11191219e..183a37ec6 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/ShireSyntaxAnalyzer.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/ShireSyntaxAnalyzer.kt @@ -14,7 +14,7 @@ import com.intellij.psi.util.elementType import com.phodal.shirecore.agent.CustomAgent import com.phodal.shirelang.compile.VariableTemplateCompiler import com.phodal.shirelang.compiler.exec.* -import com.phodal.shirelang.compiler.parser.FrontmatterParser +import com.phodal.shirelang.compiler.parser.HobbitHoleParser import com.phodal.shirelang.compiler.variable.VariableTable import com.phodal.shirelang.completion.dataprovider.BuiltinCommand import com.phodal.shirelang.completion.dataprovider.CustomCommand @@ -93,11 +93,11 @@ class ShireSyntaxAnalyzer( val nextElement = PsiTreeUtil.findChildOfType( psiElement.parent, ShireFrontMatterHeader::class.java ) ?: continue - result.config = FrontmatterParser.parse(nextElement) + result.config = HobbitHoleParser.parse(nextElement) } ShireTypes.FRONT_MATTER_HEADER -> { - result.config = FrontmatterParser.parse(psiElement as ShireFrontMatterHeader) + result.config = HobbitHoleParser.parse(psiElement as ShireFrontMatterHeader) } WHITE_SPACE, DUMMY_BLOCK -> output.append(psiElement.text) diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/HobbitHole.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/HobbitHole.kt index 18ba6a6e4..d238c6e65 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/HobbitHole.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/HobbitHole.kt @@ -14,7 +14,7 @@ import com.phodal.shirecore.middleware.PostProcessor import com.phodal.shirecore.middleware.PostProcessorNode import com.phodal.shirecore.middleware.select.SelectElementStrategy import com.phodal.shirecore.middleware.select.SelectedEntry -import com.phodal.shirelang.compiler.parser.FrontmatterParser +import com.phodal.shirelang.compiler.parser.HobbitHoleParser import com.phodal.shirelang.compiler.hobbit.base.Smials import com.phodal.shirelang.compiler.hobbit.ast.FrontMatterType import com.phodal.shirelang.compiler.hobbit.ast.MethodCall @@ -25,8 +25,21 @@ import com.phodal.shirelang.compiler.patternaction.PatternActionTransform import com.phodal.shirelang.psi.ShireFile /** - * - Normal: the action is a normal action - * - Flow: each action can be a task in a flow, which will build a DAG + * Hobbit Hole 用于定义 IDE 交互逻辑与用户数据的流处理。 + * 示例 + * ```shire + * --- + * name: "Summary" + * description: "Generate Summary" + * interaction: AppendCursor + * actionLocation: ContextMenu + * --- + * ``` + * + * 完整示例如下: + * ```shire + * + * ``` */ open class HobbitHole( /** @@ -150,7 +163,7 @@ open class HobbitHole( const val SHORTCUT = "shortcut" fun from(file: ShireFile): HobbitHole? { - return FrontmatterParser.parse(file) + return HobbitHoleParser.parse(file) } /** diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/FrontmatterParser.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/HobbitHoleParser.kt similarity index 99% rename from shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/FrontmatterParser.kt rename to shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/HobbitHoleParser.kt index 486f808ee..5325f7e62 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/FrontmatterParser.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/HobbitHoleParser.kt @@ -11,8 +11,8 @@ import com.phodal.shirelang.compiler.hobbit.ast.* import com.phodal.shirelang.compiler.patternaction.PatternActionFunc import com.phodal.shirelang.psi.* -object FrontmatterParser { - private val logger = logger() +object HobbitHoleParser { + private val logger = logger() fun hasFrontMatter(file: ShireFile): Boolean { return PsiTreeUtil.getChildrenOfTypeAsList(file, ShireFrontMatterHeader::class.java).isNotEmpty() diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/ShireAstQLParser.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/ShireAstQLParser.kt index 673228537..d91d9e318 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/ShireAstQLParser.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/parser/ShireAstQLParser.kt @@ -24,12 +24,12 @@ object ShireAstQLParser { } private fun parseWhere(whereClause: ShireWhereClause): Statement? { - return FrontmatterParser.parseExpr(whereClause.expr) + return HobbitHoleParser.parseExpr(whereClause.expr) } private fun parseSelect(selectClause: ShireSelectClause): List { return selectClause.exprList.mapNotNull { - FrontmatterParser.parseExpr(it) + HobbitHoleParser.parseExpr(it) } } } diff --git a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireCompileTest.kt b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireCompileTest.kt index 95d54275a..c537e79a6 100644 --- a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireCompileTest.kt +++ b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireCompileTest.kt @@ -3,7 +3,7 @@ package com.phodal.shirelang import com.intellij.testFramework.fixtures.BasePlatformTestCase import com.phodal.shirecore.config.ShireActionLocation import com.phodal.shirecore.config.InteractionType -import com.phodal.shirelang.compiler.parser.FrontmatterParser +import com.phodal.shirelang.compiler.parser.HobbitHoleParser import com.phodal.shirelang.compiler.ShireSyntaxAnalyzer import com.phodal.shirelang.compiler.hobbit.ast.LogicalExpression import com.phodal.shirelang.compiler.patternaction.PatternActionFunc @@ -80,7 +80,7 @@ class ShireCompileTest : BasePlatformTestCase() { val file = myFixture.configureByText("test.shire", code) - val isFrontMatterPresent = FrontmatterParser.hasFrontMatter(file as ShireFile) + val isFrontMatterPresent = HobbitHoleParser.hasFrontMatter(file as ShireFile) assertTrue(isFrontMatterPresent) }