2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @extends {WebInspector.InplaceEditor}
35 WebInspector.CodeMirrorUtils = function()
37 WebInspector.InplaceEditor.call(this);
41 * @param {!WebInspector.TextRange} range
42 * @return {!{start: !CodeMirror.Pos, end: !CodeMirror.Pos}}
44 WebInspector.CodeMirrorUtils.toPos = function(range)
47 start: new CodeMirror.Pos(range.startLine, range.startColumn),
48 end: new CodeMirror.Pos(range.endLine, range.endColumn)
53 * @param {!CodeMirror.Pos} start
54 * @param {!CodeMirror.Pos} end
55 * @return {!WebInspector.TextRange}
57 WebInspector.CodeMirrorUtils.toRange = function(start, end)
59 return new WebInspector.TextRange(start.line, start.ch, end.line, end.ch);
63 WebInspector.CodeMirrorUtils.prototype = {
67 editorContent: function(editingContext) {
68 return editingContext.codeMirror.getValue();
74 _consumeCopy: function(e)
79 setUpEditor: function(editingContext)
81 var element = editingContext.element;
82 var config = editingContext.config;
83 editingContext.cssLoadView = new WebInspector.CodeMirrorCSSLoadView();
84 editingContext.cssLoadView.show(element);
85 WebInspector.setCurrentFocusElement(element);
86 element.addEventListener("copy", this._consumeCopy, false);
87 var codeMirror = window.CodeMirror(element, {
89 lineWrapping: config.lineWrapping,
90 smartIndent: config.smartIndent,
93 value: config.initialValue
95 codeMirror.getWrapperElement().classList.add("source-code");
96 codeMirror.on("cursorActivity", function(cm) {
97 cm.display.cursorDiv.scrollIntoViewIfNeeded(false);
99 editingContext.codeMirror = codeMirror;
102 closeEditor: function(editingContext)
104 editingContext.element.removeEventListener("copy", this._consumeCopy, false);
105 editingContext.cssLoadView.detach();
108 cancelEditing: function(editingContext)
110 editingContext.codeMirror.setValue(editingContext.oldText);
113 augmentEditingHandle: function(editingContext, handle)
115 function setWidth(editingContext, width)
118 var codeMirror = editingContext.codeMirror;
119 codeMirror.getWrapperElement().style.width = (width - codeMirror.getWrapperElement().offsetLeft - padding) + "px";
120 codeMirror.refresh();
123 handle.codeMirror = editingContext.codeMirror;
124 handle.setWidth = setWidth.bind(null, editingContext);
127 __proto__: WebInspector.InplaceEditor.prototype
132 * @implements {WebInspector.TokenizerFactory}
134 WebInspector.CodeMirrorUtils.TokenizerFactory = function() { }
136 WebInspector.CodeMirrorUtils.TokenizerFactory.prototype = {
138 * @param {string} mimeType
139 * @return {function(string, function(string, ?string, number, number))}
141 createTokenizer: function(mimeType)
143 var mode = CodeMirror.getMode({indentUnit: 2}, mimeType);
144 var state = CodeMirror.startState(mode);
145 function tokenize(line, callback)
147 var stream = new CodeMirror.StringStream(line);
148 while (!stream.eol()) {
149 var style = mode.token(stream, state);
150 var value = stream.current();
151 callback(value, style, stream.start, stream.start + value.length);
152 stream.start = stream.pos;
160 * This bogus view is needed to load/unload CodeMirror-related CSS on demand.
163 * @extends {WebInspector.VBox}
165 WebInspector.CodeMirrorCSSLoadView = function()
167 WebInspector.VBox.call(this);
168 this.element.classList.add("hidden");
169 this.registerRequiredCSS("cm/codemirror.css");
170 this.registerRequiredCSS("cm/cmdevtools.css");
173 WebInspector.CodeMirrorCSSLoadView.prototype = {
174 __proto__: WebInspector.VBox.prototype