|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google Inc. nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +importScript("cm/codemirror.js"); |
| 32 | +importScript("cm/css.js"); |
| 33 | +importScript("cm/javascript.js"); |
| 34 | +importScript("cm/xml.js"); |
| 35 | +importScript("cm/htmlmixed.js"); |
| 36 | + |
| 37 | +/** |
| 38 | + * @constructor |
| 39 | + * @extends {WebInspector.View} |
| 40 | + * @implements {WebInspector.TextEditor} |
| 41 | + * @param {?string} url |
| 42 | + * @param {WebInspector.TextEditorDelegate} delegate |
| 43 | + */ |
| 44 | +WebInspector.CodeMirrorTextEditor = function(url, delegate) |
| 45 | +{ |
| 46 | + WebInspector.View.call(this); |
| 47 | + this._delegate = delegate; |
| 48 | + this._url = url; |
| 49 | + |
| 50 | + this.registerRequiredCSS("codemirror.css"); |
| 51 | + this.registerRequiredCSS("cmdevtools.css"); |
| 52 | + |
| 53 | + this._codeMirror = window.CodeMirror(this.element, { |
| 54 | + lineNumbers: true, |
| 55 | + fixedGutter: true, |
| 56 | + onChange: this._onChange.bind(this), |
| 57 | + onGutterClick: this._onGutterClick.bind(this) |
| 58 | + }); |
| 59 | + |
| 60 | + this._lastRange = this.range(); |
| 61 | + |
| 62 | + this.element.firstChild.addStyleClass("source-code"); |
| 63 | + this.element.firstChild.addStyleClass("fill"); |
| 64 | +} |
| 65 | + |
| 66 | +WebInspector.CodeMirrorTextEditor.prototype = { |
| 67 | + /** |
| 68 | + * @param {string} mimeType |
| 69 | + */ |
| 70 | + set mimeType(mimeType) |
| 71 | + { |
| 72 | + this._codeMirror.setOption("mode", mimeType); |
| 73 | + switch(mimeType) { |
| 74 | + case "text/html": this._codeMirror.setOption("theme", "web-inspector-html"); break; |
| 75 | + case "text/css": this._codeMirror.setOption("theme", "web-inspector-css"); break; |
| 76 | + case "text/javascript": this._codeMirror.setOption("theme", "web-inspector-js"); break; |
| 77 | + } |
| 78 | + }, |
| 79 | + |
| 80 | + /** |
| 81 | + * @param {boolean} readOnly |
| 82 | + */ |
| 83 | + setReadOnly: function(readOnly) |
| 84 | + { |
| 85 | + this._codeMirror.setOption("readOnly", readOnly); |
| 86 | + }, |
| 87 | + |
| 88 | + /** |
| 89 | + * @return {boolean} |
| 90 | + */ |
| 91 | + readOnly: function() |
| 92 | + { |
| 93 | + return !!this._codeMirror.getOption("readOnly"); |
| 94 | + }, |
| 95 | + |
| 96 | + /** |
| 97 | + * @return {Element} |
| 98 | + */ |
| 99 | + defaultFocusedElement: function() |
| 100 | + { |
| 101 | + return this.element.firstChild; |
| 102 | + }, |
| 103 | + |
| 104 | + focus: function() |
| 105 | + { |
| 106 | + this._codeMirror.focus(); |
| 107 | + }, |
| 108 | + |
| 109 | + beginUpdates: function() { }, |
| 110 | + |
| 111 | + endUpdates: function() { }, |
| 112 | + |
| 113 | + /** |
| 114 | + * @param {number} lineNumber |
| 115 | + */ |
| 116 | + revealLine: function(lineNumber) |
| 117 | + { |
| 118 | + this._codeMirror.setCursor({ line: lineNumber, ch: 0 }); |
| 119 | + var coords = this._codeMirror.cursorCoords(); |
| 120 | + this._codeMirror.scrollTo(coords.x, coords.y); |
| 121 | + }, |
| 122 | + |
| 123 | + _onGutterClick: function(instance, lineNumber, event) |
| 124 | + { |
| 125 | + this.dispatchEventToListeners(WebInspector.TextEditor.Events.GutterClick, { lineNumber: lineNumber, event: event }); |
| 126 | + }, |
| 127 | + |
| 128 | + /** |
| 129 | + * @param {number} lineNumber |
| 130 | + * @param {boolean} disabled |
| 131 | + * @param {boolean} conditional |
| 132 | + */ |
| 133 | + addBreakpoint: function(lineNumber, disabled, conditional) |
| 134 | + { |
| 135 | + var className = "cm-breakpoint" + (disabled ? " cm-breakpoint-disabled" : "") + (conditional ? " cm-breakpoint-conditional" : ""); |
| 136 | + this._codeMirror.setMarker(lineNumber, null, className); |
| 137 | + }, |
| 138 | + |
| 139 | + /** |
| 140 | + * @param {number} lineNumber |
| 141 | + */ |
| 142 | + removeBreakpoint: function(lineNumber) |
| 143 | + { |
| 144 | + this._codeMirror.clearMarker(lineNumber); |
| 145 | + }, |
| 146 | + |
| 147 | + /** |
| 148 | + * @param {number} lineNumber |
| 149 | + */ |
| 150 | + setExecutionLine: function(lineNumber) |
| 151 | + { |
| 152 | + this._executionLine = this._codeMirror.getLineHandle(lineNumber) |
| 153 | + this._codeMirror.setLineClass(this._executionLine, null, "cm-execution-line"); |
| 154 | + }, |
| 155 | + |
| 156 | + clearExecutionLine: function() |
| 157 | + { |
| 158 | + if (this._executionLine) |
| 159 | + this._codeMirror.setLineClass(this._executionLine, null, null); |
| 160 | + delete this._executionLine; |
| 161 | + }, |
| 162 | + |
| 163 | + /** |
| 164 | + * @param {number} lineNumber |
| 165 | + * @param {Element} element |
| 166 | + */ |
| 167 | + addDecoration: function(lineNumber, element) |
| 168 | + { |
| 169 | + // TODO implement so that it doesn't hide context code |
| 170 | + }, |
| 171 | + |
| 172 | + /** |
| 173 | + * @param {number} lineNumber |
| 174 | + * @param {Element} element |
| 175 | + */ |
| 176 | + removeDecoration: function(lineNumber, element) |
| 177 | + { |
| 178 | + // TODO implement so that it doesn't hide context code |
| 179 | + }, |
| 180 | + |
| 181 | + /** |
| 182 | + * @param {WebInspector.TextRange} range |
| 183 | + */ |
| 184 | + markAndRevealRange: function(range) |
| 185 | + { |
| 186 | + if (range) |
| 187 | + this.setSelection(range); |
| 188 | + }, |
| 189 | + |
| 190 | + /** |
| 191 | + * @param {number} lineNumber |
| 192 | + */ |
| 193 | + highlightLine: function(lineNumber) |
| 194 | + { |
| 195 | + this.clearLineHighlight(); |
| 196 | + this._highlightedLine = this._codeMirror.getLineHandle(lineNumber); |
| 197 | + this._codeMirror.setLineClass(this._highlightedLine, null, "cm-highlight"); |
| 198 | + this._clearHighlightTimeout = setTimeout(this.clearLineHighlight.bind(this), 2000); |
| 199 | + }, |
| 200 | + |
| 201 | + clearLineHighlight: function() |
| 202 | + { |
| 203 | + if (this._clearHighlightTimeout) |
| 204 | + clearTimeout(this._clearHighlightTimeout); |
| 205 | + delete this._clearHighlightTimeout; |
| 206 | + |
| 207 | + if (this._highlightedLine) |
| 208 | + this._codeMirror.setLineClass(this._highlightedLine, null, null); |
| 209 | + delete this._highlightedLine; |
| 210 | + }, |
| 211 | + |
| 212 | + /** |
| 213 | + * @return {Array.<Element>} |
| 214 | + */ |
| 215 | + elementsToRestoreScrollPositionsFor: function() |
| 216 | + { |
| 217 | + return []; |
| 218 | + }, |
| 219 | + |
| 220 | + /** |
| 221 | + * @param {WebInspector.TextEditor} textEditor |
| 222 | + */ |
| 223 | + inheritScrollPositions: function(textEditor) |
| 224 | + { |
| 225 | + }, |
| 226 | + |
| 227 | + onResize: function() |
| 228 | + { |
| 229 | + this._codeMirror.refresh(); |
| 230 | + }, |
| 231 | + |
| 232 | + /** |
| 233 | + * @param {WebInspector.TextRange} range |
| 234 | + * @param {string} text |
| 235 | + * @return {WebInspector.TextRange} |
| 236 | + */ |
| 237 | + editRange: function(range, text) |
| 238 | + { |
| 239 | + var pos = this._toPos(range); |
| 240 | + this._codeMirror.replaceRange(text, pos.start, pos.end); |
| 241 | + var newRange = this._toRange(pos.start, this._codeMirror.posFromIndex(this._codeMirror.indexFromPos(pos.start) + text.length)); |
| 242 | + this._delegate.onTextChanged(range, newRange); |
| 243 | + return newRange; |
| 244 | + }, |
| 245 | + |
| 246 | + _onChange: function() |
| 247 | + { |
| 248 | + var newRange = this.range(); |
| 249 | + this._delegate.onTextChanged(this._lastRange, newRange); |
| 250 | + this._lastRange = newRange; |
| 251 | + }, |
| 252 | + |
| 253 | + /** |
| 254 | + * @param {number} lineNumber |
| 255 | + */ |
| 256 | + scrollToLine: function(lineNumber) |
| 257 | + { |
| 258 | + this._codeMirror.setCursor({line:lineNumber, ch:0}); |
| 259 | + }, |
| 260 | + |
| 261 | + /** |
| 262 | + * @return {WebInspector.TextRange} |
| 263 | + */ |
| 264 | + selection: function(textRange) |
| 265 | + { |
| 266 | + var start = this._codeMirror.cursorCoords(true); |
| 267 | + var end = this._codeMirror.cursorCoords(false); |
| 268 | + |
| 269 | + if (start.line > end.line || (start.line == end.line && start.ch > end.ch)) |
| 270 | + return this._toRange(end, start); |
| 271 | + |
| 272 | + return this._toRange(start, end); |
| 273 | + }, |
| 274 | + |
| 275 | + /** |
| 276 | + * @return {WebInspector.TextRange?} |
| 277 | + */ |
| 278 | + lastSelection: function() |
| 279 | + { |
| 280 | + return this._lastSelection; |
| 281 | + }, |
| 282 | + |
| 283 | + /** |
| 284 | + * @param {WebInspector.TextRange} textRange |
| 285 | + */ |
| 286 | + setSelection: function(textRange) |
| 287 | + { |
| 288 | + this._lastSelection = textRange; |
| 289 | + var pos = this._toPos(textRange); |
| 290 | + this._codeMirror.setSelection(pos.start, pos.end); |
| 291 | + }, |
| 292 | + |
| 293 | + /** |
| 294 | + * @param {string} text |
| 295 | + */ |
| 296 | + setText: function(text) |
| 297 | + { |
| 298 | + this._codeMirror.setValue(text); |
| 299 | + }, |
| 300 | + |
| 301 | + /** |
| 302 | + * @return {string} |
| 303 | + */ |
| 304 | + text: function() |
| 305 | + { |
| 306 | + return this._codeMirror.getValue(); |
| 307 | + }, |
| 308 | + |
| 309 | + /** |
| 310 | + * @return {WebInspector.TextRange} |
| 311 | + */ |
| 312 | + range: function() |
| 313 | + { |
| 314 | + var lineCount = this.linesCount; |
| 315 | + var lastLine = this._codeMirror.getLine(lineCount - 1); |
| 316 | + return this._toRange({ line: 0, ch: 0 }, { line: lineCount - 1, ch: lastLine.length }); |
| 317 | + }, |
| 318 | + |
| 319 | + /** |
| 320 | + * @param {number} lineNumber |
| 321 | + * @return {string} |
| 322 | + */ |
| 323 | + line: function(lineNumber) |
| 324 | + { |
| 325 | + return this._codeMirror.getLine(lineNumber); |
| 326 | + }, |
| 327 | + |
| 328 | + /** |
| 329 | + * @return {number} |
| 330 | + */ |
| 331 | + get linesCount() |
| 332 | + { |
| 333 | + return this._codeMirror.lineCount(); |
| 334 | + }, |
| 335 | + |
| 336 | + /** |
| 337 | + * @param {number} line |
| 338 | + * @param {string} name |
| 339 | + * @param {Object?} value |
| 340 | + */ |
| 341 | + setAttribute: function(line, name, value) |
| 342 | + { |
| 343 | + var handle = this._codeMirror.getLineHandle(line); |
| 344 | + if (handle.attributes === undefined) handle.attributes = {}; |
| 345 | + handle.attributes[name] = value; |
| 346 | + }, |
| 347 | + |
| 348 | + /** |
| 349 | + * @param {number} line |
| 350 | + * @param {string} name |
| 351 | + * @return {Object|null} value |
| 352 | + */ |
| 353 | + getAttribute: function(line, name) |
| 354 | + { |
| 355 | + var handle = this._codeMirror.getLineHandle(line); |
| 356 | + return handle.attributes && handle.attributes[name] !== undefined ? handle.attributes[name] : null; |
| 357 | + }, |
| 358 | + |
| 359 | + /** |
| 360 | + * @param {number} line |
| 361 | + * @param {string} name |
| 362 | + */ |
| 363 | + removeAttribute: function(line, name) |
| 364 | + { |
| 365 | + var handle = this._codeMirror.getLineHandle(line); |
| 366 | + if (handle && handle.attributes) |
| 367 | + delete handle.attributes[name]; |
| 368 | + }, |
| 369 | + |
| 370 | + _toPos: function(range) |
| 371 | + { |
| 372 | + return { |
| 373 | + start: {line: range.startLine, ch: range.startColumn}, |
| 374 | + end: {line: range.endLine, ch: range.endColumn} |
| 375 | + } |
| 376 | + }, |
| 377 | + |
| 378 | + _toRange: function(start, end) |
| 379 | + { |
| 380 | + return new WebInspector.TextRange(start.line, start.ch, end.line, end.ch); |
| 381 | + } |
| 382 | +} |
| 383 | + |
| 384 | +WebInspector.CodeMirrorTextEditor.prototype.__proto__ = WebInspector.View.prototype; |
0 commit comments