Skip to content

Commit

Permalink
feat(variable): add SystemInfoVariable and resolver
Browse files Browse the repository at this point in the history
Add SystemInfoVariable enum with system information variables and resolver to fetch and display system information in the completion data provider.
  • Loading branch information
phodal committed Jul 5, 2024
1 parent 72ec463 commit c1a54eb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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<SystemInfoVariable> {
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<String, Any> {
return mapOf(
"os" to fetchOsData(),
"ide" to fetchIDEData(),
"time" to fetchTimezoneData(),
"locale" to fetchLocaleData()
)
}

private fun fetchOsData(): Map<String, Any> {
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<String, Any> {
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<String, Any> {
return mapOf(
"timezone" to TimeZone.getDefault().displayName,
"date" to Calendar.getInstance().time,
"today" to Calendar.getInstance().time,
"now" to System.currentTimeMillis()
)
}
val result = mutableMapOf<String, Any>()
SystemInfoVariable.all().forEach {
result[it.variableName] = it.value!!
}

private fun fetchLocaleData(): Map<String, Any> {
return mapOf("locale" to Locale.getDefault().toString())
return result
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}

0 comments on commit c1a54eb

Please sign in to comment.