Skip to content

Commit

Permalink
merged 2 ResourceEditorUtils classes & corrected deprecated API usages (
Browse files Browse the repository at this point in the history
#785)

Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Sep 30, 2024
1 parent 9266496 commit 9c69e50
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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? {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -349,3 +351,35 @@ private fun getResourceVersion(metadata: JsonProperty): JsonProperty? {
?.filterIsInstance<JsonProperty>()
?.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)
}

0 comments on commit 9c69e50

Please sign in to comment.