diff --git a/app/src/main/java/com/osfans/trime/core/RimeProto.kt b/app/src/main/java/com/osfans/trime/core/RimeProto.kt index f789831aae..5f6004aba4 100644 --- a/app/src/main/java/com/osfans/trime/core/RimeProto.kt +++ b/app/src/main/java/com/osfans/trime/core/RimeProto.kt @@ -19,16 +19,29 @@ class RimeProto { val composition: Composition, val menu: Menu, val input: String, - val caretPos: Int, + private val _caretPos: Int, ) { + /** + * Same with [Composition.cursorPos], just directly assign to it. + */ + val caretPos = composition.cursorPos + data class Composition( - val length: Int = 0, - val cursorPos: Int = 0, + private val _length: Int = 0, + private val _cursorPos: Int = 0, private val _selStart: Int = 0, private val _selEnd: Int = 0, val preedit: String? = null, val commitTextPreview: String? = null, ) { + /** + * Actually we can directly use [String.length] on [preedit], but + * we add it here for the sake of completeness as it is semantically correct + */ + val length: Int = preedit.run { if (isNullOrEmpty()) 0 else String(toByteArray(), 0, _length).length } + + val cursorPos: Int = preedit.run { if (isNullOrEmpty()) 0 else String(toByteArray(), 0, _cursorPos).length } + val selStart: Int = preedit.run { if (isNullOrEmpty()) 0 else String(toByteArray(), 0, _selStart).length } val selEnd: Int = preedit.run { if (isNullOrEmpty()) 0 else String(toByteArray(), 0, _selEnd).length }