Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/commands/surroundSelectionCommand.ts
3294 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 '../core/range.js';
7
import { Position } from '../core/position.js';
8
import { Selection } from '../core/selection.js';
9
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../editorCommon.js';
10
import { ITextModel } from '../model.js';
11
12
export class SurroundSelectionCommand implements ICommand {
13
private readonly _range: Selection;
14
private readonly _charBeforeSelection: string;
15
private readonly _charAfterSelection: string;
16
17
constructor(range: Selection, charBeforeSelection: string, charAfterSelection: string) {
18
this._range = range;
19
this._charBeforeSelection = charBeforeSelection;
20
this._charAfterSelection = charAfterSelection;
21
}
22
23
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
24
builder.addTrackedEditOperation(new Range(
25
this._range.startLineNumber,
26
this._range.startColumn,
27
this._range.startLineNumber,
28
this._range.startColumn
29
), this._charBeforeSelection);
30
31
builder.addTrackedEditOperation(new Range(
32
this._range.endLineNumber,
33
this._range.endColumn,
34
this._range.endLineNumber,
35
this._range.endColumn
36
), this._charAfterSelection || null); // addTrackedEditOperation() ignores us if the text == ''. Causing a chain of errors in computeCursorState()
37
}
38
39
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
40
const inverseEditOperations = helper.getInverseEditOperations();
41
const firstOperationRange = inverseEditOperations[0].range;
42
const secondOperationRange = inverseEditOperations[1].range;
43
44
return new Selection(
45
firstOperationRange.endLineNumber,
46
firstOperationRange.endColumn,
47
secondOperationRange.endLineNumber,
48
secondOperationRange.endColumn - this._charAfterSelection.length
49
);
50
}
51
}
52
53
/**
54
* A surround selection command that runs after composition finished.
55
*/
56
export class CompositionSurroundSelectionCommand implements ICommand {
57
58
constructor(
59
private readonly _position: Position,
60
private readonly _text: string,
61
private readonly _charAfter: string
62
) { }
63
64
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
65
builder.addTrackedEditOperation(new Range(
66
this._position.lineNumber,
67
this._position.column,
68
this._position.lineNumber,
69
this._position.column
70
), this._text + this._charAfter);
71
}
72
73
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
74
const inverseEditOperations = helper.getInverseEditOperations();
75
const opRange = inverseEditOperations[0].range;
76
77
return new Selection(
78
opRange.endLineNumber,
79
opRange.startColumn,
80
opRange.endLineNumber,
81
opRange.endColumn - this._charAfter.length
82
);
83
}
84
}
85
86