Skip to content

Commit d5dc093

Browse files
committed
fix: dont trash the document when removing clutter (#818)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent c089bae commit d5dc093

File tree

15 files changed

+260
-27
lines changed

15 files changed

+260
-27
lines changed

src/main/.DS_Store

6 KB
Binary file not shown.

src/main/kotlin/.DS_Store

6 KB
Binary file not shown.

src/main/kotlin/com/.DS_Store

6 KB
Binary file not shown.

src/main/kotlin/com/redhat/.DS_Store

6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceSerialization.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package com.redhat.devtools.intellij.kubernetes.editor
1212

1313
import com.intellij.json.JsonFileType
14-
import com.intellij.openapi.editor.Document
1514
import com.intellij.openapi.fileTypes.FileType
1615
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
1716
import com.redhat.devtools.intellij.kubernetes.model.util.createResource
@@ -26,10 +25,13 @@ object EditorResourceSerialization {
2625
private const val RESOURCE_SEPARATOR_JSON = ",\n"
2726

2827
/**
29-
* Returns a [HasMetadata] for a given [Document] instance.
28+
* Returns a list of [HasMetadata] for a given yaml or json string.
3029
*
31-
* @param jsonYaml serialized resources
32-
* @return [HasMetadata] for the given editor and clients
30+
* @param jsonYaml string representing kubernetes resources
31+
* @param fileType yaml- or json file type (no other types are supported)
32+
* @return list of [HasMetadata] for the given yaml or json string
33+
*
34+
* @see isSupported
3335
*/
3436
fun deserialize(jsonYaml: String?, fileType: FileType?, currentNamespace: String?): List<HasMetadata> {
3537
return if (jsonYaml == null
@@ -64,25 +66,27 @@ object EditorResourceSerialization {
6466
return resource
6567
}
6668

67-
fun serialize(resources: Collection<HasMetadata>, fileType: FileType?): String? {
69+
fun serialize(resources: List<HasMetadata>, fileType: FileType?): String? {
6870
if (fileType == null) {
6971
return null
7072
}
71-
if (resources.size >=2 && fileType != YAMLFileType.YML) {
73+
if (resources.size >= 2
74+
&& fileType != YAMLFileType.YML) {
7275
throw UnsupportedOperationException(
7376
"${fileType.name} is not supported for multi-resource documents. Only ${YAMLFileType.YML.name} is.")
7477
}
7578
return resources
7679
.mapNotNull { resource -> serialize(resource, fileType) }
77-
.joinToString(RESOURCE_SEPARATOR_YAML)
80+
.joinToString("\n")
81+
7882
}
7983

8084
private fun serialize(resource: HasMetadata, fileType: FileType): String? {
8185
return when(fileType) {
8286
YAMLFileType.YML ->
8387
Serialization.asYaml(resource).trim()
8488
JsonFileType.INSTANCE ->
85-
Serialization.asYaml(resource).trim()
89+
Serialization.asJson(resource).trim()
8690
else -> null
8791
}
8892
}

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditor.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class ResourceEditor(
5151
// for mocking purposes
5252
private val createResources: (string: String?, fileType: FileType?, currentNamespace: String?) -> List<HasMetadata> =
5353
EditorResourceSerialization::deserialize,
54-
private val serialize: (resources: Collection<HasMetadata>, fileType: FileType?) -> String? =
54+
private val serialize: (resources: List<HasMetadata>, fileType: FileType?) -> String? =
5555
EditorResourceSerialization::serialize,
5656
// for mocking purposes
5757
private val createResourceFileForVirtual: (file: VirtualFile?) -> ResourceFile? =
@@ -165,7 +165,7 @@ open class ResourceEditor(
165165
}
166166
}
167167

168-
private fun replaceDocument(resources: Collection<HasMetadata>): Boolean {
168+
private fun replaceDocument(resources: List<HasMetadata>): Boolean {
169169
val manager = getPsiDocumentManager.invoke(project)
170170
val document = getDocument.invoke(editor) ?: return false
171171
val jsonYaml = serialize.invoke(resources, getFileType(document, manager)) ?: return false
@@ -269,17 +269,20 @@ open class ResourceEditor(
269269
}
270270

271271
fun removeClutter() {
272-
val resources = editorResources.getAllResources().map { resource ->
272+
val resources = createResources(
273+
getDocument(editor),
274+
editor.file.fileType) // don't insert namespace if not present (no namespace param)
275+
val cleaned = resources.map { resource ->
273276
MetadataClutter.remove(resource.metadata)
274277
resource
275278
}
276279
runInUI {
277-
replaceDocument(resources)
280+
replaceDocument(cleaned)
278281
notifications.hideAll()
279282
}
280283
}
281284

282-
private fun createResources(document: Document?, fileType: FileType?, namespace: String?): List<HasMetadata> {
285+
private fun createResources(document: Document?, fileType: FileType?, namespace: String? = null): List<HasMetadata> {
283286
return createResources.invoke(
284287
document?.text,
285288
fileType,

0 commit comments

Comments
 (0)