1
0

overlay.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. CodeMirror.overlayParser = function(base, overlay, combine) {
  9. return {
  10. startState: function() {
  11. return {
  12. base: CodeMirror.startState(base),
  13. overlay: CodeMirror.startState(overlay),
  14. basePos: 0, baseCur: null,
  15. overlayPos: 0, overlayCur: null
  16. };
  17. },
  18. copyState: function(state) {
  19. return {
  20. base: CodeMirror.copyState(base, state.base),
  21. overlay: CodeMirror.copyState(overlay, state.overlay),
  22. basePos: state.basePos, baseCur: null,
  23. overlayPos: state.overlayPos, overlayCur: null
  24. };
  25. },
  26. token: function(stream, state) {
  27. if (stream.start == state.basePos) {
  28. state.baseCur = base.token(stream, state.base);
  29. state.basePos = stream.pos;
  30. }
  31. if (stream.start == state.overlayPos) {
  32. stream.pos = stream.start;
  33. state.overlayCur = overlay.token(stream, state.overlay);
  34. state.overlayPos = stream.pos;
  35. }
  36. stream.pos = Math.min(state.basePos, state.overlayPos);
  37. if (stream.eol()) state.basePos = state.overlayPos = 0;
  38. if (state.overlayCur == null) return state.baseCur;
  39. if (state.baseCur != null && combine) return state.baseCur + " " + state.overlayCur;
  40. else return state.overlayCur;
  41. },
  42. indent: function(state, textAfter) {
  43. return base.indent(state.base, textAfter);
  44. },
  45. electricChars: base.electricChars
  46. };
  47. };