forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.ts
More file actions
245 lines (227 loc) · 8.08 KB
/
selection.ts
File metadata and controls
245 lines (227 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
// tslint:disable:all
import { vscMockPosition } from './position';
import { vscMockRange } from './range';
export namespace vscMockSelection {
/**
* A selection in the editor.
* The selection is a range that has an orientation.
*/
export interface ISelection {
/**
* The line number on which the selection has started.
*/
readonly selectionStartLineNumber: number;
/**
* The column on `selectionStartLineNumber` where the selection has started.
*/
readonly selectionStartColumn: number;
/**
* The line number on which the selection has ended.
*/
readonly positionLineNumber: number;
/**
* The column on `positionLineNumber` where the selection has ended.
*/
readonly positionColumn: number;
}
/**
* The direction of a selection.
*/
export enum SelectionDirection {
/**
* The selection starts above where it ends.
*/
LTR,
/**
* The selection starts below where it ends.
*/
RTL
}
/**
* A selection in the editor.
* The selection is a range that has an orientation.
*/
export class Selection extends vscMockRange.Range {
/**
* The line number on which the selection has started.
*/
public readonly selectionStartLineNumber: number;
/**
* The column on `selectionStartLineNumber` where the selection has started.
*/
public readonly selectionStartColumn: number;
/**
* The line number on which the selection has ended.
*/
public readonly positionLineNumber: number;
/**
* The column on `positionLineNumber` where the selection has ended.
*/
public readonly positionColumn: number;
constructor(
selectionStartLineNumber: number,
selectionStartColumn: number,
positionLineNumber: number,
positionColumn: number
) {
super(selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn);
this.selectionStartLineNumber = selectionStartLineNumber;
this.selectionStartColumn = selectionStartColumn;
this.positionLineNumber = positionLineNumber;
this.positionColumn = positionColumn;
}
/**
* Clone this selection.
*/
public clone(): Selection {
return new Selection(
this.selectionStartLineNumber,
this.selectionStartColumn,
this.positionLineNumber,
this.positionColumn
);
}
/**
* Transform to a human-readable representation.
*/
public toString(): string {
return (
'[' +
this.selectionStartLineNumber +
',' +
this.selectionStartColumn +
' -> ' +
this.positionLineNumber +
',' +
this.positionColumn +
']'
);
}
/**
* Test if equals other selection.
*/
public equalsSelection(other: ISelection): boolean {
return Selection.selectionsEqual(this, other);
}
/**
* Test if the two selections are equal.
*/
public static selectionsEqual(a: ISelection, b: ISelection): boolean {
return (
a.selectionStartLineNumber === b.selectionStartLineNumber &&
a.selectionStartColumn === b.selectionStartColumn &&
a.positionLineNumber === b.positionLineNumber &&
a.positionColumn === b.positionColumn
);
}
/**
* Get directions (LTR or RTL).
*/
public getDirection(): SelectionDirection {
if (
this.selectionStartLineNumber === this.startLineNumber &&
this.selectionStartColumn === this.startColumn
) {
return SelectionDirection.LTR;
}
return SelectionDirection.RTL;
}
/**
* Create a new selection with a different `positionLineNumber` and `positionColumn`.
*/
public setEndPosition(endLineNumber: number, endColumn: number): Selection {
if (this.getDirection() === SelectionDirection.LTR) {
return new Selection(this.startLineNumber, this.startColumn, endLineNumber, endColumn);
}
return new Selection(endLineNumber, endColumn, this.startLineNumber, this.startColumn);
}
/**
* Get the position at `positionLineNumber` and `positionColumn`.
*/
public getPosition(): vscMockPosition.Position {
return new vscMockPosition.Position(this.positionLineNumber, this.positionColumn);
}
/**
* Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`.
*/
public setStartPosition(startLineNumber: number, startColumn: number): Selection {
if (this.getDirection() === SelectionDirection.LTR) {
return new Selection(startLineNumber, startColumn, this.endLineNumber, this.endColumn);
}
return new Selection(this.endLineNumber, this.endColumn, startLineNumber, startColumn);
}
// ----
/**
* Create a `Selection` from one or two positions
*/
public static fromPositions(
start: vscMockPosition.IPosition,
end: vscMockPosition.IPosition = start
): Selection {
return new Selection(start.lineNumber, start.column, end.lineNumber, end.column);
}
/**
* Create a `Selection` from an `ISelection`.
*/
public static liftSelection(sel: ISelection): Selection {
return new Selection(
sel.selectionStartLineNumber,
sel.selectionStartColumn,
sel.positionLineNumber,
sel.positionColumn
);
}
/**
* `a` equals `b`.
*/
public static selectionsArrEqual(a: ISelection[], b: ISelection[]): boolean {
if ((a && !b) || (!a && b)) {
return false;
}
if (!a && !b) {
return true;
}
if (a.length !== b.length) {
return false;
}
for (var i = 0, len = a.length; i < len; i++) {
if (!this.selectionsEqual(a[i], b[i])) {
return false;
}
}
return true;
}
/**
* Test if `obj` is an `ISelection`.
*/
public static isISelection(obj: any): obj is ISelection {
return (
obj &&
typeof obj.selectionStartLineNumber === 'number' &&
typeof obj.selectionStartColumn === 'number' &&
typeof obj.positionLineNumber === 'number' &&
typeof obj.positionColumn === 'number'
);
}
/**
* Create with a direction.
*/
public static createWithDirection(
startLineNumber: number,
startColumn: number,
endLineNumber: number,
endColumn: number,
direction: SelectionDirection
): Selection {
if (direction === SelectionDirection.LTR) {
return new Selection(startLineNumber, startColumn, endLineNumber, endColumn);
}
return new Selection(endLineNumber, endColumn, startLineNumber, startColumn);
}
}
}