|
59 | 59 | CodeMirror.on(node, "mouseout", hide); |
60 | 60 | } |
61 | 61 |
|
62 | | - function LintState(cm, options, hasGutter) { |
| 62 | + function LintState(cm, conf, hasGutter) { |
63 | 63 | this.marked = []; |
64 | | - this.options = options; |
| 64 | + if (conf instanceof Function) conf = {getAnnotations: conf}; |
| 65 | + if (!conf || conf === true) conf = {}; |
| 66 | + this.options = {}; |
| 67 | + this.linterOptions = conf.options || {}; |
| 68 | + for (var prop in defaults) this.options[prop] = defaults[prop]; |
| 69 | + for (var prop in conf) { |
| 70 | + if (defaults.hasOwnProperty(prop)) { |
| 71 | + if (conf[prop] != null) this.options[prop] = conf[prop]; |
| 72 | + } else if (!conf.options) { |
| 73 | + this.linterOptions[prop] = conf[prop]; |
| 74 | + } |
| 75 | + } |
65 | 76 | this.timeout = null; |
66 | 77 | this.hasGutter = hasGutter; |
67 | 78 | this.onMouseOver = function(e) { onMouseOver(cm, e); }; |
68 | 79 | this.waitingFor = 0 |
69 | 80 | } |
70 | 81 |
|
71 | | - function parseOptions(_cm, options) { |
72 | | - if (options instanceof Function) return {getAnnotations: options}; |
73 | | - if (!options || options === true) options = {}; |
74 | | - return options; |
| 82 | + var defaults = { |
| 83 | + highlightLines: false, |
| 84 | + tooltips: true, |
| 85 | + delay: 500, |
| 86 | + lintOnChange: true |
75 | 87 | } |
76 | 88 |
|
77 | 89 | function clearMarks(cm) { |
78 | 90 | var state = cm.state.lint; |
79 | 91 | if (state.hasGutter) cm.clearGutter(GUTTER_ID); |
80 | | - if (isHighlightErrorLinesEnabled(state)) clearErrorLines(cm); |
| 92 | + if (state.options.highlightLines) clearErrorLines(cm); |
81 | 93 | for (var i = 0; i < state.marked.length; ++i) |
82 | 94 | state.marked[i].clear(); |
83 | 95 | state.marked.length = 0; |
|
90 | 102 | }) |
91 | 103 | } |
92 | 104 |
|
93 | | - function isHighlightErrorLinesEnabled(state) { |
94 | | - return state.options.highlightLines; |
95 | | - } |
96 | | - |
97 | 105 | function makeMarker(cm, labels, severity, multiple, tooltips) { |
98 | 106 | var marker = document.createElement("div"), inner = marker; |
99 | 107 | marker.className = "CodeMirror-lint-marker CodeMirror-lint-marker-" + severity; |
|
136 | 144 | return tip; |
137 | 145 | } |
138 | 146 |
|
139 | | - function lintAsync(cm, getAnnotations, passOptions) { |
| 147 | + function lintAsync(cm, getAnnotations) { |
140 | 148 | var state = cm.state.lint |
141 | 149 | var id = ++state.waitingFor |
142 | 150 | function abort() { |
|
149 | 157 | if (state.waitingFor != id) return |
150 | 158 | if (arg2 && annotations instanceof CodeMirror) annotations = arg2 |
151 | 159 | cm.operation(function() {updateLinting(cm, annotations)}) |
152 | | - }, passOptions, cm); |
| 160 | + }, state.linterOptions, cm); |
153 | 161 | } |
154 | 162 |
|
155 | 163 | function startLinting(cm) { |
|
160 | 168 | * Passing rules in `options` property prevents JSHint (and other linters) from complaining |
161 | 169 | * about unrecognized rules like `onUpdateLinting`, `delay`, `lintOnChange`, etc. |
162 | 170 | */ |
163 | | - var passOptions = options.options || options; |
164 | 171 | var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint"); |
165 | 172 | if (!getAnnotations) return; |
166 | 173 | if (options.async || getAnnotations.async) { |
167 | | - lintAsync(cm, getAnnotations, passOptions) |
| 174 | + lintAsync(cm, getAnnotations) |
168 | 175 | } else { |
169 | | - var annotations = getAnnotations(cm.getValue(), passOptions, cm); |
| 176 | + var annotations = getAnnotations(cm.getValue(), state.linterOptions, cm); |
170 | 177 | if (!annotations) return; |
171 | 178 | if (annotations.then) annotations.then(function(issues) { |
172 | 179 | cm.operation(function() {updateLinting(cm, issues)}) |
|
213 | 220 | cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1, |
214 | 221 | state.options.tooltips)); |
215 | 222 |
|
216 | | - if (isHighlightErrorLinesEnabled(state)) |
| 223 | + if (state.options.highlightLines) |
217 | 224 | cm.addLineClass(line, "wrap", LINT_LINE_ID + maxSeverity); |
218 | 225 | } |
219 | 226 | if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm); |
|
223 | 230 | var state = cm.state.lint; |
224 | 231 | if (!state) return; |
225 | 232 | clearTimeout(state.timeout); |
226 | | - state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500); |
| 233 | + state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay); |
227 | 234 | } |
228 | 235 |
|
229 | 236 | function popupTooltips(cm, annotations, e) { |
|
263 | 270 | if (val) { |
264 | 271 | var gutters = cm.getOption("gutters"), hasLintGutter = false; |
265 | 272 | for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true; |
266 | | - var state = cm.state.lint = new LintState(cm, parseOptions(cm, val), hasLintGutter); |
267 | | - if (state.options.lintOnChange !== false) |
| 273 | + var state = cm.state.lint = new LintState(cm, val, hasLintGutter); |
| 274 | + if (state.options.lintOnChange) |
268 | 275 | cm.on("change", onChange); |
269 | 276 | if (state.options.tooltips != false && state.options.tooltips != "gutter") |
270 | 277 | CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver); |
|
0 commit comments