1
>
/*---------------------------------------------------------------------------------------------
textEdit.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { compareBy, equals } from '../../../../base/common/arrays.js';
7
>
import { assertFn, checkAdjacentItems } from '../../../../base/common/assert.js';
8
>
import { BugIndicatingError } from '../../../../base/common/errors.js';
9
>
import { commonPrefixLength, commonSuffixLength } from '../../../../base/common/strings.js';
10
>
import { ISingleEditOperation } from '../editOperation.js';
11
>
import { BaseStringEdit, StringReplacement } from './stringEdit.js';
12
>
import { Position } from '../position.js';
13
>
import { Range } from '../range.js';
14
>
import { TextLength } from '../text/textLength.js';
15
>
import { AbstractText, StringText } from '../text/abstractText.js';
16
>
import { IEquatable } from '../../../../base/common/equals.js';
17
>
18
>
export class TextEdit {
19
>
public static fromStringEdit(edit: BaseStringEdit, initialState: AbstractText): TextEdit {
20
>
const edits = edit.replacements.map(e => TextReplacement.fromStringReplacement(e, initialState));
21
>
return new TextEdit(edits);
22
>
}
23
>
24
>
public static replace(originalRange: Range, newText: string): TextEdit {
25
return new TextEdit([new TextReplacement(originalRange, newText)]);
26
}
28
>
public static delete(range: Range): TextEdit {
29
return new TextEdit([new TextReplacement(range, '')]);
30
}
32
>
public static insert(position: Position, newText: string): TextEdit {
33
return new TextEdit([new TextReplacement(Range.fromPositions(position, position), newText)]);
34
}
36
>
public static fromParallelReplacementsUnsorted(replacements: readonly TextReplacement[]): TextEdit {
37
const r = replacements.slice().sort(compareBy(i => i.range, Range.compareRangesUsingStarts));
38
return new TextEdit(r);
39
}
41
>
constructor(
42
public readonly replacements: readonly TextReplacement[]
43
) {
44
assertFn(() => checkAdjacentItems(replacements, (a, b) => a.range.getEndPosition().isBeforeOrEqual(b.range.getStartPosition())));
45
}
47
>
/**
48
>
* Joins touching edits and removes empty edits.
49
>
*/
50
>
normalize(): TextEdit {
51
const replacements: TextReplacement[] = [];
52
for (const r of this.replacements) {