Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Comma Position #697

Open
ChenZhouUC opened this issue Jan 3, 2024 · 2 comments
Open

Feature Request: Comma Position #697

ChenZhouUC opened this issue Jan 3, 2024 · 2 comments
Labels

Comments

@ChenZhouUC
Copy link

When using the previous version of sql formatter, I found comma position configuration very useful in debugging. I wondered if this feature could be brought back sometime.

@nene
Copy link
Collaborator

nene commented Jan 3, 2024

As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:

import { format } from "sql-formatter";
import { formatCommaPositions } from "./formatCommaPositions";

const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" });
console.log(formatCommaPositions(sql, "before", "  "));

Issues

You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...

The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):

SELECT
    foo
    , bar
    , baz
FROM
    tbl

But this implementation unindents each line with a comma by two spaces:

SELECT
    foo
  , bar
  , baz
FROM
    tbl

Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.

Future

I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.

@ChenZhouUC
Copy link
Author

As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:

import { format } from "sql-formatter";
import { formatCommaPositions } from "./formatCommaPositions";

const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" });
console.log(formatCommaPositions(sql, "before", "  "));

Issues

You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...

The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):

SELECT
    foo
    , bar
    , baz
FROM
    tbl

But this implementation unindents each line with a comma by two spaces:

SELECT
    foo
  , bar
  , baz
FROM
    tbl

Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.

Future

I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.

Thank you for the workaround and clear explanation. Looking forward to the future plans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants