|
| 1 | +From c11283e6d2342e0801ace7d1a8badbfe63ee71a4 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Oliver Tan <otan@cockroachlabs.com> |
| 3 | +Date: Thu, 3 Nov 2022 15:05:20 +1100 |
| 4 | +Subject: [PATCH] add extra pretty option for no new lines |
| 5 | + |
| 6 | +--- |
| 7 | + pkg/sql/sem/tree/pretty.go | 8 +++++--- |
| 8 | + pkg/util/pretty/util.go | 9 ++++++--- |
| 9 | + 2 files changed, 11 insertions(+), 6 deletions(-) |
| 10 | + |
| 11 | +diff --git a/pkg/sql/sem/tree/pretty.go b/pkg/sql/sem/tree/pretty.go |
| 12 | +index 6df5efe..5e3715f 100644 |
| 13 | +--- a/pkg/sql/sem/tree/pretty.go |
| 14 | ++++ b/pkg/sql/sem/tree/pretty.go |
| 15 | +@@ -47,6 +47,8 @@ type PrettyCfg struct { |
| 16 | + // TabWidth is the amount of spaces to use for tabs when UseTabs is |
| 17 | + // false. |
| 18 | + TabWidth int |
| 19 | ++ // DoNotNewLineAfterColName is true if we do not new line after table column names. |
| 20 | ++ DoNotNewLineAfterColName bool |
| 21 | + // Align, when set to another value than PrettyNoAlign, uses |
| 22 | + // alignment for some constructs as a first choice. If not set or if |
| 23 | + // the line width is insufficient, nesting is used instead. |
| 24 | +@@ -195,7 +197,7 @@ func (p *PrettyCfg) rlTable(rows ...pretty.TableRow) pretty.Doc { |
| 25 | + if p.Align != PrettyNoAlign { |
| 26 | + alignment = pretty.TableRightAlignFirstColumn |
| 27 | + } |
| 28 | +- return pretty.Table(alignment, pretty.Keyword, rows...) |
| 29 | ++ return pretty.Table(alignment, pretty.Keyword, p.DoNotNewLineAfterColName, rows...) |
| 30 | + } |
| 31 | + |
| 32 | + // llTable produces a Table using Left alignment of the first column. |
| 33 | +@@ -204,7 +206,7 @@ func (p *PrettyCfg) llTable(docFn func(string) pretty.Doc, rows ...pretty.TableR |
| 34 | + if p.Align != PrettyNoAlign { |
| 35 | + alignment = pretty.TableLeftAlignFirstColumn |
| 36 | + } |
| 37 | +- return pretty.Table(alignment, docFn, rows...) |
| 38 | ++ return pretty.Table(alignment, docFn, p.DoNotNewLineAfterColName, rows...) |
| 39 | + } |
| 40 | + |
| 41 | + func (p *PrettyCfg) row(lbl string, d pretty.Doc) pretty.TableRow { |
| 42 | +@@ -242,7 +244,7 @@ func (p *PrettyCfg) joinNestedOuter(lbl string, d ...pretty.Doc) pretty.Doc { |
| 43 | + } |
| 44 | + items[i].Doc = dd |
| 45 | + } |
| 46 | +- return pretty.Table(pretty.TableRightAlignFirstColumn, pretty.Keyword, items...) |
| 47 | ++ return pretty.Table(pretty.TableRightAlignFirstColumn, pretty.Keyword, p.DoNotNewLineAfterColName, items...) |
| 48 | + default: |
| 49 | + return pretty.JoinNestedRight(pretty.Keyword(lbl), d...) |
| 50 | + } |
| 51 | +diff --git a/pkg/util/pretty/util.go b/pkg/util/pretty/util.go |
| 52 | +index 568461a..b754eb8 100644 |
| 53 | +--- a/pkg/util/pretty/util.go |
| 54 | ++++ b/pkg/util/pretty/util.go |
| 55 | +@@ -238,11 +238,11 @@ const ( |
| 56 | + // |
| 57 | + // docFn should be set to Text or Keyword and will be used when converting |
| 58 | + // TableRow label's to Docs. |
| 59 | +-func Table(alignment TableAlignment, docFn func(string) Doc, rows ...TableRow) Doc { |
| 60 | ++func Table(alignment TableAlignment, docFn func(string) Doc, noNewLine bool, rows ...TableRow) Doc { |
| 61 | + // Compute the nested formatting in "sections". It's simple. |
| 62 | + // Note that we do not use NestUnder() because we are not grouping |
| 63 | + // at this level (the group is done for the final form below). |
| 64 | +- items := makeTableNestedSections(docFn, rows) |
| 65 | ++ items := makeTableNestedSections(docFn, rows, noNewLine) |
| 66 | + nestedSections := Stack(items...) |
| 67 | + |
| 68 | + finalDoc := nestedSections |
| 69 | +@@ -300,7 +300,7 @@ func makeAlignedTableItems( |
| 70 | + return items |
| 71 | + } |
| 72 | + |
| 73 | +-func makeTableNestedSections(docFn func(string) Doc, rows []TableRow) []Doc { |
| 74 | ++func makeTableNestedSections(docFn func(string) Doc, rows []TableRow, noNewLine bool) []Doc { |
| 75 | + items := make([]Doc, 0, len(rows)) |
| 76 | + for _, r := range rows { |
| 77 | + if r.Doc == nil || (r.Label == "" && r.Doc == Nil) { |
| 78 | +@@ -309,6 +309,9 @@ func makeTableNestedSections(docFn func(string) Doc, rows []TableRow) []Doc { |
| 79 | + if r.Label != "" { |
| 80 | + d := simplifyNil(docFn(r.Label), r.Doc, |
| 81 | + func(a, b Doc) Doc { |
| 82 | ++ if noNewLine { |
| 83 | ++ return ConcatSpace(a, b) |
| 84 | ++ } |
| 85 | + return Concat(a, NestT(Concat(Line, Group(b)))) |
| 86 | + }) |
| 87 | + items = append(items, d) |
| 88 | +-- |
| 89 | +2.37.1 (Apple Git-137.1) |
| 90 | + |
0 commit comments