Documentation
¶
Overview ¶
Package sqlcontext is the detection brain for schema-aware SQL completion. Given a SQL buffer and a cursor rune offset it reports which clause the cursor sits in (SELECT / FROM / JOIN / WHERE / ON) and what kind of identifier is expected there (Tables / Columns / Both), so a completion source can decide what to offer.
Detection is built entirely on top of highlight.Tokenize: the engine never re-lexes SQL itself and never imports Chroma. It is error-tolerant by construction — partial, unterminated, or otherwise malformed SQL yields a zero ContextResult (Clause=None, Expect=None) rather than a panic.
Analysis is statement-scoped. The cursor's statement is isolated via the editor's StatementRangeAt/StatementAt before any clause walking, so a clause keyword in an earlier statement never leaks context into a cursor sitting in a later one.
Scope: clause + expect detection, plus in-scope table/alias resolution, schema-qualified and quoted-identifier handling, and trailing dot-qualifier resolution (ContextResult. InScopeTables and ContextResult.Qualifier). FK-aware JOIN ranking, wiring into the completion SchemaSource, and CTE/subquery scopes (deferred) are out of scope and live in sibling tasks. This package exposes ContextResult, TableRef, Qualifier, and Analyze.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InNoise ¶
InNoise reports whether the cursor at runeOffset within sql sits inside a string literal or comment (an open OR closed one) — the positions where completion must stay silent because a trailing keyword/operator is quoted content, not a real clause position.
Analysis is statement-scoped, mirroring Analyze: the cursor's own statement is isolated first, so a string opened in an earlier statement does not bleed into a later one. Unlike the single-line stripNoiseEx it replaces, this handles multi-line strings/comments: highlight.Tokenize collapses an unterminated (and/or multi-line) construct into one token spanning to end of input, so a cursor on a later line still inside the construct is correctly detected.
InNoise never panics on partial, unterminated, or malformed SQL.
func IsAliasSlot ¶ added in v0.4.0
IsAliasSlot reports whether the cursor at runeOffset within sql sits in the alias / trailing position of a FROM/JOIN/UPDATE/INTO table reference, where table and broadened keyword completion are noise. Statement-scoped and noise-aware, mirroring Analyze. A trailing "<ident>." is a column-qualifier context, not an alias slot. Never panics on partial/malformed SQL.
Types ¶
type Clause ¶
type Clause int
Clause identifies the SQL clause the cursor sits in. It is the governing major-clause keyword most recently seen before the cursor within the cursor's own statement.
type ContextResult ¶
type ContextResult struct {
Clause Clause
Expect Expect
InScopeTables []TableRef
Qualifier Qualifier
}
ContextResult is the detection brain's output: the clause governing the cursor, what identifiers are expected there, the tables the cursor's statement brings into scope, and any trailing dot-qualifier at the cursor. The zero value (ClauseNone, ExpectNone, nil InScopeTables, absent Qualifier) is the safe "offer nothing" result returned for empty, malformed, or in-noise input.
func Analyze ¶
func Analyze(sql string, runeOffset int) ContextResult
Analyze reports the SQL completion context at runeOffset within sql.
It scopes analysis to the cursor's own statement (semicolon-delimited, semicolons inside strings/comments/dollar-quotes ignored), then walks the token stream up to the cursor and returns the clause governed by the most recent clause keyword plus the identifiers expected there.
Analyze never panics on partial, unterminated, or otherwise malformed SQL: such input yields the zero ContextResult. A cursor inside an (possibly unterminated, possibly multi-line) string or comment also yields the zero ContextResult, so noise never false-triggers.
type Expect ¶
type Expect int
Expect describes what kind of identifier a completion source should offer at the cursor.
const ( // ExpectNone means nothing schema-aware should be offered (e.g. the // cursor is inside a string/comment, or no clause governs it). ExpectNone Expect = iota // ExpectTables means table names are expected (FROM / JOIN). ExpectTables // ExpectColumns means column names are expected (SELECT / WHERE). ExpectColumns // ExpectBoth means either a table (to qualify a column) or a column // is expected (ON / USING join predicates). ExpectBoth )
type Qualifier ¶
Qualifier describes a trailing "<ident>." immediately left of the cursor — the dot-qualified prefix a completion source uses to narrow columns to one table. It is populated only when the cursor sits directly after such a dot.
Ident is the raw identifier text left of the dot (an alias or a table name), unquoted with case preserved. Table/Schema are the resolved table that Ident refers to, found by:
- matching Ident against an in-scope alias, else
- matching Ident against an in-scope table's bare name.
When Ident matches neither (an undeclared alias such as "z."), Table and Schema are empty while Ident still carries the typed text, and Present is true. Present distinguishes "no dot qualifier at the cursor" (Present == false, the zero value) from "a dot qualifier whose alias could not be resolved" (Present == true, Table == "").
type TableRef ¶
TableRef is one table brought into scope by the cursor's statement (via FROM / JOIN). Name is the table's bare (unquoted) name, Schema its optional schema qualifier (empty when unqualified), and Alias the optional alias bound to it ("FROM users u" / "FROM users AS u"). A table with no alias has Alias == "". Quoted identifiers are stored unquoted with their case preserved ("Orders" -> Orders).
An alias is never itself emitted as a TableRef.Name: the alias lives only in the Alias field of the table it labels.