Skip to content

Commit

Permalink
feat(webstorm): add JavaScript test framework detection
Browse files Browse the repository at this point in the history
Add functionality to detect the JavaScript test framework being used in a file. This is done by guessing the test framework name based on the file's package dependencies. The detected test framework name is then added to the chat context item.
  • Loading branch information
phodal committed Jan 15, 2024
1 parent f298b1d commit 3e78eee
Showing 1 changed file with 22 additions and 0 deletions.
Expand Up @@ -7,8 +7,11 @@ import cc.unitmesh.devti.provider.context.ChatCreationContext
import cc.unitmesh.ide.webstorm.JsDependenciesSnapshot
import cc.unitmesh.ide.webstorm.LanguageApplicableUtil
import com.intellij.javascript.nodejs.PackageJsonDependency
import com.intellij.javascript.testing.JSTestRunnerManager
import com.intellij.javascript.testing.JsPackageDependentTestRunConfigurationProducer
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile

class JavaScriptContextProvider : ChatContextProvider {
Expand Down Expand Up @@ -44,6 +47,16 @@ class JavaScriptContextProvider : ChatContextProvider {
)

results.add(testChatContext)
} else {
val testFrameworkName = guessTestFrameworkName(creationContext.sourceFile ?: return emptyList())
if (testFrameworkName != null) {
val testChatContext = ChatContextItem(
JavaScriptContextProvider::class,
"\nUse $testFrameworkName JavaScript test framework."
)

results.add(testChatContext)
}
}

return results
Expand All @@ -68,6 +81,14 @@ class JavaScriptContextProvider : ChatContextProvider {
)
}

private fun guessTestFrameworkName(file: PsiFile): String? {
val findPackageDependentProducers =
JSTestRunnerManager.getInstance().findPackageDependentProducers(file)

val testRunConfigurationProducer = findPackageDependentProducers.firstOrNull()
return testRunConfigurationProducer?.configurationType?.displayName
}

private fun getTypeScriptLanguageContext(snapshot: JsDependenciesSnapshot): ChatContextItem? {
val packageJson = snapshot.packages["typescript"] ?: return null
val version = packageJson.parseVersion()
Expand Down Expand Up @@ -103,6 +124,7 @@ class JavaScriptContextProvider : ChatContextProvider {
}
}
}

else -> {}
}
}
Expand Down

0 comments on commit 3e78eee

Please sign in to comment.