-------------------------------------------- -- indentation for java stuff -- -------------------------------------------- function trim (input) out = string.gsub (input, "[ \t]+", " "); out = string.gsub (out, "[ \t]+\n", "\n"); out = string.gsub (out, "\n[ \t]*", "\n"); return out end function newLineBeforeOpenBrace (input) pattern = "([^{]+){\n"; result = string.gsub (input, pattern, function (x) return (x.."\n{\n") end); return result end function setTabs (input) leng = string.len (input) buf = "" -- char c; tabs = -1; -- for i = 0,leng-1 do for i = 1,leng do c = string.sub (input, i, i) if (c == '{') then buf = buf..c; tabs = tabs+1; elseif (c == '}' ) then tabs = tabs - 1; buf = buf..c; elseif (c == '\n') then do buf = buf..c; -- if (i < leng-1) then if (i < leng) then do next = string.sub (input,i+1,i+1); if (next == '}' ) then tabs = tabs - 1 i = i + 1 for r=tabs,0,-1 do buf = buf..'\t'; end buf = buf..next; else for r=tabs,0,-1 do buf = buf..'\t'; end end end end end else buf = buf..c end end return (buf); end function newlineAfterOpeningBraces (input) --pattern = "(\t+){(.+)" --out = string.gsub (input, pattern, function (x, y) return (x.."{\n"..x.."\t"..y.."\n") end) pattern = "{" return string.gsub (input, pattern, "{\n") end function blankAfterComma (input) pattern = ("([,;])([a-zA-Z0-9])"); return string.gsub (input, pattern, function (x, y) return (x.." "..y) end) end function blankBeforeOpenBrace (input) pattern = "([a-zA-Z0-9])([([])"; return string.gsub (input, pattern, function (x, y) return (x.." "..y) end) end function blankAfterClosingBrace (input) pattern = "([])}])([a-zA-Z0-9])"; return string.gsub (input, pattern, function (x, y) return (x.." "..y) end) end function skipEmptyLines (input) pattern = "\n[ \t]*\n" sb = string.gsub (input, pattern, "\n") sb = string.gsub (sb, pattern, "\n") return string.gsub (sb, pattern, "\n") end function newLineBeforeMethod (input) -- pattern = "$\t}$\t"; -- replacement = "\n\t}\n\n\t"; pattern = "\n\t}\n\t"; replacement = "\n\t}\n\n\t"; return string.gsub (input, pattern, replacement) end function noSpaceAfterOpeningBrace (input) pattern = "([%(%{%[]) +"; return string.gsub (input, pattern, function (x) return x end) end function noSpaceBeforeClosingBrace (input) -- java: p = Pattern.compile (" +\\)"); pattern = " +([%}%]%)])" return string.gsub (input, pattern, function (x) return x end) end function noSpaceBeforeSemicolon (input) pattern = " +([;,])" return string.gsub (input, pattern, function (x) return (x) end) end function dbOut (fun, name, input) print ("--------------------------------------") print (name) out = fun (input); print (out.."\n") return out end function nodbOut (fun, name, input) out = fun (input); return out end function indentText (input) out = nodbOut (trim, "trim:", input); out = nodbOut (newLineBeforeOpenBrace, "nl before {:", out); out = nodbOut (newlineAfterOpeningBraces, "nl after {:", out); out = nodbOut (setTabs, "set tabs:", out); out = nodbOut (blankAfterComma, "blank after comma, :", out); out = nodbOut (blankBeforeOpenBrace, "blank before open brace:", out); out = nodbOut (blankAfterClosingBrace, "blank after closing brace->):", out); out = nodbOut (skipEmptyLines, "skip empty lines :", out); out = nodbOut (newLineBeforeMethod, "nl before method:", out); out = nodbOut (noSpaceAfterOpeningBrace, "no space after (opening brace:", out); out = nodbOut (noSpaceBeforeClosingBrace, "no space before closing) brace:", out); out = nodbOut (noSpaceBeforeSemicolon, "no space before semicolon:", out); -- todo: -- (a<1) (a < 1) etc. -- Lines starting with comment not to indent -- keep empty Lines return out; end function indent (text) editor:ReplaceSel (indentText (text)); end ----------------------------------------------- -- end of indentation for java -- -----------------------------------------------