Skip to content

feat: code blocks with syntax highlighting - #855

Open
MohamedRejeb wants to merge 18 commits into
mainfrom
feat/code-block-syntax-highlighting
Open

MohamedRejeb wants to merge 18 commits into
mainfrom
feat/code-block-syntax-highlighting

Conversation

@MohamedRejeb

@MohamedRejeb MohamedRejeb commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds code blocks with syntax highlighting, and a way to colour code on its own. Closes #705.

Syntax tokenizer (new highlight package, experimental API)

  • An in-house, regex-free scanner for 16 languages: Kotlin, Java, JavaScript, TypeScript, Python, Swift, Go, Rust, C, C++, C#, Dart, Shell, SQL, JSON, YAML. It is linear in the input and never throws.
  • CodeHighlighter.tokenize(code, language) and CodeHighlighter.tokenizeLines(lines, language). The second returns tokens per line while reading the lines as one piece of code, so a comment or string that opens on one line is still coloured on the next. It is meant for views that draw their own rows, such as a diff.
  • CodeLanguage.fromName("kotlin") and CodeLanguage.fromFileName("Foo.kt").
  • highlightCode(code, language, colors) returns an AnnotatedString.
  • BasicCodeText, a read-only composable for code alone, with sideways scrolling for long lines.

Code blocks in RichText and the editors

  • Fenced Markdown code and HTML <pre> import as code blocks and round-trip through Markdown, HTML and JSON ("type": "code-block").
  • Blocks are drawn in a fixed-width font on a background, coloured by the language they name. The colours are presentation only and never reach an export.
  • New RichTextConfig properties: codeBlockBackgroundColor, codeBlockFontFamily, codeBlockColors.
  • New RichTextFeature.CodeBlock, so an editor with a restricted feature set loads and pastes a code block as plain paragraphs.
  • Copy and cut write code line by line with its indentation. A code block pasted into text stays whole, and anything pasted into a block becomes plain lines of that block. Typing shortcuts are ignored inside a block, and a URL typed or pasted there is not turned into a link.

Copy button

  • BasicRichText and the Material and Material3 RichText take a codeBlockAction slot, drawn at the top end corner of each block and given its code and language. Nothing is drawn by default.
  • CodeBlockCopyButton(code, modifier, tint, contentDescription, onCopied) is a ready-made button for that slot. It copies the block's exact code.

Docs and sample

  • docs/code_blocks.md is rewritten, with smaller updates to the HTML, Markdown, JSON, document and features pages.
  • New "Code blocks" sample screen: a Markdown reply with code blocks, a standalone snippet, and a small diff view built on tokenizeLines.

Behaviour changes to be aware of

  • Pasted <pre> is now a code block. Code copied from a JetBrains IDE arrives as a <pre> with coloured spans and no language, so it is pasted as a plain code block and no longer keeps the IDE's colours and font size.
  • JSON. The format stays at version 1 with a new code-block block type. A library version from before this change rejects a document that contains one.
  • Compiled signatures. BasicRichText and both RichText composables gained a parameter. Their previous signatures are kept as hidden overloads, so code compiled against an earlier version still links.
  • Markdown correction (correctMarkdownText) now skips fenced code, on import and on export, so code is no longer altered by it.

Known limitations

  • No editor action yet to turn text into a code block or to leave one. A loaded block can be edited and Enter adds a line.
  • Long lines in a block inside RichText wrap. BasicCodeText scrolls sideways.
  • The room above and below a block comes from a taller first and last line. When that line is long enough to wrap, its wrapped lines are spaced a little wider; a one-line block that wraps shows this most.
  • Only fenced Markdown code is a code block, not code indented by four spaces. A block inside a list item or a quote moves to the top level.
  • HTML, XML, CSS and diffs are not coloured yet.
  • Text selected in a read-only RichText inside a SelectionContainer is copied with a space between paragraphs, as before. The copy button avoids this for code.
  • The editors do not draw the code block action.

Test plan

  • Desktop suite passes locally (1768 tests) after merging main, which now includes the list indent fix and Kotlin 2.4.20
  • apiCheck passes locally; API dumps refreshed
  • Rendering checked from rendered pictures and in the desktop sample
  • allTests and apiCheck passed in CI on the first push; a new run covers the latest commit
  • Independent review of the last four commits (paste fixes, review fixes, copy button, spacing and editor background). What it found is fixed in the latest commit: a URL in a block was auto linked, one line of code pasted at the start of text turned that text into code, the block background ignored the padding of a right to left editor, and the three read-only composables had lost their previous compiled signatures
  • Try the "Code blocks" sample screen on Android and iOS

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Code Block

1 participant