Skip to content

Discord: code blocks have no syntax highlighting - #828

Closed
itz4blitz wants to merge 3 commits into
mainfrom
issue/358-code-colors
Closed

itz4blitz wants to merge 3 commits into
mainfrom
issue/358-code-colors

Conversation

@itz4blitz

Copy link
Copy Markdown
Collaborator

What changed and why

A fenced code block is coloured from the language on its fence. Diff lines that start with + or - take a green or red background, and an ansi block keeps the SGR colours with the escapes removed. A block longer than 300 lines stays plain. Colouring runs off the UI thread and is cached per message and block.

Tested with chatd unit tests for a rust keyword, diff lines, ansi escapes, a long block, and the lab check. cargo clippy -p chatd --no-default-features --all-targets reported nothing new in this change. The GUI crate typechecks with the gui feature. The window was not opened, and this was not tried on a live Discord account.

Type

  • feature
  • fix
  • perf
  • refactor
  • chore
  • docs

Platforms

  • windows
  • macos
  • linux
  • android
  • ios

Proof

Scenario: e2e/scenarios/discord-code-colors

Lab run:

Compatibility

No protocol change. An older client and an older server are unchanged. The colours are drawn from the language the message already carried.

Risk and rollback

A wrong colour is only on screen. Revert this commit.

Release note

Code blocks are coloured by language, including diff and ansi.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Tiny Code Review

⚠️ Code review unavailable · run

the AI provider rate-limited the request

This is advice, not a gate. Re-run with the agent:review label or a /review comment.

@unRekable unRekable left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code Review

🟡 2 findings (1 blocking).

Adds per-language colors for fenced code blocks, drawn in the shell: a self-contained highlighter runs on the background executor, is cached per message and block, has a 300-line plain fallback, and wires the e2e prove driver the way the repo already does it. The shape is right, but the ANSI scanner panics on text the other side chose: while scanning SGR parameters it consumes only the first byte of a non-ASCII character and then slices the text at a non-char-boundary (finding 1). The #[path] source share also compiles the window from a file inside chatd's Discord adapter, with an unwritten keep-it-dependency-free constraint (finding 2).

Residual risk

  • The module is a new hand-rolled parser over peer-controlled fence language and text; the tests cover happy paths only. A fuzz pass over highlight(), especially the ANSI scanner, would be cheap insurance beyond what this diff ships.
  • ANSI SGR 256-color parameters fold into the 16-color table (sgr256 maps n to 30 + n % 8) and truecolor is ignored, so pasted terminal output in an ansi block shows approximated colors.
  • While a block's coloring is in flight, an edited message with the same key draws the stale cached text until the old task finishes, because the coloring guard returns the cached Lit for a stamp that no longer matches. Rare and self-correcting.

if open.is_none() && (fg != 0 || bg != 0 || bold) {
open = Some(text.len());
}
let c = source[i..].chars().next().unwrap();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🔴 Blocker: Crash on input the other side controls. take_sgr() bails after reading one byte when it meets a byte that is not a digit, ';' or an ASCII letter, so an escape sequence ESC-[ followed by any non-ASCII character leaves i pointing at a UTF-8 continuation byte; this line then slices source[i..] at a non-char-boundary and panics. The fence language and text are the peer's, Message::bound caps lengths only and strips nothing, and this runs on the shell's background executor, so one message with an ```ansi block containing ESC-[é panics the coloring task. Fix take_sgr() to round j up past UTF-8 continuation bytes when it bails (or guard this line with source.get(i..) and step to the next boundary), and add that input to the tests.

Suggested change
let c = source[i..].chars().next().unwrap();
let Some(c) = source.get(i..).and_then(|rest| rest.chars().next()) else { i += 1; continue };

mod viewer;
mod when;
mod wiki;
#[path = "../../../chatd/src/adapters/discord/highlight.rs"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Major: The window crate now compiles a file that lives inside chatd's Discord adapter. highlight.rs must stay free of any chatd-crate import for the window to keep building, and neither this include nor the module doc says so, so a future use crate::... in that file, or a move of it within adapters/discord, breaks the shipped window build out of nowhere. The highlighter also colors code blocks from every network, not Discord only. Give it a neutral home both crates include (or a small workspace crate) and state the shared-file constraint in the module doc.

@itz4blitz
itz4blitz force-pushed the issue/358-code-colors branch from 75310f7 to 1419d1d Compare November 17, 2026 22:35
itz4blitz and others added 3 commits November 20, 2026 15:01
…anics the coloring task

take_sgr bailed after one byte of a multi-byte character, so ansi() sliced the
source mid-character. It now leaves a non-ASCII byte for the caller, and a test
covers escapes followed by accented, emoji and CJK text.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@itz4blitz

Copy link
Copy Markdown
Collaborator Author

Superseded by and merged in #903 (d387971 — Release 2026-11-20 combined PR).

@itz4blitz itz4blitz closed this Nov 21, 2026
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.

2 participants