Skip to content

Commit

Permalink
fix(devins-lang): restrict agent_id to non-whitespace characters #101
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Mar 19, 2024
1 parent 4c8a49b commit 388d484
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion exts/devins-lang/src/grammar/DevInLexer.flex
Expand Up @@ -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]*
Expand Down
@@ -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)
}
}
Expand Up @@ -40,6 +40,7 @@
<lang.documentationProvider language="DevIn"
id="devinsDocumentationProvider"
implementationClass="cc.unitmesh.devti.language.documentation.DevInsDocumentationProvider"/>
<documentationProvider implementation="cc.unitmesh.devti.language.documentation.DevInsDocumentationProvider"/>
</extensions>

<actions>
Expand Down

0 comments on commit 388d484

Please sign in to comment.