Skip to content

Language keywords highlighted even where they should not #67

@ablenesi

Description

@ablenesi

See the following python example:

import polars as pl

def transform_dataframe(df: pl.DataFrame, max_seq_len: int, end_action_value: int) -> pl.DataFrame:
    end_val = pl.lit(end_action_value)
    max_len = pl.lit(max_seq_len)
    df_with_ends = df.with_columns(
        end_indices=pl.col("actionType").list.eval(
            pl.int_range(0, pl.len()).filter(pl.element() == end_val)
        )
    )
    df_exploded = df_with_ends.explode("end_indices")
    start_offset = (pl.col("end_indices") + 1 - max_len).clip(lower_bound=0)
    slice_len = pl.col("end_indices") - start_offset + 1
    list_cols = ["actionType", "engagementTimeMs", "actionTweetIds"]
    for col in list_cols:
        df_exploded = df_exploded.with_columns(
            pl.col(col).list.slice(start_offset, slice_len).alias(col)
        )
    result = df_exploded.select(["userId", "actionType", "engagementTimeMs", "actionTweetIds"])
    return result

in keyword is highlighted in multiple places int, int_range;

Image Image

dev.snipme:highlights-jvm:1.0.0
dev.snipme:highlights:1.0.0

Example setup theme...

val keywordColor = HorizonTheme.dynamicColors.orange800
val currentTheme = HorizonTheme.variant
val isDarkMode = isDarkMode()
val highlights = remember(codeBlockLiteral, language, currentTheme) {
    val syntaxLanguage = language?.let(SyntaxLanguage::getByName) ?: return@remember listOf()
    Highlights.Builder(
        code = codeBlockLiteral,
        language = syntaxLanguage,
        theme = SyntaxThemes.darcula(darkMode = isDarkMode)
            .copy(keyword = keywordColor.toRgb()),
    ).build().getHighlights()
}
val code = remember(codeBlockLiteral, highlights) {
    buildAnnotatedString {
        append(codeBlockLiteral)
        highlights.forEach { highlight ->
            addStyle(
                style = SpanStyle(
                    color = (highlight as? ColorHighlight)?.rgb?.let(::rgbToColor) ?: Color.Unspecified,
                    fontWeight = (highlight as? BoldHighlight)?.let { FontWeight.Bold },
                ),
                start = highlight.location.start,
                end = highlight.location.end,
            )
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions