Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ struct HighlightedTextFragment: View {
}

var body: some View {
// Convert to an owned String before the async boundary
let code = String(content.characters[...])

TextFragment(model.highlightedCode ?? AttributedString(content))
.foregroundStyle(theme.foregroundColor)
.task(id: content) {
await model.tokenize(
content: content,
languageHint: languageHint
)
.task(id: code) {
await model.tokenize(code: code, languageHint: languageHint, theme: theme)
Comment on lines +37 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include highlighter theme in tokenization task identity

When a code block is first rendered with .plain or another theme whose tokenProperties is empty, tokenize now intentionally skips Prism and stores only a single plain token; if the same view later receives a syntax-highlighting theme for the same code string, this .task(id: code) does not run again, so the block stays un-tokenized and cannot apply token colors. This affects apps that switch themes dynamically via highlighterTheme(_:); key the task on the tokenization inputs that now include the theme/tokenProperties (or otherwise retrigger tokenization when an empty-token theme changes to a highlighting theme).

Useful? React with 👍 / 👎.

}
.onChange(of: Tuple(model.tokens, textEnvironment)) { _, newValue in
model.highlight(
Expand All @@ -53,11 +53,12 @@ extension HighlightedTextFragment {
var tokens: [CodeToken] = []
var highlightedCode: AttributedString?

func tokenize(content: AttributedSubstring, languageHint: String?) async {
let code = String(content.characters[...])
func tokenize(code: String, languageHint: String?, theme: StructuredText.HighlighterTheme) async {
tokens = [CodeToken(content: code, type: .plain)]

if let tokenizer = CodeTokenizer.shared, let languageHint {
// Skip the expensive tokenizer when the theme has no token properties
if !theme.tokenProperties.isEmpty,
let tokenizer = CodeTokenizer.shared, let languageHint {
tokens = await tokenizer.tokenize(code: code, language: languageHint)
}
}
Expand Down