Skip to content

Commit

Permalink
Show desktop preview tool for functions annotated with common preview…
Browse files Browse the repository at this point in the history
… annotation (JetBrains#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 (JetBrains#2045), though only with desktop target enabled

Fixes JetBrains#4839, JetBrains#2045
  • Loading branch information
sergeshustoff committed Jun 16, 2024
1 parent a1b88db commit 9ea39d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ea39d1

Please sign in to comment.