Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added visibility changer #8

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle-plugin-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api("io.kinference.primitives:primitives-annotations:0.1.24")
api("io.kinference.primitives:primitives-annotations:0.1.25-dev-2")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@file:GeneratePrimitives(DataType.ALL)
package test

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.GeneratePrimitives
import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.DataType
import io.kinference.primitives.types.PrimitiveType

@GenerateNameFromPrimitives
class AllPrimitiveTest {
@MakePublic
internal class AllPrimitiveTest {
val x: PrimitiveType? = null
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@file:GeneratePrimitives(DataType.FLOAT, DataType.INT)
package test

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.GeneratePrimitives
import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.*

@GenerateNameFromPrimitives
class ClassPrimitiveTest {
@MakePublic
internal class ClassPrimitiveTest {
val a = PrimitiveType.MIN_VALUE

companion object {
Expand All @@ -16,6 +16,6 @@ class ClassPrimitiveTest {
}
}

fun ClassPrimitiveTest.v() = Unit
internal fun ClassPrimitiveTest.v() = Unit


Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.*

@GenerateNameFromPrimitives
class FilteringPrimitiveTest {
@MakePublic
internal class FilteringPrimitiveTest {
fun all(x: PrimitiveType) : PrimitiveType {
val y = x
return y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import io.kinference.primitives.types.*
import io.kinference.primitives.types.toPrimitive

@GenerateNameFromPrimitives
class NumbersPrimitiveTest {
@MakePublic
internal class NumbersPrimitiveTest {
fun test(): PrimitiveType {
val x: PrimitiveType = (0).toPrimitive()
val y: PrimitiveType = (x + x).toPrimitive()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@file:GeneratePrimitives(DataType.NUMBER)
package test

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.GeneratePrimitives
import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.DataType


@GenerateNameFromPrimitives
object ObjectPrimitiveTest {
@MakePublic
internal object ObjectPrimitiveTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package test.cross

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.GeneratePrimitives
import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.DataType

@GenerateNameFromPrimitives
class PrimitiveFirst {
@MakePublic
internal class PrimitiveFirst {
val second = PrimitiveSecond()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package test.cross

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.GeneratePrimitives
import io.kinference.primitives.annotations.*
import io.kinference.primitives.types.DataType

@GenerateNameFromPrimitives
class PrimitiveSecond {
@MakePublic
internal class PrimitiveSecond {
val first = PrimitiveFirst()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.kinference.primitives.annotations

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class MakePublic()
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifier
import org.jetbrains.kotlin.resolve.BindingContext
import java.io.File

Expand Down Expand Up @@ -60,6 +61,14 @@ internal class PrimitiveGenerator(
primitiveContext = tmp
}

override fun visitModifierList(list: KtModifierList) {
if (replacementProcessor.shouldChangeVisibilityModifier(list)) {
replacementProcessor.prepareReplaceText(list.visibilityModifier(), "public")
}

super.visitModifierList(list)
}

override fun visitWhiteSpace(space: PsiWhiteSpace) {
if (removalProcessor.shouldRemoveWhiteSpace(space)) return

Expand Down Expand Up @@ -160,6 +169,11 @@ internal class PrimitiveGenerator(
}

override fun visitLeafElement(element: LeafPsiElement) {
if (replacementProcessor.haveReplaceText(element)) {
builder.append(replacementProcessor.getReplacement(element))
return
}

if (element.elementType != KtTokens.IDENTIFIER) {
builder.append(element.text)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ internal fun KtAnnotationEntry.isPluginAnnotation(context: BindingContext): Bool
isAnnotation<BindPrimitives.Type2>(context) ||
isAnnotation<BindPrimitives.Type3>(context) ||
isAnnotation<FilterPrimitives>(context) ||
isAnnotation<SpecifyPrimitives>(context)
isAnnotation<SpecifyPrimitives>(context) ||
isAnnotation<MakePublic>(context)
}

internal fun DeclarationDescriptor.isNamedFunction() = findPsi() is KtNamedFunction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kinference.primitives.generator.processor

import io.kinference.primitives.annotations.GenerateNameFromPrimitives
import io.kinference.primitives.annotations.MakePublic
import io.kinference.primitives.generator.*
import io.kinference.primitives.generator.errors.require
import io.kinference.primitives.handler.Primitive
Expand All @@ -9,8 +10,12 @@ import io.kinference.primitives.utils.psi.forced
import io.kinference.primitives.utils.psi.isAnnotatedWith
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.com.intellij.openapi.util.Key
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifier
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe

Expand All @@ -24,6 +29,8 @@ internal class ReplacementProcessor(private val context: BindingContext, private
}
}

private val REPLACE_TEXT: Key<String> = Key.create("REPLACE_TEXT_IN_MODIFIER")

private val defaultReplacements: Map<String, (Primitive<*, *>) -> String> = mapOf(
(DataType::class.qualifiedName!! + ".Companion.${DataType.Companion::CurrentPrimitive.name}") to { it.dataType.name },
(PrimitiveType::class.java.`package`.name + ".toPrimitive") to this::toType,
Expand Down Expand Up @@ -83,4 +90,22 @@ internal class ReplacementProcessor(private val context: BindingContext, private
}
}

fun shouldChangeVisibilityModifier(list: KtModifierList): Boolean {
val owner = list.owner
val visibilityModifier = list.visibilityModifier()

return visibilityModifier != null && owner is KtAnnotated && owner.isAnnotatedWith<MakePublic>(context)
}

fun prepareReplaceText(psi: PsiElement?, newText: String) {
psi?.putUserData(REPLACE_TEXT, newText)
}

fun haveReplaceText(psi: LeafPsiElement): Boolean {
return psi.getUserData(REPLACE_TEXT) != null
}

fun getReplacement(psi: LeafPsiElement): String {
return psi.getUserData(REPLACE_TEXT)!!
}
}
Loading