Skip to content

Commit

Permalink
feat: add clion context as example
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 23, 2023
1 parent f74f094 commit 4489358
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
Empty file.
@@ -0,0 +1,91 @@
package cc.unitmesh.cpp.provider

import cc.unitmesh.devti.provider.context.ChatContextItem
import cc.unitmesh.devti.provider.context.ChatContextProvider
import cc.unitmesh.devti.provider.context.ChatCreationContext
import com.intellij.execution.wsl.WslPath
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.jetbrains.cidr.project.workspace.CidrWorkspace

class CLionWorkspaceContextProvider : ChatContextProvider {
private val configFiles = listOf(
"CMakeLists.txt", "meson.build", "Makefile", "ninja.build",
"vcpkg.json", "BUILD", "sln", "vcxproj", "vcproj"
)

override fun isApplicable(project: Project, creationContext: ChatCreationContext): Boolean {
return true
}

override suspend fun collect(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
val projectNameItem = createProjectNameItem(project)
val configFileItem = createConfigFilesItem(project)
val isUnderWslItem = createIsUnderWslItem(project)
val preferredLanguageItem = createPreferredLanguageItem(project, creationContext)

return (listOf(projectNameItem, configFileItem) + isUnderWslItem + preferredLanguageItem)
}

private fun createProjectNameItem(project: Project): ChatContextItem {
return ChatContextItem(
CLionWorkspaceContextProvider::class,
"You are working on project named \"${project.name}\""
)
}

private fun createConfigFilesItem(project: Project): ChatContextItem {
val configFiles = collectConfigFiles(project)
val configFileNames = configFiles.joinToString(", ") { it.name }
return ChatContextItem(
CLionWorkspaceContextProvider::class,
"The project has the following config files: $configFileNames."
)
}

private fun createIsUnderWslItem(project: Project): List<ChatContextItem> {
val basePath = project.basePath ?: ""
val isUnderWsl = WslPath.isWslUncPath(basePath)
return if (isUnderWsl) {
listOf(ChatContextItem(CLionWorkspaceContextProvider::class, "The project is opened under WSL."))
} else {
emptyList()
}
}

private fun createPreferredLanguageItem(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
val sourceFile = creationContext.sourceFile
return if (sourceFile != null) {
listOf(
ChatContextItem(
CLionWorkspaceContextProvider::class,
"Prefer ${sourceFile.language.displayName} language if the used language and toolset are not defined below or in the user messages."
)
)
} else {
val initializedWorkspaces = CidrWorkspace.getInitializedWorkspaces(project)
if (initializedWorkspaces.isEmpty()) {
emptyList()
} else {
listOf(
ChatContextItem(
CLionWorkspaceContextProvider::class,
"Prefer C++ and C languages if the used language and toolset are not defined below or in the user messages."
)
)
}
}
}

private fun collectConfigFiles(project: Project): Collection<VirtualFile> =
ProjectRootManager.getInstance(project).contentRoots
.asSequence()
.filter { it.isDirectory }
.map { it.children }
.map { file ->
file.filter { configFiles.contains(it.name) || configFiles.contains(it.extension) }
}
.flatten()
.toList()
}
1 change: 1 addition & 0 deletions cpp/src/main/resources/cc.unitmesh.cpp.xml
Expand Up @@ -5,5 +5,6 @@
</dependencies>

<extensions defaultExtensionNs="cc.unitmesh">
<chatContextProvider implementation="cc.unitmesh.cpp.provider.CLionWorkspaceContextProvider"/>
</extensions>
</idea-plugin>
2 changes: 2 additions & 0 deletions rust/src/main/resources/cc.unitmesh.rust.xml
Expand Up @@ -14,5 +14,7 @@
implementationClass="cc.unitmesh.rust.context.RustMethodContextBuilder"/>

<contextPrompter language="Rust" implementation="cc.unitmesh.rust.provider.RustContextPrompter"/>

<!-- <chatContextProvider implementation="cc.unitmesh.rust.provider.RustContextProvider"/>-->
</extensions>
</idea-plugin>

0 comments on commit 4489358

Please sign in to comment.