From 4c270c7f6e81aaa9b2cd0cd0d3bfa9406807fae0 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Mon, 28 Nov 2022 14:52:20 +0100 Subject: [PATCH] [kotlin-tooling-core] Implement HasExtras utils ^KT-55112 Verification Pending --- .../kotlin/tooling/core/HasExtras.kt | 30 ++++++++++++ .../kotlin/tooling/core/HasExtrasTest.kt | 49 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/HasExtras.kt create mode 100644 libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/HasExtrasTest.kt diff --git a/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/HasExtras.kt b/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/HasExtras.kt new file mode 100644 index 00000000000..b36ca8715ae --- /dev/null +++ b/libraries/tools/kotlin-tooling-core/src/main/kotlin/org/jetbrains/kotlin/tooling/core/HasExtras.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.tooling.core + +import kotlin.reflect.KProperty + + +interface HasExtras { + val extras: Extras +} + +interface HasMutableExtras : HasExtras { + override val extras: MutableExtras +} + +operator fun Extras.Key.getValue(receiver: HasExtras, property: KProperty<*>): T? { + return receiver.extras[this] +} + + +operator fun Extras.Key.setValue(receiver: HasMutableExtras, property: KProperty<*>, value: T?) { + if (value == null) { + receiver.extras.remove(this) + } else { + receiver.extras[this] = value + } +} diff --git a/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/HasExtrasTest.kt b/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/HasExtrasTest.kt new file mode 100644 index 00000000000..a703fa6b69d --- /dev/null +++ b/libraries/tools/kotlin-tooling-core/src/test/kotlin/org/jetbrains/kotlin/tooling/core/HasExtrasTest.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.tooling.core + +import org.jetbrains.kotlin.tooling.core.HasExtrasTest.Subject.Companion.value1 +import org.jetbrains.kotlin.tooling.core.HasExtrasTest.Subject.Companion.value2 +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class HasExtrasTest { + + class Subject : HasMutableExtras { + override val extras: MutableExtras = mutableExtrasOf() + + companion object { + var Subject.value1: Int? by extrasKeyOf() + var Subject.value2: Int? by extrasKeyOf("2") + } + } + + @Test + fun `test - non-null value`() { + val subject = Subject() + assertNull(subject.value1) + assertNull(subject.value2) + + subject.value1 = 1 + assertEquals(1, subject.value1) + assertNull(subject.value2) + + subject.value2 = 2 + assertEquals(1, subject.value1) + assertEquals(2, subject.value2) + } + + @Test + fun `test - set null`() { + val subject = Subject() + subject.value1 = 1 + assertEquals(1, subject.value1) + + subject.value1 = null + assertNull(subject.value1) + } +}