Skip to content

fix(editor): colour the SQL keywords, literals and functions the grammar already names (#2634) - #2635

Merged
datlechin merged 3 commits into
mainfrom
fix/editor-capture-names
Sep 4, 2026
Merged

datlechin merged 3 commits into
mainfrom
fix/editor-capture-names

Conversation

@datlechin

@datlechin datlechin commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Fixes #2634.

IN and AND were one of six SQL token classes the editor silently refused to colour, and the same cause left the MongoDB and Elasticsearch editors with no highlighting at all.

Root cause

CaptureName.fromString is a closed 21-string whitelist that predates the dotted capture names every bundled tree-sitter grammar emits. TreeSitterClient+Highlight.swift:105 and TreeSitterClient+Temporary.swift:44 both drop a capture the whitelist cannot name, so the token keeps the plain text attributes and simply looks unhighlighted. The grammars' capture names and the editor's vocabulary are two hand-maintained lists that must agree, and nothing forced them to.

Two more defects of the same class sat underneath it:

  • The SQL query's own pattern order. (literal) @string sits at capture index 4 and wins the lower-index-wins dedupe against @number (7), @float (8) and @type.builtin (16), so every numeric literal and every NULL, TRUE and FALSE rendered in the String colour. The #match? predicates meant to separate them are nvim Lua patterns (^[-+]?%d+$); SwiftTreeSitter compiles #match? into NSRegularExpression, where %d is a literal % followed by d and never matches.
  • The JavaScript queries do not compile. CodeLanguage.javascript declares highlights: ["injections"], so TreeSitterModel.queryFor concatenates injections.scm + highlights.scm and try?s the result. injections.scm referenced (glimmer_template), a node the vendored parser does not have, and highlights.scm used (function, which this parser calls function_expression. TreeSitterModel.shared.query(for: .javascript) returned nil, so the MQL and Query DSL editors had no highlighting whatsoever. locals.scm and tags.scm failed on the same renamed node; nothing loads them today, so they were inert, but they shipped broken.

ThemeEngine.makeEditorTheme() also never read syntax.operator or syntax.function, and EditorTheme had no attribute that could carry them, so the Operator and Function pickers in Settings > Appearance changed the preview card and nothing else.

Measured

A harness linking the vendored CodeEditLanguages and replicating highlightsFromCursor exactly, over
SELECT CAST(total AS INT), 42, NULL, 'abc' FROM orders o WHERE o.status IN ('a') AND o.total = 3 GROUP BY o.status ORDER BY total DESC;

before after
IN AND BY plain text keyword
DESC plain text keyword
INT plain text type
CAST plain text function
orders type type
42 3 string number
NULL string NULL
'abc' string string
= plain text operator
( ) , ; plain text plain text (unchanged, deliberate)

JSON gains true/false/null in the NULL colour, matching what JSONRowColors already paints in the inspector's tree mode. Bash gains |, > and command flags. JavaScript gains all highlighting, from none.

The fix

  • A guard test compiles every .scm a bundled grammar ships, not only the ones a CodeLanguage loads, so a query file cannot ship broken again.
  • CaptureName gains operator and constant, plus a grammarAlias table mapping the nvim-treesitter names the bundled grammars emit onto this editor's vocabulary. Names deliberately left uncoloured are listed in unstyledGrammarCaptures next to it rather than being invisible.
  • EditorTheme gains operators and functions; .function/.method move off variables, which TablePro fills with the NULL colour.
  • ThemeEngine.makeEditorTheme() passes syntax.operator and syntax.function, so both pickers now do what their labels say.
  • tree-sitter-sql/highlights.scm: ICU regex for the two predicates, and @number, @float and a new @constant.builtin block hoisted above (literal) @string; @function.call hoisted above @type.
  • tree-sitter-javascript: eleven (function renamed to (function_expression across highlights, locals and tags, and the Ember glimmer_template pattern removed from injections.

Deliberately not doing

@field stays uncoloured, so column identifiers keep the plain text colour: of TablePlus, DataGrip, Postico, Sequel Ace and VS Code, only DataGrip colours them. @punctuation.* stays uncoloured for the same reason and because it costs a dense viewport 440 extra highlight ranges (1560 against 1120). @spell and @embedded mark a range for another tool and sit on a range something else already colours.

Chose an alias table over a fallback rule

The first draft resolved a dotted name by stripping trailing components, which is neovim's documented rule and what these .scm files were written against. The Phase 2 critique showed why it is worse here: with the new guard test, an unknown capture fails CI loudly, whereas a stripping loop would silently resolve it to something plausible and possibly wrong. type.qualifier is the example: stripping gives type, but UNIQUE and CASCADE read as keywords.

Verified

  • build PASS
  • test (13 suites: the new one plus every suite owning EditorTheme, ThemeEngine or the editor fixture) PASS, 127 cases, 0 failures
  • lint 0 violations across the seven changed Swift files
  • Re-verified after merging main at v0.72.0
  • Before/after harness output above, from the shipping query files and the shipping resolution

No docs change: Settings > Appearance already documents Operator and Function as syntax colours, and this makes that true. The sql-editor page's images are still the "Screenshot coming soon" placeholders, so nothing there goes stale.

Before / after

The change is entirely colour inside the editor, so the table above is the exact per-token record, taken from the shipping query files through the shipping resolution path. A rendered light and dark pair of the same sample went to the author to drag in here; gh cannot upload an image to a pull request.

Reading the sample in Default Light, before against after: IN, AND, ON, NOT, BY, DESC and UNIQUE go from black to the keyword blue; INT, VARCHAR, JSONB and TIMESTAMP from black to the type teal; CAST and count from black to the function teal; 42, 3.5, 100 and 255 from the string red to the number purple; NULL and TRUE from the string red to the NULL orange. Quoted literals, table names and aliases are unchanged.

https://claude.ai/code/session_016cKgYoccE88XFeVPuBLRak

@datlechin
datlechin merged commit d7b154d into main Sep 4, 2026
14 checks passed
@datlechin
datlechin deleted the fix/editor-capture-names branch September 4, 2026 10:59
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.

Some key words in SQL is not showing highlight

1 participant