Improve: extract common method

This commit is contained in:
zt515
2017-12-04 23:44:14 +08:00
parent 3251bd8a40
commit a631a24ccf
5 changed files with 24 additions and 13 deletions

View File

@ -8,7 +8,7 @@ import io.neolang.runtime.type.NeoLangValue
class ConfigVisitor : IVisitorCallback { class ConfigVisitor : IVisitorCallback {
private var currentContext: NeoLangContext? = null private var currentContext: NeoLangContext? = null
fun getContext(contextPath: Array<String>) : NeoLangContext { fun getContext(contextPath: Array<String>): NeoLangContext {
var context = getCurrentContext() var context = getCurrentContext()
contextPath.forEach { contextPath.forEach {
context = context.getChild(it) context = context.getChild(it)
@ -16,11 +16,11 @@ class ConfigVisitor : IVisitorCallback {
return context return context
} }
fun getAttribute(contextPath: Array<String>, attrName: String) : NeoLangValue { fun getAttribute(contextPath: Array<String>, attrName: String): NeoLangValue {
return getContext(contextPath).getAttribute(attrName) return getContext(contextPath).getAttribute(attrName)
} }
fun getArray(contextPath: Array<String>, arrayName: String) : NeoLangArray { fun getArray(contextPath: Array<String>, arrayName: String): NeoLangArray {
// We use NeoLangContext as arrays and array elements now // We use NeoLangContext as arrays and array elements now
return NeoLangArray.createFromContext(getContext(contextPath).getChild(arrayName)) return NeoLangArray.createFromContext(getContext(contextPath).getChild(arrayName))
} }
@ -46,7 +46,7 @@ class ConfigVisitor : IVisitorCallback {
override fun onExitContext() { override fun onExitContext() {
val context = currentContext val context = currentContext
if (context != null && context.parent != null) { if (context?.parent != null) {
this.currentContext = context.parent this.currentContext = context.parent
} }
} }
@ -54,4 +54,9 @@ class ConfigVisitor : IVisitorCallback {
override fun getCurrentContext(): NeoLangContext { override fun getCurrentContext(): NeoLangContext {
return currentContext!! return currentContext!!
} }
fun getStringValue(path: Array<String>, name: String): String? {
val value = this.getAttribute(path, name)
return if (value.isValid()) value.asString() else null
}
} }

View File

@ -183,12 +183,10 @@ open class NeoColorScheme : CodeGenObject {
} }
private fun getMetaByVisitor(visitor: ConfigVisitor, metaName: String): String? { private fun getMetaByVisitor(visitor: ConfigVisitor, metaName: String): String? {
val value = visitor.getAttribute(COLOR_META_PATH, metaName) return visitor.getStringValue(COLOR_META_PATH, metaName)
return if (value.isValid()) value.asString() else null
} }
private fun getColorByVisitor(visitor: ConfigVisitor, colorName: String): String? { private fun getColorByVisitor(visitor: ConfigVisitor, colorName: String): String? {
val value = visitor.getAttribute(COLOR_PATH, colorName) return visitor.getStringValue(COLOR_PATH, colorName)
return if (value.isValid()) value.asString() else null
} }
} }

View File

@ -101,7 +101,6 @@ class NeoExtraKey {
} }
private fun getMetaByVisitor(visitor: ConfigVisitor, metaName: String): String? { private fun getMetaByVisitor(visitor: ConfigVisitor, metaName: String): String? {
val value = visitor.getAttribute(EKS_META_CONTEXT_PATH, metaName) return visitor.getStringValue(EKS_META_CONTEXT_PATH, metaName)
return if (value.isValid()) value.asString() else null
} }
} }

View File

@ -55,9 +55,9 @@ class FontComponent : NeoComponent {
fun reloadFonts(): Boolean { fun reloadFonts(): Boolean {
fonts.clear() fonts.clear()
fonts.put("Android Monospace", NeoFont(Typeface.MONOSPACE)) fonts.put("Monospace", NeoFont(Typeface.MONOSPACE))
fonts.put("Android Sans Serif", NeoFont(Typeface.SANS_SERIF)) fonts.put("Sans Serif", NeoFont(Typeface.SANS_SERIF))
fonts.put("Android Serif", NeoFont(Typeface.SERIF)) fonts.put("Serif", NeoFont(Typeface.SERIF))
val fontDir = File(NeoTermPath.FONT_PATH) val fontDir = File(NeoTermPath.FONT_PATH)
for (file in fontDir.listFiles({ pathname -> pathname.name.endsWith(".ttf") })) { for (file in fontDir.listFiles({ pathname -> pathname.name.endsWith(".ttf") })) {
val fontName = fontName(file) val fontName = fontName(file)

View File

@ -0,0 +1,9 @@
package io.neoterm.component.profile
/**
* @author kiva
*/
class Profile {
lateinit var profileFont: String
lateinit var profileColorScheme: String
}