forked from atom/superstring
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
355 lines (310 loc) · 10.6 KB
/
index.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
let binding
if (process.env.SUPERSTRING_USE_BROWSER_VERSION) {
binding = require('./browser');
const {TextBuffer, Patch} = binding
const {findSync, findAllSync, findAndMarkAllSync, findWordsWithSubsequenceInRange, getCharacterAtPosition} = TextBuffer.prototype
const DEFAULT_RANGE = Object.freeze({start: {row: 0, column: 0}, end: {row: Infinity, column: Infinity}})
TextBuffer.prototype.findInRangeSync = function (pattern, range) {
let ignoreCase = false
let unicode = false
if (pattern.source) {
ignoreCase = pattern.flags.includes('i')
unicode = pattern.unicode
pattern = pattern.source
}
const result = findSync.call(this, pattern, ignoreCase, unicode, range)
if (typeof result === 'string') {
throw new Error(result);
} else {
return result
}
}
TextBuffer.prototype.findSync = function (pattern, range) {
return this.findInRangeSync(pattern, DEFAULT_RANGE)
}
TextBuffer.prototype.findAllInRangeSync = function (pattern, range) {
let ignoreCase = false
let unicode = false
if (pattern.source) {
ignoreCase = pattern.flags.includes('i')
unicode = pattern.unicode
pattern = pattern.source
}
const result = findAllSync.call(this, pattern, ignoreCase, unicode, range)
if (typeof result === 'string') {
throw new Error(result);
} else {
return result
}
}
TextBuffer.prototype.findAllSync = function (pattern, range) {
return this.findAllInRangeSync(pattern, DEFAULT_RANGE)
}
TextBuffer.prototype.findAndMarkAllInRangeSync = function (markerIndex, nextId, exclusive, pattern, range) {
let ignoreCase = false
let unicode = false
if (pattern.source) {
ignoreCase = pattern.flags.includes('i')
unicode = pattern.unicode
pattern = pattern.source
}
const result = findAndMarkAllSync.call(this, markerIndex, nextId, exclusive, pattern, ignoreCase, unicode, range)
if (typeof result === 'string') {
throw new Error(result);
} else {
return result
}
}
TextBuffer.prototype.findAndMarkAllSync = function (markerIndex, nextId, exclusive, pattern) {
return this.findAndMarkAllInRangeSync(markerIndex, nextId, exclusive, pattern, DEFAULT_RANGE)
}
TextBuffer.prototype.find = function (pattern) {
return new Promise(resolve => resolve(this.findSync(pattern)))
}
TextBuffer.prototype.findInRange = function (pattern, range) {
return new Promise(resolve => resolve(this.findInRangeSync(pattern, range)))
}
TextBuffer.prototype.findAll = function (pattern) {
return new Promise(resolve => resolve(this.findAllSync(pattern)))
}
TextBuffer.prototype.findAllInRange = function (pattern, range) {
return new Promise(resolve => resolve(this.findAllInRangeSync(pattern, range)))
}
TextBuffer.prototype.findWordsWithSubsequence = function (query, extraWordCharacters, maxCount) {
const range = {start: {row: 0, column: 0}, end: this.getExtent()}
return Promise.resolve(
findWordsWithSubsequenceInRange.call(this, query, extraWordCharacters, range).slice(0, maxCount)
)
}
TextBuffer.prototype.findWordsWithSubsequenceInRange = function (query, extraWordCharacters, maxCount, range) {
return Promise.resolve(
findWordsWithSubsequenceInRange.call(this, query, extraWordCharacters, range).slice(0, maxCount)
)
}
TextBuffer.prototype.getCharacterAtPosition = function (position) {
return String.fromCharCode(getCharacterAtPosition.call(this, position))
}
const {compose} = Patch
const {splice} = Patch.prototype
Patch.compose = function (patches) {
const result = compose.call(this, patches)
if (!result) throw new Error('Patch does not apply')
return result
}
Patch.prototype.splice = Object.assign(function () {
if (!splice.apply(this, arguments)) {
throw new Error('Patch does not apply')
}
}, splice)
} else {
try {
binding = require('./build/Release/superstring.node')
} catch (e1) {
try {
binding = require('./build/Debug/superstring.node')
} catch (e2) {
throw e1
}
}
const {TextBuffer, TextWriter, TextReader} = binding
const {
load, save, baseTextMatchesFile,
find, findAll, findSync, findAllSync, findWordsWithSubsequenceInRange
} = TextBuffer.prototype
TextBuffer.prototype.load = function (source, options, progressCallback) {
if (typeof options !== 'object') {
progressCallback = options
options = {}
}
const computePatch = options.patch === false ? false : true
const discardChanges = options.force === true ? true : false
const encoding = normalizeEncoding(options.encoding || 'UTF-8')
return new Promise((resolve, reject) => {
const completionCallback = (error, result) => {
error ? reject(error) : resolve(result)
}
if (typeof source === 'string') {
const filePath = source
load.call(
this,
completionCallback,
progressCallback,
discardChanges,
computePatch,
filePath,
encoding
)
} else {
const stream = source
const writer = new TextWriter(encoding)
stream.on('data', (data) => writer.write(data))
stream.on('error', reject)
stream.on('end', () => {
writer.end()
load.call(
this,
completionCallback,
progressCallback,
discardChanges,
computePatch,
writer
)
})
}
})
}
TextBuffer.prototype.save = function (destination, encoding = 'UTF8') {
const CHUNK_SIZE = 10 * 1024
encoding = normalizeEncoding(encoding)
return new Promise((resolve, reject) => {
if (typeof destination === 'string') {
const filePath = destination
save.call(this, filePath, encoding, (error) => {
error ? reject(error) : resolve()
})
} else {
const stream = destination
const reader = new TextReader(this, encoding)
const buffer = Buffer.allocUnsafe(CHUNK_SIZE)
writeToStream(null)
stream.on('error', (error) => {
reader.destroy()
reject(error)
})
function writeToStream () {
const bytesRead = reader.read(buffer)
if (bytesRead > 0) {
stream.write(buffer.slice(0, bytesRead), (error) => {
if (!error) writeToStream()
})
} else {
stream.end(() => {
reader.end()
resolve()
})
}
}
}
})
}
TextBuffer.prototype.find = function (pattern) {
return this.findInRange(pattern, null)
}
TextBuffer.prototype.findInRange = function (pattern, range) {
return new Promise((resolve, reject) => {
find.call(this, pattern, (error, result) => {
error ? reject(error) : resolve(result.length > 0 ? interpretRange(result) : null)
}, range)
})
}
TextBuffer.prototype.findAll = function (pattern) {
return this.findAllInRange(pattern, null)
}
TextBuffer.prototype.findAllInRange = function (pattern, range) {
return new Promise((resolve, reject) => {
findAll.call(this, pattern, (error, result) => {
error ? reject(error) : resolve(interpretRangeArray(result))
}, range)
})
}
TextBuffer.prototype.findSync = function (pattern) {
return this.findInRangeSync(pattern, null)
}
TextBuffer.prototype.findInRangeSync = function (pattern, range) {
const result = findSync.call(this, pattern, range)
return result.length > 0 ? interpretRange(result) : null
}
TextBuffer.prototype.findAllSync = function (pattern) {
return interpretRangeArray(findAllSync.call(this, pattern, null))
}
TextBuffer.prototype.findAllInRangeSync = function (pattern, range) {
return interpretRangeArray(findAllSync.call(this, pattern, range))
}
TextBuffer.prototype.findWordsWithSubsequence = function (query, extraWordCharacters, maxCount) {
return this.findWordsWithSubsequenceInRange(query, extraWordCharacters, maxCount, {
start: {row: 0, column: 0},
end: this.getExtent()
})
}
TextBuffer.prototype.findWordsWithSubsequenceInRange = function (query, extraWordCharacters, maxCount, range) {
return new Promise(resolve =>
findWordsWithSubsequenceInRange.call(this, query, extraWordCharacters, maxCount, range, (matches, positions) => {
if (!matches) {
resolve(null)
return
}
let positionArrayIndex = 0
for (let i = 0, n = matches.length; i < n; i++) {
let positionCount = positions[positionArrayIndex++]
matches[i].positions = interpretPointArray(positions, positionArrayIndex, positionCount)
positionArrayIndex += 2 * positionCount
}
resolve(matches)
})
)
}
TextBuffer.prototype.baseTextMatchesFile = function (source, encoding = 'UTF8') {
return new Promise((resolve, reject) => {
const callback = (error, result) => {
if (error) {
reject(error)
} else {
resolve(result)
}
}
if (typeof source === 'string') {
baseTextMatchesFile.call(this, callback, source, encoding)
} else {
const stream = source
const writer = new TextWriter(encoding)
stream.on('data', (data) => writer.write(data))
stream.on('error', reject)
stream.on('end', () => {
writer.end()
baseTextMatchesFile.call(this, callback, writer)
})
}
})
}
function interpretPointArray (rawData, startIndex, pointCount) {
const points = []
for (let i = 0; i < pointCount; i++) {
points.push({row: rawData[startIndex++], column: rawData[startIndex++]})
}
return points
}
function interpretRangeArray (rawData) {
const rangeCount = rawData.length / 4
const ranges = new Array(rangeCount)
let rawIndex = 0
for (let rangeIndex = 0; rangeIndex < rangeCount; rangeIndex++) {
ranges[rangeIndex] = interpretRange(rawData, rawIndex)
rawIndex += 4
}
return ranges
}
function interpretRange (rawData, index = 0) {
return {
start: {
row: rawData[index],
column: rawData[index + 1]
},
end: {
row: rawData[index + 2],
column: rawData[index + 3]
}
}
}
}
function normalizeEncoding(encoding) {
return encoding.toUpperCase()
.replace(/[^A-Z\d]/g, '')
.replace(/^(UTF|UCS|ISO|WINDOWS|KOI8|EUC)(\w)/, '$1-$2')
.replace(/^(ISO-8859)(\d)/, '$1-$2')
.replace(/^(SHIFT)(\w)/, '$1_$2')
}
module.exports = {
TextBuffer: binding.TextBuffer,
Patch: binding.Patch,
MarkerIndex: binding.MarkerIndex,
}