From 942cdc8526a3347f157c35ba0e1a7f22342c14da Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 1 Jun 2024 20:53:28 -0300 Subject: [PATCH] improved line break heuristics Closes #740. --- spec/code_gen/linebreak_spec.lua | 31 +++++++++++++++++++++++++++++++ tl.lua | 6 ++++++ tl.tl | 6 ++++++ 3 files changed, 43 insertions(+) create mode 100644 spec/code_gen/linebreak_spec.lua diff --git a/spec/code_gen/linebreak_spec.lua b/spec/code_gen/linebreak_spec.lua new file mode 100644 index 00000000..d4338ed3 --- /dev/null +++ b/spec/code_gen/linebreak_spec.lua @@ -0,0 +1,31 @@ +local util = require("spec.util") + +describe("improved line break heuristics", function() + it("a line starting with ( is not a function call for the previous line", util.gen([[ + local record Obj + end + + function Obj:meth1() + print("hehe") + end + + local t = setmetatable({}, { __index = Obj }) + do + (t as Obj):meth1() + (t as Obj):meth1() + end + ]], [[ + local Obj = {} + + + function Obj:meth1() + print("hehe") + end + + local t = setmetatable({}, { __index = Obj }) + do + (t):meth1(); + (t):meth1() + end + ]])) +end) diff --git a/tl.lua b/tl.lua index eed8fa9a..ec07a1d0 100644 --- a/tl.lua +++ b/tl.lua @@ -2973,6 +2973,12 @@ do e1 = { f = ps.filename, y = tkop.y, x = tkop.x, kind = "op", op = op, e1 = e1, e2 = key } elseif tkop.tk == "(" then + local prev_tk = ps.tokens[i - 1] + if tkop.y > prev_tk.y then + table.insert(ps.tokens, i, { y = prev_tk.y, x = prev_tk.x + #prev_tk.tk, tk = ";", kind = ";" }) + break + end + local op = new_operator(tkop, 2, "@funcall") local prev_i = i diff --git a/tl.tl b/tl.tl index 73da52a6..a92a7453 100644 --- a/tl.tl +++ b/tl.tl @@ -2973,6 +2973,12 @@ do e1 = { f = ps.filename, y = tkop.y, x = tkop.x, kind = "op", op = op, e1 = e1, e2 = key } elseif tkop.tk == "(" then + local prev_tk = ps.tokens[i - 1] + if tkop.y > prev_tk.y then + table.insert(ps.tokens, i, { y = prev_tk.y, x = prev_tk.x + #prev_tk.tk, tk = ";", kind = ";" }) + break + end + local op: Operator = new_operator(tkop, 2, "@funcall") local prev_i = i