Skip to content

PostgreSQL outline support with outline.scm #26

@AlexS778

Description

@AlexS778

Summary

Add a PostgreSQL Tree-sitter outline query at:

postgres/queries/outline.scm

The goal is to let editors such as Zed show useful buffer symbols for common
PostgreSQL schema files: tables, indexes, schemas, types, functions, procedures,
triggers, views, and other CREATE ... objects.

This is an editor-navigation feature. It does not change the parser and does not
require regenerating the grammar.

Problem

PostgreSQL migration/schema files often contain many top-level definitions:

CREATE SCHEMA main;
CREATE TYPE main.TEAM_ROLE AS ENUM ('owner', 'member');
CREATE TABLE main.teams_users (...);
CREATE INDEX teams_users_team_id_idx ON main.teams_users (team_id);
CREATE FUNCTION main.team_create(...) RETURNS uuid AS $$ ... $$ LANGUAGE plpgsql;
CREATE TRIGGER teams_users_updated BEFORE UPDATE ON main.teams_users ...;

Without an outline query, editors may parse and highlight the file correctly but
cannot provide a useful symbol list / outline for quick navigation.

Intended behavior

Given PostgreSQL source, the editor outline should show major schema objects,
for example:

main
TEAM_ROLE
teams_users
teams_users_team_id_idx
team_create
teams_users_updated

The exact visual grouping is editor-dependent, but each object should have:

  • an @item capture around the syntactic object definition;
  • an @name capture on the object name;
  • optional @context captures on words such as table, function, trigger,
    etc.

Query contract

For Zed-compatible outline support, use this capture shape:

(SomeCreateNode
  (kw_create)
  (kw_table) @context
  (... (identifier) @name ...)) @item

Important distinction:

outline.scm -> @item, @name, @context

Initial coverage

The current outline query should cover common PostgreSQL DDL objects, including:

  • CREATE TABLE
  • CREATE TABLE AS
  • CREATE FOREIGN TABLE
  • CREATE INDEX
  • CREATE SCHEMA
  • CREATE VIEW
  • CREATE MATERIALIZED VIEW
  • CREATE SEQUENCE
  • CREATE TYPE
  • CREATE DOMAIN
  • CREATE FUNCTION
  • CREATE PROCEDURE
  • CREATE TRIGGER
  • CREATE EVENT TRIGGER
  • CREATE POLICY
  • CREATE EXTENSION
  • CREATE LANGUAGE
  • CREATE PUBLICATION
  • CREATE SUBSCRIPTION
  • CREATE STATISTICS
  • CREATE TABLESPACE
  • CREATE FOREIGN DATA WRAPPER
  • CREATE SERVER

The query handles both unqualified and schema-qualified object names where the
current grammar exposes them distinctly, for example:

CREATE TABLE users (...);
CREATE TABLE main.users (...);

For schema-qualified names, the outline generally uses the final object segment
as @name, e.g. users rather than main.

Non-goals

This feature does not provide full semantic navigation.

It does not implement:

  • project-wide "go to definition";
  • "find all references";
  • function overload resolution;
  • search_path-aware name resolution;
  • migration-order-aware indexing;
  • schema object dependency analysis.

Those require a real language server or a dedicated PostgreSQL schema indexer.
outline.scm is intentionally lighter: it gives useful per-buffer navigation
using only Tree-sitter syntax.

Validation

Run the query directly against PostgreSQL files:

cd postgres
../node_modules/.bin/tree-sitter query queries/outline.scm /path/to/file.sql --captures

For a whole schema directory:

find /path/to/schema -type f \( -name '*.sql' -o -name '*.pl.sql' \) | sort > /tmp/pg-schema-sql-paths.txt
cd postgres
../node_modules/.bin/tree-sitter query queries/outline.scm --paths /tmp/pg-schema-sql-paths.txt --captures

Expected result:

  • captures named item should appear for top-level definitions;
  • captures named name should appear on object names;
  • captures named context may appear on object-kind keywords;
  • no definition.* or reference.* captures should appear in outline.scm.

Also run the parser test suite to ensure query changes did not introduce invalid
node names:

cd postgres
../node_modules/.bin/tree-sitter test

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions