This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Postgres Language Server implementation providing LSP features for SQL development, including autocompletion, syntax error highlighting, type-checking, and linting. The project is built in Rust using a modular crate architecture and includes TypeScript packages for editor integrations.
# Install development tools
just install-tools
# Start database for schema introspection
docker-compose up -d
# For Nix users
nix develop && docker-compose up -d# Run all tests
just test
# or: cargo test run --no-fail-fast
# Test specific crate
just test-crate pgls_lsp
# Run doc tests
just test-doc# Format all code (Rust, TOML, JS/TS)
just format
# Lint entire codebase
just lint
# or: cargo clippy && cargo run -p rules_check && bun biome lint
# Fix linting issues
just lint-fix
# Full ready check (run before committing)
just ready# Generate linter code and configuration
just gen-lint
# Create new lint rule
just new-lintrule <group> <rulename> [severity]
# Create new crate
just new-crate <name>The main CLI binary is postgres-language-server (legacy name: postgrestools):
cargo run -p pgls_cli -- check file.sql
# or after building:
./target/release/postgres-language-server check file.sqlThe project uses a modular Rust workspace with crates prefixed with pgls_:
Core Infrastructure:
pgls_workspace- Main API and workspace managementpgls_lsp- Language Server Protocol implementationpgls_cli- Command-line interfacepgls_fs- Virtual file system abstractionpgls_configuration- Configuration management
Parser and Language Processing:
pgls_query- Postgres query parsing (wraps libpg_query)pgls_lexer- SQL tokenizer with whitespace handlingpgls_statement_splitter- Splits source into individual statementspgls_treesitter- Tree-sitter integration for additional parsing
Features:
pgls_completions- Autocompletion enginepgls_hover- Hover information providerpgls_analyser&pgls_analyse- Linting and analysis frameworkpgls_typecheck- Type checking via EXPLAINpgls_schema_cache- In-memory database schema representation
Utilities:
pgls_diagnostics- Error and warning reportingpgls_console- Terminal output and formattingpgls_text_edit- Text manipulation utilitiespgls_suppressions- Rule suppression handling
Located in packages/ and editors/:
- VSCode extension in
editors/code/ - Backend JSON-RPC bridge in
packages/@postgres-language-server/backend-jsonrpc/(legacy:packages/@postgrestools/backend-jsonrpc/) - Main TypeScript package in
packages/@postgres-language-server/postgres-language-server/(legacy:packages/@postgrestools/postgrestools/)
The server connects to a Postgres database to build an in-memory schema cache containing tables, columns, functions, and type information. This enables accurate autocompletion and type checking.
- Input source code is split into individual SQL statements
- Each statement is parsed using libpg_query (via
pgls_query) - Statements are analyzed against the schema cache
- Results are cached and updated incrementally on file changes
- SQL test cases:
crates/pgls_statement_splitter/tests/data/ - Analyzer test specs:
crates/pgls_analyser/tests/specs/ - Example SQL files:
example/,test.sql
The project uses insta for snapshot testing. Update snapshots with:
cargo insta reviewCargo.toml- Workspace definition with all crate dependenciesrust-toolchain.toml- Rust version specificationrustfmt.toml- Code formatting configurationclippy.toml- Clippy linting configuration
biome.jsonc- Biome formatter/linter configuration for JS/TStaplo.toml- TOML formatting configurationjustfile- Task runner with all development commandsdocker-compose.yml- Database setup for testing
IMPORTANT: Always run cargo clippy --all-targets --all-features and fix all warnings after making code changes. Clippy warnings must be resolved before committing code to maintain code quality standards.
IMPORTANT: NEVER add "Claude" or any AI assistant name to commit messages or pull request descriptions. Commits and PRs should appear as authored by the human developer only.
Many parser structures are generated from PostgreSQL's protobuf definitions using procedural macros in pgls_query_macros. Run just gen-lint after modifying analyzer rules or configurations.
The pgls_schema_cache crate contains SQL queries in src/queries/ that introspect the database schema to build the in-memory cache.
The project has ast-grep available for advanced code search and refactoring tasks. ast-grep is a structural search/replace tool that understands code syntax, making it useful for:
- Renaming types, functions, or variables across the codebase
- Finding and replacing code patterns
- Performing structural code transformations
Example usage:
# Search for a pattern
ast-grep --pattern 'struct $NAME { $$$FIELDS }'
# Replace a pattern across files
ast-grep --pattern 'OldType' --rewrite 'NewType' --update-allThe project includes platform-specific allocators and build configurations for Windows, macOS, and Linux.
- Seeing the Treesitter tree for an SQL query can be helpful to debug and implement features. To do this, create a file with an SQL query, and run
just tree-print <file.sql>.