Skip to content

Commit 7792907

Browse files
committed
feat(javascript): add mostPopularFrameworks method to JsDependenciesSnapshot #81
This commit adds a new method called `mostPopularFrameworks` to the `JsDependenciesSnapshot` class in the JavaScript codebase. This method filters the `packages` map to include only the most popular packages and excludes any packages starting with "@type". It then maps each entry to a string representation of the package and its version, if available. The resulting list of dependencies is returned. This method is used in the `getMostPopularPackagesContext` function in the `JavaScriptContextProvider` class to retrieve the most popular packages for the chat context.
1 parent 3e9d40a commit 7792907

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/JsDependenciesSnapshot.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cc.unitmesh.ide.javascript
22

3-
import cc.unitmesh.devti.provider.context.ChatCreationContext
3+
import cc.unitmesh.ide.javascript.provider.MOST_POPULAR_PACKAGES
44
import com.intellij.javascript.nodejs.PackageJsonData
55
import com.intellij.javascript.nodejs.packageJson.PackageJsonFileManager
66
import com.intellij.lang.javascript.buildTools.npm.PackageJsonUtil
77
import com.intellij.openapi.project.Project
88
import com.intellij.openapi.project.guessProjectDir
99
import com.intellij.openapi.vfs.VirtualFile
10+
import com.intellij.psi.PsiFile
1011

1112
/**
1213
* Represents a snapshot of JavaScript dependencies in a Kotlin language project.
@@ -21,19 +22,29 @@ import com.intellij.openapi.vfs.VirtualFile
2122
*/
2223
class JsDependenciesSnapshot(
2324
val packageJsonFiles: Set<VirtualFile>,
24-
val resolvedPackageJson: Boolean,
25-
val tsConfigs: Set<VirtualFile>,
25+
private val resolvedPackageJson: Boolean,
26+
private val tsConfigs: Set<VirtualFile>,
2627
val packages: Map<String, PackageJsonData.PackageJsonDependencyEntry>
2728
) {
29+
fun mostPopularFrameworks(): List<String> {
30+
val dependencies = this.packages
31+
.asSequence()
32+
.filter { entry -> MOST_POPULAR_PACKAGES.contains(entry.key) && !entry.key.startsWith("@type") }
33+
.map { entry ->
34+
val dependency = entry.key
35+
val version = entry.value.parseVersion()
36+
if (version != null) "$dependency: $version" else dependency
37+
}
38+
.toList()
39+
return dependencies
40+
}
41+
2842
companion object {
29-
fun create(
30-
project: Project,
31-
creationContext: ChatCreationContext?
32-
): JsDependenciesSnapshot {
43+
fun create(project: Project, psiFile: PsiFile?): JsDependenciesSnapshot {
3344
var packageJsonFiles = emptySet<VirtualFile>()
3445
var resolvedPackageJson = false
3546

36-
val virtualFile = creationContext?.sourceFile?.virtualFile
47+
val virtualFile = psiFile?.virtualFile
3748
if (virtualFile != null) {
3849
val packageJson = PackageJsonUtil.findUpPackageJson(virtualFile)
3950
if (packageJson != null) {

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/AutoPage.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import cc.unitmesh.ide.javascript.flow.model.DsComponent
1717
interface AutoPage : TaskFlow<String> {
1818
var userTask: String
1919

20+
fun getFrameworks(): List<String> {
21+
return listOf("React", "Vue", "Angular")
22+
}
23+
2024
/**
2125
* Retrieves all routes in the project, including the routes in the submodules.
2226
*

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/ReactAutoPage.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class ReactAutoPage(
7777
}
7878
}
7979

80-
8180
override fun getPages(): List<DsComponent> = pages
8281

8382
override fun getComponents(): List<DsComponent> = components

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/provider/JavaScriptBuildSystemProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.intellij.openapi.project.guessProjectDir
1212

1313
class JavaScriptBuildSystemProvider : BuildSystemProvider() {
1414
override fun collect(project: Project): DockerfileContext? {
15-
val snapshot = JsDependenciesSnapshot.create(project, null)
15+
val snapshot = JsDependenciesSnapshot.create(project, null?.sourceFile)
1616
if (snapshot.packageJsonFiles.isEmpty()) {
1717
return null
1818
}

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/provider/JavaScriptContextProvider.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ class JavaScriptContextProvider : ChatContextProvider {
2222

2323
override suspend fun collect(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
2424
val results = mutableListOf<ChatContextItem>()
25-
val snapshot = JsDependenciesSnapshot.create(project, creationContext)
26-
25+
val snapshot = JsDependenciesSnapshot.create(project, creationContext?.sourceFile)
2726
val typeScriptLanguageContext = getTypeScriptLanguageContext(snapshot)
27+
val mostPopularPackagesContext = getMostPopularPackagesContext(snapshot)
28+
29+
val techStack = prepareStack(snapshot)
30+
2831
if (typeScriptLanguageContext != null) results.add(typeScriptLanguageContext)
2932

30-
val mostPopularPackagesContext = getMostPopularPackagesContext(snapshot)
3133
if (mostPopularPackagesContext != null) results.add(mostPopularPackagesContext)
3234

33-
val techStack = prepareStack(snapshot)
3435
log.info("Tech stack: $techStack")
3536
if (techStack.coreFrameworks().isNotEmpty()) {
3637
val element = ChatContextItem(
@@ -70,15 +71,7 @@ class JavaScriptContextProvider : ChatContextProvider {
7071
* or null if no popular packages are found
7172
*/
7273
private fun getMostPopularPackagesContext(snapshot: JsDependenciesSnapshot): ChatContextItem? {
73-
val dependencies = snapshot.packages
74-
.asSequence()
75-
.filter { entry -> MOST_POPULAR_PACKAGES.contains(entry.key) && !entry.key.startsWith("@type") }
76-
.map { entry ->
77-
val dependency = entry.key
78-
val version = entry.value.parseVersion()
79-
if (version != null) "$dependency: $version" else dependency
80-
}
81-
.toList()
74+
val dependencies = snapshot.mostPopularFrameworks()
8275

8376
if (dependencies.isEmpty()) return null
8477

@@ -133,3 +126,4 @@ class JavaScriptContextProvider : ChatContextProvider {
133126
}
134127
}
135128

129+

0 commit comments

Comments
 (0)