Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Do note though that this does not change how multiple
with statements are grouped. Although I agree with that,
it's IMHO a separate feature request, while the spacing
issue is a bug.

Signed-off-by: Anders Eknert <anders@eknert.com>
  • Loading branch information
anderseknert committed Mar 26, 2022
1 parent c08b81d commit 13dc7ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
11 changes: 7 additions & 4 deletions format/format.go
Expand Up @@ -110,7 +110,7 @@ func Ast(x interface{}) ([]byte, error) {
case *ast.Expr:
w.writeExpr(x, nil)
case *ast.With:
w.writeWith(x, nil)
w.writeWith(x, nil, false)
case *ast.Term:
w.writeTerm(x, nil)
case ast.Value:
Expand Down Expand Up @@ -452,7 +452,7 @@ func (w *writer) writeExpr(expr *ast.Expr, comments []*ast.Comment) []*ast.Comme
w.endLine()
w.startLine()
}
comments = w.writeWith(with, comments)
comments = w.writeWith(with, comments, indented)
}

return comments
Expand Down Expand Up @@ -556,9 +556,12 @@ func (w *writer) writeFunctionCallPlain(terms []*ast.Term, comments []*ast.Comme
return w.writeIterable(args, loc, closingLoc(0, 0, '(', ')', loc), comments, w.listWriter())
}

func (w *writer) writeWith(with *ast.With, comments []*ast.Comment) []*ast.Comment {
func (w *writer) writeWith(with *ast.With, comments []*ast.Comment, indented bool) []*ast.Comment {
comments = w.insertComments(comments, with.Location)
w.write(" with ")
if !indented {
w.write(" ")
}
w.write("with ")
comments = w.writeTerm(with.Target, comments)
w.write(" as ")
return w.writeTerm(with.Value, comments)
Expand Down
12 changes: 0 additions & 12 deletions format/testfiles/test.rego
Expand Up @@ -195,18 +195,6 @@ declare1 := 1

declare2 := 2 { false }

multi_line_with {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {
"foo": "bar",
}
with input.d as [
1,
2,
3]
}

# more comments!
# more comments!
# more comments!
Expand Down
11 changes: 0 additions & 11 deletions format/testfiles/test.rego.formatted
Expand Up @@ -226,17 +226,6 @@ declare2 := 2 {
false
}

multi_line_with {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {"foo": "bar"}
with input.d as [
1,
2,
3,
]
}

# more comments!
# more comments!
# more comments!
Expand Down
17 changes: 17 additions & 0 deletions format/testfiles/test_with.rego
@@ -0,0 +1,17 @@
package p

single_line_with {
fn(1) with input.a as "a"
}

multi_line_with {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {
"foo": "bar",
}
with input.d as [
1,
2,
3]
}
16 changes: 16 additions & 0 deletions format/testfiles/test_with.rego.formatted
@@ -0,0 +1,16 @@
package p

single_line_with {
fn(1) with input.a as "a"
}

multi_line_with {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {"foo": "bar"}
with input.d as [
1,
2,
3,
]
}

0 comments on commit 13dc7ec

Please sign in to comment.