github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/app/public/codemirror/addon/mode/overlay.js (about)

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