multiplex.js 4.0 KB

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