You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.1 KiB

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