Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add |||- chomped text block syntax #773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.