aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/lib/util')
-rwxr-xr-ximports/codemirror/lib/util/dialog.css23
-rwxr-xr-ximports/codemirror/lib/util/dialog.js63
-rwxr-xr-ximports/codemirror/lib/util/foldcode.js66
-rwxr-xr-ximports/codemirror/lib/util/formatting.js291
-rwxr-xr-ximports/codemirror/lib/util/javascript-hint.js83
-rwxr-xr-ximports/codemirror/lib/util/overlay.js51
-rwxr-xr-ximports/codemirror/lib/util/runmode.js27
-rwxr-xr-ximports/codemirror/lib/util/search.js114
-rwxr-xr-ximports/codemirror/lib/util/searchcursor.js117
-rwxr-xr-ximports/codemirror/lib/util/simple-hint.css16
-rwxr-xr-ximports/codemirror/lib/util/simple-hint.js66
11 files changed, 917 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/dialog.css b/imports/codemirror/lib/util/dialog.css
new file mode 100755
index 00000000..4cb467ef
--- /dev/null
+++ b/imports/codemirror/lib/util/dialog.css
@@ -0,0 +1,23 @@
1.CodeMirror-dialog {
2 position: relative;
3}
4
5.CodeMirror-dialog > div {
6 position: absolute;
7 top: 0; left: 0; right: 0;
8 background: white;
9 border-bottom: 1px solid #eee;
10 z-index: 15;
11 padding: .1em .8em;
12 overflow: hidden;
13 color: #333;
14}
15
16.CodeMirror-dialog input {
17 border: none;
18 outline: none;
19 background: transparent;
20 width: 20em;
21 color: inherit;
22 font-family: monospace;
23}
diff --git a/imports/codemirror/lib/util/dialog.js b/imports/codemirror/lib/util/dialog.js
new file mode 100755
index 00000000..8950bf0c
--- /dev/null
+++ b/imports/codemirror/lib/util/dialog.js
@@ -0,0 +1,63 @@
1// Open simple dialogs on top of an editor. Relies on dialog.css.
2
3(function() {
4 function dialogDiv(cm, template) {
5 var wrap = cm.getWrapperElement();
6 var dialog = wrap.insertBefore(document.createElement("div"), wrap.firstChild);
7 dialog.className = "CodeMirror-dialog";
8 dialog.innerHTML = '<div>' + template + '</div>';
9 return dialog;
10 }
11
12 CodeMirror.defineExtension("openDialog", function(template, callback) {
13 var dialog = dialogDiv(this, template);
14 var closed = false, me = this;
15 function close() {
16 if (closed) return;
17 closed = true;
18 dialog.parentNode.removeChild(dialog);
19 }
20 var inp = dialog.getElementsByTagName("input")[0];
21 if (inp) {
22 CodeMirror.connect(inp, "keydown", function(e) {
23 if (e.keyCode == 13 || e.keyCode == 27) {
24 CodeMirror.e_stop(e);
25 close();
26 me.focus();
27 if (e.keyCode == 13) callback(inp.value);
28 }
29 });
30 inp.focus();
31 CodeMirror.connect(inp, "blur", close);
32 }
33 return close;
34 });
35
36 CodeMirror.defineExtension("openConfirm", function(template, callbacks) {
37 var dialog = dialogDiv(this, template);
38 var buttons = dialog.getElementsByTagName("button");
39 var closed = false, me = this, blurring = 1;
40 function close() {
41 if (closed) return;
42 closed = true;
43 dialog.parentNode.removeChild(dialog);
44 me.focus();
45 }
46 buttons[0].focus();
47 for (var i = 0; i < buttons.length; ++i) {
48 var b = buttons[i];
49 (function(callback) {
50 CodeMirror.connect(b, "click", function(e) {
51 CodeMirror.e_preventDefault(e);
52 close();
53 if (callback) callback(me);
54 });
55 })(callbacks[i]);
56 CodeMirror.connect(b, "blur", function() {
57 --blurring;
58 setTimeout(function() { if (blurring <= 0) close(); }, 200);
59 });
60 CodeMirror.connect(b, "focus", function() { ++blurring; });
61 }
62 });
63})(); \ No newline at end of file
diff --git a/imports/codemirror/lib/util/foldcode.js b/imports/codemirror/lib/util/foldcode.js
new file mode 100755
index 00000000..18957792
--- /dev/null
+++ b/imports/codemirror/lib/util/foldcode.js
@@ -0,0 +1,66 @@
1CodeMirror.braceRangeFinder = function(cm, line) {
2 var lineText = cm.getLine(line);
3 var startChar = lineText.lastIndexOf("{");
4 if (startChar < 0 || lineText.lastIndexOf("}") > startChar) return;
5 var tokenType = cm.getTokenAt({line: line, ch: startChar}).className;
6 var count = 1, lastLine = cm.lineCount(), end;
7 outer: for (var i = line + 1; i < lastLine; ++i) {
8 var text = cm.getLine(i), pos = 0;
9 for (;;) {
10 var nextOpen = text.indexOf("{", pos), nextClose = text.indexOf("}", pos);
11 if (nextOpen < 0) nextOpen = text.length;
12 if (nextClose < 0) nextClose = text.length;
13 pos = Math.min(nextOpen, nextClose);
14 if (pos == text.length) break;
15 if (cm.getTokenAt({line: i, ch: pos + 1}).className == tokenType) {
16 if (pos == nextOpen) ++count;
17 else if (!--count) { end = i; break outer; }
18 }
19 ++pos;
20 }
21 }
22 if (end == null || end == line + 1) return;
23 return end;
24};
25
26
27CodeMirror.newFoldFunction = function(rangeFinder, markText) {
28 var folded = [];
29 if (markText == null) markText = '<div style="position: absolute; left: 2px; color:#600">&#x25bc;</div>%N%';
30
31 function isFolded(cm, n) {
32 for (var i = 0; i < folded.length; ++i) {
33 var start = cm.lineInfo(folded[i].start);
34 if (!start) folded.splice(i--, 1);
35 else if (start.line == n) return {pos: i, region: folded[i]};
36 }
37 }
38
39 function expand(cm, region) {
40 cm.clearMarker(region.start);
41 for (var i = 0; i < region.hidden.length; ++i)
42 cm.showLine(region.hidden[i]);
43 }
44
45 return function(cm, line) {
46 cm.operation(function() {
47 var known = isFolded(cm, line);
48 if (known) {
49 folded.splice(known.pos, 1);
50 expand(cm, known.region);
51 } else {
52 var end = rangeFinder(cm, line);
53 if (end == null) return;
54 var hidden = [];
55 for (var i = line + 1; i < end; ++i) {
56 var handle = cm.hideLine(i);
57 if (handle) hidden.push(handle);
58 }
59 var first = cm.setMarker(line, markText);
60 var region = {start: first, hidden: hidden};
61 cm.onDeleteLine(first, function() { expand(cm, region); });
62 folded.push(region);
63 }
64 });
65 };
66};
diff --git a/imports/codemirror/lib/util/formatting.js b/imports/codemirror/lib/util/formatting.js
new file mode 100755
index 00000000..986bcb8f
--- /dev/null
+++ b/imports/codemirror/lib/util/formatting.js
@@ -0,0 +1,291 @@
1// ============== Formatting extensions ============================
2// A common storage for all mode-specific formatting features
3if (!CodeMirror.modeExtensions) CodeMirror.modeExtensions = {};
4
5// Returns the extension of the editor's current mode
6CodeMirror.defineExtension("getModeExt", function () {
7 return CodeMirror.modeExtensions[this.getOption("mode")];
8});
9
10// If the current mode is 'htmlmixed', returns the extension of a mode located at
11// the specified position (can be htmlmixed, css or javascript). Otherwise, simply
12// returns the extension of the editor's current mode.
13CodeMirror.defineExtension("getModeExtAtPos", function (pos) {
14 var token = this.getTokenAt(pos);
15 if (token && token.state && token.state.mode)
16 return CodeMirror.modeExtensions[token.state.mode == "html" ? "htmlmixed" : token.state.mode];
17 else
18 return this.getModeExt();
19});
20
21// Comment/uncomment the specified range
22CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
23 var curMode = this.getModeExtAtPos(this.getCursor());
24 if (isComment) { // Comment range
25 var commentedText = this.getRange(from, to);
26 this.replaceRange(curMode.commentStart + this.getRange(from, to) + curMode.commentEnd
27 , from, to);
28 if (from.line == to.line && from.ch == to.ch) { // An empty comment inserted - put cursor inside
29 this.setCursor(from.line, from.ch + curMode.commentStart.length);
30 }
31 }
32 else { // Uncomment range
33 var selText = this.getRange(from, to);
34 var startIndex = selText.indexOf(curMode.commentStart);
35 var endIndex = selText.lastIndexOf(curMode.commentEnd);
36 if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
37 // Take string till comment start
38 selText = selText.substr(0, startIndex)
39 // From comment start till comment end
40 + selText.substring(startIndex + curMode.commentStart.length, endIndex)
41 // From comment end till string end
42 + selText.substr(endIndex + curMode.commentEnd.length);
43 }
44 this.replaceRange(selText, from, to);
45 }
46});
47
48// Applies automatic mode-aware indentation to the specified range
49CodeMirror.defineExtension("autoIndentRange", function (from, to) {
50 var cmInstance = this;
51 this.operation(function () {
52 for (var i = from.line; i <= to.line; i++) {
53 cmInstance.indentLine(i);
54 }
55 });