使用CodeMirror的lint功能的异步警告源

时间:2022-01-19 23:34:38

The examples only show how to implement the JSON and Javascript lint addons with CodeMirror (syntax-highlighting Javascript-based editor), which are synchronous.

这些示例仅显示如何使用CodeMirror(语法突出显示基于Javascript的编辑器)实现JSON和Javascript lint插件,这些插件是同步的。

Unfortunately, there aren't Javascript-based parsers/lint'ers readily available for most languages. I'd like to implement my own linter for Python. Unfortunately, it seems as if the existing lint addon depends on the linter (which the lint plugin invokes) being synchronous.

不幸的是,大多数语言都没有基于Javascript的解析器/ lint'ers。我想为Python实现自己的linter。不幸的是,似乎现有的lint插件依赖于linter(lint插件调用的)是同步的。

angelozerr says that there is a "CodeMirror.remotingValidator" plugin located in remoting-lint.js, but Github show that this file was removed. marijnh says that it was removed because it was "not generally useful", but doesn't clarify any alternatives.

angelozerr说在remoting-lint.js中有一个“CodeMirror.remotingValidator”插件,但Github显示该文件已被删除。 marijnh说它被删除了,因为它“通常不是很有用”,但没有澄清任何替代方案。

Is there a strategy for, somehow, calling the linter asynchronously, so that I can do an Ajax call in order to parse the code remotely?

是否存在一种策略,以某种方式异步调用linter,以便我可以执行Ajax调用以远程解析代码?

1 个解决方案

#1


16  

Sure there is. A quick look through the contents of the last version of remoting-lint.js (https://github.com/marijnh/CodeMirror/commit/27f097ed75561e846bdb955f13f8dd2bcf0b589e) shows that it was little more than a jQuery AJAX request, which invokes a callback provided to the function as a parameter. This callback will only be passed if the "async" option is given to the lint plugin. Note that I have some options being passed-into CodeMirror that may not be relevant to you. The lint-related options are "gutters" and "lintWith".

当然有。快速浏览最新版本的remoting-lint.js(https://github.com/marijnh/CodeMirror/commit/27f097ed75561e846bdb955f13f8dd2bcf0b589e)的内容显示它只是一个jQuery AJAX请求,它调用提供的回调作为参数的功能。只有在为lint插件提供“async”选项时才会传递此回调。请注意,我有一些传入CodeMirror的选项可能与您无关。与棉绒相关的选项是“gutters”和“lintWith”。

Note that Python only presents one error at a time, but that the lint addon accepts a list of errors. Because of the former, the latter will only ever have one item, at most.

请注意,Python一次只显示一个错误,但lint插件接受错误列表。由于前者,后者最多只能有一个项目。

function python_validator(cm, updateLinting, options) {
    var text = cm.getValue() + "\n";

    if(text.trim() == "")
    {
        updateLinting(cm, []);
        return;
    }

    function result_cb(error)
    {
        var found = [];

        if(error != null) {
            var start_line = error.line_no;
            var start_char = error.column_no;
            var end_line = error.line_no;
            var end_char = error.column_no;
            var message = error.message;

            found.push({
                from: CodeMirror.Pos(start_line - 1, start_char),
                to: CodeMirror.Pos(end_line - 1, end_char),
                message: message
            });
        }

        updateLinting(cm, found);
    }

    check_python_syntax(text, result_cb)
};  

var text_obj = $discriminator_text.get()[0];
var editor = CodeMirror.fromTextArea(text_obj, {
    mode: { name: "python",
            version: 2,
            singleLineStringErrors: false },
    lineNumbers: true,
    indentUnit: 4,
    tabMode: "shift",
    matchBrackets: true,
    styleActiveLine: true,
    lineWrapping: false,
    gutters: ["CodeMirror-lint-markers"],
    lintWith: {
        "getAnnotations": python_validator,
        "async" : true,
    },
});

I've posted a very simple project to make this a little more straightforward: https://github.com/dsoprea/CodeMirrorRemoteValidator

我发布了一个非常简单的项目,使其更简单:https://github.com/dsoprea/CodeMirrorRemoteValidator

#1


16  

Sure there is. A quick look through the contents of the last version of remoting-lint.js (https://github.com/marijnh/CodeMirror/commit/27f097ed75561e846bdb955f13f8dd2bcf0b589e) shows that it was little more than a jQuery AJAX request, which invokes a callback provided to the function as a parameter. This callback will only be passed if the "async" option is given to the lint plugin. Note that I have some options being passed-into CodeMirror that may not be relevant to you. The lint-related options are "gutters" and "lintWith".

当然有。快速浏览最新版本的remoting-lint.js(https://github.com/marijnh/CodeMirror/commit/27f097ed75561e846bdb955f13f8dd2bcf0b589e)的内容显示它只是一个jQuery AJAX请求,它调用提供的回调作为参数的功能。只有在为lint插件提供“async”选项时才会传递此回调。请注意,我有一些传入CodeMirror的选项可能与您无关。与棉绒相关的选项是“gutters”和“lintWith”。

Note that Python only presents one error at a time, but that the lint addon accepts a list of errors. Because of the former, the latter will only ever have one item, at most.

请注意,Python一次只显示一个错误,但lint插件接受错误列表。由于前者,后者最多只能有一个项目。

function python_validator(cm, updateLinting, options) {
    var text = cm.getValue() + "\n";

    if(text.trim() == "")
    {
        updateLinting(cm, []);
        return;
    }

    function result_cb(error)
    {
        var found = [];

        if(error != null) {
            var start_line = error.line_no;
            var start_char = error.column_no;
            var end_line = error.line_no;
            var end_char = error.column_no;
            var message = error.message;

            found.push({
                from: CodeMirror.Pos(start_line - 1, start_char),
                to: CodeMirror.Pos(end_line - 1, end_char),
                message: message
            });
        }

        updateLinting(cm, found);
    }

    check_python_syntax(text, result_cb)
};  

var text_obj = $discriminator_text.get()[0];
var editor = CodeMirror.fromTextArea(text_obj, {
    mode: { name: "python",
            version: 2,
            singleLineStringErrors: false },
    lineNumbers: true,
    indentUnit: 4,
    tabMode: "shift",
    matchBrackets: true,
    styleActiveLine: true,
    lineWrapping: false,
    gutters: ["CodeMirror-lint-markers"],
    lintWith: {
        "getAnnotations": python_validator,
        "async" : true,
    },
});

I've posted a very simple project to make this a little more straightforward: https://github.com/dsoprea/CodeMirrorRemoteValidator

我发布了一个非常简单的项目,使其更简单:https://github.com/dsoprea/CodeMirrorRemoteValidator