overlay.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Utility function that allows modes to be combined. The mode given
  2. // as the base argument takes care of most of the normal mode
  3. // functionality, but a second (typically simple) mode is used, which
  4. // can override the style of text. Both modes get to parse all of the
  5. // text, but when both assign a non-null style to a piece of code, the
  6. // overlay wins, unless the combine argument was true, in which case
  7. // the styles are combined.
  8. // overlayParser is the old, deprecated name
  9. CodeMirror.overlayMode = CodeMirror.overlayParser = function(base, overlay, combine) {
  10. return {
  11. startState: function() {
  12. return {
  13. base: CodeMirror.startState(base),
  14. overlay: CodeMirror.startState(overlay),
  15. basePos: 0, baseCur: null,
  16. overlayPos: 0, overlayCur: null
  17. };
  18. },
  19. copyState: function(state) {
  20. return {
  21. base: CodeMirror.copyState(base, state.base),
  22. overlay: CodeMirror.copyState(overlay, state.overlay),
  23. basePos: state.basePos, baseCur: null,
  24. overlayPos: state.overlayPos, overlayCur: null
  25. };
  26. },
  27. token: function(stream, state) {
  28. if (stream.start == state.basePos) {
  29. state.baseCur = base.token(stream, state.base);
  30. state.basePos = stream.pos;
  31. }
  32. if (stream.start == state.overlayPos) {
  33. stream.pos = stream.start;
  34. state.overlayCur = overlay.token(stream, state.overlay);
  35. state.overlayPos = stream.pos;
  36. }
  37. stream.pos = Math.min(state.basePos, state.overlayPos);
  38. if (stream.eol()) state.basePos = state.overlayPos = 0;
  39. if (state.overlayCur == null) return state.baseCur;
  40. if (state.baseCur != null && combine) return state.baseCur + " " + state.overlayCur;
  41. else return state.overlayCur;
  42. },
  43. indent: base.indent && function(state, textAfter) {
  44. return base.indent(state.base, textAfter);
  45. },
  46. electricChars: base.electricChars,
  47. innerMode: function(state) { return {state: state.base, mode: base}; },
  48. blankLine: function(state) {
  49. if (base.blankLine) base.blankLine(state.base);
  50. if (overlay.blankLine) overlay.blankLine(state.overlay);
  51. }
  52. };
  53. };