From 388d484bede47d5dfcb6a32687c6d06971d8a46e Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 19 Mar 2024 22:13:22 +0800 Subject: [PATCH] fix(devins-lang): restrict agent_id to non-whitespace characters #101 The regex pattern for agent_id in DevInLexer.flex has been updated to only allow non-whitespace characters, ensuring that agent IDs are syntactically valid and do not include spaces or tabs. This change aligns with the language specification and improves the parsing and validation of agent IDs in the DevInLang grammar. Additionally, the DevInsDocumentationProvider class has been refactored to extend AbstractDocumentationProvider instead of DocumentationProvider directly, and it now handles documentation lookup for AGENT_ID and COMMAND_ID elements. The class now imports the necessary packages for interacting with agent configurations and built-in commands, and it provides a more detailed documentation string for each element type. This refactoring enhances the documentation capabilities of the language plugin and improves the developer experience. --- exts/devins-lang/src/grammar/DevInLexer.flex | 2 +- .../DevInsDocumentationProvider.kt | 41 +++++++++++++++++-- .../resources/cc.unitmesh.devti.language.xml | 1 + 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/exts/devins-lang/src/grammar/DevInLexer.flex b/exts/devins-lang/src/grammar/DevInLexer.flex index b05ad690e0..e3afeb7cc8 100644 --- a/exts/devins-lang/src/grammar/DevInLexer.flex +++ b/exts/devins-lang/src/grammar/DevInLexer.flex @@ -37,7 +37,7 @@ import com.intellij.psi.TokenType; IDENTIFIER=[a-zA-Z0-9][_\-a-zA-Z0-9]* VARIABLE_ID=[a-zA-Z0-9][_\-a-zA-Z0-9]* -AGENT_ID=[a-zA-Z0-9][_\-a-zA-Z0-9\s]* +AGENT_ID=[a-zA-Z0-9][_\-a-zA-Z0-9]* COMMAND_ID=[a-zA-Z0-9][_\-a-zA-Z0-9]* LANGUAGE_ID=[a-zA-Z][_\-a-zA-Z0-9 .]* SYSTEM_ID=[a-zA-Z][_\-a-zA-Z0-9]* diff --git a/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/documentation/DevInsDocumentationProvider.kt b/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/documentation/DevInsDocumentationProvider.kt index d79522b838..7318a302df 100644 --- a/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/documentation/DevInsDocumentationProvider.kt +++ b/exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/documentation/DevInsDocumentationProvider.kt @@ -1,10 +1,43 @@ package cc.unitmesh.devti.language.documentation -import com.intellij.lang.documentation.DocumentationProvider +import cc.unitmesh.devti.agent.configurable.loadAgentConfigs +import cc.unitmesh.devti.language.completion.BuiltinCommand +import cc.unitmesh.devti.language.psi.DevInTypes +import com.intellij.lang.documentation.AbstractDocumentationProvider +import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.util.elementType -class DevInsDocumentationProvider : DocumentationProvider { - override fun getQuickNavigateInfo(element: PsiElement?, originalElement: PsiElement?): String? { - return super.getQuickNavigateInfo(element, originalElement) +class DevInsDocumentationProvider : AbstractDocumentationProvider() { + override fun generateDoc(element: PsiElement?, originalElement: PsiElement?): String? { + val project = element?.project ?: return null + return when (element.elementType) { + DevInTypes.AGENT_ID -> { + val agentConfigs = loadAgentConfigs(project).filter { + it.name == element.text + } + + if (agentConfigs.isEmpty()) return null + agentConfigs.joinToString("\n") { it.description } + } + + DevInTypes.COMMAND_ID -> { + BuiltinCommand.all().find { it.commandName == element.text }?.description + } + + else -> { + element.text + } + } + } + + override fun getCustomDocumentationElement( + editor: Editor, + file: PsiFile, + contextElement: PsiElement?, + targetOffset: Int + ): PsiElement? { + return contextElement ?: file.findElementAt(targetOffset) } } diff --git a/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml b/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml index cc58cad170..f7d784bf1d 100644 --- a/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml +++ b/exts/devins-lang/src/main/resources/cc.unitmesh.devti.language.xml @@ -40,6 +40,7 @@ +