From 9ea39d16b0879d5359a9558a5485f69287e81877 Mon Sep 17 00:00:00 2001 From: serge shustoff Date: Sun, 16 Jun 2024 12:25:09 +0200 Subject: [PATCH] Show desktop preview tool for functions annotated with common preview annotation (#4839) Allows desktop preview to work with org.jetbrains.compose.ui.tooling.preview.Preview annotation in addition to desktop preview annotation. That allows using preview in common code (#2045), though only with desktop target enabled Fixes #4839, #2045 --- .../compose/desktop/ide/preview/PreviewEntryPoint.kt | 3 ++- .../jetbrains/compose/desktop/ide/preview/locationUtils.kt | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewEntryPoint.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewEntryPoint.kt index 1d6c274c8b8..f378e10d121 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewEntryPoint.kt +++ b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewEntryPoint.kt @@ -37,7 +37,8 @@ class PreviewEntryPoint : EntryPoint() { override fun isEntryPoint(refElement: RefElement, psiElement: PsiElement): Boolean = isEntryPoint(psiElement) override fun isEntryPoint(psiElement: PsiElement): Boolean = - psiElement is PsiMethod && psiElement.hasAnnotation(DESKTOP_PREVIEW_ANNOTATION_FQN) + psiElement is PsiMethod && (psiElement.hasAnnotation(DESKTOP_PREVIEW_ANNOTATION_FQN) || + psiElement.hasAnnotation(COMMON_PREVIEW_ANNOTATION_FQN)) override fun readExternal(element: Element) = element.deserializeInto(this) diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/locationUtils.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/locationUtils.kt index c802e6369db..b98baeb7755 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/locationUtils.kt +++ b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/locationUtils.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode internal const val DESKTOP_PREVIEW_ANNOTATION_FQN = "androidx.compose.desktop.ui.tooling.preview.Preview" +internal const val COMMON_PREVIEW_ANNOTATION_FQN = "org.jetbrains.compose.ui.tooling.preview.Preview" internal const val COMPOSABLE_FQ_NAME = "androidx.compose.runtime.Composable" /** @@ -120,7 +121,9 @@ internal fun KtNamedFunction.isValidComposablePreviewFunction(): Boolean { while (annotationIt.hasNext() && !(hasComposableAnnotation && hasPreviewAnnotation)) { val annotation = annotationIt.next() hasComposableAnnotation = hasComposableAnnotation || annotation.fqNameMatches(COMPOSABLE_FQ_NAME) - hasPreviewAnnotation = hasPreviewAnnotation || annotation.fqNameMatches(DESKTOP_PREVIEW_ANNOTATION_FQN) + hasPreviewAnnotation = hasPreviewAnnotation || + annotation.fqNameMatches(DESKTOP_PREVIEW_ANNOTATION_FQN) || + annotation.fqNameMatches(COMMON_PREVIEW_ANNOTATION_FQN) } return hasComposableAnnotation && hasPreviewAnnotation