Skip to content

Commit

Permalink
feat(completion): add support for built-in agent completion #101
Browse files Browse the repository at this point in the history
This commit introduces a new completion provider for built-in agents in the DevInLanguage. It extends the completion type to include psi elements for agent identifiers and provides a list of built-in agents along with their descriptions. The completion provider also adds a custom insert handler to insert a colon after the agent name is selected, allowing for the specification of agent parameters.
  • Loading branch information
phodal committed Mar 14, 2024
1 parent 632372d commit a09cd0f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
@@ -0,0 +1,43 @@
package cc.unitmesh.devti.language.completion

import cc.unitmesh.devti.language.DevInIcons
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.util.ProcessingContext

enum class BuiltinAgent(val agentName: String, val description: String) {
FILE("file", "Read the content of a file"),
REV("rev", "Read git revision of a file"),
SYMBOL("symbol", "Read content by Java/Kotlin canonicalName"),

;

companion object {
fun all(): List<BuiltinAgent> {
return values().toList()
}
}
}

class BuiltinAgentProvider : CompletionProvider<CompletionParameters>() {
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet,
) {
val builtinAgents = BuiltinAgent.all()
builtinAgents.forEach {
val withTypeText = LookupElementBuilder.create(it.agentName)
.withIcon(DevInIcons.DEFAULT)
.withTypeText(it.description, true)
.withInsertHandler { context, _ ->
context.document.insertString(context.tailOffset, ":")
context.editor.caretModel.moveCaretRelatively(0, 1, false, false, false)
}

result.addElement(withTypeText)
}
}
}

Expand Up @@ -13,6 +13,7 @@ class DevInCompletionContributor : CompletionContributor() {
init {
extend(CompletionType.BASIC, psiElement(DevInTypes.LANGUAGE_ID), CodeFenceLanguageProvider())
extend(CompletionType.BASIC, psiElement(DevInTypes.VARIABLE_ID), CustomVariableProvider())
extend(CompletionType.BASIC, psiElement(DevInTypes.AGENT_ID), BuiltinAgentProvider())
}

override fun beforeCompletion(context: CompletionInitializationContext) {
Expand Down

0 comments on commit a09cd0f

Please sign in to comment.