[EMAIL PROTECTED] wrote:
I am wondering if SciTE can be used to clean up the formatting of LUA 4 scripts?
Specifically, I would like to batch-format some messy scripts so that braces
are all appear on their own lines with proper indentation.
Here's an example of how I'd like a LUA 4 table formatted:
tableName =
{
newTable1 =
{
"text",
[snip]
Try this:
function ReformatTables()
local indent = " "
local last = editor.Length
local txt = editor:textrange(0, last)
local i, lvl = 1, 0
local selbeg, selend, fix
local function tab() return string.rep(indent, lvl) end
local function snip(t) fix = fix..t end
editor:BeginUndoAction()
while true do
if lvl == 0 then
local x1, x2, syn = string.find(txt,
"([_%a][_%w]*)%s*=%s*{", i)
if x1 == nil then break end
selbeg = x1 - 1; fix = ""
i = x2 + 1; snip(tab()..syn.." =\n"..tab().."{\n")
lvl = lvl + 1;
else
local x1, x2, syn = string.find(txt,
"([_%a][_%w]*)%s*=%s*{", i)
local y1, y2, itm = string.find(txt, [[("[^"]*",?)]], i)
local z1, z2, del = string.find(txt, "(},?%s[\r\n]+)", i)
local tok = math.min(x1 or last, y1 or last, z1 or last)
if tok == last then
print("table unbalanced"); return
elseif tok == x1 then
i = x2 + 1; snip(tab()..syn.." =\n"..tab().."{\n")
lvl = lvl + 1
elseif tok == y1 then
i = y2 + 1; snip(tab()..itm.."\n")
else-- tok == z1 then
lvl = lvl - 1
i = z2 + 1; snip(tab()..del)
if lvl == 0 then
selend = i - 1
editor.TargetStart = selbeg
editor.TargetEnd = selend
editor:ReplaceTarget(fix)
i = i + (string.len(fix) - (selend - selbeg))
last = editor.Length
txt = editor:textrange(0, last)
end
end--if tok
end--if lvl
end--while
editor:EndUndoAction()
end
Note that it will fail if comments appears in the tables or if
different syntax (e.g. [[string]]) is used. You'll probably need
to tweak the regexes. HTH.
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia
_______________________________________________
Scite-interest mailing list
[email protected]
http://mailman.lyra.org/mailman/listinfo/scite-interest