Skip to content

Commit

Permalink
feat: add |||- chomped text block syntax
Browse files Browse the repository at this point in the history
Resolves google/jsonnet#289

Companion PR to google/jsonnet#1175
  • Loading branch information
vergenzt committed Oct 11, 2024
1 parent 2b4d753 commit 0055be6
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/formatter/fix_indentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func (c *FixIndentation) Visit(expr ast.Node, currIndent indent, crowded bool) {
node.BlockIndent = strings.Repeat(" ", currIndent.base+c.Options.Indent)
node.BlockTermIndent = strings.Repeat(" ", currIndent.base)
c.column = currIndent.base // blockTermIndent
c.column += 3 // "|||"
c.column += 3 // always "|||" (never "|||-" because we're only accounting for block end)
case ast.VerbatimStringSingle:
c.column += 3 // Include @, start and end quotes
for _, r := range node.Value {
Expand Down
9 changes: 8 additions & 1 deletion internal/formatter/unparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ func (u *unparser) unparse(expr ast.Node, crowded bool) {
u.write(node.Value)
u.write("'")
case ast.StringBlock:
u.write("|||\n")
u.write("|||")
if node.Value[len(node.Value)-1] != '\n' {
u.write("-")
}
u.write("\n")
if node.Value[0] != '\n' {
u.write(node.BlockIndent)
}
Expand All @@ -481,6 +485,9 @@ func (u *unparser) unparse(expr ast.Node, crowded bool) {
u.write(node.BlockIndent)
}
}
if node.Value[len(node.Value)-1] != '\n' {
u.write("\n")
}
u.write(node.BlockTermIndent)
u.write("|||")
case ast.VerbatimStringDouble:
Expand Down
17 changes: 15 additions & 2 deletions internal/parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,13 @@ func (l *lexer) lexSymbol() error {
if r == '|' && strings.HasPrefix(l.input[l.pos.byteNo:], "||") {
commentStartLoc := l.tokenStartLoc
l.acceptN(2) // Skip "||"

var chompTrailingNl bool = false
if l.peek() == '-' {
chompTrailingNl = true
l.next()
}

var cb bytes.Buffer

// Skip whitespace
Expand Down Expand Up @@ -775,7 +782,13 @@ func (l *lexer) lexSymbol() error {
return l.makeStaticErrorPoint("Text block not terminated with |||", commentStartLoc)
}
l.acceptN(3) // Skip '|||'
l.emitFullToken(tokenStringBlock, cb.String(),

var str string = cb.String()
if chompTrailingNl {
str = str[:len(str)-1]
}

l.emitFullToken(tokenStringBlock, str,
stringBlockIndent, stringBlockTermIndent)
l.resetTokenStart()
return nil
Expand All @@ -793,7 +806,7 @@ func (l *lexer) lexSymbol() error {
if r == '/' && strings.HasPrefix(l.input[l.pos.byteNo:], "*") {
break
}
// Not allowed ||| in operators
// Not allowed ||| in operators (accounts for |||-)
if r == '|' && strings.HasPrefix(l.input[l.pos.byteNo:], "||") {
break
}
Expand Down
1 change: 1 addition & 0 deletions testdata/block_string_chomped.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo\nbar\nbaz"
5 changes: 5 additions & 0 deletions testdata/block_string_chomped.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
|||-
foo
bar
baz
|||
Empty file.
1 change: 1 addition & 0 deletions testdata/block_string_chomped_concatted.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo bar baz all one line"
5 changes: 5 additions & 0 deletions testdata/block_string_chomped_concatted.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
|||-
foo bar baz
||| + |||-
all one line
|||
Empty file.

0 comments on commit 0055be6

Please sign in to comment.