Skip to content

Lexer-based syntax highlighting in the Web UI - #105105

Merged
alexey-milovidov merged 14 commits into
masterfrom
play-syntax-highlighting
May 19, 2026
Merged

alexey-milovidov merged 14 commits into
masterfrom
play-syntax-highlighting

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 16, 2026 •

Copy link
Copy Markdown
Member

Highlight queries in the textarea of programs/server/play.html using the WebAssembly build of the ClickHouse lexer.

When the lexer is available, the textarea text is hidden (color: transparent) and the backdrop div paints the tokens with per-category CSS classes. The caret remains visible via an explicit caret-color, and the multi-query active background is preserved by wrapping the tokens of the active query in a q-active span.

The palette is modelled after clickhouse-client: keywords are bold in the default text color, identifiers in cyan, function calls in brown, numbers in bright green, strings in green, quoted identifiers in teal, and comments in gray italics. Function calls are distinguished from plain identifiers by looking ahead in the token stream for an opening round bracket. A separate dark-theme palette uses the xterm 16-color values that the client renders to in a dark terminal.

The backdrop is updated synchronously on every input event so that newly typed text is never out of sync with the visible glyphs.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added lexer-based syntax highlighting to the query editor in the Web UI (play.html), modelled after clickhouse-client colors.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.798

Highlight queries in the textarea of `programs/server/play.html` using the
WebAssembly build of the ClickHouse lexer. When the lexer is available, the
textarea text is hidden (`color: transparent`) and the backdrop div paints the
tokens with per-category CSS classes. The caret remains visible via an explicit
`caret-color`, and the multi-query active background is preserved by wrapping
the tokens of the active query in a `q-active` span.

The palette is modelled after `clickhouse-client`: keywords are bold in the
default text color, identifiers in cyan, function calls in brown, numbers in
bright green, strings in green, quoted identifiers in teal, and comments in
gray italics. Function calls are distinguished from plain identifiers by
looking ahead in the token stream for an opening round bracket. A separate
dark-theme palette uses the xterm 16-color values that the client renders to in
a dark terminal.

The backdrop is updated synchronously on every input event so that newly typed
text is never out of sync with the visible glyphs.
The reasoning was written for the earlier `mix-blend-mode: difference` design;
update it to match the current `color: transparent` approach.
@clickhouse-gh

clickhouse-gh Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1256fe3]

Summary: ✅


AI Review

Summary

This PR adds lexer-based syntax highlighting in play.html by rendering a tokenized backdrop under the query textarea, with token classes and active-query highlighting. I reviewed the current PR head code plus all prior review discussion/threads; the previously reported correctness issues (missing TT.OpeningRoundBracket, stale async generation handling, backdrop fallback on tokenization failures, and missing synthetic input events after in-place editor mutations) are fixed in the current code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 16, 2026
Comment thread programs/server/play.html Resolved
A hundred rapid paste operations would otherwise re-tokenize the entire query
on every input event. 10 ms is short enough that the gap between the new text
appearing in the textarea (invisible while highlighting is active) and the
backdrop catching up is imperceptible, but long enough to coalesce a burst of
events into a single update.
On input, the textarea gets a `.stale` modifier class that overrides
`color: transparent` so freshly typed characters are visible immediately. The
backdrop is left on display: its pre-edit highlighting now acts as a brief
\"shadow\" under the live text, instead of being hidden altogether.

The class is cleared in `renderQueryBackdrop` once the lexer has caught up.
@alexey-milovidov
alexey-milovidov requested review from azat and pamarcos May 16, 2026 19:41
`TT.OpeningRoundBracket` was referenced in `tokenClass` to distinguish
function calls (`foo(`) from bare identifiers, but the constant was
missing from the `TT` map. In JavaScript, that comparison evaluated
against `undefined`, so bare words were never classified as `q-fn` and
always fell back to `q-id`.

Add `OpeningRoundBracket` (and `ClosingRoundBracket` for completeness)
with values matching the `TokenType` enum order in `Lexer.h`.
Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good.

Comment thread programs/server/play.html Resolved
The empty-query branch of `updateQueryParams` cleared the UI state and
returned without bumping `updateQueryParamsGeneration`, so an in-flight
older call could resume after its `await tokenize` and find a matching
generation, then overwrite the just-cleared backdrop and query params
with stale text.

Bump the counter up-front so any older in-flight run bails out on its
post-`await` check, including the empty-query case.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Comment thread programs/server/play.html Resolved

@pamarcos pamarcos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another divergence in programs/server/play.html:3511 and programs/server/play.html:3547: Enter auto-indent and Ctrl/Cmd+/ still mutate elem.value without dispatching input, so the visible backdrop can diverge from the real query

const q = document.getElementById('query');
const backdrop = document.getElementById('query-backdrop');
console.table({
    actual_textarea_value: JSON.stringify(q.value),
    visible_backdrop_text: JSON.stringify(backdrop.textContent),
    has_backdrop_class: q.classList.contains('has-backdrop'),
    values_match: q.value === backdrop.textContent,
});

Broken result:

actual_textarea_value = "SELECT 1"
visible_backdrop_text = "-- SELECT 1"
has_backdrop_class = true
values_match = false

Comment thread programs/server/play.html Resolved
`updateQueryParams` awaited `tokenize(query)` without error handling. Because
`#query.has-backdrop` paints the textarea text transparent and lets the backdrop
div show the tokenized output, any failure inside `tokenize` (e.g. a pasted
query larger than the WASM memory window currently allocated to the lexer)
would reject the promise and leave the previous `.has-backdrop` state in place
— so users would see stale backdrop text while their real textarea content was
invisible.

Wrap `tokenize` and the subsequent `updateQueryBackdrop` call in `try`/`catch`.
On failure, call `updateQueryBackdrop('', [], [])` which removes the
`has-backdrop` class and falls back to the raw textarea.

Reported in PR review (#105105).
The Enter auto-indent and Ctrl/Cmd+/ comment-toggling handlers mutated
`elem.value` directly, which does not trigger an `input` event. As a result,
the syntax-highlighting backdrop kept the pre-edit token stream while the
textarea's actual value was already updated, so the visible text could diverge
from the real query — e.g. typing `SELECT 1` and pressing Ctrl+/ left the
backdrop showing `SELECT 1` while the textarea actually held `-- SELECT 1`.

Dispatch a synthetic `input` event after each in-place edit so the existing
`input` listener re-tokenizes and re-renders the backdrop.

Reported in PR review (#105105).
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 19, 2026
Merged via the queue into master with commit e1d14be May 19, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the play-syntax-highlighting branch May 19, 2026 04:44
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants