Skip to content

Commit

Permalink
feat(devins-lang): add documentation provider and refactor custom age…
Browse files Browse the repository at this point in the history
…nt completion

#101

The commit adds a new documentation provider for the DevIn language, which improves the user experience by providing quick navigation information for PsiElements. Additionally, the commit refactors the CustomAgentCompletion class to use the new documentation provider and fixes an issue with the import statements. The changes also include modifications to the plugin.xml file to register the documentation provider and a refactoring of the loadAgentConfigs function to use the new documentation provider.
  • Loading branch information
phodal committed Mar 19, 2024
1 parent 8e46a8a commit 4c8a49b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
Expand Up @@ -8,8 +8,5 @@ import com.intellij.openapi.vfs.VirtualFileManager
fun Project.lookupFile(path: String): VirtualFile? {
val projectPath = this.guessProjectDir()?.toNioPath()
val realpath = projectPath?.resolve(path)

val virtualFile =
VirtualFileManager.getInstance().findFileByUrl("file://${realpath?.toAbsolutePath()}")
return virtualFile
return VirtualFileManager.getInstance().findFileByUrl("file://${realpath?.toAbsolutePath()}")
}
@@ -1,29 +1,20 @@
package cc.unitmesh.devti.language.completion

import cc.unitmesh.devti.agent.configurable.customAgentSetting
import cc.unitmesh.devti.agent.configurable.loadAgentConfigs
import cc.unitmesh.devti.agent.model.CustomAgentConfig
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
import kotlinx.serialization.json.Json
// DON'T CHANGE THIS IMPORT
import kotlinx.serialization.decodeFromString

class CustomAgentCompletion : CompletionProvider<CompletionParameters>() {
override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
result: CompletionResultSet,
) {
val project = parameters.originalFile.project
val configs: List<CustomAgentConfig> = try {
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig
Json.decodeFromString(ragsJsonConfig)
} catch (e: Exception) {
emptyList()
}
val configs: List<CustomAgentConfig> = loadAgentConfigs(parameters.originalFile.project)

configs.forEach {
result.addElement(
Expand Down
Expand Up @@ -56,7 +56,6 @@ class FileReferenceLanguageProvider : CompletionProvider<CompletionParameters>()

fun canBeAdded(file: VirtualFile): Boolean {
if (!file.isValid || file.isDirectory) return false

if (file.fileType.isBinary || FileUtilRt.isTooLarge(file.length)) return false

return true
Expand Down
@@ -0,0 +1,10 @@
package cc.unitmesh.devti.language.documentation

import com.intellij.lang.documentation.DocumentationProvider
import com.intellij.psi.PsiElement

class DevInsDocumentationProvider : DocumentationProvider {
override fun getQuickNavigateInfo(element: PsiElement?, originalElement: PsiElement?): String? {
return super.getQuickNavigateInfo(element, originalElement)
}
}
Expand Up @@ -37,6 +37,9 @@
<runLineMarkerContributor language="DevIn"
implementationClass="cc.unitmesh.devti.language.run.DevInsRunLineMarkersProvider"/>

<lang.documentationProvider language="DevIn"
id="devinsDocumentationProvider"
implementationClass="cc.unitmesh.devti.language.documentation.DevInsDocumentationProvider"/>
</extensions>

<actions>
Expand All @@ -46,6 +49,6 @@
</actions>

<extensions defaultExtensionNs="cc.unitmesh">
<customAgentResponse implementation="cc.unitmesh.devti.language.provider.DevInsCustomAgentResponse" />
<customAgentResponse implementation="cc.unitmesh.devti.language.provider.DevInsCustomAgentResponse"/>
</extensions>
</idea-plugin>
@@ -1,11 +1,25 @@
package cc.unitmesh.devti.agent.configurable

import cc.unitmesh.devti.agent.model.CustomAgentConfig
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import kotlinx.serialization.json.Json
// DON'T CHANGE THIS IMPORT
import kotlinx.serialization.decodeFromString

val Project.customAgentSetting: CoUnitProjectSettingsService
get() = service<CoUnitProjectSettingsService>()

fun loadAgentConfigs(project: Project): List<CustomAgentConfig> {
val configs: List<CustomAgentConfig> = try {
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig
Json.decodeFromString(ragsJsonConfig)
} catch (e: Exception) {
emptyList()
}
return configs
}

@Service(Service.Level.PROJECT)
@State(name = "CoUnitProjectSettings", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)])
class CoUnitProjectSettingsService(
Expand Down

0 comments on commit 4c8a49b

Please sign in to comment.