diff --git a/snapshots/snapshots-processor/src/main/kotlin/com/emergetools/snapshots/processor/utils/PreviewFinderUtils.kt b/snapshots/snapshots-processor/src/main/kotlin/com/emergetools/snapshots/processor/utils/PreviewFinderUtils.kt index c4df5c5c..5df3d80a 100644 --- a/snapshots/snapshots-processor/src/main/kotlin/com/emergetools/snapshots/processor/utils/PreviewFinderUtils.kt +++ b/snapshots/snapshots-processor/src/main/kotlin/com/emergetools/snapshots/processor/utils/PreviewFinderUtils.kt @@ -18,11 +18,15 @@ fun List.functionsWithPreviewAnnotation(): Map.functionsWithMultiPreviewAnnotations( - resolver: Resolver + resolver: Resolver, ): Map> { val uniqueSnapshotConfigs = filterIsInstance() .map { function -> - val allPreviewAnnotations = function.annotations.flatMap { resolver.findAllDirectOrTransitivePreviewAnnotations(it) }.toList() + val allPreviewAnnotations = function.annotations.flatMap { + resolver.findAllDirectOrTransitivePreviewAnnotations( + it + ) + }.toList() function to getUniqueSnapshotConfigsFromMultiPreviewAnnotation( annotations = allPreviewAnnotations, previewFunction = function, @@ -54,19 +58,28 @@ fun Resolver.getMultiPreviewAnnotations(): List { .toSet() .filter { val annotationQN = it.annotationType.resolve().declaration.qualifiedName - val annotationClassDecl = annotationQN?.let { qualifiedName -> getClassDeclarationByName(qualifiedName) } - annotationClassDecl?.let { classDecl -> hasDirectOrTransitivePreviewAnnotation(classDecl) } ?: false + val annotationClassDecl = + annotationQN?.let { qualifiedName -> getClassDeclarationByName(qualifiedName) } + annotationClassDecl?.let { classDecl -> hasDirectOrTransitivePreviewAnnotation(classDecl) } + ?: false } .toList() .sortedBy { it.shortName.asString() } } -fun Resolver.hasDirectOrTransitivePreviewAnnotation(declaration: KSAnnotated, seenAnnotations: MutableSet = mutableSetOf()): Boolean { +fun Resolver.hasDirectOrTransitivePreviewAnnotation( + declaration: KSAnnotated, + seenAnnotations: MutableSet = mutableSetOf(), +): Boolean { if (declaration in seenAnnotations) { return false } - if (declaration.annotations.any { it.annotationType.resolve().declaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME }) { + val hasPreviewAnnotation = declaration.annotations.any { + it.annotationType.resolve().declaration.qualifiedName?.asString() == + COMPOSE_PREVIEW_ANNOTATION_NAME + } + if (hasPreviewAnnotation) { return true } @@ -75,13 +88,23 @@ fun Resolver.hasDirectOrTransitivePreviewAnnotation(declaration: KSAnnotated, se return declaration.annotations.any { annotation -> val annotationQualifiedName = annotation.annotationType.resolve().declaration.qualifiedName val classDeclaration = annotationQualifiedName?.let { getClassDeclarationByName(it) } - classDeclaration?.let { hasDirectOrTransitivePreviewAnnotation(classDeclaration, seenAnnotations) } ?: false + classDeclaration?.let { + hasDirectOrTransitivePreviewAnnotation(classDeclaration, seenAnnotations) + } ?: false } } -fun Resolver.findAllDirectOrTransitivePreviewAnnotations(annotation: KSAnnotation, seenAnnotations: MutableSet = mutableSetOf()): List { - val classDeclaration = annotation.annotationType.resolve().declaration.qualifiedName?.let { getClassDeclarationByName(it) } - val isPreviewAnnotation = classDeclaration?.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME +fun Resolver.findAllDirectOrTransitivePreviewAnnotations( + annotation: KSAnnotation, + seenAnnotations: MutableSet = mutableSetOf(), +): List { + val classDeclaration = annotation.annotationType.resolve().declaration.qualifiedName?.let { + getClassDeclarationByName( + it + ) + } + val isPreviewAnnotation = + classDeclaration?.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME // Annotations can recursively reference each other so be sure to have a base recursion case // @Preview itself can't have a recursive relation so we can exclude them from our check @@ -91,11 +114,12 @@ fun Resolver.findAllDirectOrTransitivePreviewAnnotations(annotation: KSAnnotatio seenAnnotations.add(classDeclaration) - val currentPreviewAnnotations = if (classDeclaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME) { - listOf(annotation) - } else { - emptyList() - } + val currentPreviewAnnotations = + if (classDeclaration.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME) { + listOf(annotation) + } else { + emptyList() + } val nestedPreviewAnnotations = classDeclaration.annotations.flatMap { findAllDirectOrTransitivePreviewAnnotations(it, seenAnnotations)