Expand description
LSP semantic-token mapping for the lexical highlighter.
The crate::highlight pass produces HighlightKinds with byte ranges. Editors that speak
the Language Server Protocol want semantic tokens: each significant token tagged with a
standard token type (keyword, string, number, …) and a bitset of modifiers, then
delta-encoded as (deltaLine, deltaStartChar, length, tokenType, modifiers) quintuples.
This module is transport-free and depends on nothing but the lexer/syntax crates — the
sql-dialect-fmt-lsp binary maps these onto lsp_types, but the mapping itself (and its tests) live
here so the highlighter and the editor adapter can never disagree on what a kind means.
It also understands Injections: a $$ … $$ body may embed JavaScript, Python, Java, Scala,
or SQL (per a LANGUAGE clause). Tokens that fall inside an injection region are tagged so an
editor can either re-highlight them with the embedded grammar or shade the whole body.
Structs§
- Injection
- An embedded-language region inside a
$$ … $$body. Built bydetect_injections; consumers can re-highlight therangewith the embedded grammar named bylanguage.#[non_exhaustive]so fields can be added compatibly. - Line
Token - A semantic token as the LSP wire format wants it: zero-based line / UTF-16 char position with a
UTF-16 length, plus the legend indices. These are absolute (not yet delta-encoded); call
delta_encodefor the on-wire(deltaLine, deltaStartChar, …)form.#[non_exhaustive]so fields can be added without breaking downstream matches. - Resolved
Token - A semantic token resolved to absolute byte coordinates (pre delta-encoding).
#[non_exhaustive]so fields can be added without breaking downstream matches. - Semantic
Token Modifiers - LSP semantic token modifiers, as a bitset. Only the few the lexical layer can justify are
modelled;
bits()produces thetokenModifiersvalue an editor decodes against its legend. - Semantic
Tokens - The full semantic-token result: the per-token tagging plus the embedded-language regions.
#[non_exhaustive]so fields can be added without breaking downstream matches.
Enums§
- Injected
Language - A language that can be embedded in a Snowflake
$$ … $$UDF / procedure body. - Semantic
Token Type - The standard LSP semantic token types this highlighter emits.
Functions§
- delta_
encode - Delta-encode absolute
LineTokens into the LSP wire form: each quintuple is(deltaLine, deltaStartChar, length, tokenType, tokenModifiers), where deltas are relative to the previous token (anddeltaStartCharresets to the absolute column when the line advances). Tokens must be sorted by(line, start_char);line_tokensalready produces them that way. - detect_
injections - Scan
inputfor embedded$$ … $$bodies and tag each with its language. Routine bodies useLANGUAGE <name> ... AS $$...$$; dynamic SQL usesEXECUTE IMMEDIATE $$...$$. Bodies afterAS $$...$$with noLANGUAGEclause default toInjectedLanguage::Sql. Runs off the lexical highlighter, so it never parses or panics. - line_
tokens - Lower resolved tokens to LSP
LineTokens: split multi-line tokens (block comments, dollar-quoted bodies) into one token per line — the LSP encoding forbids a token spanning a newline — and compute UTF-16 positions and lengths. Output is sorted by(line, start_char). - line_
tokens_ utf8 - Like
line_tokens, but columns and token lengths are measured in UTF-8 bytes. - resolve_
tokens - Resolve
inputto absolute-coordinate semantic tokens (no delta-encoding). Trivia, punctuation and lex errors drop out; everything else becomes aResolvedTokentagged persemantic_token. Injections do not change a token’s type — the lexical kind already wins — but they are returned alongside bysemantic_tokensso an editor can layer an embedded grammar over the body’s range. - semantic_
token - The LSP token type + modifiers for a highlight kind, or
Nonefor kinds that carry no semantic token: whitespace (insignificant), punctuation (delimiters editors theme structurally), and lex errors (surfaced as diagnostics, not tokens). - semantic_
tokens - Resolve
inputto semantic tokens and its embedded-language injection regions in one pass. - semantic_
tokens_ lsp - One-shot: resolve, lower to lines, and delta-encode. The vector an LSP server hands back for
textDocument/semanticTokens/full(modulo the server’s wrapper type). - semantic_
tokens_ lsp_ utf8 - One-shot semantic tokens using UTF-8 columns/lengths.