From 4c33e07713ba0f4bbbb31654593ea171c3b8bbab Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Mon, 23 Sep 2024 16:49:35 +0200 Subject: [PATCH] merged 2 ResourceEditorUtils classes & corrected deprecated API usages (#785) Signed-off-by: Andre Dietisheim --- .../kubernetes/editor/EditorFocusListener.kt | 5 +-- .../editor/EditorResourceSerialization.kt | 7 ++-- .../editor/ResourceEditorFactory.kt | 6 ++-- .../kubernetes/editor/ResourceEditorUtils.kt | 35 ------------------- .../editor/util/ResourceEditorUtils.kt | 34 ++++++++++++++++++ 5 files changed, 43 insertions(+), 44 deletions(-) delete mode 100644 src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorUtils.kt diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorFocusListener.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorFocusListener.kt index 7c3e3f2be..0cfeda043 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorFocusListener.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorFocusListener.kt @@ -17,6 +17,7 @@ import com.intellij.openapi.fileEditor.FileEditorManagerListener import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import com.redhat.devtools.intellij.kubernetes.editor.notification.ErrorNotification +import com.redhat.devtools.intellij.kubernetes.editor.util.getExistingResourceEditor class EditorFocusListener(private val project: Project) : FileEditorManagerListener, FileEditorManagerListener.Before { @@ -28,7 +29,7 @@ class EditorFocusListener(private val project: Project) : FileEditorManagerListe override fun fileClosed(source: FileEditorManager, file: VirtualFile) { // editor cannot be found via manager once file was closed // deleting file before file was closed (#beforeFileClosed) causes recursion #fileClosed - getExisting(file)?.close() + getExistingResourceEditor(file)?.close() } private fun selectionGained(editor: FileEditor?, project: Project) { @@ -49,7 +50,7 @@ class EditorFocusListener(private val project: Project) : FileEditorManagerListe return } try { - getExisting(editor)?.stopWatch() + getExistingResourceEditor(editor)?.stopWatch() } catch (e: RuntimeException) { showErrorNotification(e, editor, project) } diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceSerialization.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceSerialization.kt index e7f26833d..8ee35c365 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceSerialization.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResourceSerialization.kt @@ -78,14 +78,13 @@ object EditorResourceSerialization { } private fun serialize(resource: HasMetadata, fileType: FileType): String? { - val serializer = when(fileType) { + return when(fileType) { YAMLFileType.YML -> - Serialization.yamlMapper().writerWithDefaultPrettyPrinter() + Serialization.asYaml(resource).trim() JsonFileType.INSTANCE -> - Serialization.jsonMapper().writerWithDefaultPrettyPrinter() + Serialization.asYaml(resource).trim() else -> null } - return serializer?.writeValueAsString(resource)?.trim() } private fun isSupported(fileType: FileType?): Boolean { diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt index 61b2b8ef7..40715302a 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt @@ -19,6 +19,7 @@ import com.intellij.openapi.project.ProjectManagerListener import com.intellij.openapi.util.Key import com.intellij.openapi.vfs.VirtualFile import com.redhat.devtools.intellij.kubernetes.editor.notification.ErrorNotification +import com.redhat.devtools.intellij.kubernetes.editor.util.getExistingResourceEditor import com.redhat.devtools.intellij.kubernetes.editor.util.getKubernetesResourceInfo import com.redhat.devtools.intellij.kubernetes.editor.util.hasKubernetesResource import com.redhat.devtools.intellij.kubernetes.model.IResourceModel @@ -27,7 +28,6 @@ import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder import io.fabric8.kubernetes.api.model.HasMetadata - open class ResourceEditorFactory protected constructor( /* for mocking purposes */ private val getFileEditorManager: (project: Project) -> FileEditorManager = FileEditorManager::getInstance, @@ -88,7 +88,7 @@ open class ResourceEditorFactory protected constructor( private fun getExisting(resource: HasMetadata, project: Project): ResourceEditor? { return getFileEditorManager.invoke(project).allEditors .mapNotNull { editor -> - getExisting(editor) } + getExistingResourceEditor(editor) } .firstOrNull { resourceEditor -> // get editor for a temporary file thus only editors for temporary files are candidates isTemporary.invoke(resourceEditor.editor.file) @@ -108,7 +108,7 @@ open class ResourceEditorFactory protected constructor( return null } - return getExisting(editor) ?: create(editor, project) + return getExistingResourceEditor(editor) ?: create(editor, project) } private fun create(editor: FileEditor, project: Project): ResourceEditor? { diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorUtils.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorUtils.kt deleted file mode 100644 index fe2bcb842..000000000 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorUtils.kt +++ /dev/null @@ -1,35 +0,0 @@ -package com.redhat.devtools.intellij.kubernetes.editor - -import com.intellij.openapi.fileEditor.FileEditor -import com.intellij.openapi.vfs.VirtualFile - -/** - * Returns a [ResourceEditor] for the given [FileEditor] if it exists. Returns `null` otherwise. - * The editor exists if it is contained in the user data for the given editor or its file. - * - * @param editor for which an existing [ResourceEditor] is returned. - * @return [ResourceEditor] that exists. - * - * @see [FileEditor.getUserData] - * @see [VirtualFile.getUserData] - */ -fun getExisting(editor: FileEditor?): ResourceEditor? { - if (editor == null) { - return null - } - return editor.getUserData(ResourceEditor.KEY_RESOURCE_EDITOR) - ?: getExisting(editor.file) -} - -/** - * Returns a [ResourceEditor] for the given [VirtualFile] if it exists. Returns `null` otherwise. - * The editor exists if it is contained in the user data for the given file. - * - * @param file for which an existing [VirtualFile] is returned. - * @return [ResourceEditor] that exists. - * - * @see [VirtualFile.getUserData] - */ -fun getExisting(file: VirtualFile?): ResourceEditor? { - return file?.getUserData(ResourceEditor.KEY_RESOURCE_EDITOR) -} diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt index b3b4ca6f2..96de04d5c 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt @@ -17,6 +17,7 @@ import com.intellij.json.psi.JsonProperty import com.intellij.json.psi.JsonValue import com.intellij.openapi.application.ReadAction import com.intellij.openapi.editor.Document +import com.intellij.openapi.fileEditor.FileEditor import com.intellij.openapi.project.Project import com.intellij.openapi.util.text.Strings import com.intellij.openapi.vfs.VirtualFile @@ -26,6 +27,7 @@ import com.intellij.psi.PsiFile import com.intellij.psi.PsiManager import com.intellij.refactoring.suggested.startOffset import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo +import com.redhat.devtools.intellij.kubernetes.editor.ResourceEditor import org.jetbrains.yaml.YAMLElementGenerator import org.jetbrains.yaml.YAMLUtil import org.jetbrains.yaml.psi.YAMLFile @@ -349,3 +351,35 @@ private fun getResourceVersion(metadata: JsonProperty): JsonProperty? { ?.filterIsInstance() ?.find { it.name == KEY_RESOURCE_VERSION } } + +/** + * Returns a [ResourceEditor] for the given [FileEditor] if it exists. Returns `null` otherwise. + * The editor exists if it is contained in the user data for the given editor or its file. + * + * @param editor for which an existing [ResourceEditor] is returned. + * @return [ResourceEditor] that exists. + * + * @see [FileEditor.getUserData] + * @see [VirtualFile.getUserData] + */ +fun getExistingResourceEditor(editor: FileEditor?): ResourceEditor? { + if (editor == null) { + return null + } + return editor.getUserData(ResourceEditor.KEY_RESOURCE_EDITOR) + ?: getExistingResourceEditor(editor.file) +} + +/** + * Returns a [ResourceEditor] for the given [VirtualFile] if it exists. Returns `null` otherwise. + * The editor exists if it is contained in the user data for the given file. + * + * @param file for which an existing [VirtualFile] is returned. + * @return [ResourceEditor] that exists. + * + * @see [VirtualFile.getUserData] + */ +fun getExistingResourceEditor(file: VirtualFile?): ResourceEditor? { + return file?.getUserData(ResourceEditor.KEY_RESOURCE_EDITOR) +} +