ace高亮显示错误行
使用ace.js做代码编辑器,当代码运行报错时,如何显示错误行。
通过session的setAnnotations方法来实现。
// onError("SyntaxError: bad input on line 1")
function onError(str) {
var arr = str.split("\n");
var result = [];
for(var i = 0; i < arr.length; i++) {
var s = arr[i];
var index = s.lastIndexOf("on line ");
if (index == -1) {
result.push({
row: 0,
msg: s
});
} else {
result.push({
row: (parseFloat(s.substring(index + "on line ".length)) || 1) - 1,
msg: s.substr(0, index)
});
}
}
var annotations = [];
for (var i = 0; i < result.length; i++) {
var o = result[i];
annotations.push({
row: o.row,
column: o.col || 0,
text: o.msg || "",
type: o.type || "error" // also warning and information
});
}
editor.getSession().setAnnotations(annotations);
}当用户输入代码或者点击运行的时候,需要清除掉错误信息,可以通过下面方法来实现。
editor.getSession().setAnnotations();