From c1a54eb4cdc1f06cbb0b47d7ce58e639d56404bd Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 5 Jul 2024 10:22:22 +0800 Subject: [PATCH] feat(variable): add SystemInfoVariable and resolver Add SystemInfoVariable enum with system information variables and resolver to fetch and display system information in the completion data provider. --- .../resolver/SystemInfoVariableResolver.kt | 129 ++++++++++++------ .../dataprovider/CompositeVariableProvider.kt | 5 + 2 files changed, 89 insertions(+), 45 deletions(-) diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/variable/resolver/SystemInfoVariableResolver.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/variable/resolver/SystemInfoVariableResolver.kt index 6162ea748..49e2feeb8 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/variable/resolver/SystemInfoVariableResolver.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/variable/resolver/SystemInfoVariableResolver.kt @@ -1,59 +1,98 @@ package com.phodal.shirelang.compiler.variable.resolver import com.intellij.openapi.application.ApplicationInfo +import com.phodal.shirecore.provider.variable.model.ToolchainVariable import com.phodal.shirelang.compiler.variable.base.VariableResolver import com.phodal.shirelang.compiler.variable.base.VariableResolverContext import java.util.* + +enum class SystemInfoVariable( + override val variableName: String, + override val description: String, +) : ToolchainVariable { + OS_NAME("os.name", "The name of the operating system") { + override var value: Any? + get() = System.getProperty("os.name") + set(_) {} + }, + OS_VERSION("os.version", "The version of the operating system") { + override var value: Any? + get() = System.getProperty("os.version") + set(_) {} + }, + OS_ARCH("os.arch", "The architecture of the operating system") { + override var value: Any? + get() = System.getProperty("os.arch") + set(_) {} + }, + + IDE_NAME("ide.name", "The name of the IDE") { + override var value: Any? + get() = System.getProperty("idea.platform.prefix", "idea") + set(_) {} + }, + IDE_VERSION("ide.version", "The version of the IDE") { + override var value: Any? + get() = ApplicationInfo.getInstance().build.asString() + set(_) {} + }, + IDE_CODE("ide.code", "The code of the IDE") { + override var value: Any? + get() = ApplicationInfo.getInstance().build.productCode + set(_) {} + }, + + TIMEZONE("timezone", "The timezone") { + override var value: Any? + get() = TimeZone.getDefault().displayName + set(_) {} + }, + DATE("date", "The current date") { + override var value: Any? + get() = Calendar.getInstance().time + set(_) {} + }, + TODAY("today", "Today's date") { + override var value: Any? + get() = Calendar.getInstance().time + set(_) {} + }, + NOW("now", "The current time in milliseconds") { + override var value: Any? + get() = System.currentTimeMillis() + set(_) {} + }, + + LOCALE("locale", "The default locale") { + override var value: Any? + get() = Locale.getDefault().toString() + set(_) {} + }; + + companion object { + fun from(variableName: String): SystemInfoVariable? { + return values().firstOrNull { it.variableName == variableName } + } + + fun all(): List { + return values().toList() + } + } +} + /** - * SystemInfoVariable is a class that provides a way to resolve system information variables. - * like: - * - OS, OS Version.. - * - IDE information, version, etc. - * - Timezone, locale, etc. + * SystemInfoVariableResolver is a class that provides a way to resolve system information variables. */ class SystemInfoVariableResolver( - private val context: VariableResolverContext + private val context: VariableResolverContext, ) : VariableResolver { override fun resolve(): Map { - return mapOf( - "os" to fetchOsData(), - "ide" to fetchIDEData(), - "time" to fetchTimezoneData(), - "locale" to fetchLocaleData() - ) - } - - private fun fetchOsData(): Map { - return mapOf( - "name" to System.getProperty("os.name"), - "version" to System.getProperty("os.version"), - "arch" to System.getProperty("os.arch") - ) - } - - /** - * Get data from [com.intellij.util.PlatformUtils] - */ - private fun fetchIDEData(): Map { - val buildNumber = ApplicationInfo.getInstance().build - return mapOf( - "name" to System.getProperty("idea.platform.prefix", "idea"), - "version" to buildNumber.asString(), - "code" to buildNumber.productCode - ) - } - - private fun fetchTimezoneData(): Map { - return mapOf( - "timezone" to TimeZone.getDefault().displayName, - "date" to Calendar.getInstance().time, - "today" to Calendar.getInstance().time, - "now" to System.currentTimeMillis() - ) - } + val result = mutableMapOf() + SystemInfoVariable.all().forEach { + result[it.variableName] = it.value!! + } - private fun fetchLocaleData(): Map { - return mapOf("locale" to Locale.getDefault().toString()) + return result } -} +} \ No newline at end of file diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/completion/dataprovider/CompositeVariableProvider.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/completion/dataprovider/CompositeVariableProvider.kt index 363c827e4..45e8b97d5 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/completion/dataprovider/CompositeVariableProvider.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/completion/dataprovider/CompositeVariableProvider.kt @@ -3,6 +3,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 +import com.phodal.shirelang.compiler.variable.resolver.SystemInfoVariable data class VariableDisplay( val name: String, @@ -30,6 +31,10 @@ object CompositeVariableProvider { results.add(VariableDisplay(it.variableName, it.description, 70.0)) } + SystemInfoVariable.all().forEach { + results.add(VariableDisplay(it.variableName, it.description, 60.0)) + } + return results } } \ No newline at end of file