選択範囲のRubyスクリプトの構文のチェックをします。
実行時のエラーを補足するものではありません。 beginとendの対応が分からなくなったときなどに使う目的で作りました。
選択範囲のコードをrubyw.exeに喰わせてevalしていますが、コード自体は実行されないはずです。
rubyw.exeがパスのとおったフォルダにないと実行できません。
/***************************************************
* Ruby構文チェッカー
* for EmEditor Professional 4.00 or higher
* 参考: http://blade.nagaokaut.ac.jp/ruby/ruby-list/30401-30600.shtml#30453
***************************************************/
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("rubyw");
if (Document.selection.IsEmpty) {
Document.selection.SelectLine();
}
oExec.StdIn.WriteLine('def valid?(code) ');
oExec.StdIn.WriteLine(' begin ');
oExec.StdIn.WriteLine(' eval("BEGIN{return true}; #{code}") ');
oExec.StdIn.WriteLine(' rescue SyntaxError ');
oExec.StdIn.WriteLine(' print $! ');
oExec.StdIn.WriteLine(' return false ');
oExec.StdIn.WriteLine(' end ');
oExec.StdIn.WriteLine(' return false ');
oExec.StdIn.WriteLine('end ');
oExec.StdIn.WriteLine('if valid?(DATA.read) ');
oExec.StdIn.WriteLine('then exit(0) ');
oExec.StdIn.WriteLine('else exit(1) ');
oExec.StdIn.WriteLine('end ');
oExec.StdIn.WriteLine('__END__');
oExec.StdIn.WriteLine(Document.selection.Text);
oExec.StdIn.Close();
var allInput = "";
var tryCount = 0;
function ReadAllFromAny(oExec) {
if (!oExec.StdOut.AtEndOfStream)
return oExec.StdOut.ReadAll();
return -1;
}
while (true) {
var input = ReadAllFromAny(oExec);
if (-1 == input) {
if (tryCount++ > 10 && oExec.Status == 1)
break;
Sleep(10);
} else {
allInput += input;
tryCount = 0;
}
}
function AddLineNum(str) {
var ary = str.split("\n");
var ret = []
for (i = 0; i < ary.length; i++) {
ret.push( (i+1).toString() + "| " + ary[i] );
}
return ret.join("\n");
}
/*
while (oExec.Status != 1) {
Sleep(100);
}
*/
//code = AddLineNum(Document.selection.Text)
if (oExec.ExitCode == 0) {
//alert("ok\n\n" + code);
alert("ok");
} else {
//alert("SyntaxError\n\n" + code + "\n\n" + allInput);
alert("SyntaxError\n\n" + allInput);
}
このページを編集しながら思いついたのですが、
/***************************************************
* Ruby 文法チェッカ 2
* for EmEditor Professional 4.00 or higher
***************************************************/
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("rubyw -c");
if (Document.selection.IsEmpty) {
Document.selection.SelectLine();
}
oExec.StdIn.WriteLine(Document.selection.Text);
oExec.StdIn.Close();
while (oExec.Status != 1) {
Sleep(100);
}
if (oExec.ExitCode == 0) {
alert("ok");
} else {
alert("SyntaxError");
}
の方が速くて安心かもしれません。
文責: とらめ
