Skip to content

Commit

Permalink
feat(core): add reflection support for ToolchainVariable
Browse files Browse the repository at this point in the history
- Added reflection support to dynamically create instances of ToolchainVariable subclasses.
- Introduced a new method `all()` to retrieve all instances of ToolchainVariable subclasses.
  • Loading branch information
phodal committed Jul 5, 2024
1 parent 38161f5 commit 72ec463
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ project(":core") {
}

dependencies {
implementation("org.reflections:reflections:0.10.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
package com.phodal.shirecore.provider.variable.model

import org.reflections.Reflections
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.functions

interface ToolchainVariable {
val variableName: String;
var value: Any?;
val description: String;
val description: String

companion object {
private val subclasses: Set<KClass<out ToolchainVariable>> by lazy {
val reflections = Reflections("com.phodal.shirecore.provider.variable.model")
reflections.getSubTypesOf(ToolchainVariable::class.java)
.map { it.kotlin }
.toSet()
}

fun from(variableName: String): ToolchainVariable? {
return VcsToolchainVariable.from(variableName) ?: TerminalVariable.from(variableName)
for (subclass in subclasses) {
val companion = subclass.companionObjectInstance ?: continue
val fromFunction = companion::class.declaredFunctions.find { it.name == "from" } ?: continue
val result = fromFunction.call(companion, variableName) as? ToolchainVariable
if (result != null) {
return result
}
}
return null
}

fun all(): List<ToolchainVariable> {
val allVariables = mutableListOf<ToolchainVariable>()
for (subclass in subclasses) {
val valuesFunction = subclass.functions.find { it.name == "values" } ?: continue
val enumConstants = valuesFunction.call() as Array<ToolchainVariable>
allVariables.addAll(enumConstants)
}

return allVariables
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.phodal.shirelang.completion.dataprovider

import com.phodal.shirecore.provider.variable.model.PsiContextVariable
import com.phodal.shirecore.provider.variable.model.ToolchainVariable
import com.phodal.shirecore.provider.variable.model.VcsToolchainVariable

data class VariableDisplay(
Expand All @@ -25,6 +26,10 @@ object CompositeVariableProvider {
results.add(VariableDisplay(it.variableName, it.description, 80.0))
}

ToolchainVariable.all().forEach {
results.add(VariableDisplay(it.variableName, it.description, 70.0))
}

return results
}
}

0 comments on commit 72ec463

Please sign in to comment.