Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize emit to wrap overlength lines #74

Closed
wants to merge 27 commits into from

Conversation

rimuy
Copy link
Contributor

@rimuy rimuy commented Jun 3, 2022

Currently the emit doesn't wrap the lines for things like array members, call expressions or function parameter names if necessary. This makes debugging very difficulty when these are too long.

This PR implements all of these with a few small optimizations.

Current output

playground link

New output

Expand

-- Compiled with roblox-ts v1.3.3
-- * Class
local MyLongClassName
do
	MyLongClassName = setmetatable({}, {
		__tostring = function()
			return "MyLongClassName"
		end,
	})
	MyLongClassName.__index = MyLongClassName
	function MyLongClassName.new(...)
		local self = setmetatable({}, MyLongClassName)
		return self:constructor(...) or self
	end
	function MyLongClassName:constructor(num, str, bool, obj)
	end
	function MyLongClassName:Method(...)
		local values = { ... }
		return self
	end
	function MyLongClassName:Method2(
		aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
		bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
                ccccccccccccccccccccccccccccccc,
	)
		print("hi")
	end
end
local myClass = MyLongClassName.new(MyArrayValues[1], "MyLongString", true)
-- * Object with a long property
local myObject = {
	aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = {
		b = "foo",
	},
       -- gets past 100
	b = {
		"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
	},
	c = { "a", "b", "c" },
	d = "foo",
}
-- * Mixed table
local mixedTable = {
	a = "foo",
       -- does not get past 100
	[0] = { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" },
}
-- * Function declaration with long parameters
local function test(
	aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
	bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
        ccccccccccccccccccccccccccccccc
)
	local myConfig = {
		MyLongClassName.new(
			MyArrayValues[1],
			"MyLongString1",
			true,
			MyLongClassName.new(
				MyArrayValues[1],
				"MyLongString1",
				true,
				MyLongClassName.new(
					MyArrayValues[1],
					"MyLongString1",
					true,
					MyLongClassName.new(MyArrayValues[1], "MyLongString1", true)
				)
			)
		),
		MyLongClassName.new(MyArrayValues[2], "MyLongString2", false),
		MyLongClassName.new(
			MyArrayValues[2],
			"MyLongString2",
			false,
			MyLongClassName.new(
				MyArrayValues[2],
				"MyLongString2",
				false,
				MyLongClassName.new(MyArrayValues[2], "MyLongString2", false)
			)
		),
		MyLongClassName.new(MyArrayValues[3], "MyLongString3", true),
		MyLongClassName.new(MyArrayValues[4], "MyLongString4", true),
		MyLongClassName.new(MyArrayValues[5], "MyLongString5", false),
		myClass:Method(
			923409234902903490234,
			923409234902903490234,
			923409234902903490234,
			923409234902903490234,
			923409234902903490234,
			923409234902903490234
		):Method(
			4985689458964, 
			4895689458964895896, 
			45896849
		):Method(
			4985689458964,
			4895689458964895896,
			45896849,
			4985689458964,
			4895689458964895896,
			45896849
		)
	}
end
-- * Expressions
do
	-- * Array with a function expression
	local arrWithEmptyFnExpr = { function() end }
	local arrWithFnExpr = {
		function()
			return print("hey")
		end
	}
	-- * Call with long values
	local callExpr = print(
		"aaaaaaaaaaaaaaaaaaaaa",
		"bbbbbbbbbbbbbbbbbbbbb",
		"ccccccccccccccccccccc"
	)
	-- * Method call with long values
	local methodCallExpr = myClass:Method(
		923409234902903490234,
		923409234902903490234,
		923409234902903490234,
		923409234902903490234,
		923409234902903490234,
		923409234902903490234
	)
	-- * Function
	local function functionExpr(callback)
		-- * Property access
		myObject
			.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
			.b = "bar"
		myObject
			.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
			.b = "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz"
		myObject.d = "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
	end
	functionExpr(function(a, b, c, d, e) end)
	functionExpr(
		function(
			aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
			bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
			ccccccccccccccccccccccccccccc
		) end
	)
	functionExpr(
		function(a, b, c, d, e)
			print("test")
			print("test")
			print("test")
		end
	)
	functionExpr(
		function(
			aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
			bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
			ccccccccccccccccccccccccccccc
		)
			print("test")
			print("test")
			print("test")
		end
	)
end
return nil

Closes #13

@rimuy rimuy marked this pull request as draft June 4, 2022 17:33
@rimuy rimuy marked this pull request as ready for review June 5, 2022 18:28
@rimuy
Copy link
Contributor Author

rimuy commented Feb 7, 2024

Closing this because I'm not really satisfied with this approach I used back then, and there was a discussion about having the emit memorize newlines and/or possibly bundle StyLua for the rendering step.

@rimuy rimuy closed this Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Overlength line wrapping
1 participant