Skip to content

Web UI: rainbow parentheses, matched brackets, matching identifiers, digit groups - #109108

Merged
alexey-milovidov merged 5 commits into
masterfrom
play-syntax-highlighting-brackets-identifiers
Jul 3, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
play-syntax-highlighting-brackets-identifiers

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Extend the syntax highlighting in the Web UI (play.html) backdrop to reach parity with clickhouse-client:

  • Rainbow parentheses — brackets ((), [], {}) are colored by their nesting depth, cycling through seven colors. A type-aware stack assigns each opening bracket its depth and gives the matching closing bracket the same color. The palettes are dark for the light theme and light for the dark theme.
  • Matched brackets — the bracket pair around the cursor is emphasized in bold (with a faint background box). Bold monospace has the same advance width, so nothing shifts.
  • Matching identifiers — when the cursor is inside an identifier, every occurrence of the same identifier is underlined together (keywords and function names are excluded).
  • Digit groups — long plain decimal numbers get a subtle underline at each thousands boundary. This mirrors the highlightWithLexer algorithm in src/Client/ClientBaseHelpers.cpp: only the integer part of a regular base-10 number (at least 5 digits, no exponent / hex / bin / _ separators) is considered.

All highlighting is computed from the existing WASM lexer token stream and re-rendered on cursor movement.

Changelog category (leave one):

  • Improvement

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

Added rainbow parentheses, matched-bracket, matching-identifier, and digit-group highlighting to the Web UI (play.html), matching clickhouse-client.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.7.1.464 (included in 26.7 and later)

…digit groups

Extend the syntax highlighting in the Web UI (`play.html`) backdrop to match
the features of `clickhouse-client`:

- Rainbow parentheses: brackets (`()`, `[]`, `{}`) are colored by nesting depth,
  cycling through seven colors. The palettes are dark for the light theme and
  light for the dark theme.
- Matched brackets: the bracket pair around the cursor is emphasized in bold.
- Matching identifiers: when the cursor is inside an identifier, every occurrence
  of the same identifier is underlined together.
- Digit groups: long plain decimal numbers get a subtle underline at each
  thousands boundary, mirroring the `highlightWithLexer` algorithm in
  `src/Client/ClientBaseHelpers.cpp`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d99b93a]

Summary: ✅


AI Review

Summary

This PR adds four new lexer-driven highlighting behaviors to play.html plus a C++ regression test for bracket-depth assignment. The rainbow-coloring fixes look reasonable, but the new matched-bracket emphasis still misses common cursor positions inside a pair, so the feature does not yet satisfy the “pair around the cursor” behavior described in the PR.

Findings

⚠️ Majors

  • [programs/server/play.html:5169-5185] computeBracketInfo only picks a bracket when the caret is immediately before or after a bracket token. With a query like (ab) and the caret between a and b, found stays -1, so no q-br-match is emitted even though the cursor is still inside the enclosing pair. If the intended behavior is “highlight the pair around the cursor”, this needs to choose the innermost matched pair whose open/close offsets surround cursor, and the coverage should include a non-adjacent cursor case.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix matched-bracket detection so a caret anywhere inside a matched pair still highlights that pair, not only positions adjacent to a bracket character.
    • Add a focused regression case (for example (ab) with the caret between a and b) to lock the behavior.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 2, 2026
Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved
…tion-name detection

Address the AI review on PR #109108
(#109108) with two fixes to
`play.html` syntax highlighting, both aligning the Web UI with
`highlightWithLexer` in `src/Client/ClientBaseHelpers.cpp`:

- `computeBracketInfo` assigned a rainbow depth to every opening bracket
  eagerly and gave unmatched closing brackets depth `0`, so a lone `(`, a
  stray `)`, or a mismatched pair rendered as a normal `q-br*` bracket. Now a
  bracket receives a depth (and thus a rainbow color) only once it has a real
  mate; unmatched brackets stay at depth `-1` and keep the default color, as
  `clickhouse-client` does.

- `tokenClass` skipped only `Whitespace` when deciding whether a `BareWord` is
  a function name, so `sum/*c*/(x)` was classified as a plain identifier and
  underlined by the matching-identifier pass. It now skips `Comment` too, so
  such function names are excluded, matching the CLI.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed both Majors from the AI review in 7b71cef (play.html only):

  • Unmatched brackets — computeBracketInfo used to assign a rainbow depth to every opening bracket eagerly and gave unmatched closers depth 0, so SELECT (, SELECT ), and mismatched pairs rendered as normal q-br* brackets. A bracket now receives a depth only after a successful match; unmatched brackets stay at depth -1 and keep the default color, as clickhouse-client does.
  • Function names before a comment — tokenClass skipped only Whitespace, so sum/*c*/(x) was classified as an identifier and underlined by the matching-identifier pass. It now skips Comment too, matching highlightWithLexer in src/Client/ClientBaseHelpers.cpp.

There is no JS unit-test harness for play.html (the only /play test, 01528_play.sh, just checks the endpoint serves the page), so I verified the logic with a standalone Node harness exercising the cases from the review — lone ( / ), mismatched ( ], nested ((())) depths 0,1,2,2,1,0, cursor bold pair, sum/*c*/(x) → q-fn, and sum (x) → q-fn — all pass.

Comment thread programs/server/play.html Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member Author

I looked into the remaining AI Major (computeBracketInfo crossing-nest / cross-; matching) and want to flag one subtlety before it gets "fixed", since part of the suggestion pulls away from clickhouse-client rather than toward it.

Semicolon reset. The AI claims matching (…) across ; in SELECT (; SELECT ) diverges from the client, but the interactive client's rainbow loop (ClientBaseHelpers.cpp:486-562) does not reset brace_stack at a ; — it just advances past the token (while (… == Semicolon) ++it;), and it runs even on unparseable input (highlight is called from ClientBase.cpp:4116 with the default lexer_fallback=false, so !parse_res does not early-return before the loop). So the client matches (↔) across the ; too, and play.html currently mirrors that. If strict parity is the goal, resetting at ; would be the divergence; if the goal is "cleaner than the client", resetting is a fine call — just making the trade-off explicit.

Crossing nest ([)]. This one is genuinely play.html-only territory: the client's rainbow loop tracks round brackets only (OpeningRoundBracket/ClosingRoundBracket, :491/:506), so it colors (↔) and ignores []; play.html (three bracket types) currently colors [↔] and leaves (/) unmatched. Two reasonable resolutions with opposite results:

  • pop-until-match on a mismatched closer → ([)] colors the outer (↔) and drops [/] (aligns with the client's round-bracket behavior);
  • invalidate all outstanding openers on a mismatch → ([)] colors nothing (the AI's literal "reset outstanding openers when nesting is broken").

Both are defensible — it's a UX call.

Test. The regression coverage the AI asks for is feasible via the existing src/Parsers/tests/gtest_play_get_query_under_cursor.cpp pattern (port computeBracketInfo onto the real DB::Lexer and assert depths/matches for ([)], ((())), (; )), once the intended behavior is nailed down.

I've left the code untouched since this is your design call, not a correctness/safety issue. Happy to implement whichever variant you pick plus the gtest.

@thevar1able thevar1able self-assigned this Jul 2, 2026
…boundaries

`computeBracketInfo` in `play.html` assigned a rainbow depth to a bracket only
after finding a properly-nested mate, but it left the outstanding openers on the
stack after a mismatched closer and across `;` boundaries. A later closer could
then reach over the break and retroactively "match" an opener that was never
properly closed: `SELECT ([)]` still rainbow-colored `[` / `]`, and
`SELECT (; SELECT )` matched the `)` in the second statement back to the `(` in
the first.

Discard the outstanding openers when a closer does not match the innermost
opener, and reset the stack at each `;`, so a bracket from a broken or a previous
statement can no longer become matchable. A valid pair after the break or after
the `;` is still colored.
There is no JavaScript/WebAssembly runtime in CI, so we cannot run the browser
code directly. Port the depth-assignment part of `computeBracketInfo` on top of
the real `DB::Lexer` — exactly as `gtest_play_get_query_under_cursor.cpp` already
does for `getQueryUnderCursor` — and lock the contract that a bracket is colored
only once it has a real, properly-nested mate:

- proper nesting across all bracket kinds gets ascending depths;
- a lone `(`, a stray `)`, or a simple mismatch stays uncolored;
- a crossing nest such as `([)]` colors nothing (the `]` must not reach over the
  stray `)` to match the `[`);
- brackets do not match across a `;` statement boundary, while a fresh pair after
  the break or the `;` is still colored.
The bracket matcher in `play.html` reset its stack at every `;`, so
`SELECT (; SELECT )` left the `(` / `)` unmatched. But `clickhouse-client`'s
rainbow loop (`highlightWithLexer` in `src/Client/ClientBaseHelpers.cpp`)
skips `;` without clearing `brace_stack`, so it still matches the `(` with a
`)` in a later statement. The reset therefore diverged from the client this
change claims parity with (AI review finding on commit `4f4ccadc`).

Drop the `TT.Semicolon` reset in `computeBracketInfo` to restore parity. The
crossing-nest fix (a mismatched closer discards the outstanding openers, so
`([)]` colors nothing) is a separate, genuine `play.html`-only correction and
is left in place.

Update the regression gtest accordingly: `(; )` and `SELECT (; SELECT )` now
match with depth 0, mirroring the client.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread programs/server/play.html
Comment on lines +5169 to +5185
/// Prefer the bracket ending at the cursor (the char just before the caret), then
/// fall back to the bracket starting at the cursor (the char just after).
let found = -1;
for (let i = 0; i < tokens.length; ++i) {
const type = tokens[i].type;
if (!OPENING_BRACKETS.has(type) && !CLOSING_BRACKETS.has(type)) continue;
if (starts[i] + tokens[i].token.length === cursor) { found = i; break; }
}
if (found < 0) {
for (let i = 0; i < tokens.length; ++i) {
const type = tokens[i].type;
if (!OPENING_BRACKETS.has(type) && !CLOSING_BRACKETS.has(type)) continue;
if (starts[i] === cursor) { found = i; break; }
}
}
if (found >= 0 && matchOf[found] >= 0) {
bold.add(found);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

computeBracketInfo only sets found when the caret is exactly at a bracket boundary. A query like (ab) with the caret between a and b leaves found == -1, so the new q-br-match style never appears even though the cursor is still inside a matched pair.

That falls short of the PR's “bracket pair around the cursor” behavior. Can we select the innermost matched pair whose open/close offsets surround cursor (and add a regression case for a non-adjacent cursor position) instead of only the bracket immediately before/after the caret?

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Looked into the remaining AI Major (computeBracketInfo only bolding the matched pair when the caret is adjacent to a bracket, not when it sits anywhere inside the pair). The observation is accurate, but "highlight the pair whenever the cursor is anywhere inside it" would pull away from clickhouse-client, not toward it.

What the client does. The interactive client brightens a matched pair only when the caret is on a bracket, never for a caret in the middle of the enclosed text. In ClientBaseHelpers.cpp the bright color is applied only when cursor_on_current_brace or cursor_on_matching_brace holds — i.e. cursor_position == highlight_pos (the closing bracket) or cursor_position == matching_brace_pos (the opening bracket) (ClientBaseHelpers.cpp:531-550). A caret between a and b in (ab) is on neither bracket position, so the client leaves the pair un-emphasized. play.html mirrors this, and is in fact slightly more permissive — it accepts the caret just before or just after a bracket (starts[i] + len === cursor, then starts[i] === cursor), covering both edges rather than only the just-before position the client uses.

So the current adjacent-only behavior is the parity behavior; making it fire for a caret anywhere inside the pair is the divergence (same shape as the earlier ;-reset suggestion). The only thing to reconcile is the PR-description wording "the bracket pair around the cursor", which reads as enclosing. Two clean resolutions:

  • keep the client-parity adjacent-only behavior and reword the description to "the bracket pair at the cursor"; or
  • intentionally diverge from the client and highlight the innermost enclosing pair whenever the caret is inside — a bigger UX departure.

Left the code untouched since this is a parity/UX call, not a correctness issue. Happy to implement the enclosing-pair variant plus a non-adjacent-caret gtest case if you want the divergence.

@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 60/61 (98.36%) · Uncovered code

Full report · Diff report

@thevar1able

thevar1able commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Colors look too washed out.

image image

Behavior is inconsistent between local and Web UI.
image
image

image image

@alexey-milovidov

alexey-milovidov commented Jul 2, 2026 •

Copy link
Copy Markdown
Member Author

I made it more washed deliberately after the first iteration, because otherwise their contrast is similar to other syntax elements, and it becomes too much.

PS. Maybe not contrast, but perceived brightness.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 3, 2026
Merged via the queue into master with commit f76c091 Jul 3, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the play-syntax-highlighting-brackets-identifiers branch July 3, 2026 02:00
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 3, 2026
alexey-milovidov added a commit that referenced this pull request Jul 3, 2026
…ntax-error-position

Resolve conflicts in `programs/server/play.html` between this PR's
syntax-error position highlight and the Web UI features that merged into
`master` in the meantime (query tabs `#107826`, expanded-database restore
`#108964`, and rainbow-bracket / matched-identifier highlighting `#109108`).

- `postSingle` now takes both `params_restore_pending` (tabs feature) and
  `queryStart` (this PR); all call sites use the newer `my_request_num`
  ownership handling and pass both arguments.
- The token render loop keeps `master`'s rainbow-bracket, bold matched-bracket,
  matched-identifier and per-digit-group number rendering, and additionally
  OR-s the `q-err-pos` class onto the token at the reported error position.

The net diff of the branch over `master` is exactly this PR's
syntax-error-highlight feature; no tabs / restore / bracket functionality
was dropped.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…tion-name detection

Address the AI review on PR ClickHouse#109108
(ClickHouse#109108) with two fixes to
`play.html` syntax highlighting, both aligning the Web UI with
`highlightWithLexer` in `src/Client/ClientBaseHelpers.cpp`:

- `computeBracketInfo` assigned a rainbow depth to every opening bracket
  eagerly and gave unmatched closing brackets depth `0`, so a lone `(`, a
  stray `)`, or a mismatched pair rendered as a normal `q-br*` bracket. Now a
  bracket receives a depth (and thus a rainbow color) only once it has a real
  mate; unmatched brackets stay at depth `-1` and keep the default color, as
  `clickhouse-client` does.

- `tokenClass` skipped only `Whitespace` when deciding whether a `BareWord` is
  a function name, so `sum/*c*/(x)` was classified as a plain identifier and
  underlined by the matching-identifier pass. It now skips `Comment` too, so
  such function names are excluded, matching the CLI.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…lighting-brackets-identifiers

Web UI: rainbow parentheses, matched brackets, matching identifiers, digit groups
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ntax-error-position

Resolve conflicts in `programs/server/play.html` between this PR's
syntax-error position highlight and the Web UI features that merged into
`master` in the meantime (query tabs `ClickHouse#107826`, expanded-database restore
`ClickHouse#108964`, and rainbow-bracket / matched-identifier highlighting `ClickHouse#109108`).

- `postSingle` now takes both `params_restore_pending` (tabs feature) and
  `queryStart` (this PR); all call sites use the newer `my_request_num`
  ownership handling and pass both arguments.
- The token render loop keeps `master`'s rainbow-bracket, bold matched-bracket,
  matched-identifier and per-digit-group number rendering, and additionally
  OR-s the `q-err-pos` class onto the token at the reported error position.

The net diff of the branch over `master` is exactly this PR's
syntax-error-highlight feature; no tabs / restore / bracket functionality
was dropped.
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