Skip to content

Commit

Permalink
feat(completion): improve file reference completion support #101
Browse files Browse the repository at this point in the history
The completion system for the DevInT language has been enhanced to provide better support for file references. The previous approach, which used a regex-based pattern matching, has been replaced with a more robust and specific pattern that captures file references. This change ensures that completion suggestions are more accurate and relevant to the user's context. Additionally, a new provider class, `FileReferenceLanguageProvider`, has been introduced to handle the completion of file references. This class is responsible for providing a list of possible file names based on a predefined list, which is a common scenario in many programming languages. The new provider is more efficient and easier to maintain, as it separates the logic for file reference completion from the general completion logic.
  • Loading branch information
phodal committed Mar 14, 2024
1 parent a3b37a1 commit 1d2e422
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Expand Up @@ -17,7 +17,11 @@ class DevInCompletionContributor : CompletionContributor() {
extend(CompletionType.BASIC, PlatformPatterns.psiElement(DevInTypes.LANGUAGE_ID), CodeFenceLanguageProvider())
extend(CompletionType.BASIC, PlatformPatterns.psiElement(DevInTypes.VARIABLE_ID), CustomVariableProvider())
extend(CompletionType.BASIC, PlatformPatterns.psiElement(DevInTypes.AGENT_ID), BuiltinAgentProvider())
extend(CompletionType.BASIC, filePattern(), CodeFenceLanguageProvider())
extend(
CompletionType.BASIC,
valuePattern(FileReferenceLanguageProvider.REF_TYPE),
FileReferenceLanguageProvider()
)
}

override fun beforeCompletion(context: CompletionInitializationContext) {
Expand All @@ -34,8 +38,11 @@ class DevInCompletionContributor : CompletionContributor() {
PlatformPatterns.psiElement()
.inside(psiElement<DevInUsed>())

private fun filePattern(): PsiElementPattern.Capture<PsiElement> =
private fun valuePattern(text: String): PsiElementPattern.Capture<PsiElement> =
baseUsedPattern()
.withElementType(DevInTypes.COLON)

.withElementType(DevInTypes.PROPERTY_VALUE)
.afterLeafSkipping(
PlatformPatterns.psiElement(DevInTypes.COLON),
PlatformPatterns.psiElement().withText(text)
)
}
@@ -0,0 +1,28 @@
package cc.unitmesh.devti.language.completion

import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.util.ProcessingContext

class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>() {
companion object {
const val REF_TYPE = "file"
}

override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet,
) {
// sample file: "file1", "file2"
listOf("file1", "file2").forEach {
result.addElement(
LookupElementBuilder.create(it)
.withTypeText("file", true)
)
}
}

}

0 comments on commit 1d2e422

Please sign in to comment.