BigText, BigTextField, BigTextLabel



BigText is an in-memory data structure capable of manipulating large string (tested up to 100 MB) specialized for text editing applications. It is independent of UI frameworks, only supports JVM currently but is planned to go Kotlin Multiplatform. It is possible to implement your own text buffer for BigText to support other CharSequence.
BigTextField is a Jetpack Compose text field component utilizing BigText to provide the capability of editing large styleable text, and is designed to fix the known issues and limitations of the existing TextField, BasicTextField and BasicTextField2. It aims to replace BasicTextField. It is efficient enough to be used for syntax highlighting. It is available for desktop platforms only, but is planned to support all platforms that supported by Jetpack Compose Multiplatform.
BigTextLabel, a Jetpack Compose text component, is the read-only version of BigTextField.

Comparing with TextField / BasicTextField / BasicTextField2
The goods:
- It does not freeze when a 300K text, styled with text transformation, is rendered -- it renders instantly.
- It does not crash when a 10 MB text is feed in.
The bads:
- It does not embrace immutability.
- You cannot always access the full string, or it would be as slow as
BasicTextField.
- You write incremental transformation instead, or it would be as slow as .
Performance
Time
In the demo, typing in a 100 MB text reacts fast enough to every keystroke.
The recomposition time of a 10 MB text field was measured to be within 2ms.
Memory
A 100 MB BigText has an overhead of around 20 MB memory, so it consumes around 120 MB (Latin) or 220 MB (Unicode) memory in JVM initially. The overhead grows according to user inputs in order to support undo/redo and transformations, and is configurable. Note that use of UI framework may bring additional overheads.
Memory usage complexity: O(BigText buffer size) + O(transformed buffer size) + O(undo capacity) + O(redo capacity) + O(node count)
Limitations
Demo App
./gradlew :demo-ui-composable:run
Getting Started

To use without Jetpack Compose,
implementation("io.github.sunny-chung:bigtext-datastructure:<version>")
To use with Jetpack Compose,
implementation("io.github.sunny-chung:bigtext-ui-composable:<version>")
Usage Examples
Simplest
val bigTextFieldState by rememberConcurrentLargeAnnotatedBigTextFieldState("initial super big string", cacheKey)
val scrollState = rememberScrollState()
Box {
BigTextField(
textFieldState = bigTextFieldState,
color = Color.Black,
cursorColor = Color.Blue,
isSoftWrapEnabled = true,
scrollState = scrollState,
modifier = Modifier.fillMaxSize()
)
VerticalScrollbar(
adapter = rememberScrollbarAdapter(scrollState),
modifier = Modifier.align(Alignment.TopEnd).fillMaxHeight()
)
}
Asynchronous Loading with a Loading Spin
Transformation and More
https://github.com/user-attachments/assets/5733ccfc-8c4c-4678-a893-fa22994a1c7b
BigTextField(
textTransformation = remember { PhoneNumberIncrementalTransformation() },
isSingleLineInput = true,
maxInputLength = 3 + 4 + 4,
inputFilter = remember { { it.replace("[^0-9]".toRegex(), "") } },
)
Incremental Transformation & Decorator
Incremental Transformation means when the text is changed, the transformation only processes and updates the changed portions.
For an example of incremental transformation, see VariableIncrementalTransformation.
It can be a mess to implement incremental transformation. You may want to look at incremental parsers.
The time complexity of implementations of the following functions should be significantly lower than O(BigText's length). Ideally, O(lg(BigText's length)).
IncrementalTextTransformation<*>.beforeTextChange
IncrementalTextTransformation<*>.afterTextChange
BigTextDecorator.beforeTextChange
BigTextDecorator.afterTextChange
BigTextDecorator.onApplyDecorationOnOriginal
BigTextDecorator.onApplyDecorationOnTransformation
Decorator allows large amount of changing dense styles that do not change the text layout or character width. It is especially designed for syntax highlighting. Different from incremental transformation, decorator transforms styles just before they are rendered, only transforms text that within the viewport, and the transformation result is not persisted.
See GraphqlSyntaxHighlightDecorator is an example to use an incremental parser to handle input events, and use BigTextDecorator to apply syntax highlighting styles.
Transformation Offset Mapping
BigText offers two types of offset mapping:
- Block -- the cursor never goes into the transformation
- Incremental (it is a different thing to Incremental Transformation) -- the cursor can navigate through the transformation as if it is a normal text, until
min(length of original subsequence, length of transformed subsequence) has been reached
Different offset mapping can be mixed and applied to the same text. It depends on the transformation operations.
For inserts, it is always block-offset transformations. For each replacement, you specify which offset mapping type to take:
override fun afterTextChange(change: BigTextChangeEvent, transformer: BigTextTransformer, context: Unit) {
transformer.replace(
range = matchText.range,
text = AnnotatedString(matchText.value, spanStyle),
offsetMapping = BigTextTransformOffsetMapping.Incremental
)
transformer.replace(
range = matchText.range.endInclusive + 1 .. matchText.range.endInclusive + 5,
text = AnnotatedString("0123456789"),
offsetMapping = BigTextTransformOffsetMapping.Block
)
}
Modify the BigText value
Modify BigText directly:
val bigTextFieldState: BigTextFieldState by rememberConcurrentLargeAnnotatedBigTextFieldState("")
val text = bigTextFieldState.text
Button(
onClick = {
val selection = bigTextFieldState.viewState.selection
text.insertAt(selection.last + 1, "}}")
text.insertAt(selection.first, "\${{")
text.recordCurrentChangeSequenceIntoUndoHistory()
}
) {
Text("Transform")
}
BigTextField(
textFieldState = bigTextFieldState,
)
To move the cursor along with the input:
fun onPressEnterAddIndent(textState: BigTextFieldState) {
val newSpaces = "\n "
textState.replaceTextAtCursor(newSpaces)
}
Keep Using String for Compatibility
If you don't need to deal with big text and want to keep using String, you must use a unique cache key for each content with BigTextField. For example:
@Composable
fun MyTextField(initialValue: String, onValueChange: (String) -> Unit, recordType: RecordType, recordId: Long) {
val textState by rememberConcurrentLargeAnnotatedBigTextFieldState(initialValue, recordType, recordId )
BigTextField(
textFieldState = textState,
onTextChange = {
onValueChange(it.bigText.buildString())
},
)
}
The initialValue is used only in the first recomposition where cache keys are changed. It is then ignored until cache keys change.
Requesting focus (a temporary workaround available since v2.0.1)
To request focus before BigTextField is ready,
val focusRequester = remember { FocusRequester() }
CoreBigTextField(
onFinishInit = { focusRequester.requestFocus() }
)
If requestFocus() is invoked after BigTextField is ready, it would work without this workaround.
Reducing Latency while Using Many BigTexts or BigTextFields
It may experience significant delays (0.6+ seconds) while rendering 10+ BigTextField. It is possible to improve (not eliminate) the latency by configuring BigTextField to process synchronously.
BigTextField(
onHeavyComputation = AsyncOperation.Synchronous
)
FAQ
Q: Can it be used for general-purpose small text?
A: Sure, and use small or tiny text buffer to save memory.
val textState = rememberSaveable(cacheKeys) {
mutableStateOf(
BigTextFieldState(
createFromTinyAnnotatedString(AnnotatedString(initialValue)),
BigTextViewState()
)
)
}
Q: My text is small, and my application use String everywhere. Can BigTextField be used with String instead of BigText?
A: Yes. Refer to Keep Using String for Compatibility.
Q: The cursor goes into middle of a character when multiple fonts are used for the same text field! Is it a bug?
A: Only text value and text transformation support multiple fonts, not decorators. If it is a must to use decorator, use together with transformation -- transform it to a desired font first, then decorate it.
More References
The previous version of BigTextField, BigMonospaceTextField, is already in production use by the Hello HTTP software. There are also some incremental transformation/decorator examples. This project is a good production example for reference.