简体   繁体   English

CodeMirror - 从编辑器外部获取 linting 结果

[英]CodeMirror - get linting result from outside of the editor

I'm using the CodeMirror library which is awesome.我正在使用很棒的 CodeMirror 库。 The code editor that I'm istantiating is a part of a form and therefore I want to do a basic check with linting to see whether the user's input seems valid.我正在实例化的代码编辑器是表单的一部分,因此我想使用 linting 进行基本检查,以查看用户的输入是否有效。 Unless the code is fine, I don't want to process the form.除非代码没问题,否则我不想处理表单。

So the question is: is there a method on the CodeMirror editor instance that would allow me to retrieve the result of linting?所以问题是:CodeMirror 编辑器实例上是否有允许我检索 linting 结果的方法? I'm looking through the docs and Google but failed to find anything helpful.我正在浏览文档和谷歌,但没有找到任何有用的东西。 There's this performLint method that is added to the editor, however it does not return the results of linting.这个performLint方法被添加到编辑器中,但是它不返回performLint的结果。

There isn't a specific method to get the linting results, but there is a hook provided when you define a getAnnotations property in the lint options object.没有获取 linting 结果的特定方法,但是当您在 lint 选项对象中定义getAnnotations属性时会提供一个钩子。

Here's a basic options object that would trigger linting:这是一个会触发 linting 的基本选项对象:

var codeMirrorOptions = {
    "lineNumbers": true,
    "indentWithTabs": true,
    "mode": "css",
    "gutters": ['CodeMirror-lint-markers'],
    "lint": true
}

You can specify an object (instead of a boolean) as the lint property:您可以指定一个对象(而不是布尔值)作为lint属性:

"lint": {
    "getAnnotations": css_validator,
    "async": true 
}

Then, define your own validator function.然后,定义您自己的验证器函数。 This function can just call CodeMirror's bundled validator:这个函数可以直接调用 CodeMirror 的捆绑验证器:

     function css_validator(cm, updateLinting, options) {
            // call the built in css linter from addon/lint/css-lint.js
            var errors = CodeMirror.lint.css(cm);

            updateLinting(errors);

        }

At this point you've replicated the behavior of lint:true -- but now the errors variable contains an array of lint errors.此时您已经复制了 lint:true 的行为——但是现在errors变量包含了一个 lint 错误数组。 If errors.length == 0 , no errors were found.如果errors.length == 0 ,则未发现错误。

Note : this sample assumes you are linting CSS, but the same would apply for other types.注意:此示例假设您正在整理 CSS,但同样适用于其他类型。

The updateLinting function in lint.js passes its annotations (and editor) to the onUpdateLinting option: lint.js 中的 updateLinting 函数将其注释(和编辑器)传递给 onUpdateLinting 选项:

if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);

so all you have to do is have your handler as the lint option property:所以你所要做的就是让你的处理程序作为 lint 选项属性:

lint: { onUpdateLinting: handleErrors }

This is a hint for Dezzas Answer:这是对 Dezzas Answer 的提示:

Write,写,

var errors = CodeMirror.lint.css(cm, options);

Otherwise, it will fail.否则,它将失败。

@Clem's answer led me in the right direction, though I did run into a few issues. @Clem 的回答使我朝着正确的方向前进,尽管我确实遇到了一些问题。 The first was seeing Bad option: onUpdateLinting repeatedly in the console (See this reported Codemirror issue ).第一个是在控制台中反复看到Bad option: onUpdateLinting (请参阅此报告的Codemirror 问题)。 The second was seeing the annotations array containing null entries sometimes.第二个是有时看到包含空条目的注释数组。 Here is my linting configuration passed into Codemirror that solves these issues.这是我传递给 Codemirror 的 linting 配置,用于解决这些问题。 Note that I'm using react-codemirror2 , but the options get passed into Codemirror in the same format.请注意,我使用的是react-codemirror2 ,但选项以相同的格式传递给 Codemirror。 My component has an optional onLintingComplete callback that can be provided by the consuming component and you'll see that callback referenced below where it is passed the array of lint annotations:我的组件有一个可选的onLintingComplete回调,它可以由消费组件提供,您将看到下面引用的回调,其中传递了 lint 注释数组:

  lint: onLintingComplete
    ? {
        onUpdateLinting: (_annotationsNotSorted, annotations) =>
          onLintingComplete(
            // sometimes codemirror includes null annotations in the array, so we want to filter these out
            annotations.filter(annotation => annotation != null)
          ),
        // This empty lint options object is needed here, see: https://github.com/codemirror/CodeMirror/issues/4198
        options: {}, 
      }
    : true,

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

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