Repository navigation
Lexer-based syntax highlighting in the Web UI - #105105
Conversation
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.
|
Workflow [PR], commit [1256fe3] Summary: ✅ AI ReviewSummaryThis PR adds lexer-based syntax highlighting in Final VerdictStatus: ✅ Approve |
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.
This reverts commit 3918676.
…ursts" This reverts commit c8d3ce8.
`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`.
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]>
pamarcos
left a comment
There was a problem hiding this comment.
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
`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).
Highlight queries in the textarea of
programs/server/play.htmlusing 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 explicitcaret-color, and the multi-query active background is preserved by wrapping the tokens of the active query in aq-activespan.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):
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 afterclickhouse-clientcolors.Documentation entry for user-facing changes
Version info
26.5.1.798