简体   繁体   English

Codemirror-linting - 是否有事件明确触发linting?

[英]Codemirror- linting - Is there a event to trigger linting explictly?

I am designing an application using CodeMirror that comes with toolbar. 我正在使用工具栏附带的CodeMirror设计应用程序。 Because of performance reason, I am not executing the lint to either through async or asynch mode. 由于性能原因,我没有通过异步或异步模式执行lint。

I have provided an icon in the toolbar, on clicking this I am doing the parsing and constructing the errors. 我在工具栏中提供了一个图标,点击这个我正在进行解析并构建错误。 But I am stuck with how I can update lint error in editor? 但我仍然坚持如何在编辑器中更新lint错误?

Any pointer would be really helpful. 任何指针都非常有用。

The lint addon adds an "extension" method to all Editor instances called performLint , combine with editor options of lint: { lintOnChange: false } , and then you can invoke the linter by calling mirror.performLint() . lint插件为名为performLint所有Editor实例添加了一个“扩展”方法,并与lint: { lintOnChange: false }编辑器选项结合使用lint: { lintOnChange: false } ,然后您可以通过调用mirror.performLint()来调用mirror.performLint()

If you define your own lint methods ala 如果你定义自己的lint方法ala

CodeMirror.registerHelper('lint', 'mode', (text) => { /* your cool stuff here */ })

that'll get invoked on performLint() . 这将在performLint()上调用。

Have you tried setting the lint value dynamically using the below code? 您是否尝试使用以下代码动态设置lint值?

//enable
editor.setOption("lint",true);

//disable
editor.setOption("lint",false);

You can see a demo here JSFiddle link 你可以在这里看到JSFiddle链接的演示

It sounds like a good feature that doesn't appear to exist. 这听起来像是一个看似不存在的好功能。

It is straighforward to create a button to launch a validation from an external button. 创建一个按钮以从外部按钮启动验证是很简单的。

For example [no credit] https://jsfiddle.net/q43drhyk/ 例如[没有信用] https://jsfiddle.net/q43drhyk/

function checkFormat(editor) {
    var success = JSHINT(editor.getValue());
    var output = '';
    if (!success) {
        output = "Check format error:\n\n";
        for (var i in JSHINT.errors) {
            var err = JSHINT.errors[i];
            if (null != err) {
                output += err.line + '[' + err.character + ']: ' + err.reason + '\n';
            } else {
                output += "Check format unknown error:\n";
            }
        }
        alert(output);
    }
    return success;
}

However this does not render the validation messages in the CodeMirror editor to display in the linenumber gutter, which is what the OP was looking for. 但是,这不会使CodeMirror编辑器中的验证消息显示在亚麻布装订线中,而这正是OP所寻求的。

To do that, you could customise the lint add-on code. 为此,您可以自定义lint附加代码。 For example from the json-lint provided in the standard addons [ https://github.com/codemirror/CodeMirror/blob/master/addon/lint/json-lint.js] 例如,来自标准插件中提供的json-lint [ https://github.com/codemirror/CodeMirror/blob/master/addon/lint/json-lint.js]

1.Extract the body of registerHelper() into a new function that reproduces the existing function: 1.将registerHelper()的主体提取到一个重现现有函数的新函数中:

CodeMirror.registerHelper("lint", "json", function(text) {
   return mylinthandler(text)
}

mylinthandler(text) {
 var found = [];
  jsonlint.parseError = function(str, hash) {
    var loc = hash.loc;
    found.push({from: CodeMirror.Pos(loc.first_line - 1, loc.first_column),
                to: CodeMirror.Pos(loc.last_line - 1, loc.last_column),
                message: str});
  };
  try { jsonlint.parse(text); }
  catch(e) {}
  return found;
}
  1. Turn off the auto-lint CodeMirror options with. 使用关闭自动lint CodeMirror选项。

    lintOnChange: false lintOnChange:false

  2. Then when you wish to lint, call mylinthandler(editor.getValue()) 然后当你想要lint时,调用mylinthandler(editor.getValue())

To trigger lint in one line: 要在一行中触发lint:

// trigger registered lint handler
editor.setOption("lint", {});

// trigger custom lint handler defined on the fly
editor.setOption("lint", { 
  getAnnotations: function() { /* some smart code */ return found; }
});

If you wonder why it should ever work, have look into addon/lint/lint.js , specially the following lines: 如果你想知道为什么它应该工作,请查看addon / lint / lint.js ,特别是以下几行:

  CodeMirror.defineOption("lint", false, function(cm, val, old) {
  ...
    if (val) {
      ...
      startLinting(cm);
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM