Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

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.

## Key Commands

### Development Setup
```bash
# Install development tools
just install-tools

# Start database for schema introspection
docker-compose up -d

# For Nix users
nix develop && docker-compose up -d
```

### Building and Testing
```bash
# Run all tests
just test
# or: cargo test run --no-fail-fast

# Test specific crate
just test-crate pgt_lsp

# Run doc tests
just test-doc
```

### Code Quality
```bash
# 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
```

### Code Generation
```bash
# 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>
```

### CLI Usage
The main CLI binary is `postgrestools`:
```bash
cargo run -p pgt_cli -- check file.sql
# or after building:
./target/release/postgrestools check file.sql
```

## Architecture

### Crate Structure
The project uses a modular Rust workspace with crates prefixed with `pgt_`:

**Core Infrastructure:**
- `pgt_workspace` - Main API and workspace management
- `pgt_lsp` - Language Server Protocol implementation
- `pgt_cli` - Command-line interface
- `pgt_fs` - Virtual file system abstraction
- `pgt_configuration` - Configuration management

**Parser and Language Processing:**
- `pgt_query` - Postgres query parsing (wraps libpg_query)
- `pgt_lexer` - SQL tokenizer with whitespace handling
- `pgt_statement_splitter` - Splits source into individual statements
- `pgt_treesitter` - Tree-sitter integration for additional parsing

**Features:**
- `pgt_completions` - Autocompletion engine
- `pgt_hover` - Hover information provider
- `pgt_analyser` & `pgt_analyse` - Linting and analysis framework
- `pgt_typecheck` - Type checking via EXPLAIN
- `pgt_schema_cache` - In-memory database schema representation

**Utilities:**
- `pgt_diagnostics` - Error and warning reporting
- `pgt_console` - Terminal output and formatting
- `pgt_text_edit` - Text manipulation utilities
- `pgt_suppressions` - Rule suppression handling

### TypeScript Packages
Located in `packages/` and `editors/`:
- VSCode extension in `editors/code/`
- Backend JSON-RPC bridge in `packages/@postgrestools/backend-jsonrpc/`
- Main TypeScript package in `packages/@postgrestools/postgrestools/`

### Database Integration
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.

### Statement Processing Flow
1. Input source code is split into individual SQL statements
2. Each statement is parsed using libpg_query (via `pgt_query`)
3. Statements are analyzed against the schema cache
4. Results are cached and updated incrementally on file changes

## Testing

### Test Data Location
- SQL test cases: `crates/pgt_statement_splitter/tests/data/`
- Analyzer test specs: `crates/pgt_analyser/tests/specs/`
- Example SQL files: `example/`, `test.sql`

### Snapshot Testing
The project uses `insta` for snapshot testing. Update snapshots with:
```bash
cargo insta review
```

## Configuration Files

### Rust Configuration
- `Cargo.toml` - Workspace definition with all crate dependencies
- `rust-toolchain.toml` - Rust version specification
- `rustfmt.toml` - Code formatting configuration
- `clippy.toml` - Clippy linting configuration

### Other Tools
- `biome.jsonc` - Biome formatter/linter configuration for JS/TS
- `taplo.toml` - TOML formatting configuration
- `justfile` - Task runner with all development commands
- `docker-compose.yml` - Database setup for testing

## Development Notes

### Code Generation
Many parser structures are generated from PostgreSQL's protobuf definitions using procedural macros in `pgt_query_macros`. Run `just gen-lint` after modifying analyzer rules or configurations.

### Database Schema
The `pgt_schema_cache` crate contains SQL queries in `src/queries/` that introspect the database schema to build the in-memory cache.

### Multi-Platform Support
The 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>`.
153 changes: 1 addition & 152 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,155 +1,4 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
@./AGENTS.md

## Project Overview

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.

## Key Commands

### Development Setup
```bash
# Install development tools
just install-tools

# Start database for schema introspection
docker-compose up -d

# For Nix users
nix develop && docker-compose up -d
```

### Building and Testing
```bash
# Run all tests
just test
# or: cargo test run --no-fail-fast

# Test specific crate
just test-crate pgt_lsp

# Run doc tests
just test-doc
```

### Code Quality
```bash
# 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
```

### Code Generation
```bash
# 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>
```

### CLI Usage
The main CLI binary is `postgrestools`:
```bash
cargo run -p pgt_cli -- check file.sql
# or after building:
./target/release/postgrestools check file.sql
```

## Architecture

### Crate Structure
The project uses a modular Rust workspace with crates prefixed with `pgt_`:

**Core Infrastructure:**
- `pgt_workspace` - Main API and workspace management
- `pgt_lsp` - Language Server Protocol implementation
- `pgt_cli` - Command-line interface
- `pgt_fs` - Virtual file system abstraction
- `pgt_configuration` - Configuration management

**Parser and Language Processing:**
- `pgt_query` - Postgres query parsing (wraps libpg_query)
- `pgt_lexer` - SQL tokenizer with whitespace handling
- `pgt_statement_splitter` - Splits source into individual statements
- `pgt_treesitter` - Tree-sitter integration for additional parsing

**Features:**
- `pgt_completions` - Autocompletion engine
- `pgt_hover` - Hover information provider
- `pgt_analyser` & `pgt_analyse` - Linting and analysis framework
- `pgt_typecheck` - Type checking via EXPLAIN
- `pgt_schema_cache` - In-memory database schema representation

**Utilities:**
- `pgt_diagnostics` - Error and warning reporting
- `pgt_console` - Terminal output and formatting
- `pgt_text_edit` - Text manipulation utilities
- `pgt_suppressions` - Rule suppression handling

### TypeScript Packages
Located in `packages/` and `editors/`:
- VSCode extension in `editors/code/`
- Backend JSON-RPC bridge in `packages/@postgrestools/backend-jsonrpc/`
- Main TypeScript package in `packages/@postgrestools/postgrestools/`

### Database Integration
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.

### Statement Processing Flow
1. Input source code is split into individual SQL statements
2. Each statement is parsed using libpg_query (via `pgt_query`)
3. Statements are analyzed against the schema cache
4. Results are cached and updated incrementally on file changes

## Testing

### Test Data Location
- SQL test cases: `crates/pgt_statement_splitter/tests/data/`
- Analyzer test specs: `crates/pgt_analyser/tests/specs/`
- Example SQL files: `example/`, `test.sql`

### Snapshot Testing
The project uses `insta` for snapshot testing. Update snapshots with:
```bash
cargo insta review
```

## Configuration Files

### Rust Configuration
- `Cargo.toml` - Workspace definition with all crate dependencies
- `rust-toolchain.toml` - Rust version specification
- `rustfmt.toml` - Code formatting configuration
- `clippy.toml` - Clippy linting configuration

### Other Tools
- `biome.jsonc` - Biome formatter/linter configuration for JS/TS
- `taplo.toml` - TOML formatting configuration
- `justfile` - Task runner with all development commands
- `docker-compose.yml` - Database setup for testing

## Development Notes

### Code Generation
Many parser structures are generated from PostgreSQL's protobuf definitions using procedural macros in `pgt_query_macros`. Run `just gen-lint` after modifying analyzer rules or configurations.

### Database Schema
The `pgt_schema_cache` crate contains SQL queries in `src/queries/` that introspect the database schema to build the in-memory cache.

### Multi-Platform Support
The 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>`.
6 changes: 6 additions & 0 deletions crates/pgt_configuration/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ use serde::{Deserialize, Serialize};
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
pub struct DatabaseConfiguration {
/// A connection string that encodes the full connection setup.
/// When provided, it takes precedence over the individual fields.
#[partial(bpaf(long("connection-string")))]
pub connection_string: Option<String>,

/// The host of the database.
/// Required if you want database-related features.
/// All else falls back to sensible defaults.
Expand Down Expand Up @@ -47,6 +52,7 @@ pub struct DatabaseConfiguration {
impl Default for DatabaseConfiguration {
fn default() -> Self {
Self {
connection_string: None,
disable_connection: false,
host: "127.0.0.1".to_string(),
port: 5432,
Expand Down
1 change: 1 addition & 0 deletions crates/pgt_configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl PartialConfiguration {
..Default::default()
}),
db: Some(PartialDatabaseConfiguration {
connection_string: None,
host: Some("127.0.0.1".to_string()),
port: Some(5432),
username: Some("postgres".to_string()),
Expand Down
Loading