Ace Editor setAnnotations方法一次只显示一个错误

时间:2015-01-25 00:47:59

标签: javascript text-editor ace-editor

我目前正在运行一个外部插件,它检测ace编辑器选项卡中的代码味道,我想使用setAnnotations迭代一个行号和错误消息数组来突出显示所有错误。但是,只有我的数组中的最后一个对象被突出显示,一旦设置了第二个注释,就会擦除前一个注释。

function highlightError(errorMsg, line){
editor.getSession().setAnnotations([{
    row: line-1,
    column: 10,
    text: errorMsg,
    type: "error" // also warning and information
}]);

}

这是我用来做的方法。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

setAnnotations采用注释数组,它取代了之前调用的注释,因此您需要将所有注释收集到数组中,然后调用setAnnotations,一次

答案 1 :(得分:0)

您可以定义一个包含所有注释的JSON数组,然后将其设置为编辑器。

//This array holds all the errors
var jsonErrorArray = [];

var errorLinesArray = [1,5, 7];
var errorMessagesArray = ["Error on line 1", "Error on line 5", "Error on line 7"];

for (var i = 0; i < errorLinesArray.length; i++) {
            jsonErrorArray[fileNamesArray[i]].push({
                row: errorLinesArray[i]-1,
                column: 10,
                type:"error",
                text: errorMessagesArray[i]
            });
        }

//Set the annotations to the editor
editor.getSession().setAnnotations(jsonErrorArray[currentFileName]);