Repository navigation
feat: code blocks with syntax highlighting - #855
Open
MohamedRejeb wants to merge 18 commits into
Open
MohamedRejeb wants to merge 18 commits into
MohamedRejeb wants to merge 18 commits into
Conversation
… precedes, right to left padding, and the old RichText signatures stay callable
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds code blocks with syntax highlighting, and a way to colour code on its own. Closes #705.
Syntax tokenizer (new
highlightpackage, experimental API)CodeHighlighter.tokenize(code, language)andCodeHighlighter.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")andCodeLanguage.fromFileName("Foo.kt").highlightCode(code, language, colors)returns anAnnotatedString.BasicCodeText, a read-only composable for code alone, with sideways scrolling for long lines.Code blocks in
RichTextand the editors<pre>import as code blocks and round-trip through Markdown, HTML and JSON ("type": "code-block").RichTextConfigproperties:codeBlockBackgroundColor,codeBlockFontFamily,codeBlockColors.RichTextFeature.CodeBlock, so an editor with a restricted feature set loads and pastes a code block as plain paragraphs.Copy button
BasicRichTextand the Material and Material3RichTexttake acodeBlockActionslot, 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.mdis rewritten, with smaller updates to the HTML, Markdown, JSON, document and features pages.tokenizeLines.Behaviour changes to be aware of
<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.code-blockblock type. A library version from before this change rejects a document that contains one.BasicRichTextand bothRichTextcomposables gained a parameter. Their previous signatures are kept as hidden overloads, so code compiled against an earlier version still links.correctMarkdownText) now skips fenced code, on import and on export, so code is no longer altered by it.Known limitations
RichTextwrap.BasicCodeTextscrolls sideways.RichTextinside aSelectionContaineris copied with a space between paragraphs, as before. The copy button avoids this for code.Test plan
main, which now includes the list indent fix and Kotlin 2.4.20apiCheckpasses locally; API dumps refreshedallTestsandapiCheckpassed in CI on the first push; a new run covers the latest commit