Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/linesOperations/browser/copyLinesCommand.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, SelectionDirection } 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 CopyLinesCommand implements ICommand {
12
13
private readonly _selection: Selection;
14
private readonly _isCopyingDown: boolean;
15
private readonly _noop: boolean;
16
17
private _selectionDirection: SelectionDirection;
18
private _selectionId: string | null;
19
private _startLineNumberDelta: number;
20
private _endLineNumberDelta: number;
21
22
constructor(selection: Selection, isCopyingDown: boolean, noop?: boolean) {
23
this._selection = selection;
24
this._isCopyingDown = isCopyingDown;
25
this._noop = noop || false;
26
this._selectionDirection = SelectionDirection.LTR;
27
this._selectionId = null;
28
this._startLineNumberDelta = 0;
29
this._endLineNumberDelta = 0;
30
}
31
32
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
33
let s = this._selection;
34
35
this._startLineNumberDelta = 0;
36
this._endLineNumberDelta = 0;
37
if (s.startLineNumber < s.endLineNumber && s.endColumn === 1) {
38
this._endLineNumberDelta = 1;
39
s = s.setEndPosition(s.endLineNumber - 1, model.getLineMaxColumn(s.endLineNumber - 1));
40
}
41
42
const sourceLines: string[] = [];
43
for (let i = s.startLineNumber; i <= s.endLineNumber; i++) {
44
sourceLines.push(model.getLineContent(i));
45
}
46
const sourceText = sourceLines.join('\n');
47
48
if (sourceText === '') {
49
// Duplicating empty line
50
if (this._isCopyingDown) {
51
this._startLineNumberDelta++;
52
this._endLineNumberDelta++;
53
}
54
}
55
56
if (this._noop) {
57
builder.addEditOperation(new Range(s.endLineNumber, model.getLineMaxColumn(s.endLineNumber), s.endLineNumber + 1, 1), s.endLineNumber === model.getLineCount() ? '' : '\n');
58
} else {
59
if (!this._isCopyingDown) {
60
builder.addEditOperation(new Range(s.endLineNumber, model.getLineMaxColumn(s.endLineNumber), s.endLineNumber, model.getLineMaxColumn(s.endLineNumber)), '\n' + sourceText);
61
} else {
62
builder.addEditOperation(new Range(s.startLineNumber, 1, s.startLineNumber, 1), sourceText + '\n');
63
}
64
}
65
66
this._selectionId = builder.trackSelection(s);
67
this._selectionDirection = this._selection.getDirection();
68
}
69
70
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
71
let result = helper.getTrackedSelection(this._selectionId!);
72
73
if (this._startLineNumberDelta !== 0 || this._endLineNumberDelta !== 0) {
74
let startLineNumber = result.startLineNumber;
75
let startColumn = result.startColumn;
76
let endLineNumber = result.endLineNumber;
77
let endColumn = result.endColumn;
78
79
if (this._startLineNumberDelta !== 0) {
80
startLineNumber = startLineNumber + this._startLineNumberDelta;
81
startColumn = 1;
82
}
83
84
if (this._endLineNumberDelta !== 0) {
85
endLineNumber = endLineNumber + this._endLineNumberDelta;
86
endColumn = 1;
87
}
88
89
result = Selection.createWithDirection(startLineNumber, startColumn, endLineNumber, endColumn, this._selectionDirection);
90
}
91
92
return result;
93
}
94
}
95
96