Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inPlaceReplace/browser/inPlaceReplaceCommand.ts
4779 views
1
/*---------------------------------------------------------------------------------------------
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 { Range } from '../../../common/core/range.js';
7
import { Selection } from '../../../common/core/selection.js';
8
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js';
9
import { ITextModel } from '../../../common/model.js';
10
11
export class InPlaceReplaceCommand implements ICommand {
12
13
private readonly _editRange: Range;
14
private readonly _originalSelection: Selection;
15
private readonly _text: string;
16
17
constructor(editRange: Range, originalSelection: Selection, text: string) {
18
this._editRange = editRange;
19
this._originalSelection = originalSelection;
20
this._text = text;
21
}
22
23
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
24
builder.addTrackedEditOperation(this._editRange, this._text);
25
}
26
27
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
28
const inverseEditOperations = helper.getInverseEditOperations();
29
const srcRange = inverseEditOperations[0].range;
30
31
if (!this._originalSelection.isEmpty()) {
32
// Preserve selection and extends to typed text
33
return new Selection(
34
srcRange.endLineNumber,
35
srcRange.endColumn - this._text.length,
36
srcRange.endLineNumber,
37
srcRange.endColumn
38
);
39
}
40
41
return new Selection(
42
srcRange.endLineNumber,
43
Math.min(this._originalSelection.positionColumn, srcRange.endColumn),
44
srcRange.endLineNumber,
45
Math.min(this._originalSelection.positionColumn, srcRange.endColumn)
46
);
47
}
48
}
49
50