Skip to content

Commit

Permalink
Print function arguments on single line
Browse files Browse the repository at this point in the history
  • Loading branch information
FPtje committed Jan 1, 2024
1 parent e959668 commit b1bbdb7
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 319 deletions.
48 changes: 27 additions & 21 deletions src/GLua/AG/PrettyPrint.ag
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ attr MaybeMExpr Declaration VarsList
attr MStatList MExprList VarsList
syn isLast :: Bool

attr MExprList
-- The presence of comments can cause an MExpr in an MExprList to be printed on a new line. This
-- can affect e.g. AReturn, which should then not print a space after the word `return`.
syn startsWithNewline :: Bool

attr MExprList FieldList
-- Normally, the decision on whether something is multiline is synthesized, i.e. it bubbles up.
-- With lists, this decision can also flow downwards. This happens when one element in the list
Expand Down Expand Up @@ -414,25 +419,30 @@ sem MStat

sem MExprList
| Cons
lhs.pretty = if @loc.isMultiline then @loc.prettyMultiLine else @loc.prettySingleLine
loc.prettySingleLine =
@hd.pretty
<> @loc.comma
<> @tl.pretty
loc.prettyMultiLine =
renderMLComments @lhs.ppconf @lhs.indent (fst @loc.commentsBeforeLine)
$+$ indent @lhs.ppconf @lhs.indent @hd.pretty
<> @loc.comma
<-> renderSLComments @lhs.ppconf @lhs.indent (fst @loc.commentsAfter)
$+$ @tl.pretty
lhs.pretty =
-- If there are comments before the line, render the this part as multiline
(if @loc.startsWithNewline then
zchr '\n' <>
renderMLComments @lhs.ppconf (@lhs.indent + 1) (fst @loc.commentsBeforeLine) $+$
indent @lhs.ppconf (@lhs.indent + 1) @hd.pretty
else
@hd.pretty)
<> @loc.comma
-- If there are comments after the expression, render as multiline
<> (if (not $ null $ fst @loc.commentsAfter) then
renderSLComments @lhs.ppconf (@lhs.indent + 1) (fst @loc.commentsAfter) $+$
indent @lhs.ppconf (@lhs.indent + 1) @tl.pretty
else
@tl.pretty)
loc.comma =
if @tl.isLast then
empty
else
(if spaceBeforeComma @lhs.ppconf then zchr ' ' else empty)
<> zchr ','
<> (if not @loc.isMultiline && spaceAfterComma @lhs.ppconf then zchr ' ' else empty)
<> (if spaceAfterComma @lhs.ppconf then zchr ' ' else empty)

loc.startsWithNewline = not $ null $ fst @loc.commentsBeforeLine
loc.commentsBeforeLine = span (\(MToken pos' _) -> pos' `before` @hd.pos) @lhs.comments
hd.comments = snd @loc.commentsBeforeLine
loc.commentsAfter = span (\(MToken pos' _) -> pos' `before` @tl.pos) @hd.comments
Expand Down Expand Up @@ -462,6 +472,7 @@ sem MExprList
lhs.pos = emptyRg
lhs.isMultiline = False
lhs.isLast = True
lhs.startsWithNewline = False

sem FieldList
| Cons
Expand Down Expand Up @@ -866,13 +877,14 @@ sem AReturn
lhs.pretty =
renderMLComments @lhs.ppconf @lhs.indent (fst @loc.commentsBefore)
$+$ indent @lhs.ppconf @lhs.indent (zeroWidthText "return")
<-> @values.pretty
`@loc.sep` @values.pretty
<> @loc.semicolon
<-> renderSLComments @lhs.ppconf @lhs.indent (fst @loc.commentsAfter)
lhs.isMultiline =
@values.isMultiline
|| not (null $ fst @loc.commentsBefore)
|| not (null $ fst @loc.commentsAfter)
loc.sep = if @values.startsWithNewline then (<>) else (<->)
loc.semicolon = if semicolons @lhs.ppconf then zchr ';' else empty
loc.commentsBefore = span (\(MToken pos _) -> pos `before` @pos) @lhs.comments
values.comments = snd @loc.commentsBefore
Expand Down Expand Up @@ -1061,16 +1073,10 @@ sem Expr

sem Args
| ListArgs
lhs.pretty =
if @args.isMultiline then
zchr '(' $+$
@args.pretty $+$
indent @lhs.ppconf @lhs.indent (zchr ')')
else
parens @lhs.ppconf @loc.emptyParams @args.pretty
lhs.pretty = parens @lhs.ppconf @loc.emptyParams @args.pretty
lhs.isMultiline = @args.isMultiline
loc.emptyParams = toEmpty $ null @args.copy
args.indent = if @args.isMultiline then @lhs.indent + 1 else 0
args.indent = @lhs.indent
args.someElementsInListAreMultiline = False
| TableArg
lhs.pretty = if @arg.isMultiline then @loc.prettyMulti else @loc.prettySingle
Expand Down
67 changes: 67 additions & 0 deletions tests/golden/data/input/test-mexprlist-rendering.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function a()
return 1, 2, 3, 4
end

function a()
return
-- 1
1, -- 2
2, -- 3
-- 4
3,
-- 5
4 -- 6
-- 7
end

function a()
return
--[[8]]
1, --[[9]]
2, --[[10]]
--[[11]]
3,
--[[12]]
4 --[[13]]
--[[14]]
end

function a()
return
--[[15]]
1, --[[16]]
2, --[[17]]
--[[18]]
function() print(3) return 4 end,
--[[19]]
4 --[[20]]
--[[21]]
end

function a()
return 1, 2, function() print(3) return 4 end, 4
end

hook.Add("OnPrettyPrint", "test", function()
-- yes
a = 1 + 1
return function()
-- fancy
end
-- no
end)

for b, c, d in 2, 3, function()
-- A function in a for loop, is it possible? Syntactically, yes!
-- And this comment makes it _really_ awkward to pretty print!
end, 4, 5 do
print(a, b, c, d)
end

for b, c, d in 2, 3, {"foo", "bar"}, 4, 5 do
print(a, b, c, d)
end

for b, c, d in 2, 3, {a = "foo", b = "bar"}, 4, 5 do
print(a, b, c, d)
end
Loading

0 comments on commit b1bbdb7

Please sign in to comment.