aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util/multiplex.js
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/lib/util/multiplex.js')
-rw-r--r--imports/codemirror/lib/util/multiplex.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/multiplex.js b/imports/codemirror/lib/util/multiplex.js
new file mode 100644
index 00000000..822ee62a
--- /dev/null
+++ b/imports/codemirror/lib/util/multiplex.js
@@ -0,0 +1,72 @@
1CodeMirror.multiplexingMode = function(outer /*, others */) {
2 // Others should be {open, close, mode [, delimStyle]} objects
3 var others = Array.prototype.slice.call(arguments, 1);
4 var n_others = others.length;
5
6 return {
7 startState: function() {
8 return {
9 outer: CodeMirror.startState(outer),
10 innerActive: null,
11 inner: null
12 };
13 },
14
15 copyState: function(state) {
16 return {
17 outer: CodeMirror.copyState(outer, state.outer),
18 innerActive: state.innerActive,
19 inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
20 };
21 },
22
23 token: function(stream, state) {
24 if (!state.innerActive) {
25 for (var i = 0; i < n_others; ++i) {
26 var other = others[i];
27 if (stream.match(other.open)) {
28 state.innerActive = other;
29 state.inner = CodeMirror.startState(other.mode);
30 return other.delimStyle;
31 }
32 }
33 var outerToken = outer.token(stream, state.outer);
34 var cur = stream.current();
35 for (var i = 0; i < n_others; ++i) {
36 var other = others[i], found = cur.indexOf(other.open);
37 if (found > -1) {
38 stream.backUp(cur.length - found);
39 cur = cur.slice(0, found);
40 }
41 }
42 return outerToken;
43 } else {
44 var curInner = state.innerActive;
45 if (stream.match(curInner.close)) {
46 state.innerActive = state.inner = null;
47 return curInner.delimStyle;
48 }
49 var innerToken = curInner.mode.token(stream, state.inner);
50 var cur = stream.current(), found = cur.indexOf(curInner.close);
51 if (found > -1) stream.backUp(cur.length - found);
52 return innerToken;
53 }
54 },
55
56 indent: function(state, textAfter) {
57 var mode = state.innerActive || outer;
58 if (!mode.indent) return CodeMirror.Pass;
59 return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
60 },
61
62 compareStates: function(a, b) {
63 if (a.innerActive != b.innerActive) return false;
64 var mode = a.innerActive || outer;
65 if (!mode.compareStates) return CodeMirror.Pass;
66 return mode.compareStates(a.innerActive ? a.inner : a.outer,
67 b.innerActive ? b.inner : b.outer);
68 },
69
70 electricChars: outer.electricChars
71 };
72};