Commit | Line | Data |
---|---|---|
77b7b761 TD |
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 | ||
837afb80 TD |
9 | (function(mod) { |
10 | if (typeof exports == "object" && typeof module == "object") // CommonJS | |
11 | mod(require("../../lib/codemirror")); | |
12 | else if (typeof define == "function" && define.amd) // AMD | |
13 | define(["../../lib/codemirror"], mod); | |
14 | else // Plain browser env | |
15 | mod(CodeMirror); | |
16 | })(function(CodeMirror) { | |
17 | "use strict"; | |
18 | ||
19 | CodeMirror.overlayMode = function(base, overlay, combine) { | |
77b7b761 TD |
20 | return { |
21 | startState: function() { | |
22 | return { | |
23 | base: CodeMirror.startState(base), | |
24 | overlay: CodeMirror.startState(overlay), | |
25 | basePos: 0, baseCur: null, | |
26 | overlayPos: 0, overlayCur: null | |
27 | }; | |
28 | }, | |
29 | copyState: function(state) { | |
30 | return { | |
31 | base: CodeMirror.copyState(base, state.base), | |
32 | overlay: CodeMirror.copyState(overlay, state.overlay), | |
33 | basePos: state.basePos, baseCur: null, | |
34 | overlayPos: state.overlayPos, overlayCur: null | |
35 | }; | |
36 | }, | |
37 | ||
38 | token: function(stream, state) { | |
39 | if (stream.start == state.basePos) { | |
40 | state.baseCur = base.token(stream, state.base); | |
41 | state.basePos = stream.pos; | |
42 | } | |
43 | if (stream.start == state.overlayPos) { | |
44 | stream.pos = stream.start; | |
45 | state.overlayCur = overlay.token(stream, state.overlay); | |
46 | state.overlayPos = stream.pos; | |
47 | } | |
48 | stream.pos = Math.min(state.basePos, state.overlayPos); | |
49 | if (stream.eol()) state.basePos = state.overlayPos = 0; | |
50 | ||
51 | if (state.overlayCur == null) return state.baseCur; | |
52 | if (state.baseCur != null && combine) return state.baseCur + " " + state.overlayCur; | |
53 | else return state.overlayCur; | |
54 | }, | |
55 | ||
56 | indent: base.indent && function(state, textAfter) { | |
57 | return base.indent(state.base, textAfter); | |
58 | }, | |
59 | electricChars: base.electricChars, | |
60 | ||
61 | innerMode: function(state) { return {state: state.base, mode: base}; }, | |
62 | ||
63 | blankLine: function(state) { | |
64 | if (base.blankLine) base.blankLine(state.base); | |
65 | if (overlay.blankLine) overlay.blankLine(state.overlay); | |
66 | } | |
67 | }; | |
68 | }; | |
837afb80 TD |
69 | |
70 | }); |