Script indenter


  • I made a plugin that properly indents poorly/un-indented scripts.

    https://www.roblox.com/library/592898457/Script-indenter

    source:

    plugin:CreateToolbar("Indenter"):CreateButton("Indent","Indent","").Click:connect(function()
    	local timer=tick()
    	local selection=game.Selection:Get()[1]
    	if not selection or not selection:IsA("BaseScript")then return end
    	local list={}
    	local keywords={"do","repeat","function","if","end","until","else","elseif"}
    	local iterators={
    		function(s)
    			local a,b,c,d=s:match("^(.-)%[(=*)%[(.-)%]%2%](.*)")
    			if not a then parse(s)return end
    			iterate(a)
    			list[#list+1]="["..b.."["..c.."]"..b.."]"
    			iterate(d)
    		end;
    		function(s)
    			local _,i=s:find([[(['"])(\*)(%2%1)]])
    			if not i then
    				_,i=s:find([[(['"])(.-[^\])(\*)(%3%1)]])
    			end
    			local j=s:find("%-%-")
    			if i and i<(j or i+1)then
    				local a,b,c,d,e=s:match([[^(.-)(['"])(\*)(%3%2)(.*)]])
    				if a then
    					iterate(a)
    					list[#list+1]=b..c..d
    					iterate(e)
    				else
    					local a,b,c,d,e,f=s:match([[^(.-)(['"])(.-[^\])(\*)(%4%2)(.*)]])
    					iterate(a)
    					list[#list+1]=b..c..d..e
    					iterate(f)
    				end
    			elseif j then
    				local a,b,c=s:match("^(.-)(%-%-[^\n]*)(.*)")
    				iterate(a)
    				list[#list+1]=b
    				iterate(c)
    			else
    				parse(s)
    			end
    		end;
    		function(s)
    			local a,b,c
    			for i=1,#keywords do
    				a,b,c=s:match("^(.-)(%f[%w]"..keywords[i].."%f[%W])(.*)")
    				if a then break end
    			end
    			if not a then return parse(s)end
    			iterate(a)
    			list[#list+1]=b
    			iterate(c)
    		end;
    		function(s)
    			local a,b,c=s:match("^(.-)([(){}])(.*)")
    			if not a then return parse(s)end
    			iterate(a)
    			list[#list+1]=b
    			iterate(c)
    		end;
    		function(s)
    			local a,b=s:match("^(.-)\n(.*)")
    			if not a then return parse(s)end
    			iterate(a)
    			list[#list+1]="\n"
    			b=b:match("%s*(.*)")
    			iterate(b)
    		end;
    		function(s)list[#list+1]=s end
    	}
    	for i=1,#iterators do
    		local f=iterators[i]
    		setfenv(f,{iterate=f,parse=iterators[i+1],list=list})
    	end
    	iterators[1](selection.Source)
    	local levels={}
    	local new,level,sameline="",0
    	for i=1,#list do
    		local s=list[i]
    		if s=="\n"then
    			new,sameline=new.."\n"..("\t"):rep(level)
    		else
    			if s=="do"or s=="repeat"or s=="function"or s=="if"or s=="{"or s=="("then
    				if not sameline then
    					level,sameline=level+1,true
    				end
    				levels[#levels+1]=level
    			elseif s=="end"or s=="until"or s=="}"or s==")"then
    				table.remove(levels)
    				level,sameline=levels[#levels]or 0
    				if new:sub(-1)=="\t"then
    					new=new:sub(1,-2)
    				end
    			elseif s=="else"or s=="elseif"then
    				if new:sub(-1)=="\t"then
    					new=new:sub(1,-2)
    				end
    			end
    			new=new..s
    		end
    	end
    	selection.Source=new
    	print("Clock: "..(tick()-timer))
    end)
    

  • That is so helpful.

    Thank you so much for this!


  • If only I could force everyone on the ROBLOX website to use this.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.