comparison .cms/lib/codemirror/addon/mode/multiplex.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11 })(function(CodeMirror) {
12 "use strict";
13
14 CodeMirror.multiplexingMode = function(outer /*, others */) {
15 // Others should be {open, close, mode [, delimStyle] [, innerStyle] [, parseDelimiters]} objects
16 var others = Array.prototype.slice.call(arguments, 1);
17
18 function indexOf(string, pattern, from, returnEnd) {
19 if (typeof pattern == "string") {
20 var found = string.indexOf(pattern, from);
21 return returnEnd && found > -1 ? found + pattern.length : found;
22 }
23 var m = pattern.exec(from ? string.slice(from) : string);
24 return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1;
25 }
26
27 return {
28 startState: function() {
29 return {
30 outer: CodeMirror.startState(outer),
31 innerActive: null,
32 inner: null,
33 startingInner: false
34 };
35 },
36
37 copyState: function(state) {
38 return {
39 outer: CodeMirror.copyState(outer, state.outer),
40 innerActive: state.innerActive,
41 inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner),
42 startingInner: state.startingInner
43 };
44 },
45
46 token: function(stream, state) {
47 if (!state.innerActive) {
48 var cutOff = Infinity, oldContent = stream.string;
49 for (var i = 0; i < others.length; ++i) {
50 var other = others[i];
51 var found = indexOf(oldContent, other.open, stream.pos);
52 if (found == stream.pos) {
53 if (!other.parseDelimiters) stream.match(other.open);
54 state.startingInner = !!other.parseDelimiters
55 state.innerActive = other;
56
57 // Get the outer indent, making sure to handle CodeMirror.Pass
58 var outerIndent = 0;
59 if (outer.indent) {
60 var possibleOuterIndent = outer.indent(state.outer, "", "");
61 if (possibleOuterIndent !== CodeMirror.Pass) outerIndent = possibleOuterIndent;
62 }
63
64 state.inner = CodeMirror.startState(other.mode, outerIndent);
65 return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open");
66 } else if (found != -1 && found < cutOff) {
67 cutOff = found;
68 }
69 }
70 if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
71 var outerToken = outer.token(stream, state.outer);
72 if (cutOff != Infinity) stream.string = oldContent;
73 return outerToken;
74 } else {
75 var curInner = state.innerActive, oldContent = stream.string;
76 if (!curInner.close && stream.sol()) {
77 state.innerActive = state.inner = null;
78 return this.token(stream, state);
79 }
80 var found = curInner.close && !state.startingInner ?
81 indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
82 if (found == stream.pos && !curInner.parseDelimiters) {
83 stream.match(curInner.close);
84 state.innerActive = state.inner = null;
85 return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close");
86 }
87 if (found > -1) stream.string = oldContent.slice(0, found);
88 var innerToken = curInner.mode.token(stream, state.inner);
89 if (found > -1) stream.string = oldContent;
90 else if (stream.pos > stream.start) state.startingInner = false
91
92 if (found == stream.pos && curInner.parseDelimiters)
93 state.innerActive = state.inner = null;
94
95 if (curInner.innerStyle) {
96 if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;
97 else innerToken = curInner.innerStyle;
98 }
99
100 return innerToken;
101 }
102 },
103
104 indent: function(state, textAfter, line) {
105 var mode = state.innerActive ? state.innerActive.mode : outer;
106 if (!mode.indent) return CodeMirror.Pass;
107 return mode.indent(state.innerActive ? state.inner : state.outer, textAfter, line);
108 },
109
110 blankLine: function(state) {
111 var mode = state.innerActive ? state.innerActive.mode : outer;
112 if (mode.blankLine) {
113 mode.blankLine(state.innerActive ? state.inner : state.outer);
114 }
115 if (!state.innerActive) {
116 for (var i = 0; i < others.length; ++i) {
117 var other = others[i];
118 if (other.open === "\n") {
119 state.innerActive = other;
120 state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "", "") : 0);
121 }
122 }
123 } else if (state.innerActive.close === "\n") {
124 state.innerActive = state.inner = null;
125 }
126 },
127
128 electricChars: outer.electricChars,
129
130 innerMode: function(state) {
131 return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
132 }
133 };
134 };
135
136 });