1
0

multiplex.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.multiplexingMode = function(outer /*, others */) {
  13. // Others should be {open, close, mode [, delimStyle] [, innerStyle]} objects
  14. var others = Array.prototype.slice.call(arguments, 1);
  15. var n_others = others.length;
  16. function indexOf(string, pattern, from) {
  17. if (typeof pattern == "string") return string.indexOf(pattern, from);
  18. var m = pattern.exec(from ? string.slice(from) : string);
  19. return m ? m.index + from : -1;
  20. }
  21. return {
  22. startState: function() {
  23. return {
  24. outer: CodeMirror.startState(outer),
  25. innerActive: null,
  26. inner: null
  27. };
  28. },
  29. copyState: function(state) {
  30. return {
  31. outer: CodeMirror.copyState(outer, state.outer),
  32. innerActive: state.innerActive,
  33. inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
  34. };
  35. },
  36. token: function(stream, state) {
  37. if (!state.innerActive) {
  38. var cutOff = Infinity, oldContent = stream.string;
  39. for (var i = 0; i < n_others; ++i) {
  40. var other = others[i];
  41. var found = indexOf(oldContent, other.open, stream.pos);
  42. if (found == stream.pos) {
  43. stream.match(other.open);
  44. state.innerActive = other;
  45. state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0);
  46. return other.delimStyle;
  47. } else if (found != -1 && found < cutOff) {
  48. cutOff = found;
  49. }
  50. }
  51. if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
  52. var outerToken = outer.token(stream, state.outer);
  53. if (cutOff != Infinity) stream.string = oldContent;
  54. return outerToken;
  55. } else {
  56. var curInner = state.innerActive, oldContent = stream.string;
  57. if (!curInner.close && stream.sol()) {
  58. state.innerActive = state.inner = null;
  59. return this.token(stream, state);
  60. }
  61. var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos) : -1;
  62. if (found == stream.pos) {
  63. stream.match(curInner.close);
  64. state.innerActive = state.inner = null;
  65. return curInner.delimStyle;
  66. }
  67. if (found > -1) stream.string = oldContent.slice(0, found);
  68. var innerToken = curInner.mode.token(stream, state.inner);
  69. if (found > -1) stream.string = oldContent;
  70. if (curInner.innerStyle) {
  71. if (innerToken) innerToken = innerToken + ' ' + curInner.innerStyle;
  72. else innerToken = curInner.innerStyle;
  73. }
  74. return innerToken;
  75. }
  76. },
  77. indent: function(state, textAfter) {
  78. var mode = state.innerActive ? state.innerActive.mode : outer;
  79. if (!mode.indent) return CodeMirror.Pass;
  80. return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
  81. },
  82. blankLine: function(state) {
  83. var mode = state.innerActive ? state.innerActive.mode : outer;
  84. if (mode.blankLine) {
  85. mode.blankLine(state.innerActive ? state.inner : state.outer);
  86. }
  87. if (!state.innerActive) {
  88. for (var i = 0; i < n_others; ++i) {
  89. var other = others[i];
  90. if (other.open === "\n") {
  91. state.innerActive = other;
  92. state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0);
  93. }
  94. }
  95. } else if (state.innerActive.close === "\n") {
  96. state.innerActive = state.inner = null;
  97. }
  98. },
  99. electricChars: outer.electricChars,
  100. innerMode: function(state) {
  101. return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
  102. }
  103. };
  104. };
  105. });