mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 00:41:03 +02:00
feat: add |||- chomped text block syntax (#773)
Resolves google/jsonnet#289 Companion PR to google/jsonnet#1175
This commit is contained in:
parent
a45dd8a8f4
commit
e6f64e89f1
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
@ -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:
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
}
|
||||
|
1
testdata/block_string_chomped.golden
vendored
Normal file
1
testdata/block_string_chomped.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"foo\nbar\nbaz"
|
5
testdata/block_string_chomped.jsonnet
vendored
Normal file
5
testdata/block_string_chomped.jsonnet
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
|||-
|
||||
foo
|
||||
bar
|
||||
baz
|
||||
|||
|
0
testdata/block_string_chomped.linter.golden
vendored
Normal file
0
testdata/block_string_chomped.linter.golden
vendored
Normal file
1
testdata/block_string_chomped_concatted.golden
vendored
Normal file
1
testdata/block_string_chomped_concatted.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"foo bar baz all one line"
|
5
testdata/block_string_chomped_concatted.jsonnet
vendored
Normal file
5
testdata/block_string_chomped_concatted.jsonnet
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
|||-
|
||||
foo bar baz
|
||||
||| + |||-
|
||||
all one line
|
||||
|||
|
0
testdata/block_string_chomped_concatted.linter.golden
vendored
Normal file
0
testdata/block_string_chomped_concatted.linter.golden
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user