Chetan
Chetan

Reputation: 1537

Codemirror- linting - Is there a event to trigger linting explictly?

I am designing an application using CodeMirror that comes with toolbar. Because of performance reason, I am not executing the lint to either through async or asynch mode.

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?

Any pointer would be really helpful.

Upvotes: 7

Views: 8006

Answers (4)

intotecho
intotecho

Reputation: 5684

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/

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.

To do that, you could customise the lint add-on code. For example from the json-lint provided in the standard addons [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:

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.

    lintOnChange: false

  2. Then when you wish to lint, call mylinthandler(editor.getValue())

Upvotes: 2

Svend
Svend

Reputation: 8158

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().

If you define your own lint methods ala

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

that'll get invoked on performLint().

Upvotes: 7

Luc Guyot
Luc Guyot

Reputation: 161

To trigger lint in one line:

// 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:

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

Upvotes: 2

vjy
vjy

Reputation: 1204

Have you tried setting the lint value dynamically using the below code?

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

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

You can see a demo here JSFiddle link

Upvotes: 3

Related Questions