-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTextProvider.ts
More file actions
143 lines (126 loc) · 4.25 KB
/
TextProvider.ts
File metadata and controls
143 lines (126 loc) · 4.25 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
import { TextSpan } from '../../types';
import {
END,
START,
spanIntersects,
spanIncludesIndex,
spanGetText,
spanIntersection,
spanLen
} from '../../../../utils/textSpan';
import { findLargestIndex } from '../common/findLargestIndex';
const MAX_HISTORY = 3;
export type TextMatch = {
/**
* matched text span
*/
span: TextSpan;
/**
* text before the matched text. i.e. text that will be skipped by using this match
*/
skipText: string;
/**
* distance from the nearest cursors
*/
minHistoryDistance: number;
/**
* text after the matched text
*/
textAfterEnd: string;
};
/**
* Manage text in a source (larger) cell.
* - Find text (in a target cell) from the _unused_ text
* - Once a span is mapped to a target (smaller) cell, mark the the correspondent span _used_
*/
export class TextProvider {
private readonly fieldText: string;
private remainingSpans: TextSpan[];
private history: number[] = [0]; // Keep MAX_HISTORY last recently consumed
constructor(fieldText: string) {
this.fieldText = fieldText;
this.remainingSpans = [[0, fieldText.length]];
}
/**
* Get how the given `text` matches to the currently available text
* @param text text to search
* @param { minLength, maxLength, searchSpan } options match options
* minLength, maxLength: specify min/max length of the match.
* searchSpan: the span where the `text` is searched
*/
getMatches(
text: string,
options: { minLength?: number; maxLength?: number; searchSpan?: TextSpan } = {}
): TextMatch[] {
const { minLength = 1, maxLength = text.length, searchSpan } = options;
const match = findLargestIndex(minLength, maxLength + 1, index => {
const lengthToMatch = index;
const textToMatch = text.substring(0, lengthToMatch);
const result: TextMatch[] = [];
for (const remainingSpan of this.remainingSpans) {
const aSpan = searchSpan ? spanIntersection(searchSpan, remainingSpan) : remainingSpan;
if (spanLen(aSpan) <= 0) {
continue;
}
const [spanBegin, spanEnd] = aSpan;
const spanText = this.fieldText.slice(spanBegin, spanEnd);
const foundIndex = spanText.indexOf(textToMatch);
if (foundIndex >= 0) {
const foundSpanBegin = spanBegin + foundIndex;
const foundSpanEnd = foundSpanBegin + textToMatch.length;
const historyDistances = this.history.map(i => {
const v = foundSpanBegin - i;
return v >= 0 ? v : Number.MAX_SAFE_INTEGER;
});
const textSkippedBySearchSpan =
remainingSpan[0] < spanBegin ? this.fieldText.slice(remainingSpan[0], spanBegin) : '';
result.push({
span: [foundSpanBegin, foundSpanEnd],
skipText: textSkippedBySearchSpan + spanText.substring(0, foundIndex),
minHistoryDistance: Math.min(...historyDistances, this.fieldText.length),
textAfterEnd: this.remainingSpans
.map(span => {
const validSpan = spanIntersection([foundSpanEnd, this.fieldText.length], span);
return spanGetText(this.fieldText, validSpan);
})
.join('')
});
}
}
return result.length > 0 ? result : null;
});
return match ? match.value : [];
}
/**
* Mark the `span` as used
*/
consume(span: TextSpan) {
const remaining: TextSpan[] = [];
this.remainingSpans.forEach(remainingSpan => {
if (spanIntersects(span, remainingSpan)) {
if (remainingSpan[START] < span[START]) {
remaining.push([remainingSpan[START], span[START]]);
}
if (span[END] < remainingSpan[END]) {
remaining.push([span[END], remainingSpan[END]]);
}
} else {
remaining.push(remainingSpan);
}
});
this.remainingSpans = remaining;
// update history
const validSpans = [span[END], ...this.history].filter(index => {
if (spanIncludesIndex(span, index)) return false;
if (!this.remainingSpans.some(s => spanIncludesIndex(s, index))) return false;
return true;
});
this.history = validSpans.slice(0, MAX_HISTORY);
}
/**
* Cleanup the consumed text position history
*/
resetHistory() {
this.history = [];
}
}