Skip to main content

Module goto_definition

Module goto_definition 

Source
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.sql

Functions§

find_reference_at_position
Find the dot-qualified identifier chain at the given byte offset.
resolve_object_id
Resolve identifier parts to an ObjectId using the file’s path context.
resolve_reference
Resolve identifier parts to a file location using the ProjectCache.