formatting - Is it possible to achieve JetBrains-like type alignment with SQLFluff? - Stack Overflow

admin2025-04-24  1

I’m using SQLFluff for formatting SQL files and trying to replicate the behavior of JetBrains IDEs where types are perfectly aligned under each other, and constraints (e.g., NOT NULL) dynamically adjust with spaces for proper alignment. Here’s an example of the alignment I’m trying to achieve:

CREATE TABLE products (
    product_id   BIGINT      PRIMARY KEY,
    name         TEXT        NOT NULL,
    description  TEXT,
    price        NUMERIC(10, 2) NOT NULL,
    in_stock     BOOLEAN     DEFAULT TRUE,
    created_at   TIMESTAMP   DEFAULT now()
);

The key formatting features I’m looking for are:

  1. Dynamic type alignment: Types (TEXT, BIGINT, NUMERIC) should align under each other.
  2. Constraint alignment: Keywords like NOT NULL, DEFAULT, or PRIMARY KEY should also align dynamically.

I’ve explored the following SQLFluff configuration: • layout.indent: For consistent indentation. • layout.spacing: To prevent extra spaces. • Rules like layout.type:datatype and layout.type:column_constraint for alignment.

Here’s an excerpt from my .sqlfluff file:

[sqlfluff]
dialect = postgres

[sqlfluff:rules:layout.indent]
indent_unit = space
tab_space_size = 4

[sqlfluff:layout:type:datatype]
align_with = type

[sqlfluff:layout:type:column_constraint]
align_with = type

Any help or suggestions would be greatly appreciated!

转载请注明原文地址:http://anycun.com/QandA/1745456792a90692.html