1
1
package cc.unitmesh.devti.gui.snippet.container
2
2
3
+ import com.fasterxml.jackson.databind.JsonNode
4
+ import com.fasterxml.jackson.databind.ObjectMapper
5
+ import com.fasterxml.jackson.module.kotlin.KotlinModule
3
6
import com.intellij.json.JsonLanguage
4
- import com.intellij.json.psi.JsonFile
5
- import com.intellij.json.psi.JsonObject
6
- import com.intellij.json.psi.JsonStringLiteral
7
- import com.intellij.json.psi.JsonValue
8
- import com.intellij.openapi.application.runReadAction
9
7
import com.intellij.openapi.project.Project
10
- import com.intellij.psi.PsiFile
11
- import com.intellij.psi.PsiManager
12
8
import com.intellij.testFramework.LightVirtualFile
13
9
14
10
object AutoDevContainer {
15
11
private val DEV_CONTAINER_PROPS =
16
12
setOf (" image" , " dockerFile" , " containerEnv" , " remoteUser" , " customizations" , " features" )
17
13
18
- fun isDevContainerProperty (propName : String ): Boolean {
19
- return propName in DEV_CONTAINER_PROPS
20
- }
21
-
22
14
fun updateForDevContainer (
23
15
project : Project ,
24
16
lightVirtualFile : LightVirtualFile ,
@@ -31,22 +23,31 @@ object AutoDevContainer {
31
23
return lightVirtualFile
32
24
}
33
25
34
- val psiFile = runReadAction { PsiManager .getInstance(project).findFile(lightVirtualFile) } ? : return null
35
- val rootObject = (psiFile as ? JsonFile )?.topLevelValue as ? JsonObject ? : return null
26
+ val objectMapper = ObjectMapper ().registerModule(KotlinModule .Builder ().build())
27
+ val jsonNode: JsonNode
28
+ try {
29
+ jsonNode = objectMapper.readTree(content)
30
+ } catch (e: Exception ) {
31
+ return null
32
+ }
36
33
37
- val hasDevContainerProps = rootObject.propertyList.any { isDevContainerProperty(it.name) }
34
+ if ( ! jsonNode.isObject) return null
38
35
36
+ // Check if any dev container property exists
37
+ val hasDevContainerProps = DEV_CONTAINER_PROPS .any { jsonNode.has(it) }
39
38
if (! hasDevContainerProps) return null
40
39
41
- val image = getPropValue (" image" , psiFile) as ? JsonStringLiteral
42
- val dockerfile = getPropValue (" dockerFile" , psiFile) as ? JsonStringLiteral
43
- val remoteUser = getPropValue (" remoteUser" , psiFile) as ? JsonStringLiteral
40
+ val image = jsonNode.path (" image" )
41
+ val dockerfile = jsonNode.path (" dockerFile" )
42
+ val remoteUser = jsonNode.path (" remoteUser" )
44
43
45
44
val isDevContainer = when {
46
- image != null && image.value.contains(" mcr.microsoft.com/devcontainers" ) -> true
47
- dockerfile != null -> true
48
- remoteUser != null -> true
49
- rootObject.propertyList.size >= 3 && hasDevContainerProps -> true
45
+ ! image.isMissingNode && image.isTextual && image.asText()
46
+ .contains(" mcr.microsoft.com/devcontainers" ) -> true
47
+
48
+ ! dockerfile.isMissingNode && dockerfile.isTextual -> true
49
+ ! remoteUser.isMissingNode && remoteUser.isTextual -> true
50
+ jsonNode.size() >= 3 -> true
50
51
else -> false
51
52
}
52
53
@@ -55,9 +56,4 @@ object AutoDevContainer {
55
56
56
57
return newFile
57
58
}
58
-
59
- fun getPropValue (propName : String , psiFile : PsiFile ): JsonValue ? {
60
- val rootObject = (psiFile as ? JsonFile )?.topLevelValue as ? JsonObject ? : return null
61
- return rootObject.propertyList.firstOrNull { it.name == propName }?.value
62
- }
63
59
}
0 commit comments