aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util/multiplex.js
blob: 822ee62a863f527559a6a86e6fbd54c59937e8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
CodeMirror.multiplexingMode = function(outer /*, others */) {
  // Others should be {open, close, mode [, delimStyle]} objects
  var others = Array.prototype.slice.call(arguments, 1);
  var n_others = others.length;

  return {
    startState: function() {
      return {
        outer: CodeMirror.startState(outer),
        innerActive: null,
        inner: null
      };
    },

    copyState: function(state) {
      return {
        outer: CodeMirror.copyState(outer, state.outer),
        innerActive: state.innerActive,
        inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
      };
    },

    token: function(stream, state) {
      if (!state.innerActive) {
        for (var i = 0; i < n_others; ++i) {
          var other = others[i];
          if (stream.match(other.open)) {
            state.innerActive = other;
            state.inner = CodeMirror.startState(other.mode);
            return other.delimStyle;
          }
        }
        var outerToken = outer.token(stream, state.outer);
        var cur = stream.current();
        for (var i = 0; i < n_others; ++i) {
          var other = others[i], found = cur.indexOf(other.open);
          if (found > -1) {
            stream.backUp(cur.length - found);
            cur = cur.slice(0, found);
          }
        }
        return outerToken;
      } else {
        var curInner = state.innerActive;
        if (stream.match(curInner.close)) {
          state.innerActive = state.inner = null;
          return curInner.delimStyle;
        }
        var innerToken = curInner.mode.token(stream, state.inner);
        var cur = stream.current(), found = cur.indexOf(curInner.close);
        if (found > -1) stream.backUp(cur.length - found);
        return innerToken;
      }
    },
    
    indent: function(state, textAfter) {
      var mode = state.innerActive || outer;
      if (!mode.indent) return CodeMirror.Pass;
      return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
    },

    compareStates: function(a, b) {
      if (a.innerActive != b.innerActive) return false;
      var mode = a.innerActive || outer;
      if (!mode.compareStates) return CodeMirror.Pass;
      return mode.compareStates(a.innerActive ? a.inner : a.outer,
                                b.innerActive ? b.inner : b.outer);
    },

    electricChars: outer.electricChars
  };
};