Code:
||文字列のスプリット
{define-proc public {split-string
str:String, split-str:String
}:StringArray
def str-ary = {StringArray}
def found-index = {str.find-string split-str}
{if found-index > -1 then
{str-ary.append {str.substr 0, found-index}}
{str-ary.append {str.substr found-index, str.size - found-index}}
else
{str-ary.append str}
}
{return str-ary}
}
||ソースのインデント
{define-proc public {indent-source
source:String,
indent-string:String,
indent-length:int = 0
}:String
||インデント対象の文字で分割
def lines = {source.split split-chars = '\n'}
def split-str = {{Array-of StringArray}}
{for s in lines do
{split-str.append {split-string s, indent-string}}
}
||インデント位置の測定
||各配列の1番目の要素の最大長を取得する
let max-indent-length:int = indent-length
{for str-ary in split-str do
{if str-ary.size == 2 then
set max-indent-length = {max max-indent-length, str-ary[0].size}
}
}
||インデント位置まで空白を埋める
{for str-ary in split-str do
{if str-ary.size == 2 then
let pudding:String = ""
{for i:int = 0 to max-indent-length - str-ary[0].size do
set pudding = pudding & " "
}
set str-ary[0] = str-ary[0] & pudding
}
}
||ソース形式に戻す
def ret-str:StringBuf = {StringBuf}
{for str-ary in split-str do
{for s in str-ary do
{ret-str.concat s}
}
{ret-str.append '\n'}
}
{return {ret-str.to-String}}
}
{def fm = "MS ゴシック"}
{def ta-pre = {TextArea width = 7in, height = 2in, font-family = fm}}
{def ta-indent = {TextArea width = 7in, height = 2in, font-family = fm}}
{def tf-str1 = {TextField }}
{def tf-str2 = {TextField }}
{def tf-str3 = {TextField }}
{def cb-indent =
{CommandButton
label ="インデント",
{on Action do
let indented-source:String = ""
set indented-source = {indent-source ta-pre.value, tf-str1.value}
set indented-source = {indent-source indented-source, tf-str2.value}
set indented-source = {indent-source indented-source, tf-str3.value}
set ta-indent.value = indented-source
}
}
}
{VBox
ta-pre,
ta-indent,
cb-indent,
{HBox "インデント対象文字1", tf-str1},
{HBox "インデント対象文字2", tf-str2},
{HBox "インデント対象文字3", tf-str3}
}
{do
def test-str = "
let abc:String = \"100\" \|\| テスト用文字列
let qwertyu:#RecordGrid = null \|\| テスト用グリッド
let i:int = 1 \|\| テスト用整数"
set ta-pre.value = test-str
set tf-str1.value = ":"
set tf-str2.value = "="
set tf-str3.value = "\|\|"
}