-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextMarker.js
245 lines (236 loc) · 6.65 KB
/
textMarker.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class StringDimension {
constructor(logical=0, physical=0) {
this.logical = logical; // [...string].length
this.physical = physical; // string.length
}
static plus(x, y) {
return new StringDimension(
x.logical + y.logical,
x.physical + y.physical
);
}
static addChar(x, char) {
return new StringDimension(
x.logical + 1,
x.physical + char.length
);
}
}
function* charIterator(string, end=true) {
let index = new StringDimension();
for (let char of string) {
yield [index, char];
index = StringDimension.addChar(index, char);
}
if (end)
yield [index, ''];
}
class SymbolManager {
constructor() {
this.trie = new Map();
this.dict = new Map();
}
take(symbol) {
if (!this.dict.has(symbol)) {
this.trieAdd(symbol);
this.dict.set(symbol, {
opening: false,
closing: false,
alone: false,
view: undefined
})
}
return this.dict.get(symbol);
}
setView(symbol, view) {
this.take(symbol).view = view;
}
trieAdd(symbol) {
let trie = this.trie;
for (let char of symbol)
trie = trie.get(char) || trie.set(char, new Map()).get(char);
trie.set('END', symbol);
}
}
class MarkManager {
constructor() {
this.dict = new Map();
this.refresh();
}
refresh() {
this.list = [];
this.amount = new Map();
}
take(symbol, mark='-UNDEFINED') {
if (!this.dict.has(symbol))
this.dict.set(symbol, {
mark: mark,
closedBy: [],
consume: true
});
return this.dict.get(symbol);
}
getUnduplicatedList() {
return Array.from(this.amount.keys(), symbol => this.dict.get(symbol).mark);
}
open(symbol) {
let amount = this.amount.get(symbol) || 0;
this.amount.set(symbol, amount + 1);
this.list.push(symbol);
}
close(symbol) {
let notYetClosedMarks = [];
let closedMarks = [];
while (this.list.length) {
let opening = this.list.pop();
if (!this.take(opening).closedBy.includes(symbol)) {
notYetClosedMarks.unshift(opening);
continue;
}
this.amountDec(opening);
closedMarks.push(opening);
if (this.take(opening).consume)
break;
}
this.list = this.list.concat(notYetClosedMarks);
return closedMarks;
}
amountDec(symbol) {
let amount = this.amount.get(symbol) || 1;
this.amount.set(symbol, amount - 1);
if (amount === 1)
this.amount.delete(symbol);
}
}
class ContextManager {
constructor() {
this.Symbol = new SymbolManager();
this.Mark = new MarkManager();
}
setSymbolView(symbol, view) {
this.Symbol.setView(symbol, view);
}
addMarkOnly(mark, symbol) {
this.Symbol.take(symbol).alone = true;
this.Mark.take(symbol, mark);
}
addMarkBetween(mark, opening, closing=opening, consume=true) {
this.Symbol.take(opening).opening = true;
this.Symbol.take(closing).closing = true;
this.Mark.take(opening, mark).closedBy.push(closing);
if (!consume)
this.Mark.take(opening).consume = false;
}
addMarkAfter(mark, leading, closedBySpace=false) {
this.addMarkBetween(mark, leading, '\n', false);
if (closedBySpace)
this.addMarkBetween(mark, leading, ' ', false);
}
}
function mkTextObj(rawContent, start, symbolId) {
return {
isSymbol: symbolId !== undefined,
symbolId: symbolId,
raw: rawContent,
view: rawContent,
start: start
};
}
function newBranch(trie, start) {
return {
currNode: trie,
achieved: undefined,
start: start,
currText: '',
currTextLength: new StringDimension(),
inactive: false
};
}
function walkBranches(branchList, char) {
let nextBranchList = [];
for (let branch of branchList) {
if (!branch.currNode.has(char))
branch.inactive = true;
if (branch.inactive) {
if (branch.achieved !== undefined)
nextBranchList.push(branch);
continue;
}
nextBranchList.push(branch);
branch.currNode = branch.currNode.get(char);
branch.currText += char;
branch.currTextLength = StringDimension.addChar(branch.currTextLength, char);
if (branch.currNode.has('END')) {
branch.achieved = {
symbolId: branch.currNode.get('END'),
text: branch.currText,
textLength: branch.currTextLength
};
break; // we don't need the branches in the rest of the list
}
}
return nextBranchList;
}
function* checkBranches(branchList, buffer) {
while (branchList.length && branchList[0].inactive) {
let branch = branchList.shift();
let prefixLength = branch.start.logical - buffer.start.logical;
if (prefixLength > 0) {
let text = buffer.chars.splice(0, prefixLength).join('');
yield mkTextObj(text, buffer.start);
}
buffer.chars.splice(0, branch.achieved.textLength.logical);
buffer.start = StringDimension.plus(branch.start, branch.achieved.textLength);
yield mkTextObj(branch.achieved.text, branch.start, branch.achieved.symbolId);
}
return [branchList, buffer];
}
function* textObjGenerator(symbolManager, wholeContent) {
let branchList = [];
let buffer = {
chars: [],
start: new StringDimension(),
};
for (let [index, char] of charIterator(wholeContent)) {
buffer.chars.push(char);
if (symbolManager.trie.has(char))
branchList.push(newBranch(symbolManager.trie, index));
branchList = walkBranches(branchList, char);
[branchList, buffer] = yield* checkBranches(branchList, buffer);
}
let remainder = buffer.chars.join('');
if (remainder.length > 0)
yield mkTextObj(remainder, buffer.start);
}
function mkTextMark(textObj, markList) {
textObj.markList = markList;
textObj.startOffset = textObj.start.physical;
return textObj;
}
function* textMarkGenerator(ctxManager, wholeContent) {
ctxManager.Mark.refresh();
for (let textObj of textObjGenerator(ctxManager.Symbol, wholeContent)) {
if (!textObj.isSymbol) {
yield mkTextMark(textObj, ctxManager.Mark.getUnduplicatedList());
continue;
}
let markList = [];
let symbol = textObj.symbolId;
let symbolData = ctxManager.Symbol.take(symbol);
if (symbolData.view)
textObj.view = symbolData.view;
if (symbolData.alone)
markList.push(`${ctxManager.Mark.take(symbol).mark}`);
if (symbolData.closing) {
for (let opening of ctxManager.Mark.close(symbol))
markList.push(`${ctxManager.Mark.take(opening).mark}-end`);
}
let symbolUsed = markList.length > 0;
markList = markList.concat(ctxManager.Mark.getUnduplicatedList());
if (!symbolUsed && symbolData.opening) {
ctxManager.Mark.open(symbol);
markList.push(`${ctxManager.Mark.take(symbol).mark}-start`);
}
yield mkTextMark(textObj, markList);
}
}