Skip to content

Commit be74706

Browse files
committed
refactor(container): replace PSI-based JSON parsing with Jackson #306
- Remove PSI-based JSON parsing and replace it with Jackson for better performance and simplicity. - Update property checks and value extraction to use JsonNode. - Remove unused PSI-related imports and helper methods.
1 parent 558be04 commit be74706

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed
Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
package cc.unitmesh.devti.gui.snippet.container
22

3+
import com.fasterxml.jackson.databind.JsonNode
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.module.kotlin.KotlinModule
36
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
97
import com.intellij.openapi.project.Project
10-
import com.intellij.psi.PsiFile
11-
import com.intellij.psi.PsiManager
128
import com.intellij.testFramework.LightVirtualFile
139

1410
object AutoDevContainer {
1511
private val DEV_CONTAINER_PROPS =
1612
setOf("image", "dockerFile", "containerEnv", "remoteUser", "customizations", "features")
1713

18-
fun isDevContainerProperty(propName: String): Boolean {
19-
return propName in DEV_CONTAINER_PROPS
20-
}
21-
2214
fun updateForDevContainer(
2315
project: Project,
2416
lightVirtualFile: LightVirtualFile,
@@ -31,22 +23,31 @@ object AutoDevContainer {
3123
return lightVirtualFile
3224
}
3325

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+
}
3633

37-
val hasDevContainerProps = rootObject.propertyList.any { isDevContainerProperty(it.name) }
34+
if (!jsonNode.isObject) return null
3835

36+
// Check if any dev container property exists
37+
val hasDevContainerProps = DEV_CONTAINER_PROPS.any { jsonNode.has(it) }
3938
if (!hasDevContainerProps) return null
4039

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")
4443

4544
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
5051
else -> false
5152
}
5253

@@ -55,9 +56,4 @@ object AutoDevContainer {
5556

5657
return newFile
5758
}
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-
}
6359
}

0 commit comments

Comments
 (0)