sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.32k stars 398 forks source link

[FORMATTING] No new line except before `;` and no caps #708

Closed CHRIBUR0309 closed 6 months ago

CHRIBUR0309 commented 8 months ago

Input data

Which SQL and options did you provide as input?

create table if not exists user (user_id text primary key, password text not null);

Expected Output

CREATE TABLE IF NOT EXISTS user (
    user_id TEXT PRIMARY KEY,
    password TEXT NOT NULL)
;

Actual Output

create table if not exists user (user_id text primary key, password text not null)
;

Usage

settings.json of VS Code:

  "SQL-Formatter-VSCode.dataTypeCase": "upper",
  "SQL-Formatter-VSCode.dialect": "sql",
  "SQL-Formatter-VSCode.expressionWidth": 88,
  "SQL-Formatter-VSCode.functionCase": "upper",
  "SQL-Formatter-VSCode.identifierCase": "lower",
  "SQL-Formatter-VSCode.ignoreTabSettings": true,
  "SQL-Formatter-VSCode.newlineBeforeSemicolon": true,

sql

4.0.2

nene commented 8 months ago

There are multiple problems here...

1. You are using the default "sql" dialect

This is generally not recommended. I don't know which actual SQL dialect your code is written in, but the default dialect has a very limited number of keywords and data type names (e.g. text happens to not be among them and so is not upper-cased). Pick a more specific dialect if possible.

2. You haven't configured the keywordCase option.

The default is preserve. Therefore no keywords have been converted to uppercase.

3. You have configured a pretty large value for the expressionWidth option

I now notice that the description of it in VSCode settings is misleading:

Number of characters allowed in each line before breaking

Actually that's not the case. What it really controls is the maximum number of characters between a pair of parenthesis before they get broken to multiple lines.

Although in this specific case, even the default value 50 will still keep the parenthesis on a single line.

I admit that this is a crappy and unintuitive behavior. Something I've been planning to fix for ages...

nene commented 6 months ago

Not much to fix here. Closing.