Skip to content

Commit

Permalink
refactor: Change the project indentation to 4 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
whitescent committed Feb 14, 2025
1 parent 9cd673e commit 8f3a200
Show file tree
Hide file tree
Showing 69 changed files with 2,530 additions and 2,530 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
charset = utf-8
indent_size = 2
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
86 changes: 43 additions & 43 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import com.diffplug.gradle.spotless.SpotlessExtension

plugins {
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.dokka) apply false
alias(libs.plugins.spotless) apply false
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.dokka) apply false
alias(libs.plugins.spotless) apply false
}

subprojects {
version = findProperty("storytale.deploy.version")
?: error("'storytale.deploy.version' was not set")
version = findProperty("storytale.deploy.version")
?: error("'storytale.deploy.version' was not set")

plugins.withId("maven-publish") {
configureIfExists<PublishingExtension> {
repositories {
maven {
name = "ComposeRepo"
setUrl(System.getenv("COMPOSE_REPO_URL"))
credentials {
username = System.getenv("COMPOSE_REPO_USERNAME")
password = System.getenv("COMPOSE_REPO_KEY")
}
plugins.withId("maven-publish") {
configureIfExists<PublishingExtension> {
repositories {
maven {
name = "ComposeRepo"
setUrl(System.getenv("COMPOSE_REPO_URL"))
credentials {
username = System.getenv("COMPOSE_REPO_USERNAME")
password = System.getenv("COMPOSE_REPO_KEY")
}
}
}
}
}
}
}
plugins.apply(rootProject.libs.plugins.spotless.get().pluginId)
extensions.configure<SpotlessExtension> {
kotlin {
target("src/**/*.kt")
targetExclude("src/test/resources/**")
ktlint(libs.ktlint.get().version)
.editorConfigOverride(
mapOf(
"ktlint_compose_modifier-missing-check" to "disabled",
"ktlint_compose_compositionlocal-allowlist" to "disabled",
),
)
.customRuleSets(listOf(libs.composeRules.get().toString()))
}
kotlinGradle {
ktlint(libs.ktlint.get().version)
plugins.apply(rootProject.libs.plugins.spotless.get().pluginId)
extensions.configure<SpotlessExtension> {
kotlin {
target("src/**/*.kt")
targetExclude("src/test/resources/**")
ktlint(libs.ktlint.get().version)
.editorConfigOverride(
mapOf(
"indent_size" to "4",
"ktlint_compose_modifier-missing-check" to "disabled",
"ktlint_compose_compositionlocal-allowlist" to "disabled",
),
)
.customRuleSets(listOf(libs.composeRules.get().toString()))
}
kotlinGradle {
ktlint(libs.ktlint.get().version)
}
}
}
}

inline fun <reified T> Project.configureIfExists(fn: T.() -> Unit) {
extensions.findByType(T::class.java)?.fn()
extensions.findByType(T::class.java)?.fn()
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration

class StorytaleComponentRegistrar : CompilerPluginRegistrar() {
override val supportsK2: Boolean get() = true
override val supportsK2: Boolean get() = true

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
Companion.registerExtensions(this)
}
override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
Companion.registerExtensions(this)
}

companion object {
fun registerExtensions(extensionStorage: ExtensionStorage) = with(extensionStorage) {
IrGenerationExtension.registerExtension(StorytaleLoweringExtension())
companion object {
fun registerExtensions(extensionStorage: ExtensionStorage) = with(extensionStorage) {
IrGenerationExtension.registerExtension(StorytaleLoweringExtension())
}
}
}
}
212 changes: 106 additions & 106 deletions modules/compiler-plugin/src/kotlin/StorytaleLoweringExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,135 +42,135 @@ import org.jetbrains.kotlin.text
private val storytaleFqn = FqName("org.jetbrains.compose.storytale")

private class AddCodeSnippetToStoriesLowering(context: IrPluginContext) : BodyLoweringPass {
val transformer = ReplaceStoryCallWithItsSuccessorWithCodeParameter(context)
override fun lower(irBody: IrBody, container: IrDeclaration) {
container.transform(transformer, null)
}
val transformer = ReplaceStoryCallWithItsSuccessorWithCodeParameter(context)
override fun lower(irBody: IrBody, container: IrDeclaration) {
container.transform(transformer, null)
}

class ReplaceStoryCallWithItsSuccessorWithCodeParameter(private val context: IrPluginContext) : IrElementTransformerVoidWithContext() {
private val storyFunction = CallableId(storytaleFqn, Name.identifier("story"))
class ReplaceStoryCallWithItsSuccessorWithCodeParameter(private val context: IrPluginContext) : IrElementTransformerVoidWithContext() {
private val storyFunction = CallableId(storytaleFqn, Name.identifier("story"))

@OptIn(UnsafeDuringIrConstructionAPI::class)
override fun visitCall(expression: IrCall): IrExpression {
val owner = expression.symbol.owner
if (!owner.isTopLevel || owner.callableId != storyFunction) return super.visitCall(expression)
@OptIn(UnsafeDuringIrConstructionAPI::class)
override fun visitCall(expression: IrCall): IrExpression {
val owner = expression.symbol.owner
if (!owner.isTopLevel || owner.callableId != storyFunction) return super.visitCall(expression)

val callee = expression.getValueArgument(2)
val storyDescriber = when (callee) {
is IrFunctionExpression -> callee.function
else -> error("Unexpected callee: $callee")
}
val callee = expression.getValueArgument(2)
val storyDescriber = when (callee) {
is IrFunctionExpression -> callee.function
else -> error("Unexpected callee: $callee")
}

val sourceCode = storyDescriber.getFileSourceCode() ?: return super.visitCall(expression)
val storyCodeSnippet = sourceCode.substring(storyDescriber.startOffset + 1, storyDescriber.endOffset - 1).trimIndent()
val sourceCode = storyDescriber.getFileSourceCode() ?: return super.visitCall(expression)
val storyCodeSnippet = sourceCode.substring(storyDescriber.startOffset + 1, storyDescriber.endOffset - 1).trimIndent()

val storyTitle = storyDescriber.file.name.substringAfterLast("/").substringBeforeLast(".story.kt")
val storyTitle = storyDescriber.file.name.substringAfterLast("/").substringBeforeLast(".story.kt")

expression.putValueArgument(0, storyCodeSnippet.toIrConst(context.irBuiltIns.stringType))
expression.putValueArgument(1, storyTitle.toIrConst(context.irBuiltIns.stringType))
expression.putValueArgument(2, callee)
expression.putValueArgument(0, storyCodeSnippet.toIrConst(context.irBuiltIns.stringType))
expression.putValueArgument(1, storyTitle.toIrConst(context.irBuiltIns.stringType))
expression.putValueArgument(2, callee)

return super.visitCall(expression)
}
return super.visitCall(expression)
}

private fun IrDeclaration.getFileSourceCode(): CharSequence? {
return (file.metadata as FirMetadataSource).fir.source.text
private fun IrDeclaration.getFileSourceCode(): CharSequence? {
return (file.metadata as FirMetadataSource).fir.source.text
}
}
}
}

private class MentionAllStoriesGettersInsideMainFunctionLowering(
private val context: IrPluginContext,
private val context: IrPluginContext,
) : DeclarationTransformer {
private val allFirstStoriesGetter = mutableListOf<IrStatement>()
private val alreadyMentionedFiles = mutableSetOf<IrFile>()
private val allFirstStoriesGetter = mutableListOf<IrStatement>()
private val alreadyMentionedFiles = mutableSetOf<IrFile>()

private val storyFactoryId = ClassId(storytaleFqn, Name.identifier("StoryDelegate"))

private val kotlinNativeFqn = FqName("kotlin.native")
private val hideFromObjcId = ClassId(kotlinNativeFqn, Name.identifier("HiddenFromObjC"))

private val anyType = context.irBuiltIns.anyType
private val unitType = context.irBuiltIns.unitType
private val storyFactory = context.referenceClass(storyFactoryId)
private val hiddenFromObjCAnnotation = context.referenceConstructors(hideFromObjcId).single()

private val storyFactoryId = ClassId(storytaleFqn, Name.identifier("StoryDelegate"))
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
val factoryType = storyFactory?.defaultType ?: return null

private val kotlinNativeFqn = FqName("kotlin.native")
private val hideFromObjcId = ClassId(kotlinNativeFqn, Name.identifier("HiddenFromObjC"))
if (declaration is IrProperty && declaration.isStory(factoryType)) {
declaration.addHiddenFromObjCAnnotation()
declaration.mentionGetterInsideMainFunction()
}

private val anyType = context.irBuiltIns.anyType
private val unitType = context.irBuiltIns.unitType
private val storyFactory = context.referenceClass(storyFactoryId)
private val hiddenFromObjCAnnotation = context.referenceConstructors(hideFromObjcId).single()
if (declaration is IrSimpleFunction && declaration.isGeneratedMainViewController()) {
declaration.addAllMentionedGettersIntoBody()
}

override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
val factoryType = storyFactory?.defaultType ?: return null
return null
}

private fun IrProperty.mentionGetterInsideMainFunction() {
if (parent in alreadyMentionedFiles) return

val getter = getter!!

alreadyMentionedFiles.add(parent as IrFile)
allFirstStoriesGetter.add(
IrCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
getter.returnType,
getter.symbol,
0,
),
)
}

if (declaration is IrProperty && declaration.isStory(factoryType)) {
declaration.addHiddenFromObjCAnnotation()
declaration.mentionGetterInsideMainFunction()
private fun IrSimpleFunction.addAllMentionedGettersIntoBody() {
(body as? IrBlockBody)?.statements?.add(
0,
IrObservableBlockImpl(unitType, allFirstStoriesGetter),
)
}

if (declaration is IrSimpleFunction && declaration.isGeneratedMainViewController()) {
declaration.addAllMentionedGettersIntoBody()
private fun IrProperty.isStory(factoryType: IrType) = isTopLevel &&
isDelegated &&
backingField?.let {
it.origin == IrDeclarationOrigin.PROPERTY_DELEGATE && it.type == factoryType
} ?: false

private fun IrSimpleFunction.isGeneratedMainViewController() = isTopLevel &&
visibility.isPublicAPI &&
kotlinFqName.asString() == "org.jetbrains.compose.storytale.generated.MainViewController"

private fun IrDeclaration.addHiddenFromObjCAnnotation() {
val annotation = IrConstructorCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
anyType,
hiddenFromObjCAnnotation,
0,
0,
)
context.metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement(this, annotation)
}

return null
}

private fun IrProperty.mentionGetterInsideMainFunction() {
if (parent in alreadyMentionedFiles) return

val getter = getter!!

alreadyMentionedFiles.add(parent as IrFile)
allFirstStoriesGetter.add(
IrCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
getter.returnType,
getter.symbol,
0,
),
)
}

private fun IrSimpleFunction.addAllMentionedGettersIntoBody() {
(body as? IrBlockBody)?.statements?.add(
0,
IrObservableBlockImpl(unitType, allFirstStoriesGetter),
)
}

private fun IrProperty.isStory(factoryType: IrType) = isTopLevel &&
isDelegated &&
backingField?.let {
it.origin == IrDeclarationOrigin.PROPERTY_DELEGATE && it.type == factoryType
} ?: false

private fun IrSimpleFunction.isGeneratedMainViewController() = isTopLevel &&
visibility.isPublicAPI &&
kotlinFqName.asString() == "org.jetbrains.compose.storytale.generated.MainViewController"

private fun IrDeclaration.addHiddenFromObjCAnnotation() {
val annotation = IrConstructorCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
anyType,
hiddenFromObjCAnnotation,
0,
0,
)
context.metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement(this, annotation)
}

private class IrObservableBlockImpl(
override var type: IrType,
override val statements: MutableList<IrStatement>,
) : IrBlock() {
override val startOffset = UNDEFINED_OFFSET
override val endOffset = UNDEFINED_OFFSET
override var origin: IrStatementOrigin? = null
override var attributeOwnerId: IrAttributeContainer = this
override var originalBeforeInline: IrAttributeContainer? = null
}
private class IrObservableBlockImpl(
override var type: IrType,
override val statements: MutableList<IrStatement>,
) : IrBlock() {
override val startOffset = UNDEFINED_OFFSET
override val endOffset = UNDEFINED_OFFSET
override var origin: IrStatementOrigin? = null
override var attributeOwnerId: IrAttributeContainer = this
override var originalBeforeInline: IrAttributeContainer? = null
}
}

class StorytaleLoweringExtension : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
MentionAllStoriesGettersInsideMainFunctionLowering(pluginContext).lower(moduleFragment)
AddCodeSnippetToStoriesLowering(pluginContext).lower(moduleFragment)
}
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
MentionAllStoriesGettersInsideMainFunctionLowering(pluginContext).lower(moduleFragment)
AddCodeSnippetToStoriesLowering(pluginContext).lower(moduleFragment)
}
}
Loading

0 comments on commit 8f3a200

Please sign in to comment.