Search code examples
wordpresscodemirror

Cannot get error count in CodeMirror editor


Is there any way to access the number of errors in code mirror editor. Is it have something to do with lint ?

I created an instance using

var editor = wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);

I can take the value using

editor.codemirror.getValue();

But this returns the value in editor including the code with error. Is there any function or option to get count of errors ?


Solution

  • You can grab your editors wrapper and count how many errors there are by .length.

    Find the errors by their class name .CodeMirror-lint-marker-error

    var editor = wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);
    var errors = codeEditor.find('.CodeMirror-lint-marker-error');
    console.log(errors.length);
    

    Or, use editor.state.lint.marked to receive an array of the errors.

    var editor = wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);   
    var error_array = editor.state.lint.marked;
    console.log(error_array.length);