|
| 1 | +# Formatting |
| 2 | + |
| 3 | +> **Preview Feature**: The formatter is currently in preview. We'd love feedback from early adopters! Please report any issues or unexpected output at [GitHub Issues](https://github.com/supabase-community/postgres-language-server/issues). |
| 4 | +
|
| 5 | +The language server provides SQL formatting that produces consistent, readable code. Built on Postgres' own parser, the formatter ensures 100% syntax compatibility with your SQL. |
| 6 | + |
| 7 | +## Configuration |
| 8 | + |
| 9 | +Configure formatting behavior in your `postgres-language-server.jsonc`: |
| 10 | + |
| 11 | +```json |
| 12 | +{ |
| 13 | + "format": { |
| 14 | + "enabled": true, |
| 15 | + "lineWidth": 100, |
| 16 | + "indentSize": 2, |
| 17 | + "indentStyle": "spaces", |
| 18 | + "keywordCase": "lower", |
| 19 | + "constantCase": "lower", |
| 20 | + "typeCase": "lower" |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### Options |
| 26 | + |
| 27 | +| Option | Default | Description | |
| 28 | +|--------|---------|-------------| |
| 29 | +| `enabled` | `true` | Enable or disable the formatter | |
| 30 | +| `lineWidth` | `100` | Maximum line width before breaking | |
| 31 | +| `indentSize` | `2` | Number of spaces (or tab width) for indentation | |
| 32 | +| `indentStyle` | `"spaces"` | Use `"spaces"` or `"tabs"` for indentation | |
| 33 | +| `keywordCase` | `"lower"` | Casing for SQL keywords: `"upper"` or `"lower"` | |
| 34 | +| `constantCase` | `"lower"` | Casing for constants (NULL, TRUE, FALSE): `"upper"` or `"lower"` | |
| 35 | +| `typeCase` | `"lower"` | Casing for data types (text, int, varchar): `"upper"` or `"lower"` | |
| 36 | + |
| 37 | +### Example Output |
| 38 | + |
| 39 | +With default settings (lowercase): |
| 40 | + |
| 41 | +```sql |
| 42 | +create table users ( |
| 43 | + id serial primary key, |
| 44 | + name text not null, |
| 45 | + active boolean default true |
| 46 | +); |
| 47 | + |
| 48 | +select * from users where active = true; |
| 49 | +``` |
| 50 | + |
| 51 | +With uppercase keywords and constants: |
| 52 | + |
| 53 | +```sql |
| 54 | +CREATE TABLE users ( |
| 55 | + id serial PRIMARY KEY, |
| 56 | + name text NOT NULL, |
| 57 | + active boolean DEFAULT TRUE |
| 58 | +); |
| 59 | + |
| 60 | +SELECT * FROM users WHERE active = TRUE; |
| 61 | +``` |
| 62 | + |
| 63 | +## CLI Usage |
| 64 | + |
| 65 | +Format files using the CLI: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Format and show diff |
| 69 | +postgres-language-server format file.sql |
| 70 | + |
| 71 | +# Format and write changes |
| 72 | +postgres-language-server format file.sql --write |
| 73 | + |
| 74 | +# Format entire directory |
| 75 | +postgres-language-server format migrations/ --write |
| 76 | +``` |
| 77 | + |
| 78 | +## Editor Integration |
| 79 | + |
| 80 | +The formatter integrates with your editor via the Language Server Protocol. Use your editor's format document command (typically bound to a keyboard shortcut) to format SQL files. |
| 81 | + |
| 82 | +## Ignoring Files |
| 83 | + |
| 84 | +Use the `ignore` and `include` options to control which files are formatted: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "format": { |
| 89 | + "ignore": ["**/generated/**", "**/vendor/**"], |
| 90 | + "include": ["**/*.sql"] |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments