How to match CLI formatting with VS Code Prettier-SQL format? #920
-
|
I wanted to use the same formatting from the CLI as the VS Code Prettier-SQL formatter does and I found this repo from that page. So I installed {
"$schema": "https://raw.githubusercontent.com/sql-formatter-org/sql-formatter/refs/heads/master/schema.json",
"language": "postgresql",
"expressionWidth": 100,
"keywordCase": "upper",
"logicalOperatorNewline": "after",
"linesBetweenQueries": 0,
"tabWidth": 2,
"useTabs": false,
"dataTypeCase": "upper"
}Trying to match my VS Code Prettier-SQL settings, which are: {
"Prettier-SQL.expressionWidth": 100,
"Prettier-SQL.keywordCase": "upper",
"Prettier-SQL.logicalOperatorNewline": "after",
"Prettier-SQL.SQLFlavourOverride": "postgresql",
"Prettier-SQL.linesBetweenQueries": 0,
}But the CLI doesn't format nearly the same way as the extension. Do I have to use an older version of Here is how it formats with the CLICREATE TABLE "rev_log" (
"id" bigserial PRIMARY KEY NOT NULL,
"schema_name" TEXT NOT NULL,
"table_name" TEXT NOT NULL,
"relid" "oid" NOT NULL,
"action" TEXT NOT NULL,
"at" TIMESTAMP WITH TIME ZONE NOT NULL,
"session_user_name" TEXT,
"tx" BIGINT,
"app" TEXT,
"addr" "inet",
"port" INTEGER,
"data" JSONB,
"query" TEXT
);And here is how it formats with VS CodeCREATE TABLE
"rev_log" (
"id" bigserial PRIMARY KEY NOT NULL,
"schema_name" TEXT NOT NULL,
"table_name" TEXT NOT NULL,
"relid" "oid" NOT NULL,
"action" TEXT NOT NULL,
"at" TIMESTAMP WITH TIME ZONE NOT NULL,
"session_user_name" TEXT,
"tx" BIGINT,
"app" TEXT,
"addr" "inet",
"port" INTEGER,
"data" JSONB,
"query" TEXT
); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Turns out that the answer is indeed to use an older version, specifically With that, the only difference between CLI and VS Code formatting is that the CLI adds a final newline and VS Code Prettier-SQL does not. |
Beta Was this translation helpful? Give feedback.
-
|
Well... if you really want to use that outdated & unmaintained VSCode extension, then yes, you only option is to also use an old version of this library. I would recommend though switching to the official sql-formatter-vscode extension. Then you could use both the latest version of the command line tool and the latest version of the VSCode plugin. |
Beta Was this translation helpful? Give feedback.
-
|
Alternatively, as you're using PostgreSQL, you might want to try prettier-plugin-sql-cst, which does a much better job at formatting SQL, and you can seamlessly also use it from command line like any other prettier plugin. The caveat is that it doesn't have full PostgreSQL support. Then again, SQL Formatter doesn't have a full support either, but it usually fails more gracefully. |
Beta Was this translation helpful? Give feedback.
Well... if you really want to use that outdated & unmaintained VSCode extension, then yes, you only option is to also use an old version of this library.
I would recommend though switching to the official sql-formatter-vscode extension. Then you could use both the latest version of the command line tool and the latest version of the VSCode plugin.