Skip to content

Commit 2ac4d85

Browse files
committed
cue/format: indent multiline string literals based on context.
Change-Id: Ic58d29dfa347a2f0b3642e56676c2c944dbb8b2d Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7283 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent c886094 commit 2ac4d85

File tree

9 files changed

+169
-67
lines changed

9 files changed

+169
-67
lines changed

cmd/cue/cmd/testdata/script/import_files.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ name: "booster"
88
replicas: 1
99
kind: "Service"
1010
name: """
11-
supplement
12-
foo
13-
"""
11+
supplement
12+
foo
13+
"""
1414
json: "[1, 2]"
1515
-- import/services.jsonl --
1616
{

cmd/cue/cmd/testdata/script/import_hoiststr.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ service: {
1414
"supplement\nfoo": [{
1515
kind: "Service"
1616
name: """
17-
supplement
18-
foo
19-
"""
17+
supplement
18+
foo
19+
"""
2020
json: json656e63.Marshal(_cue_json)
2121
let _cue_json = [1, 2]
2222
}]

cue/format/printer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"cuelang.org/go/cue/ast"
2424
"cuelang.org/go/cue/errors"
25+
"cuelang.org/go/cue/literal"
2526
"cuelang.org/go/cue/token"
2627
)
2728

@@ -116,6 +117,16 @@ func (p *printer) Print(v interface{}) {
116117
case *ast.BasicLit:
117118
data = x.Value
118119
switch x.Kind {
120+
case token.STRING:
121+
// TODO: only do this when simplifying. Right now this does not
122+
// give the right result, but it should be better if:
123+
// 1) simplification is done as a separate step
124+
// 2) simplified structs are explicitly referenced separately
125+
// in the AST.
126+
if p.indent < 6 {
127+
data = literal.IndentTabs(data, p.indent+1)
128+
}
129+
119130
case token.INT:
120131
if len(data) > 1 &&
121132
data[0] == '0' &&

cue/literal/indent.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package literal
16+
17+
import "strings"
18+
19+
// IndentTabs takes a quoted string and reindents it for the given indentation.
20+
// If a string is not a multiline string it will return the string as is.
21+
func IndentTabs(s string, n int) string {
22+
indent := tabs(n)
23+
24+
qi, _, _, err := ParseQuotes(s, s)
25+
if err != nil || !qi.multiline || qi.whitespace == indent {
26+
return s
27+
}
28+
29+
search := "\n" + qi.whitespace
30+
replace := "\n" + indent
31+
32+
return strings.ReplaceAll(s, search, replace)
33+
}

cue/literal/indent_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package literal
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestIndentTabs(t *testing.T) {
22+
testCases := []struct {
23+
in string
24+
out string
25+
}{{
26+
in: `"""
27+
foo
28+
bar
29+
"""`,
30+
out: `"""
31+
foo
32+
bar
33+
"""`,
34+
}, {
35+
in: `"""
36+
foo
37+
bar
38+
"""`,
39+
out: `"""
40+
foo
41+
bar
42+
"""`,
43+
}, {
44+
in: `""`,
45+
out: `""`,
46+
}}
47+
for _, tc := range testCases {
48+
t.Run("", func(t *testing.T) {
49+
got := IndentTabs(tc.in, 3)
50+
if got != tc.out {
51+
t.Errorf("got %s; want %s", got, tc.out)
52+
}
53+
})
54+
}
55+
}

cue/literal/quote.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ type Form struct {
4545

4646
// WithTabIndent returns a new Form with indentation set to the given number
4747
// of tabs. The result will be a multiline string.
48-
func (f Form) WithTabIndent(tabs int) Form {
49-
if tabs < len(tabIndent) {
50-
f.indent = tabIndent[:tabs]
51-
} else {
52-
f.indent = strings.Repeat("\t", tabs)
53-
}
48+
func (f Form) WithTabIndent(n int) Form {
49+
f.indent = tabs(n)
5450
f.multiline = true
5551
return f
5652
}
5753

5854
const tabIndent = "\t\t\t\t\t\t\t\t\t\t\t\t"
5955

56+
func tabs(n int) string {
57+
if n < len(tabIndent) {
58+
return tabIndent[:n]
59+
}
60+
return strings.Repeat("\t", n)
61+
}
62+
6063
// WithOptionalIndent is like WithTabIndent, but only returns a multiline
6164
// strings if it doesn't contain any newline characters.
6265
func (f Form) WithOptionalTabIndent(tabs int) Form {

doc/tutorial/kubernetes/quick/services/mon/alertmanager/configmap.cue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ configMap: alertmanager: {
1515
slack_configs: [{
1616
channel: "#cloudmon"
1717
text: """
18-
{{ range .Alerts }}{{ .Annotations.description }}
19-
{{ end }}
20-
"""
18+
{{ range .Alerts }}{{ .Annotations.description }}
19+
{{ end }}
20+
"""
2121
send_resolved: true
2222
}]
2323
}]

internal/core/export/testdata/strings.txtar

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ c: d: a
1010

1111
-- out/definition --
1212
a: """
13-
multi
14-
line
15-
"""
13+
multi
14+
line
15+
"""
1616
b: "message: \(a)!"
1717
c: {
1818
d: a
@@ -27,68 +27,68 @@ c: {
2727
== Simplified
2828
{
2929
a: """
30-
multi
31-
line
32-
"""
33-
b: """
34-
message: multi
35-
line!
36-
"""
37-
c: {
38-
d: """
3930
multi
4031
line
4132
"""
33+
b: """
34+
message: multi
35+
line!
36+
"""
37+
c: {
38+
d: """
39+
multi
40+
line
41+
"""
4242
}
4343
}
4444
== Raw
4545
{
4646
a: """
47-
multi
48-
line
49-
"""
50-
b: """
51-
message: multi
52-
line!
53-
"""
54-
c: {
55-
d: """
5647
multi
5748
line
5849
"""
50+
b: """
51+
message: multi
52+
line!
53+
"""
54+
c: {
55+
d: """
56+
multi
57+
line
58+
"""
5959
}
6060
}
6161
== Final
6262
{
6363
a: """
64-
multi
65-
line
66-
"""
67-
b: """
68-
message: multi
69-
line!
70-
"""
71-
c: {
72-
d: """
7364
multi
7465
line
7566
"""
67+
b: """
68+
message: multi
69+
line!
70+
"""
71+
c: {
72+
d: """
73+
multi
74+
line
75+
"""
7676
}
7777
}
7878
== All
7979
{
8080
a: """
81-
multi
82-
line
83-
"""
84-
b: """
85-
message: multi
86-
line!
87-
"""
88-
c: {
89-
d: """
9081
multi
9182
line
9283
"""
84+
b: """
85+
message: multi
86+
line!
87+
"""
88+
c: {
89+
d: """
90+
multi
91+
line
92+
"""
9393
}
9494
}

internal/third_party/yaml/decode_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,26 @@ apple: "newline"`,
205205
"scalar: | # Comment\n\n literal\n\n \ttext\n\n",
206206
`scalar: """
207207
208-
literal
208+
literal
209209
210-
\ttext
210+
\ttext
211211
212-
"""`,
212+
"""`,
213213
},
214214

215215
// Folded block scalar
216216
{
217217
"scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
218218
`scalar: """
219219
220-
folded line
221-
next line
222-
* one
223-
* two
220+
folded line
221+
next line
222+
* one
223+
* two
224224
225-
last line
225+
last line
226226
227-
"""`,
227+
"""`,
228228
},
229229

230230
// Structs
@@ -456,10 +456,10 @@ b: {
456456
` Line separator\u2028\` + "\n" +
457457
` Paragraph separator\u2029"` + "\n",
458458
`"""
459-
Generic line break (no glyph)
460-
Generic line break (glyphed)
461-
Line separator\u2028Paragraph separator\u2029
462-
"""`,
459+
Generic line break (no glyph)
460+
Generic line break (glyphed)
461+
Line separator\u2028Paragraph separator\u2029
462+
"""`,
463463
},
464464

465465
// bug 1243827

0 commit comments

Comments
 (0)