Skip to main content

Overview

Prisma Format (prisma-fmt) is a multi-purpose tool that provides:
  1. Schema formatting: Consistent, automatic formatting for Prisma schema files
  2. Language Server Protocol (LSP): Powers IDE features like autocomplete, hover, diagnostics, and more
  3. Schema linting: Warnings and suggestions for schema improvements
  4. Code actions: Quick fixes and refactoring suggestions
prisma-fmt is the bridge between your editor and the Prisma Schema Language. It’s what makes the Prisma VS Code extension and other editor integrations possible.

Architecture

Prisma Format is built on top of the PSL (Prisma Schema Language) parser and provides LSP-compliant features:

Formatting

Consistent schema formatting with configurable indentation

LSP Features

Autocomplete, hover info, diagnostics, references, and code actions

Validation

Real-time schema validation and error reporting

Quick Fixes

Automated solutions for common schema issues

Core Modules

The crate is organized into focused modules:

API Functions

Schema Formatting

Format Prisma schemas with consistent style:
Input:
  • datamodel: JSON-serialized SchemaFileInput (single or multiple files)
  • params: JSON-serialized DocumentFormattingParams (LSP standard)
Example:
Output: Formatted schema string
The formatter preserves comments and uses the configured tab size (typically 2 spaces for Prisma schemas).

Text Document Completion

Provides autocomplete suggestions:
Completions provided for:
  • Model names
  • Field types
  • Attribute names (@id, @unique, @default, etc.)
  • Attribute arguments
  • Datasource providers
  • Generator providers
  • Relation field names
  • Enum values
Example completion scenarios:

Hover Information

Provides contextual information on hover:
Hover info includes:
  • Field type documentation
  • Attribute descriptions
  • Model relationship information
  • Native type mappings
  • Validation constraints

Code Actions

Suggests quick fixes and refactorings:
Available code actions:

Block Actions

Add missing opposite relation fields, add @@map for naming conventions

Field Actions

Add missing relation arguments, fix relation field types

MongoDB Actions

Add @db.ObjectId for MongoDB IDs, fix composite type usage

Multi-Schema Actions

Add schema references for multi-schema setups

Relation Mode Actions

Add relation mode configuration, add missing indexes

Relation Actions

Fix broken relations, add relation names, add referential actions

Find References

Finds all references to a symbol:
Can find references for:
  • Model names (used in relations, @@map, etc.)
  • Enum names (used in field types)
  • Field names (used in relations, indexes)
  • Composite type names (MongoDB)

Schema Validation

Validates schema and returns errors:
Validation includes:
  • Syntax errors
  • Type errors
  • Relation errors
  • Constraint violations
  • Attribute validation
  • Database-specific validations
Error format:

Configuration Extraction

Extracts datasource and generator configuration:
Input params:
Output: JSON with datasources and generators

DMMF Generation

Generates Data Model Meta Format:
DMMF includes:
  • All models and their fields
  • All enums and their values
  • Relations and their properties
  • Unique constraints and indexes
  • Used for Prisma Client generation

Schema Merging

Merges multiple schema files:
Useful for:
  • Multi-file schema support
  • Client generation from split schemas
  • Schema composition

Linting

Provides schema quality warnings:
Lint warnings include:
  • Naming convention violations
  • Missing indexes on foreign keys
  • Inefficient relation patterns
  • Deprecated syntax usage

LSP Context

Internal LSP operations use a shared context:
This context provides access to:
  • Parsed schema database
  • Configuration (datasources, generators)
  • Active file being edited
  • Database connector information

Code Actions in Detail

Block-Level Actions

Add missing opposite relation field:

Field-Level Actions

Fix relation field types:

MongoDB-Specific Actions

Add @db.ObjectId for MongoDB:

Relation Mode Actions

Add missing indexes for Prisma relation mode:

Integration with Editors

VS Code

The Prisma VS Code extension uses prisma-fmt:
  • Runs as a language server process
  • Provides real-time diagnostics
  • Offers autocomplete and hover info
  • Enables code actions (quick fixes)
  • Handles formatting on save

Other Editors

Any editor with LSP support can use prisma-fmt:
  • Neovim: Via nvim-lspconfig
  • Emacs: Via lsp-mode
  • Sublime Text: Via LSP package
  • IntelliJ IDEA: Via Prisma plugin
LSP server command:

Testing

prisma-fmt uses snapshot testing extensively:

Test Structure

Tests use expect! macros for snapshot testing:
When changing diagnostics or completions, always run with UPDATE_EXPECT=1 to regenerate snapshots, then review the diffs carefully.

Preview Features

Get available preview features:
Returns JSON with all preview features and their status (active, deprecated, etc.).

Native Types

Get native type information:
Returns available native types for the specified connector:

Referential Actions

Get available referential actions:
Returns referential actions supported by the datasource:

Binary Distribution

prisma-fmt is distributed as:
  1. Native binary: For CLI usage and local LSP servers
  2. WASM module: For browser-based tools and web editors

Building

WASM Build

See prisma-schema-wasm crate for browser-compatible builds.

Common Issues

CRLF Line Endings

Some test fixtures expect CRLF line endings (Windows-style). Don’t convert these to LF or tests will fail.
Example test:

Snapshot Updates

After changing diagnostics:
  1. Run tests with UPDATE_EXPECT=1
  2. Review git diff carefully
  3. Ensure changes are intentional
  4. Commit updated snapshots

Feature Flags

Some tests require specific feature flags:

Performance

The LSP implementation is designed for real-time performance:
  • Incremental parsing: Only re-parse changed portions
  • Caching: Parser database cached between requests
  • Async operations: Non-blocking LSP server
  • Efficient completion: O(1) lookup for most completions

Example Usage

Formatting via CLI

Running as LSP Server

The server communicates via stdin/stdout using JSON-RPC 2.0.

Validation

Source Code

Explore the prisma-fmt source code:
  • Main crate: prisma-fmt/
  • LSP implementation: prisma-fmt/src/
  • Code actions: prisma-fmt/src/code_actions/
  • Completions: prisma-fmt/src/text_document_completion.rs
  • Repository: prisma/prisma-engines