multiplex.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. function indexOf(string, pattern, from, returnEnd) {
  16. if (typeof pattern == "string") {
  17. var found = string.indexOf(pattern, from);
  18. return returnEnd && found > -1 ? found + pattern.length : found;
  19. }
  20. var m = pattern.exec(from ? string.slice(from) : string);
  21. return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1;
  22. }
  23. return {
  24. startState: function() {
  25. return {
  26. outer: CodeMirror.startState(outer),
  27. innerActive: null,
  28. inner: null
  29. };
  30. },
  31. copyState: function(state) {
  32. return {
  33. outer: CodeMirror.copyState(outer, state.outer),
  34. innerActive: state.innerActive,
  35. inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
  36. };
  37. },
  38. token: function(stream, state) {
  39. if (!state.innerActive) {
  40. var cutOff = Infinity, oldContent = stream.string;
  41. for (var i = 0; i < others.length; ++i) {
  42. var other = others[i];
  43. var found = indexOf(oldContent, other.open, stream.pos);
  44. if (found == stream.pos) {
  45. if (!other.parseDelimiters) stream.match(other.open);
  46. state.innerActive = other;
  47. // Get the outer indent, making sure to handle CodeMirror.Pass
  48. var outerIndent = 0;
  49. if (outer.indent) {
  50. var possibleOuterIndent = outer.indent(state.outer, "");
  51. if (possibleOuterIndent !== CodeMirror.Pass) outerIndent = possibleOuterIndent;
  52. }
  53. state.inner = CodeMirror.startState(other.mode, outerIndent);
  54. return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open");
  55. } else if (found != -1 && found < cutOff) {
  56. cutOff = found;
  57. }
  58. }
  59. if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
  60. var outerToken = outer.token(stream, state.outer);
  61. if (cutOff != Infinity) stream.string = oldContent;
  62. return outerToken;
  63. } else {
  64. var curInner = state.innerActive, oldContent = stream.string;
  65. if (!curInner.close && stream.sol()) {
  66. state.innerActive = state.inner = null;
  67. return this.token(stream, state);
  68. }
  69. var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
  70. if (found == stream.pos && !curInner.parseDelimiters) {
  71. stream.match(curInner.close);
  72. state.innerActive = state.inner = null;
  73. return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close");
  74. }
  75. if (found > -1) stream.string = oldContent.slice(0, found);
  76. var innerToken = curInner.mode.token(stream, state.inner);
  77. if (found > -1) stream.string = oldContent;
  78. if (found == stream.pos && curInner.parseDelimiters)
  79. state.innerActive = state.inner = null;
  80. if (curInner.innerStyle) {
  81. if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;
  82. else innerToken = curInner.innerStyle;
  83. }
  84. return innerToken;
  85. }
  86. },
  87. indent: function(state, textAfter) {
  88. var mode = state.innerActive ? state.innerActive.mode : outer;
  89. if (!mode.indent) return CodeMirror.Pass;
  90. return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
  91. },
  92. blankLine: function(state) {
  93. var mode = state.innerActive ? state.innerActive.mode : outer;
  94. if (mode.blankLine) {
  95. mode.blankLine(state.innerActive ? state.inner : state.outer);
  96. }
  97. if (!state.innerActive) {
  98. for (var i = 0; i < others.length; ++i) {
  99. var other = others[i];
  100. if (other.open === "\n") {
  101. state.innerActive = other;
  102. state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0);
  103. }
  104. }
  105. } else if (state.innerActive.close === "\n") {
  106. state.innerActive = state.inner = null;
  107. }
  108. },
  109. electricChars: outer.electricChars,
  110. innerMode: function(state) {
  111. return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
  112. }
  113. };
  114. };
  115. });