From b86c1d46350be3d3dd4f2eb119356756dc335a87 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sat, 20 Jan 2024 11:36:19 +0200 Subject: [PATCH] More dynamic formatting of CREATE INDEX --- src/syntax/index.ts | 30 ++++++++++++++++-------------- test/ddl/index.test.ts | 8 +++++++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/syntax/index.ts b/src/syntax/index.ts index ab497c3..09409f8 100644 --- a/src/syntax/index.ts +++ b/src/syntax/index.ts @@ -1,22 +1,24 @@ import { AllIndexNodes } from "sql-parser-cst"; -import { hardline, join } from "../print_utils"; +import { group, join } from "../print_utils"; import { CstToDocMap } from "../CstToDocMap"; export const indexMap: CstToDocMap = { - create_index_stmt: (print, node) => - join(hardline, [ - print.spaced([ - "createKw", - "indexTypeKw", - "indexKw", - "ifNotExistsKw", - "name", - "onKw", - "table", - "columns", + create_index_stmt: (print) => + group( + join(print.dynamicLine(), [ + print.spaced([ + "createKw", + "indexTypeKw", + "indexKw", + "ifNotExistsKw", + "name", + "onKw", + "table", + "columns", + ]), + ...print("clauses"), ]), - ...(node.clauses.length > 0 ? [join(hardline, print("clauses"))] : []), - ]), + ), drop_index_stmt: (print) => print.spaced([ "dropKw", diff --git a/test/ddl/index.test.ts b/test/ddl/index.test.ts index c7abb45..e80a2c6 100644 --- a/test/ddl/index.test.ts +++ b/test/ddl/index.test.ts @@ -31,7 +31,13 @@ describe("index", () => { `); }); - it(`formats WHERE clause on separate line`, async () => { + it(`formats WHERE clause on same line (if user prefers)`, async () => { + await test(dedent` + CREATE INDEX my_index ON my_table (col) WHERE col > 10 + `); + }); + + it(`formats WHERE clause on separate line (if user prefers)`, async () => { await test(dedent` CREATE INDEX my_index ON my_table (col) WHERE col > 10