Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/caretOperations/browser/moveCaretCommand.ts
3296 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 MoveCaretCommand implements ICommand {
12
13
private readonly _selection: Selection;
14
private readonly _isMovingLeft: boolean;
15
16
constructor(selection: Selection, isMovingLeft: boolean) {
17
this._selection = selection;
18
this._isMovingLeft = isMovingLeft;
19
}
20
21
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
22
if (this._selection.startLineNumber !== this._selection.endLineNumber || this._selection.isEmpty()) {
23
return;
24
}
25
const lineNumber = this._selection.startLineNumber;
26
const startColumn = this._selection.startColumn;
27
const endColumn = this._selection.endColumn;
28
if (this._isMovingLeft && startColumn === 1) {
29
return;
30
}
31
if (!this._isMovingLeft && endColumn === model.getLineMaxColumn(lineNumber)) {
32
return;
33
}
34
35
if (this._isMovingLeft) {
36
const rangeBefore = new Range(lineNumber, startColumn - 1, lineNumber, startColumn);
37
const charBefore = model.getValueInRange(rangeBefore);
38
builder.addEditOperation(rangeBefore, null);
39
builder.addEditOperation(new Range(lineNumber, endColumn, lineNumber, endColumn), charBefore);
40
} else {
41
const rangeAfter = new Range(lineNumber, endColumn, lineNumber, endColumn + 1);
42
const charAfter = model.getValueInRange(rangeAfter);
43
builder.addEditOperation(rangeAfter, null);
44
builder.addEditOperation(new Range(lineNumber, startColumn, lineNumber, startColumn), charAfter);
45
}
46
}
47
48
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
49
if (this._isMovingLeft) {
50
return new Selection(this._selection.startLineNumber, this._selection.startColumn - 1, this._selection.endLineNumber, this._selection.endColumn - 1);
51
} else {
52
return new Selection(this._selection.startLineNumber, this._selection.startColumn + 1, this._selection.endLineNumber, this._selection.endColumn + 1);
53
}
54
}
55
}
56
57