-
Notifications
You must be signed in to change notification settings - Fork 0
/
Documents.ts
352 lines (314 loc) · 11.2 KB
/
Documents.ts
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
import { DocumentUri,
Range,
Position,
TextDocumentContentChangeEvent,
TextDocumentItem
} from 'vscode-languageserver';
import { languagesIds, newRange } from './Utils';
import { getLogger, logSources } from './Logger';
import { URI } from 'vscode-uri';
export interface ChangeInfo{
oldRange: Range,
newRange: Range
}
export interface PMTextDocument {
/**
* the filesystem path of the current file
*
* @readonly
*/
readonly path: string;
/**
* The associated URI for this document. Most documents have the __file__-scheme, indicating that they
* represent files on disk. However, some documents may have other schemes indicating that they are not
* available on disk.
*
* @readonly
*/
readonly uri: string;
/**
* The identifier of the language associated with this document.
*
* @readonly
*/
readonly languageId: languagesIds;
/**
* The version number of this document (it will increase after each
* change, including undo/redo).
*/
version: number;
/**
* Get the text of this document. A substring can be retrieved by
* providing a range.
*
* @param range (optional) An range within the document to return.
* If no range is passed, the full content is returned.
* Invalid range positions are adjusted as described in [Position.line](#Position.line)
* and [Position.character](#Position.character).
* If the start range position is greater than the end range position,
* then the effect of getText is as if the two positions were swapped.
* @return The text of this document or a substring of the text if a
* range is provided.
*/
getText(range?: Range): string;
/**
* Converts a zero-based offset to a position.
*
* @param offset A zero-based offset.
* @return A valid [position](#Position).
*/
positionAt(offset: number): Position;
/**
* Converts the position to a zero-based offset.
* Invalid positions are adjusted as described in [Position.line](#Position.line)
* and [Position.character](#Position.character).
*
* @param position A position.
* @return A valid zero-based offset.
*/
offsetAt(position: Position): number;
/**
* The number of lines in this document.
*
* @readonly
*/
readonly lineCount: number;
/**
* @param other
* @returns true if both files have identical infromation
*/
isEqual(other:PMTextDocument): boolean;
/**
* updates file text and version according to changes
* this supports both incremental and full change
* @param changes array of changes
* @param version version to set to file after change
* @returns array of the ranges of the changed text, this is array of the new text range, not the old
*/
update(changes: TextDocumentContentChangeEvent[], version: number): ChangeInfo[];
/**
* array of the last changes range made to the file
* updates after every call to update
*/
lastChanges: ChangeInfo[]
}
class FullTextDocument implements PMTextDocument {
private _path: string;
private _uri: string;
private _languageId: languagesIds;
private _version: number;
private _content: string;
private _lineOffsets: number[] | undefined; // only use getter for this, this value is lazy
private _lastChanges: ChangeInfo[];
public constructor(uri: DocumentUri, languageId: languagesIds, version: number, content: string) {
this._path = URI.parse(uri).fsPath;
this._uri = uri;
this._languageId = languageId;
this._version = version;
this._content = content;
this._lineOffsets = undefined;
this._lastChanges = [];
}
public get lastChanges(): ChangeInfo[] {
return this._lastChanges;
}
public set lastChanges(changes : ChangeInfo[]) {
this._lastChanges = changes;
}
public get path(): string {
return this._path;
}
public get uri(): DocumentUri{
return this._uri;
}
public get languageId(): languagesIds {
return this._languageId;
}
public get version(): number {
return this._version;
}
public set version(version: number){
this._version = version;
}
public getText(range?: Range): string {
if (range) {
const start = this.offsetAt(range.start);
const end = this.offsetAt(range.end);
return this._content.substring(start, end);
}
return this._content;
}
public update(changes: TextDocumentContentChangeEvent[], version: number): ChangeInfo[] {
let changesRange: ChangeInfo[] = []; // keeps all the changes Range
for (let change of changes) {
if (FullTextDocument.isIncremental(change)) {
// makes sure start is before end
const range = getWellformedRange(change.range);
// update content
const startOffset = this.offsetAt(range.start);
const endOffset = this.offsetAt(range.end);
this._content = this._content.substring(0, startOffset) + change.text + this._content.substring(endOffset, this._content.length);
// update the offsets
const startLine = Math.max(range.start.line, 0);
const endLine = Math.max(range.end.line, 0);
let lineOffsets = this._lineOffsets!;
const addedLineOffsets = computeLineOffsets(change.text, false, startOffset);
if (endLine - startLine === addedLineOffsets.length) { //this updates the _linesOffset array with the new values
for (let i = 0, len = addedLineOffsets.length; i < len; i++) { // this updates in case no new lines were added and only existing lines end offset was changed
lineOffsets[i + startLine + 1] = addedLineOffsets[i];
}
} else { //this is in case new lines were added or lines were removed
if (addedLineOffsets.length < 10000) {
lineOffsets.splice(startLine + 1, endLine - startLine, ...addedLineOffsets);
} else { // avoid too many arguments for splice
this._lineOffsets = lineOffsets = lineOffsets.slice(0, startLine + 1).concat(addedLineOffsets, lineOffsets.slice(endLine + 1));
}
}
const diff = change.text.length - (endOffset - startOffset);
if (diff !== 0) { //this updates all the lines offset after the change lines
for (let i = startLine + 1 + addedLineOffsets.length, len = lineOffsets.length; i < len; i++) {
lineOffsets[i] = lineOffsets[i] + diff;
}
}
//change.text.length + startOffset = offset of the end of change
//addedLineOffsets this can give the new offset of lines if needed this represent all the lines that were changed
// made longer/ shorter / added/ removed
let newChangePosition: Range = {
start: change.range.start,
end: this.positionAt(change.text.length + startOffset)
}
changesRange.push({
oldRange: change.range,
newRange: newChangePosition
});
} else if (FullTextDocument.isFull(change)) {
let oldRange: Range = {start: this.positionAt(0), end: this.positionAt(this._content.length) }
this._content = change.text;
this._lineOffsets = undefined;
changesRange.push({
oldRange: oldRange,
newRange : {
start: this.positionAt(0),
end: this.positionAt(change.text.length)
}
});
} else {
getLogger(logSources.documents).error('wrong update type');
return undefined;
}
}
this._version = version;
this._lastChanges = changesRange;
return changesRange;
}
private getLineOffsets(): number[] {
if (this._lineOffsets === undefined) {
this._lineOffsets = computeLineOffsets(this._content, true);
}
return this._lineOffsets;
}
public positionAt(offset: number): Position {
offset = Math.max(Math.min(offset, this._content.length), 0);
let lineOffsets = this.getLineOffsets();
let low = 0, high = lineOffsets.length;
if (high === 0) {
return { line: 0, character: offset };
}
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (lineOffsets[mid] > offset) {
high = mid;
} else {
low = mid + 1;
}
}
// low is the least x for which the line offset is larger than the current offset
// or array.length if no line offset is larger than the current offset
let line = low - 1;
return { line, character: offset - lineOffsets[line] };
}
public offsetAt(position: Position) {
let lineOffsets = this.getLineOffsets();
if (position.line >= lineOffsets.length) {
return this._content.length;
} else if (position.line < 0) {
return 0;
}
let lineOffset = lineOffsets[position.line];
let nextLineOffset = (position.line + 1 < lineOffsets.length) ? lineOffsets[position.line + 1] : this._content.length;
return Math.max(Math.min(lineOffset + position.character, nextLineOffset), lineOffset);
}
public get lineCount(): number {
return this.getLineOffsets().length;
}
/**
* checks if the document update type is incremental
* @param event
*/
private static isIncremental(event: TextDocumentContentChangeEvent): event is { range: Range; rangeLength?: number; text: string; } {
let candidate: { range: Range; rangeLength?: number; text: string; } = event as any;
return candidate !== undefined && candidate !== null &&
typeof candidate.text === 'string' && candidate.range !== undefined &&
(candidate.rangeLength === undefined || typeof candidate.rangeLength === 'number');
}
/**
* checks if the document update type is full document change
* @param event
*/
private static isFull(event: TextDocumentContentChangeEvent): event is { text: string; } {
let candidate: { range?: Range; rangeLength?: number; text: string; } = event as any;
return candidate !== undefined && candidate !== null &&
typeof candidate.text === 'string' && candidate.range === undefined && candidate.rangeLength === undefined;
}
public isEqual(other:PMTextDocument): boolean {
if (! (other instanceof FullTextDocument)) {
return false;
}
if (this._path !== other._path || this._version !== other.version || this._languageId !== other._languageId ||
this.getLineOffsets() !== other.getLineOffsets() || this._content !== other._content ) {
return false;
}
return true;
}
}
const enum CharCode {
/**
* The `\n` character.
*/
LineFeed = 10,
/**
* The `\r` character.
*/
CarriageReturn = 13,
}
function computeLineOffsets(text: string, isAtLineStart: boolean, textOffset = 0): number[] {
const result: number[] = isAtLineStart ? [textOffset] : [];
for (let i = 0; i < text.length; i++) {
let ch = text.charCodeAt(i);
if (ch === CharCode.CarriageReturn || ch === CharCode.LineFeed) {
if (ch === CharCode.CarriageReturn && i + 1 < text.length && text.charCodeAt(i + 1) === CharCode.LineFeed) {
i++;
}
result.push(textOffset + i + 1);
}
}
return result;
}
function getWellformedRange(range: Range): Range {
const start = range.start;
const end = range.end;
if (start.line > end.line || (start.line === end.line && start.character > end.character)) {
return { start: end, end: start };
}
return range;
}
export function createNewTextDocument(uri: string, languageId: any, version: number = - 1, content: string = ""): PMTextDocument {
if (typeof languageId === 'string') {
return new FullTextDocument(uri, languagesIds[languageId],version, content);
}else{
return new FullTextDocument(uri, languageId,version, content);
}
}
export function createNewTextDocumentFromItem (textDocument: TextDocumentItem): PMTextDocument {
return createNewTextDocument(textDocument.uri,languagesIds[textDocument.languageId],textDocument.version,textDocument.text);
}