multiplex.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 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. state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0);
  48. return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open");
  49. } else if (found != -1 && found < cutOff) {
  50. cutOff = found;
  51. }
  52. }
  53. if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
  54. var outerToken = outer.token(stream, state.outer);
  55. if (cutOff != Infinity) stream.string = oldContent;
  56. return outerToken;
  57. } else {
  58. var curInner = state.innerActive, oldContent = stream.string;
  59. if (!curInner.close && stream.sol()) {
  60. state.innerActive = state.inner = null;
  61. return this.token(stream, state);
  62. }
  63. var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
  64. if (found == stream.pos && !curInner.parseDelimiters) {
  65. stream.match(curInner.close);
  66. state.innerActive = state.inner = null;
  67. return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close");
  68. }
  69. if (found > -1) stream.string = oldContent.slice(0, found);
  70. var innerToken = curInner.mode.token(stream, state.inner);
  71. if (found > -1) stream.string = oldContent;
  72. if (found == stream.pos && curInner.parseDelimiters)
  73. state.innerActive = state.inner = null;
  74. if (curInner.innerStyle) {
  75. if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;
  76. else innerToken = curInner.innerStyle;
  77. }
  78. return innerToken;
  79. }
  80. },
  81. indent: function(state, textAfter) {
  82. var mode = state.innerActive ? state.innerActive.mode : outer;
  83. if (!mode.indent) return CodeMirror.Pass;
  84. return mode.indent(state.innerActive ? state.inner : state.outer, textAfter);
  85. },
  86. blankLine: function(state) {
  87. var mode = state.innerActive ? state.innerActive.mode : outer;
  88. if (mode.blankLine) {
  89. mode.blankLine(state.innerActive ? state.inner : state.outer);
  90. }
  91. if (!state.innerActive) {
  92. for (var i = 0; i < others.length; ++i) {
  93. var other = others[i];
  94. if (other.open === "\n") {
  95. state.innerActive = other;
  96. state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0);
  97. }
  98. }
  99. } else if (state.innerActive.close === "\n") {
  100. state.innerActive = state.inner = null;
  101. }
  102. },
  103. electricChars: outer.electricChars,
  104. innerMode: function(state) {
  105. return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
  106. }
  107. };
  108. };
  109. });