Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror - get linting result from outside of the editor

I'm using the CodeMirror library which is awesome. 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. 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? 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.

like image 498
patryk Avatar asked Jan 08 '17 16:01

patryk


People also ask

How do you use CodeMirror lint?

There's two ways to do this: Find a linter that you can run in the browser, run it on the content, and translate its output into the expected format. Write one from scratch, possibly using the syntax tree kept by the editor.

How do I use CodeMirror in textarea?

This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.

Does GitHub use CodeMirror?

About. CodeMirror is open source under a permissive license (MIT). It is being developed on GitHub. Contributions are welcome.

What is CodeMirror CSS?

CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code, and comes with a number of language modes and addons that implement more advanced editing functionality.


2 Answers

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.

Here's a basic options object that would trigger 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": {
    "getAnnotations": css_validator,
    "async": true 
}

Then, define your own validator function. This function can just call CodeMirror's bundled validator:

     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. If errors.length == 0, no errors were found.

Note: this sample assumes you are linting CSS, but the same would apply for other types.

like image 58
Dezza Avatar answered Sep 30 '22 08:09

Dezza


The updateLinting function in lint.js passes its annotations (and editor) to the onUpdateLinting option:

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

so all you have to do is have your handler as the lint option property:

lint: { onUpdateLinting: handleErrors }
like image 38
Clem Avatar answered Sep 30 '22 08:09

Clem