diff --git a/README.md b/README.md index a801e57..1ccba59 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,15 @@ There are also several SQL-specific options: _Since 0.21.0_ +- **`sqlWrap`**: `"preserve" | "collapse"` (default `"preserve"`) + + How to wrap SQL that could fit on fewer lines. + With `"preserve"`, code that's multi-line in the original stays multi-line. + With `"collapse"`, the original line breaks are ignored and lines are only broken when they don't fit, + like Prettier's [objectWrap][] option does for JavaScript objects. + + _Since 0.24.0_ + ## Usage inside VSCode To use this plugin inside VSCode, @@ -282,6 +291,7 @@ Though the general principles should be mostly in place by now. [printWidth]: https://prettier.io/docs/en/options.html#print-width [tabWidth]: https://prettier.io/docs/en/options.html#tab-width [useTabs]: https://prettier.io/docs/en/options.html#tabs +[objectWrap]: https://prettier.io/docs/en/options.html#object-wrap [sql-parser-cst]: https://github.com/nene/sql-parser-cst [wadler-prettier]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf [prettier-plugin-embed]: https://github.com/Sec-ant/prettier-plugin-embed diff --git a/src/options.ts b/src/options.ts index 9603a1f..5a59055 100644 --- a/src/options.ts +++ b/src/options.ts @@ -11,6 +11,7 @@ export interface SqlPluginOptions { sqlCanonicalSyntax: boolean; sqlFinalSemicolon: boolean; sqlExperimentalPlpgsql: boolean; + sqlWrap: "preserve" | "collapse"; } // Prettier builtin options + options of this plugin @@ -165,4 +166,23 @@ export const options: SupportOptions = { "Enables formatting for PL/pgSQL in PostgreSQL function bodies", // Since 0.21.0 }, + sqlWrap: { + type: "choice", + category: "SQL", + default: "preserve", + description: "How to wrap SQL that could fit on fewer lines", + choices: [ + { + value: "preserve", + description: + "keeps code multi-line when the original code is multi-line", + since: "0.24.0", + }, + { + value: "collapse", + description: "breaks lines only when they don't fit", + since: "0.24.0", + }, + ], + }, }; diff --git a/src/print_utils.ts b/src/print_utils.ts index e2139bc..06d0c8f 100644 --- a/src/print_utils.ts +++ b/src/print_utils.ts @@ -1,5 +1,6 @@ import { Doc, doc, util } from "prettier"; import { Node, Whitespace } from "sql-parser-cst"; +import { SqlPluginOptions } from "./options"; import { isArray } from "./utils"; export const { @@ -21,11 +22,17 @@ export const { stripTrailingHardline } = doc.utils; export const join = (sep: Doc, docs: Doc): Doc => isArray(docs) ? origJoin(sep, docs) : docs; -/** True when the Node contains a newline in original source code */ +/** + * True when the Node contains a newline in original source code. + * Always false with sqlWrap: "collapse", which ignores the original line breaks. + */ export const containsNewline = ( node: Node, - opts: { originalText: string }, + opts: { originalText: string; sqlWrap: SqlPluginOptions["sqlWrap"] }, ): boolean => { + if (opts.sqlWrap === "collapse") { + return false; + } if (!node.range) { throw new Error("containsNewline() expected a Node with range info"); } diff --git a/test/options/wrap.test.ts b/test/options/wrap.test.ts new file mode 100644 index 0000000..4154f09 --- /dev/null +++ b/test/options/wrap.test.ts @@ -0,0 +1,93 @@ +import dedent from "dedent-js"; +import { pretty } from "../test_utils"; + +describe("sqlWrap option", () => { + describe("with sqlWrap: preserve (the default)", () => { + it(`keeps multi-line statement on multiple lines`, async () => { + expect( + await pretty(dedent` + SELECT a, b, c + FROM tbl + WHERE x > 10 + `), + ).toBe(dedent` + SELECT a, b, c + FROM tbl + WHERE x > 10 + `); + }); + }); + + describe("with sqlWrap: collapse", () => { + it(`formats multi-line statement on single line when it fits`, async () => { + expect( + await pretty( + dedent` + SELECT a, b, c + FROM tbl + WHERE x > 10 + `, + { sqlWrap: "collapse" }, + ), + ).toBe(`SELECT a, b, c FROM tbl WHERE x > 10`); + }); + + it(`formats multi-line SELECT columns on single line when they fit`, async () => { + expect( + await pretty( + dedent` + SELECT + id, + name + FROM person + `, + { sqlWrap: "collapse" }, + ), + ).toBe(`SELECT id, name FROM person`); + }); + + it(`formats multi-line CREATE TABLE on single line when it fits`, async () => { + expect( + await pretty( + dedent` + CREATE TABLE person ( + id INT, + name TEXT + ) + `, + { sqlWrap: "collapse" }, + ), + ).toBe(`CREATE TABLE person (id INT, name TEXT)`); + }); + + it(`still breaks statement that does not fit`, async () => { + expect( + await pretty(`SELECT a, b, c FROM tbl WHERE x > 10`, { + sqlWrap: "collapse", + printWidth: 20, + }), + ).toBe(dedent` + SELECT a, b, c + FROM tbl + WHERE x > 10 + `); + }); + + it(`keeps empty line between statements`, async () => { + expect( + await pretty( + dedent` + SELECT 1; + + SELECT 2 + `, + { sqlWrap: "collapse" }, + ), + ).toBe(dedent` + SELECT 1; + + SELECT 2 + `); + }); + }); +});