【问题标题】:CodeMirror - get linting result from outside of the editorCodeMirror - 从编辑器外部获取 linting 结果
【发布时间】:2017-05-22 22:26:50
【问题描述】:

我正在使用很棒的 CodeMirror 库。我正在实例化的代码编辑器是表单的一部分,因此我想对 linting 进行基本检查,以查看用户的输入是否有效。除非代码没问题,否则我不想处理表单。

所以问题是:CodeMirror 编辑器实例上是否有一个方法可以让我检索 linting 的结果?我正在浏览文档和谷歌,但没有找到任何有用的东西。这个performLint 方法被添加到编辑器中,但是它不返回linting 的结果。

【问题讨论】:

  • 什么是 lint 结果?
  • 我的意思是从为编辑器定义的 lint 工具返回的错误/警告列表。我需要在编辑器之外,这样我才能知道编辑器的内容是否OK
  • 您可以使用自定义工具验证编辑器的字符串value,或者查找codemirror 用于指示这些警告的标记。

标签: javascript forms validation codemirror lint


【解决方案1】:

没有获取 lint 结果的特定方法,但是当您在 lint 选项对象中定义 getAnnotations 属性时提供了一个挂钩。

这是一个触发 linting 的基本选项对象:

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

您可以将对象(而不是布尔值)指定为 lint 属性:

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

然后,定义您自己的验证器函数。这个函数可以调用 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);

        }

此时您已经复制了 lint:true 的行为——但现在 errors 变量包含一个 lint 错误数组。如果errors.length == 0,则没有发现错误。

注意:此示例假定您正在 linting CSS,但同样适用于其他类型。

【讨论】:

  • 谢谢!额外:使用自定义 javascript linting 时,我必须使用以下代码 errors = CodeMirror.lint.javascript(cm, options); ,否则会抛出错误 "undefined indent property"
  • 您好,我正在尝试将XML Lint 添加到我的CodeMirror 应用程序中。当 XML 在语法上不正确时,我想在该行旁边显示一个 X Button 并显示错误消息。我尝试使用faster-xml-parser,但它没有按我的意愿工作。我已经发布了代码,如果你有机会,你能帮帮我吗? codesandbox.io/s/great-browser-gfzdf?file=/pages/index.vue
【解决方案2】:

lint.js 中的 updateLinting 函数将其注解(和编辑器)传递给 onUpdateLinting 选项:

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

所以您所要做的就是将您的处理程序作为 lint 选项属性:

lint: { onUpdateLinting: handleErrors }

【讨论】:

  • 这是正确的答案,它避免了“间接” linting 函数。测试和批准。但是,由于没有记录 onUpdateLinting() 选项,它可能会在 CodeMirror 的未来版本中消失。
  • 也同意这应该是公认的答案。这对我很有用,尽管我建议将您的解决方案更新为 lint: { onUpdateLinting: handleErrors, options: {} } 需要空选项对象以防止大量Bad option: onUpdateLinting 形式的控制台警告以及传递给您的回调函数的相应虚假 lint 注释。见:github.com/codemirror/CodeMirror/issues/4198
【解决方案3】:

这是给 Dezzas 答案的提示:

写,

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

否则会失败。

【讨论】:

  • 如果在带有 jshint.js 的 javascript 模式下使用,则很有用且需要。
【解决方案4】:

@Clem 的回答使我朝着正确的方向前进,尽管我确实遇到了一些问题。第一个是在控制台中反复看到Bad option: onUpdateLinting(参见报告的Codemirror issue)。第二个是有时会看到包含空条目的注释数组。这是我传递给 Codemirror 的 linting 配置,它解决了这些问题。请注意,我使用的是react-codemirror2,但选项以相同的格式传递到 Codemirror。我的组件有一个可选的 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,

【讨论】:

  • 感谢您的回复。为选项分配一个空对象帮助我处理控制台警告(错误)Cannot display JSHint error (invalid line 0) {id: "(error)", raw: "Bad {a}option: '{b}'.", code: "E001", evidence: "", line: 0, …}
猜你喜欢
  • 2023-04-02
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多