KodeView icon indicating copy to clipboard operation
KodeView copied to clipboard

Address slow responsiveness in CodeEditText

Open dgmltn opened this issue 1 year ago • 2 comments

Thanks a lot for your contribution to Compose MP!

Have you given any thought to performance? For instance, CodeEditText with the onValueChange -> highlights loop builds a new Highlights object on every keypress. This takes a long time, especially if code is long. But even if code is short, it is still too slow. Here's a video, trying to type "world":

https://github.com/SnipMeDev/KodeView/assets/698270/f24b98e5-3e11-4eb0-97da-008e8e6120a1

One solution is to have a separate coroutine applying color to the new text after it has changed. The coroutine would be cancelled if the text changed again quickly, before the last round of colorization finished. But that's ok!

I might call it like this:

@Composable
fun CodeEditor2(
    code: String,
    onCodeChanged: (String) -> Unit,
    modifier: Modifier = Modifier,
) {
    val highlightBuilder = Highlights
        .Builder(
            language = SyntaxLanguage.JAVASCRIPT,
            theme = SyntaxThemes.darcula(true),
        )
    CodeEditText(
        modifier = modifier,
        code = code,
        onValueChange = onCodeChanged,
        highlightBuilder = highlightBuilder,
        colors = TextFieldDefaults.colors(),
    )
}

and the HighlightBuilder could be used internally, inside CodeEditText. At first I would expect new keys I type to just use a default color, and then after a short delay, their color might change.

dgmltn avatar Jan 25 '24 20:01 dgmltn

@dgmltn Hi 👋

Thank you for such a descriptive issue posted :)

Yes, I know that the performance is not top-notch.

Main focus was put on making this work and next iterations should improve speed.

Thanks again for some code snippets, I will take them into consideration and yes coroutines can help us here :)

tmaxxdd avatar Jan 27 '24 17:01 tmaxxdd

Update on that.

I am sorry to say that but I had no time to address this problem.

I think I will redo that once again in next months

tmaxxdd avatar Aug 11 '24 06:08 tmaxxdd

@dgmltn The new #32 should fix the issue

tmaxxdd avatar Nov 03 '24 20:11 tmaxxdd