Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SqlPluginOptions {
sqlCanonicalSyntax: boolean;
sqlFinalSemicolon: boolean;
sqlExperimentalPlpgsql: boolean;
sqlWrap: "preserve" | "collapse";
}

// Prettier builtin options + options of this plugin
Expand Down Expand Up @@ -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",
},
],
},
};
11 changes: 9 additions & 2 deletions src/print_utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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");
}
Expand Down
93 changes: 93 additions & 0 deletions test/options/wrap.test.ts
Original file line number Diff line number Diff line change
@@ -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
`);
});
});
});
Loading