From 139dfe361030733272dfca2974d7d9f5048e0d2b Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Mon, 4 Sep 2023 20:48:02 +0200 Subject: [PATCH] subclass platform editor tab title provider to respect settings (#652) Signed-off-by: Andre Dietisheim --- .../editor/ResourceEditorTabTitleProvider.kt | 11 ++++---- src/main/resources/test.yml | 0 .../ResourceEditorTabTitleProviderTest.kt | 25 +++++++++---------- 3 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 src/main/resources/test.yml diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProvider.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProvider.kt index 18d1e8490..058f353cb 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProvider.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProvider.kt @@ -11,12 +11,15 @@ package com.redhat.devtools.intellij.kubernetes.editor import com.intellij.openapi.fileEditor.impl.EditorTabTitleProvider +import com.intellij.openapi.fileEditor.impl.UniqueNameEditorTabTitleProvider import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource -open class ResourceEditorTabTitleProvider : EditorTabTitleProvider { +open class ResourceEditorTabTitleProvider( + private val fallback: EditorTabTitleProvider = UniqueNameEditorTabTitleProvider() +) : EditorTabTitleProvider { companion object { const val TITLE_UNKNOWN_CLUSTERRESOURCE = "Unknown Cluster Resource" @@ -34,14 +37,10 @@ open class ResourceEditorTabTitleProvider : EditorTabTitleProvider { TITLE_UNKNOWN_CLUSTERRESOURCE } } else { - getTitleFor(file) + fallback.getEditorTabTitle(project, file) } } - private fun getTitleFor(file: VirtualFile): String { - return file.name - } - private fun getTitleFor(info: KubernetesResourceInfo): String { val name = info.name ?: TITLE_UNKNOWN_NAME val namespace = info.namespace diff --git a/src/main/resources/test.yml b/src/main/resources/test.yml new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProviderTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProviderTest.kt index 7444e5ddb..bb124387c 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProviderTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorTabTitleProviderTest.kt @@ -11,10 +11,10 @@ package com.redhat.devtools.intellij.kubernetes.editor +import com.intellij.openapi.fileEditor.impl.EditorTabTitleProvider import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile -import com.nhaarman.mockitokotlin2.doReturn -import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.* import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo import com.redhat.devtools.intellij.kubernetes.editor.ResourceEditorTabTitleProvider.Companion.TITLE_UNKNOWN_NAME import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.kubernetesResourceInfo @@ -28,7 +28,6 @@ class ResourceEditorTabTitleProviderTest { private val namespace = "WildWest" private val kind = "Cowboy" private val apiGroup = "sillyLads/v1" - private val resourceInfo = kubernetesResourceInfo(name, namespace, kubernetesTypeInfo(kind, apiGroup)) @Test fun `#getTitle should return 'resourcename@namespace' if file is temporary and contains kubernetes resource with namespace`() { @@ -71,28 +70,28 @@ class ResourceEditorTabTitleProviderTest { } @Test - fun `#getTitle should return filename if file is NOT temporary`() { + fun `#getTitle should ask fallback provider for title if file is NOT temporary`() { // given - val fileName = "JollyJumper" val isTemporary = false - val file: VirtualFile = mock { - on { getName() } doReturn fileName - } + val file: VirtualFile = mock() val resourceInfo = kubernetesResourceInfo( "", "", kubernetesTypeInfo("", "")) - val provider = createResourceEditorTabTitleProvider(isTemporary, resourceInfo) + val fallback: EditorTabTitleProvider = mock() + val provider = createResourceEditorTabTitleProvider(isTemporary, resourceInfo, fallback) // when - val title = provider.getEditorTabTitle(mock(), file) + provider.getEditorTabTitle(mock(), file) // then - assertThat(title).isEqualTo(fileName) + verify(fallback).getEditorTabTitle(any(), eq(file)) } private fun createResourceEditorTabTitleProvider( - isTemporary: Boolean, info: KubernetesResourceInfo + isTemporary: Boolean, + info: KubernetesResourceInfo, + fallback: EditorTabTitleProvider = mock() ): ResourceEditorTabTitleProvider { - return object : ResourceEditorTabTitleProvider() { + return object : ResourceEditorTabTitleProvider(fallback) { override fun getKubernetesResourceInfo(file: VirtualFile, project: Project): KubernetesResourceInfo { return info