Skip to content

Instantly share code, notes, and snippets.

@chriscdn
Last active September 2, 2018 00:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chriscdn/5455381 to your computer and use it in GitHub Desktop.
Save chriscdn/5455381 to your computer and use it in GitHub Desktop.
beautify_oscript.lxe - A code beautifier for OpenText Content Server OScript
/**
* beautify_oscript.lxe - A code beautifier for OpenText Content Server OScript
*
* This script will beautify your OScript. Copy to a file within opentext/scripts/
* (e.g., opentext/scripts/beautify_oscript.lxe), restart Builder, place the focus on a
* script window, and run from the Tools menu.
*
* Some coding conventions assumed. Use at your own risk.
*
* Christopher Meyer (chris@schwiiz.org)
* http://schwiiz.org/
*
* v1.0.5
*/
Object context = WindowInFocusContext()
if IsScriptEditor(context) && context.IsWriteable()
context.tagText.pLabel = beautify(context.tagText.pLabel)
end
function Boolean IsScriptEditor(Object context)
return ( OS.Parent(context) == $Builder.ScriptEditor )
end
function Dynamic WindowInFocusContext()
Dynamic VisableWindow = Vis.gProgram().pFrontWindow
return VisableWindow.pContext
end
function String beautify(String input)
Object indentStack = $Kernel.Stack.New()
List newLines
String line, newLine
input = Str.Compress( input )
input = normalizeEOLs(input)
List lines = Str.Elements(input , System.Eol() )
Boolean previousLineTerminatesWithBackslash = false
for line in lines
line = Str.Trim(line)
if isEndIndent(line)
indentStack.Pop()
end
// Add a space before lines that begin with *
if startsWith(line, '*')
line = Str.Format(' %1', line)
end
if previousLineTerminatesWithBackslash
line = Str.Format('%1%2', Str.Tab(), line)
end
if endsWith(line, ":")
newLine = Str.Format("%1%2", set(indentStack.size()-1, Str.Tab()), line )
else
newLine = Str.Format("%1%2", set(indentStack.size(), Str.Tab()), line )
end
if isStartIndent(line)
indentStack.Push(line)
end
newLines = {@newLines, newLine}
previousLineTerminatesWithBackslash = endsWith(newLine, '\')
end
indentStack.Delete()
return Str.Trim( join( newLines, System.EOL() ) )
end
function String join(List items, String separator="")
integer sepLength = length(separator) + 1
return Str.Catenate(items, separator)[:-sepLength]
end
function String set(Dynamic length, String value)
if length >= 0
return Str.Set( length, value )
else
return ""
end
end
function String normalizeEOLs(String s)
String txt = Str.Trim(s) + System.Eol()
txt = Str.ReplaceAll(txt, Str.CRLF, System.Eol() )
txt = Str.ReplaceAll(txt, Str.CR, System.Eol() )
txt = Str.ReplaceAll(txt, Str.LF, System.Eol() )
return txt
end
function Boolean startsWith(String a, String b)
return Str.Locate(a, b) == 1
end
function Boolean endsWith(String a, String b)
return ( Str.Locate(a, b) + length(b) - 1 == length(a) )
end
function Boolean isStartIndent(String line)
String indent
for indent in {'function ','if ','for ','while ','switch ','case','default','else','elseif'}
if startsWith(line, indent)
return true
end
end
return false
end
function Boolean isEndIndent(String line)
String indent
for indent in {'end','else','elseif'}
if startsWith(line, indent)
return true
end
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment