From 2671e0769ac92fe49c11641326dd678b4c4ce4e4 Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Tue, 16 Apr 2024 20:35:21 +0200 Subject: [PATCH] fix: corrected incompatibilities (#749) * removed call to ExtensionPointName.create (#749) * made instantiation of PresentationFactory compatible (#749) * removed TriConsumer from commons to make build compatible (#749) * removed deprecated UtilKt.isNull0rEmpty(Collection)(#749) * removed deprecated EdtInvocationManager.isEventDispatchThread(#749) * replaced deprecated StructureTreeModel.invalidate()(#749) Signed-off-by: Andre Dietisheim --- gradle.properties | 4 +-- .../kubernetes/editor/ResourceFile.kt | 8 +++--- .../editor/inlay/Base64Presentations.kt | 9 +++--- .../inlay/PresentationFactoryBuilder.kt | 28 +++++++++++++++++++ .../editor/notification/PushNotification.kt | 5 ++-- .../editor/notification/PushedNotification.kt | 7 ++--- .../kubernetes/model/context/ActiveContext.kt | 2 +- .../intellij/kubernetes/tree/TreeStructure.kt | 2 +- .../intellij/kubernetes/tree/TreeUpdater.kt | 2 +- .../kubernetes/tree/TreeUpdaterTest.kt | 2 +- 10 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/PresentationFactoryBuilder.kt diff --git a/gradle.properties b/gradle.properties index 3c5eee389..11ede15a4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -ideaVersion=IC-2024.1 +ideaVersion=IC-2022.3 # build number ranges # https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html sinceIdeaBuild=223 @@ -7,7 +7,7 @@ jetBrainsToken=invalid jetBrainsChannel=stable intellijPluginVersion=1.16.1 kotlinJvmPluginVersion=1.8.0 -intellijCommonVersion=1.9.4 +intellijCommonVersion=1.9.5-SNAPSHOT telemetryPluginVersion=1.1.0.52 kotlin.stdlib.default.dependency = false kotlinVersion=1.6.21 diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceFile.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceFile.kt index a38286b0d..0ac851d12 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceFile.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceFile.kt @@ -20,21 +20,21 @@ import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile -import com.intellij.util.ui.EdtInvocationManager +import com.intellij.util.ui.EDT import com.redhat.devtools.intellij.common.editor.AllowNonProjectEditing import com.redhat.devtools.intellij.common.utils.UIHelper import com.redhat.devtools.intellij.kubernetes.model.Notification import com.redhat.devtools.intellij.kubernetes.model.util.trimWithEllipsis import io.fabric8.kubernetes.api.model.HasMetadata import io.fabric8.kubernetes.client.utils.Serialization +import org.apache.commons.io.FileUtils +import org.jetbrains.yaml.YAMLFileType import java.io.IOException import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths import java.util.function.Supplier -import org.apache.commons.io.FileUtils -import org.jetbrains.yaml.YAMLFileType /** * Helper that offers operations on the [VirtualFile] for a [ResourceEditor] @@ -135,7 +135,7 @@ open class ResourceFile protected constructor( * When invoking synchronous refresh from a thread other than the event dispatch thread, * the current thread must NOT be in a read action, otherwise a deadlock may occur */ - if (EdtInvocationManager.getInstance().isEventDispatchThread) { + if (EDT.isCurrentThreadEdt()) { executeReadAction { virtualFile.refresh(false, false) } diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/Base64Presentations.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/Base64Presentations.kt index 4f39f8bb0..b2d8517e4 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/Base64Presentations.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/Base64Presentations.kt @@ -11,16 +11,17 @@ @file:Suppress("UnstableApiUsage") package com.redhat.devtools.intellij.kubernetes.editor.inlay +import PresentationFactoryBuilder import com.intellij.codeInsight.hints.InlayHintsSink import com.intellij.codeInsight.hints.presentation.InlayPresentation -import com.intellij.codeInsight.hints.presentation.PresentationFactory import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.impl.EditorImpl import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo import com.redhat.devtools.intellij.kubernetes.balloon.StringInputBalloon +import com.redhat.devtools.intellij.kubernetes.editor.inlay.Base64Presentations.InlayPresentationsFactory +import com.redhat.devtools.intellij.kubernetes.editor.inlay.Base64Presentations.create import com.redhat.devtools.intellij.kubernetes.editor.util.getBinaryData import com.redhat.devtools.intellij.kubernetes.editor.util.getData import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource @@ -94,7 +95,7 @@ object Base64Presentations { } private fun create(text: String, onClick: (event: MouseEvent) -> Unit, editor: Editor): InlayPresentation? { - val factory = PresentationFactory(editor as EditorImpl) + val factory = PresentationFactoryBuilder.build(editor) ?: return null val trimmed = trimWithEllipsis(text, INLAY_HINT_MAX_WIDTH) ?: return null val textPresentation = factory.smallText(trimmed) val hoverPresentation = factory.referenceOnHover(textPresentation) { event, _ -> @@ -129,7 +130,7 @@ object Base64Presentations { } private fun create(bytes: ByteArray, editor: Editor): InlayPresentation? { - val factory = PresentationFactory(editor as EditorImpl) + val factory = PresentationFactoryBuilder.build(editor) ?: return null val hex = toHexString(bytes) ?: return null val trimmed = trimWithEllipsis(hex, INLAY_HINT_MAX_WIDTH) ?: return null return factory.roundWithBackground(factory.smallText(trimmed)) diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/PresentationFactoryBuilder.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/PresentationFactoryBuilder.kt new file mode 100644 index 000000000..2ab23187b --- /dev/null +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/PresentationFactoryBuilder.kt @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +import com.intellij.codeInsight.hints.presentation.PresentationFactory +import com.intellij.openapi.editor.Editor + +/** + * A factory that creates a [PresentationFactory]. This class bridges the difference in API between + * <= IC-2022.3 and above. + */ +object PresentationFactoryBuilder { + fun build(editor: Editor): PresentationFactory? { + try { + val constructor = PresentationFactory::class.java.constructors.firstOrNull() ?: return null + // IC-2022.3: PresentationFactory(EditorImpl), > IC-2022.3: PresentationFactory(Editor) + return constructor.newInstance(editor) as PresentationFactory? + } catch (e: Exception) { + return null + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushNotification.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushNotification.kt index 8908722e5..98c419e79 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushNotification.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushNotification.kt @@ -14,7 +14,6 @@ import com.intellij.openapi.fileEditor.FileEditor import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key import com.intellij.ui.EditorNotificationPanel -import com.intellij.util.containers.isNullOrEmpty import com.redhat.devtools.intellij.kubernetes.editor.Different import com.redhat.devtools.intellij.kubernetes.editor.EditorResource import com.redhat.devtools.intellij.kubernetes.editor.hideNotification @@ -67,10 +66,10 @@ class PushNotification(private val editor: FileEditor, private val project: Proj private fun createText(toCreate: Collection?, toUpdate: Collection?): String { return StringBuilder().apply { - if (false == toCreate?.isNullOrEmpty()) { + if (false == toCreate?.isEmpty()) { append("Push to create ${toKindAndNames(toCreate.map { editorResource -> editorResource.getResource() })}") } - if (false == toUpdate?.isNullOrEmpty()) { + if (false == toUpdate?.isEmpty()) { if (isNotEmpty()) { append(", ") } else { diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushedNotification.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushedNotification.kt index c86ffe243..79eba99e3 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushedNotification.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/PushedNotification.kt @@ -14,7 +14,6 @@ import com.intellij.openapi.fileEditor.FileEditor import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key import com.intellij.ui.EditorNotificationPanel -import com.intellij.util.containers.isNullOrEmpty import com.redhat.devtools.intellij.kubernetes.editor.EditorResource import com.redhat.devtools.intellij.kubernetes.editor.FILTER_PUSHED import com.redhat.devtools.intellij.kubernetes.editor.Pushed @@ -62,16 +61,16 @@ class PushedNotification(private val editor: FileEditor, private val project: Pr private fun createText(created: List?, updated: List?): String { return StringBuilder().apply { - if (!created.isNullOrEmpty()) { + if (false == created?.isEmpty()) { append("Created ${toKindAndNames(created?.map { editorResource -> editorResource.getResource() })} ") } - if (!updated.isNullOrEmpty()) { + if (false == updated?.isEmpty()) { if (isNotEmpty()) { append(", updated") } else { append("Updated") } - append(" ${toKindAndNames(updated?.map { editorResource -> editorResource.getResource() })}") + append(" ${toKindAndNames(updated.map { editorResource -> editorResource.getResource() })}") } append(" on cluster.") } diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt index 2208a1d0f..b189dcfb4 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt @@ -75,7 +75,7 @@ abstract class ActiveContext( protected abstract val namespaceKind : ResourceKind private val extensionName: ExtensionPointName>> = - ExtensionPointName.create("com.redhat.devtools.intellij.kubernetes.resourceOperators") + ExtensionPointName("com.redhat.devtools.intellij.kubernetes.resourceOperators") protected open val nonNamespacedOperators: MutableMap, INonNamespacedResourceOperator<*, *>> by lazy { getAllResourceOperators(INonNamespacedResourceOperator::class.java) diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeStructure.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeStructure.kt index b56164c46..6cd31a3f4 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeStructure.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeStructure.kt @@ -45,7 +45,7 @@ open class TreeStructure( private val project: Project, private val model: IResourceModel, private val extensionPoint: ExtensionPointName = - ExtensionPointName.create("com.redhat.devtools.intellij.kubernetes.structureContribution")) + ExtensionPointName("com.redhat.devtools.intellij.kubernetes.structureContribution")) : AbstractTreeStructure(), MultiParentTreeStructure { private val contributions by lazy { diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdater.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdater.kt index 241eca4ea..9e895a929 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdater.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdater.kt @@ -129,7 +129,7 @@ class TreeUpdater( } private fun invalidateRoot() { - treeModel.invalidate() + treeModel.invalidateAsync() } private fun getPotentialParentNodes(element: Any): Collection { diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdaterTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdaterTest.kt index 71bed2300..09f662120 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdaterTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/tree/TreeUpdaterTest.kt @@ -242,7 +242,7 @@ class TreeUpdaterTest { // when updater.modified(resourceModel) // then - verify(treeModel).invalidate() + verify(treeModel).invalidateAsync() } @Test