Reputation: 469
I want to count the number and type of linting errors and display a summary elsewhere in the page. To do that I would like to know when the linter completes execution so that I don't have to poll for changes.
What is the event-based solution?
Thank you for your time! (I upvote all valid attempts ...that aren't obviously wrong of course)
Here is my error counter snippet (coffeescript):
for eachError in doc.getAllMarks()
numStaticErrors++ if eachError.className is "CodeMirror-lint-mark-error"
numStaticErrors++ if eachError.className is "CodeMirror-lint-marker-warning"
numStaticErrors++ if eachError.className is "CodeMirror-lint-marker-multiple"
Upvotes: 3
Views: 1370
Reputation: 469
Many thanks to Marijn for writing the excellent CodeMirror and supporting it so well. This answer seeks to provide a complete explanation/solution (incorporating Marijn's answer above).
Specifically, the following features/requirements:
In CoffeeScript (because it's clearer):
options =
mode: "javascript"
lineNumbers: true
gutters: ["CodeMirror-lint-markers"]
lintWith:
getAnnotations: CodeMirror.javascriptValidator
onUpdateLinting: (lintErrorsWarnings) ->
# Do something with the linter's errors and warnings
CodeMirror $("#editor"), options
The linter's completion callback takes a single parameter with is an array of objects as shown in this example:
[
{
from: Pos
ch: 0
line: 0
message: "Expected an assignment or function call and instead saw an expression."
severity: "error"
to: Pos
ch: 5
line: 0
},
{
from: Pos
ch: 5
line: 0
message: "Missing semicolon."
severity: "error"
to: Pos
ch: 6
line: 0
}
]
Upvotes: 1
Reputation: 8929
Currently, the only way to be notified of a linting event is to set the "lintWith"
option to an object containing an onUpdateLinting
property, which is a callback that will be called with the list of linting errors/warning as its first argument whenever the markers are updated.
Upvotes: 4