Skip to content

Commit e6f64e8

Browse files
authored
feat: add |||- chomped text block syntax (#773)
Resolves google/jsonnet#289 Companion PR to google/jsonnet#1175
1 parent a45dd8a commit e6f64e8

9 files changed

+36
-4
lines changed

Diff for: internal/formatter/fix_indentation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ func (c *FixIndentation) Visit(expr ast.Node, currIndent indent, crowded bool) {
685685
node.BlockIndent = strings.Repeat(" ", currIndent.base+c.Options.Indent)
686686
node.BlockTermIndent = strings.Repeat(" ", currIndent.base)
687687
c.column = currIndent.base // blockTermIndent
688-
c.column += 3 // "|||"
688+
c.column += 3 // always "|||" (never "|||-" because we're only accounting for block end)
689689
case ast.VerbatimStringSingle:
690690
c.column += 3 // Include @, start and end quotes
691691
for _, r := range node.Value {

Diff for: internal/formatter/unparser.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,11 @@ func (u *unparser) unparse(expr ast.Node, crowded bool) {
467467
u.write(node.Value)
468468
u.write("'")
469469
case ast.StringBlock:
470-
u.write("|||\n")
470+
u.write("|||")
471+
if node.Value[len(node.Value)-1] != '\n' {
472+
u.write("-")
473+
}
474+
u.write("\n")
471475
if node.Value[0] != '\n' {
472476
u.write(node.BlockIndent)
473477
}
@@ -481,6 +485,9 @@ func (u *unparser) unparse(expr ast.Node, crowded bool) {
481485
u.write(node.BlockIndent)
482486
}
483487
}
488+
if node.Value[len(node.Value)-1] != '\n' {
489+
u.write("\n")
490+
}
484491
u.write(node.BlockTermIndent)
485492
u.write("|||")
486493
case ast.VerbatimStringDouble:

Diff for: internal/parser/lexer.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ func (l *lexer) lexSymbol() error {
719719
if r == '|' && strings.HasPrefix(l.input[l.pos.byteNo:], "||") {
720720
commentStartLoc := l.tokenStartLoc
721721
l.acceptN(2) // Skip "||"
722+
723+
var chompTrailingNl bool = false
724+
if l.peek() == '-' {
725+
chompTrailingNl = true
726+
l.next()
727+
}
728+
722729
var cb bytes.Buffer
723730

724731
// Skip whitespace
@@ -775,7 +782,13 @@ func (l *lexer) lexSymbol() error {
775782
return l.makeStaticErrorPoint("Text block not terminated with |||", commentStartLoc)
776783
}
777784
l.acceptN(3) // Skip '|||'
778-
l.emitFullToken(tokenStringBlock, cb.String(),
785+
786+
var str string = cb.String()
787+
if chompTrailingNl {
788+
str = str[:len(str)-1]
789+
}
790+
791+
l.emitFullToken(tokenStringBlock, str,
779792
stringBlockIndent, stringBlockTermIndent)
780793
l.resetTokenStart()
781794
return nil
@@ -793,7 +806,7 @@ func (l *lexer) lexSymbol() error {
793806
if r == '/' && strings.HasPrefix(l.input[l.pos.byteNo:], "*") {
794807
break
795808
}
796-
// Not allowed ||| in operators
809+
// Not allowed ||| in operators (accounts for |||-)
797810
if r == '|' && strings.HasPrefix(l.input[l.pos.byteNo:], "||") {
798811
break
799812
}

Diff for: testdata/block_string_chomped.golden

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"foo\nbar\nbaz"

Diff for: testdata/block_string_chomped.jsonnet

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
|||-
2+
foo
3+
bar
4+
baz
5+
|||

Diff for: testdata/block_string_chomped.linter.golden

Whitespace-only changes.

Diff for: testdata/block_string_chomped_concatted.golden

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"foo bar baz all one line"

Diff for: testdata/block_string_chomped_concatted.jsonnet

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
|||-
2+
foo bar baz
3+
||| + |||-
4+
all one line
5+
|||

Diff for: testdata/block_string_chomped_concatted.linter.golden

Whitespace-only changes.

0 commit comments

Comments
 (0)