sql-formatter icon indicating copy to clipboard operation
sql-formatter copied to clipboard

Feature Request: Inline mode

Open ericyangpan opened this issue 2 years ago • 5 comments

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.

ericyangpan avatar Jul 29 '22 07:07 ericyangpan

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?

nene avatar Jul 29 '22 09:07 nene

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

inferrinizzard avatar Jul 29 '22 14:07 inferrinizzard

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

ericyangpan avatar Aug 01 '22 07:08 ericyangpan

+1 It could be a very useful feature

borgogelli avatar Nov 03 '22 17:11 borgogelli

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.

patdx avatar Dec 12 '22 20:12 patdx