pgFormatter icon indicating copy to clipboard operation
pgFormatter copied to clipboard

Feature request: Vertical alignment

Open ufukty opened this issue 1 year ago • 3 comments

Feature Description

I would like to request a new feature for pgFormatter: vertical alignment for segments of column definitions in table definitions. This feature would improve the readability of the SQL scripts, especially those with multiple column definitions.

Current Behavior

Currently, pgFormatter formats table definitions, but the columns and their types are not vertically aligned, which can make the scripts less readable when there are many columns. For example:

CREATE TABLE example (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

Proposed Behavior

I propose a formatting option that aligns the column names, types, and other attributes vertically. For example:

CREATE TABLE example (
    id         SERIAL PRIMARY KEY,
    name       VARCHAR(100)             NOT NULL,
    email      VARCHAR(100)             NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

This formatting makes it easier to scan and compare the columns and their attributes.

Other examples

Vertical alignment is a common feature in many code formatters for other languages and would be a valuable addition to pgFormatter for those who prioritize code readability and consistency.

  • Go fmt has this functionality. Just like
    type person struct {
        name string
        age  int
    }
    
  • There is an open PR on Python Yapf

Previous discussions

I saw there were another request in this repo similar to this.

Last

I understand the feature is difficult to implement. Although its contribution to readability justifies. Example can be go code commonly referred as easily readable by developers. Maybe it is harmless to leave this issue open to collect more feedback.

ufukty avatar Jan 25 '24 10:01 ufukty

+1

chriswailes avatar Feb 22 '24 21:02 chriswailes

+1

salgado-unleashed avatar May 08 '24 00:05 salgado-unleashed

+1 still waiting 😏

PS: Comments should also get aligned I think:

CREATE TABLE example (
    id         SERIAL PRIMARY KEY,                                -- some comment
    name       VARCHAR(100)             NOT NULL,                 -- another comment
    email      VARCHAR(100)             NOT NULL,                 -- third comment
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP -- last comment
);

firu11 avatar Oct 30 '25 14:10 firu11