sql-formatter-org / sql-formatter

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

Feature Request: Inline mode #360

Open ericyangpan opened 2 years ago

ericyangpan commented 2 years ago

Describe the Feature Sometimes I need inline mode. For example, 'select from tbl' to 'SELECT FROM tbl;'.

Why do you want this feature? Inline mode is a frequent scene for simple SQL statement in docs.

nene commented 2 years ago

Seems like a pretty sensible request. Though it probably makes more sense for use in an editor plugin, e.g. to format a selected line.

It would actually be not that hard to implement. We already have some-sort of inline mode inside the formatter to format short parenthesized blocks. We'd need to extend this to also apply for other parts of SQL which currently trigger line breaks.

Perhaps another value for the indentStyle option, like indentStyle: 'inline' or indentStyle: 'none'.

@inferrinizzard what do you think of this?

inferrinizzard commented 2 years ago

Yes, definitely a use case for many SQL devs - this is along the lines of what the keywordNewline config was trying to achieve but now is easier with a parser structure

ericyangpan commented 2 years ago

Yes, definitely a use case for many SQL devs - this is along the lines of what the keywordNewline config was trying to achieve but now is easier with a parser structure

I saw it ware removed in changelog of keywordNewline

borgogelli commented 2 years ago

+1 It could be a very useful feature

patdx commented 1 year ago

This would be a cool feature to have. I have come across two different situations when I wished for this feature recently. I found a neat trick to get quite close:

import { format } from "sql-formatter";

console.log(
  format(
    `insert into my_table ( a, b, c, d ) values ( 1, "this is a very long value that wraps", "this is a very long value that wraps", "this is a very long value that wraps" )`,
    {
      language: "sqlite",
      tabWidth: 0,
      expressionWidth: 100000,
      keywordCase: "upper",
    }
  ).replace(/\n+/g, " ")
);

// Output:
// INSERT INTO my_table (a, b, c, d) VALUES (1, "this is a very long value that wraps", "this is a very long value that wraps", "this is a very long value that wraps")

Setting the expressionWidth to a really big value solves the issue with extra space instead of parenthesis, which looks weird on one line. Though I am not 100% sure if this works in all situations since it is kind of a hack. Having it as an official feature would give extra confidence about the reliability.