generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
91 changes: 91 additions & 0 deletions
91
cpp/src/main/kotlin/cc/unitmesh/cpp/provider/CLionWorkspaceContextProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters