generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
89 additions
and
45 deletions.
There are no files selected for viewing
129 changes: 84 additions & 45 deletions
129
...main/kotlin/com/phodal/shirelang/compiler/variable/resolver/SystemInfoVariableResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters