Expand description
Two-phase go-to-definition for SQL identifiers.
Go-to-definition resolves a cursor position in a .sql file to the source
file that defines the referenced object. The algorithm has two phases:
§Phase A — Find identifier at cursor
find_reference_at_position() uses the lexer (mz_sql_lexer::lexer::lex())
to tokenize the text, finds the token containing the cursor byte offset, and
collects dot-separated identifier chains. For example, in FROM myschema.orders,
clicking on either myschema or orders returns ["myschema", "orders"].
Only Token::Ident tokens are considered identifiers. Token::Keyword
tokens (e.g., SELECT, FROM) are not resolved. Tokens inside string
literals are also ignored.
§Phase B — Resolve to file location
resolve_reference() takes the identifier parts and the ProjectCache
(SQLite), constructs an ObjectId using the same 1/2/3-part resolution as
ObjectId::from_item_name(), looks up the object in the cache, and
returns the file path from CachedObject::file_path.
§Examples
-- File: models/mydb/public/bar.sql
CREATE VIEW bar AS SELECT * FROM foo;
^^^
cursor here
Phase A: ["foo"]
Phase B: default_db="mydb", default_schema="public"
→ ObjectId("mydb.public.foo")
→ models/mydb/public/foo.sqlFunctions§
- find_
reference_ at_ position - Find the dot-qualified identifier chain at the given byte offset.
- resolve_
object_ id - Resolve identifier parts to an
ObjectIdusing the file’s path context. - resolve_
reference - Resolve identifier parts to a file location using the
ProjectCache.