forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmltiplexMode.ts
More file actions
144 lines (128 loc) · 5.2 KB
/
mltiplexMode.ts
File metadata and controls
144 lines (128 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type CodeMirrorLike = any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyObj = any
const multiplexMode = (CodeMirror: CodeMirrorLike): void => {
CodeMirror.multiplexingMode = function(outer: AnyObj /*, others */): AnyObj {
// Others should be {open, close, mode [, delimStyle] [, innerStyle]} objects
// eslint-disable-next-line prefer-rest-params
const others = Array.prototype.slice.call(arguments, 1)
function indexOf(
string: string,
pattern: string | RegExp,
from: number,
returnEnd?: boolean
): number {
if (typeof pattern === 'string') {
const found = string.indexOf(pattern, from)
return returnEnd && found > -1 ? found + pattern.length : found
}
const m = pattern.exec(from ? string.slice(from) : string)
return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1
}
return {
startState() {
return {
outer: CodeMirror.startState(outer),
innerActive: null,
inner: null
}
},
copyState(state: AnyObj) {
return {
outer: CodeMirror.copyState(outer, state.outer),
innerActive: state.innerActive,
inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
}
},
token(stream: AnyObj, state: AnyObj): string | null {
if (!state.innerActive) {
let cutOff = Infinity
const oldContent = stream.string
for (let i = 0; i < others.length; ++i) {
const other = others[i]
const found = indexOf(oldContent, other.open, stream.pos)
if (found === stream.pos) {
if (!other.parseDelimiters) stream.match(other.open)
state.innerActive = other
// Get the outer indent, making sure to handle CodeMirror.Pass
let outerIndent = 0
if (outer.indent) {
const possibleOuterIndent = outer.indent(state.outer, '')
if (possibleOuterIndent !== CodeMirror.Pass) outerIndent = possibleOuterIndent
}
state.inner = CodeMirror.startState(other.mode, outerIndent)
return other.delimStyle && other.delimStyle + ' ' + other.delimStyle + '-open'
} else if (found !== -1 && found < cutOff) {
cutOff = found
}
}
if (cutOff !== Infinity) stream.string = oldContent.slice(0, cutOff)
const outerToken = outer.token(stream, state.outer)
if (cutOff !== Infinity) stream.string = oldContent
return outerToken
} else {
const curInner = state.innerActive
const oldContent = stream.string
if (!curInner.close && stream.sol()) {
state.innerActive = state.inner = null
return this.token(stream, state)
}
const found = curInner.close
? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters)
: -1
if (found === stream.pos && !curInner.parseDelimiters) {
stream.match(curInner.close)
state.innerActive = state.inner = null
return curInner.delimStyle && curInner.delimStyle + ' ' + curInner.delimStyle + '-close'
}
if (found > -1) stream.string = oldContent.slice(0, found)
let innerToken = curInner.mode.token(stream, state.inner)
if (found > -1) stream.string = oldContent
if (found === stream.pos && curInner.parseDelimiters) {
state.innerActive = state.inner = null
}
if (curInner.innerStyle) {
if (innerToken) innerToken = innerToken + ' ' + curInner.innerStyle
else innerToken = curInner.innerStyle
}
return innerToken
}
},
indent(state: AnyObj, textAfter: string) {
const mode = state.innerActive ? state.innerActive.mode : outer
if (!mode.indent) return CodeMirror.Pass
return mode.indent(state.innerActive ? state.inner : state.outer, textAfter)
},
blankLine(state: AnyObj) {
const mode = state.innerActive ? state.innerActive.mode : outer
if (mode.blankLine) {
mode.blankLine(state.innerActive ? state.inner : state.outer)
}
if (!state.innerActive) {
for (let i = 0; i < others.length; ++i) {
const other = others[i]
if (other.open === '\n') {
state.innerActive = other
state.inner = CodeMirror.startState(
other.mode,
mode.indent ? mode.indent(state.outer, '') : 0
)
}
}
} else if (state.innerActive.close === '\n') {
state.innerActive = state.inner = null
}
},
electricChars: outer.electricChars,
innerMode(state: AnyObj) {
return state.inner
? { state: state.inner, mode: state.innerActive.mode }
: { state: state.outer, mode: outer }
}
}
}
}
export default multiplexMode