overlay.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. // Utility function that allows modes to be combined. The mode given
  4. // as the base argument takes care of most of the normal mode
  5. // functionality, but a second (typically simple) mode is used, which
  6. // can override the style of text. Both modes get to parse all of the
  7. // text, but when both assign a non-null style to a piece of code, the
  8. // overlay wins, unless the combine argument was true and not overridden,
  9. // or state.overlay.combineTokens was true, in which case the styles are
  10. // combined.
  11. (function(mod) {
  12. if (typeof exports == "object" && typeof module == "object") // CommonJS
  13. mod(require("../../lib/codemirror"));
  14. else if (typeof define == "function" && define.amd) // AMD
  15. define(["../../lib/codemirror"], mod);
  16. else // Plain browser env
  17. mod(CodeMirror);
  18. })(function(CodeMirror) {
  19. "use strict";
  20. CodeMirror.overlayMode = function(base, overlay, combine) {
  21. return {
  22. startState: function() {
  23. return {
  24. base: CodeMirror.startState(base),
  25. overlay: CodeMirror.startState(overlay),
  26. basePos: 0, baseCur: null,
  27. overlayPos: 0, overlayCur: null,
  28. streamSeen: null
  29. };
  30. },
  31. copyState: function(state) {
  32. return {
  33. base: CodeMirror.copyState(base, state.base),
  34. overlay: CodeMirror.copyState(overlay, state.overlay),
  35. basePos: state.basePos, baseCur: null,
  36. overlayPos: state.overlayPos, overlayCur: null
  37. };
  38. },
  39. token: function(stream, state) {
  40. if (stream != state.streamSeen ||
  41. Math.min(state.basePos, state.overlayPos) < stream.start) {
  42. state.streamSeen = stream;
  43. state.basePos = state.overlayPos = stream.start;
  44. }
  45. if (stream.start == state.basePos) {
  46. state.baseCur = base.token(stream, state.base);
  47. state.basePos = stream.pos;
  48. }
  49. if (stream.start == state.overlayPos) {
  50. stream.pos = stream.start;
  51. state.overlayCur = overlay.token(stream, state.overlay);
  52. state.overlayPos = stream.pos;
  53. }
  54. stream.pos = Math.min(state.basePos, state.overlayPos);
  55. // state.overlay.combineTokens always takes precedence over combine,
  56. // unless set to null
  57. if (state.overlayCur == null) return state.baseCur;
  58. else if (state.baseCur != null &&
  59. state.overlay.combineTokens ||
  60. combine && state.overlay.combineTokens == null)
  61. return state.baseCur + " " + state.overlayCur;
  62. else return state.overlayCur;
  63. },
  64. indent: base.indent && function(state, textAfter) {
  65. return base.indent(state.base, textAfter);
  66. },
  67. electricChars: base.electricChars,
  68. innerMode: function(state) { return {state: state.base, mode: base}; },
  69. blankLine: function(state) {
  70. if (base.blankLine) base.blankLine(state.base);
  71. if (overlay.blankLine) overlay.blankLine(state.overlay);
  72. }
  73. };
  74. };
  75. });