From 97090fa8c9020ba269460fb9818363bd81e39afe 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 000000000..d4338ed3c --- /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 eed8fa9a3..ec07a1d05 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 73da52a6d..a92a74533 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