@@ -16,24 +16,25 @@ import com.intellij.json.psi.JsonFile
16
16
import com.intellij.json.psi.JsonProperty
17
17
import com.intellij.json.psi.JsonValue
18
18
import com.intellij.openapi.application.ReadAction
19
- import com.intellij.openapi.editor.Document
20
19
import com.intellij.openapi.fileEditor.FileEditor
21
20
import com.intellij.openapi.project.Project
22
21
import com.intellij.openapi.util.text.Strings
23
22
import com.intellij.openapi.vfs.VirtualFile
24
- import com.intellij.psi.PsiDocumentManager
25
23
import com.intellij.psi.PsiElement
26
- import com.intellij.psi.PsiFile
27
24
import com.intellij.psi.PsiManager
25
+ import com.intellij.psi.PsiNamedElement
28
26
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
27
+ import com.redhat.devtools.intellij.common.validation.KubernetesTypeInfo
29
28
import com.redhat.devtools.intellij.kubernetes.editor.ResourceEditor
30
29
import org.jetbrains.yaml.YAMLElementGenerator
31
- import org.jetbrains.yaml.YAMLUtil
32
- import org.jetbrains.yaml.psi.YAMLFile
30
+ import org.jetbrains.yaml.psi.YAMLDocument
33
31
import org.jetbrains.yaml.psi.YAMLKeyValue
32
+ import org.jetbrains.yaml.psi.YAMLMapping
34
33
import org.jetbrains.yaml.psi.YAMLPsiElement
34
+ import org.jetbrains.yaml.psi.YAMLSequence
35
35
import org.jetbrains.yaml.psi.YAMLValue
36
- import java.util.Base64
36
+ import java.util.*
37
+
37
38
38
39
private const val KEY_METADATA = " metadata"
39
40
private const val KEY_DATA = " data"
@@ -67,9 +68,9 @@ fun isKubernetesResource(resourceInfo: KubernetesResourceInfo?): Boolean {
67
68
&& resourceInfo?.typeInfo?.kind?.isNotBlank() ? : false
68
69
}
69
70
70
- fun isKubernetesResource (kind : String , resourceInfo : KubernetesResourceInfo ? ): Boolean {
71
- return resourceInfo?.typeInfo?. apiGroup?.isNotBlank() ? : false
72
- && kind == resourceInfo?.typeInfo? .kind
71
+ fun isKubernetesResource (kind : String , info : KubernetesTypeInfo ? ): Boolean {
72
+ return info?. apiGroup?.isNotBlank() != null
73
+ && kind == info .kind
73
74
}
74
75
75
76
/* *
@@ -93,89 +94,21 @@ fun getKubernetesResourceInfo(file: VirtualFile?, project: Project): KubernetesR
93
94
}
94
95
}
95
96
96
- /* *
97
- * Sets or creates the given [resourceVersion] in the given document for the given [PsiDocumentManager] and [Project].
98
- * The document is **not** committed to allow further modifications before a commit to happen.
99
- * Does nothing if the resource version or the document is `null`.
100
- *
101
- * @param resourceVersion the resource version to set/create
102
- * @param document the document to set the resource version to
103
- * @param manager the [PsiDocumentManager] to use for the operation
104
- * @param project the [Project] to use
105
- */
106
- fun setResourceVersion (resourceVersion : String? , document : Document ? , manager : PsiDocumentManager , project : Project ) {
107
- if (resourceVersion == null
108
- || document == null ) {
109
- return
110
- }
111
- val metadata = getMetadata(document, manager) ? : return
112
- createOrUpdateResourceVersion(resourceVersion, metadata, project)
113
- }
114
-
115
- private fun createOrUpdateResourceVersion (resourceVersion : String , metadata : PsiElement , project : Project ) {
116
- when (metadata) {
117
- is YAMLKeyValue -> createOrUpdateResourceVersion(resourceVersion, metadata, project)
118
- is JsonProperty -> createOrUpdateResourceVersion(resourceVersion, metadata, project)
119
- }
120
- }
121
-
122
- private fun createOrUpdateResourceVersion (resourceVersion : String , metadata : JsonProperty , project : Project ) {
123
- val metadataObject = metadata.value ? : return
124
- val generator = JsonElementGenerator (project)
125
- val version = generator.createProperty(KEY_RESOURCE_VERSION , " \" $resourceVersion \" " )
126
- val existingVersion = getResourceVersion(metadata)
127
- if (existingVersion != null ) {
128
- metadataObject.addAfter(version, existingVersion)
129
- existingVersion.delete()
130
- } else {
131
- metadataObject.addBefore(generator.createComma(), metadataObject.lastChild)
132
- metadataObject.addBefore(version, metadataObject.lastChild)
133
- }
134
- }
135
-
136
- private fun createOrUpdateResourceVersion (resourceVersion : String , metadata : YAMLKeyValue , project : Project ) {
137
- val metadataObject = metadata.value ? : return
138
- val existingVersion = getResourceVersion(metadata)
139
- val generator = YAMLElementGenerator .getInstance(project)
140
- val version = generator.createYamlKeyValue(KEY_RESOURCE_VERSION , " \" $resourceVersion \" " )
141
- if (existingVersion != null ) {
142
- existingVersion.setValue(version.value!! )
143
- } else {
144
- metadataObject.add(generator.createEol())
145
- metadataObject.add(generator.createIndent(YAMLUtil .getIndentToThisElement(metadataObject)))
146
- metadataObject.add(version)
147
- }
148
- }
149
-
150
- fun getContent (element : PsiElement ): PsiElement ? {
151
- if (element !is PsiFile ) {
152
- return null
153
- }
154
- return getContent(element)
155
- }
97
+ fun getChildren (document : YAMLDocument ): List <YAMLPsiElement > {
98
+ val topLevelValue: YAMLValue = document.topLevelValue ? : return emptyList()
156
99
157
- private fun getContent (file : PsiFile ): PsiElement ? {
158
- return when (file) {
159
- is YAMLFile -> {
160
- if (file.documents == null
161
- || file.documents.isEmpty()) {
162
- return null
163
- }
164
- file.documents[0 ].topLevelValue
165
- }
166
- is JsonFile ->
167
- file.topLevelValue
168
- else -> null
100
+ return when (topLevelValue) {
101
+ is YAMLMapping ->
102
+ topLevelValue.keyValues.toList()
103
+ is YAMLSequence ->
104
+ topLevelValue.items
105
+ else ->
106
+ emptyList()
169
107
}
170
108
}
171
109
172
- fun getMetadata (document : Document ? , psi : PsiDocumentManager ): PsiElement ? {
173
- if (document == null ) {
174
- return null
175
- }
176
- val file = psi.getPsiFile(document) ? : return null
177
- val content = getContent(file) ? : return null
178
- return getMetadata(content)
110
+ fun getChildren (file : JsonFile ): List <JsonElement > {
111
+ return file.allTopLevelValues
179
112
}
180
113
181
114
/* *
@@ -185,16 +118,20 @@ fun getMetadata(document: Document?, psi: PsiDocumentManager): PsiElement? {
185
118
* @param element the PsiElement whose "metadata" child should be found.
186
119
* @return the PsiElement named "metadata"
187
120
*/
188
- private fun getMetadata (content : PsiElement ): PsiElement ? {
189
- return when (content) {
121
+ private fun getMetadata (parent : PsiElement ): PsiElement ? {
122
+ return getElement(KEY_METADATA , parent)
123
+ }
124
+
125
+ private fun getElement (key : String , parent : PsiElement ): PsiElement ? {
126
+ return when (parent) {
190
127
is YAMLValue ->
191
- content .children
192
- .filterIsInstance<YAMLKeyValue >()
193
- .find { it.name == KEY_METADATA }
128
+ parent .children
129
+ .filterIsInstance<YAMLKeyValue >()
130
+ .find { it.name == key }
194
131
is JsonValue ->
195
- content .children.toList()
132
+ parent .children.toList()
196
133
.filterIsInstance<JsonProperty >()
197
- .find { it.name == KEY_METADATA }
134
+ .find { it.name == key }
198
135
else ->
199
136
null
200
137
}
@@ -208,17 +145,14 @@ private fun getMetadata(content: PsiElement): PsiElement? {
208
145
* @return the PsiElement named "data"
209
146
*/
210
147
fun getData (element : PsiElement ): PsiElement ? {
211
- return when (element) {
212
- is YAMLPsiElement ->
213
- element.children
214
- .filterIsInstance<YAMLKeyValue >()
215
- .find { it.name == KEY_DATA }
216
- ?.value
217
- is JsonElement ->
218
- element.children.toList()
219
- .filterIsInstance<JsonProperty >()
220
- .find { it.name == KEY_DATA }
221
- ?.value
148
+ val dataElement = element.children
149
+ .filterIsInstance<PsiNamedElement >()
150
+ .find { it.name == KEY_DATA }
151
+ return when (dataElement) {
152
+ is YAMLKeyValue ->
153
+ dataElement.value
154
+ is JsonProperty ->
155
+ dataElement.value
222
156
else ->
223
157
null
224
158
}
0 commit comments