UNPKG

11.3 kBTypeScriptView Raw
1import {
2 Disposable,
3 PointCompatible,
4 Range,
5 RangeCompatible,
6 RangeLike,
7 ReadonlyEditOptions,
8 TextInsertionOptions,
9} from '../index';
10
11/** Represents a selection in the TextEditor. */
12export interface Selection {
13 // Event Subscription
14 /** Calls your callback when the selection was moved. */
15 onDidChangeRange(callback: (event: SelectionChangedEvent) => void): Disposable;
16
17 /** Calls your callback when the selection was destroyed. */
18 onDidDestroy(callback: () => void): Disposable;
19
20 // Managing the selection range
21 /** Returns the screen Range for the selection. */
22 getScreenRange(): Range;
23
24 /** Modifies the screen range for the selection. */
25 setScreenRange(
26 screenRange: RangeCompatible,
27 options?: {
28 preserveFolds?: boolean | undefined;
29 autoscroll?: boolean | undefined;
30 },
31 ): void;
32
33 /** Returns the buffer Range for the selection. */
34 getBufferRange(): Range;
35
36 /** Modifies the buffer Range for the selection. */
37 setBufferRange(
38 bufferRange: RangeCompatible,
39 options?: {
40 reversed?: boolean | undefined;
41 preserveFolds?: boolean | undefined;
42 autoscroll?: boolean | undefined;
43 },
44 ): void;
45
46 /** Returns the starting and ending buffer rows the selection is highlighting. */
47 getBufferRowRange(): [number, number];
48
49 // Info about the selection
50 /** Determines if the selection contains anything. */
51 isEmpty(): boolean;
52
53 /**
54 * Determines if the ending position of a marker is greater than the starting position.
55 * This can happen when, for example, you highlight text "up" in a TextBuffer.
56 */
57 isReversed(): boolean;
58
59 /** Returns whether the selection is a single line or not. */
60 isSingleScreenLine(): boolean;
61
62 /** Returns the text in the selection. */
63 getText(): string;
64
65 // NOTE: this calls into Range.intersectsWith(), which is one of the few functions
66 // that doesn't take a range-compatible range, despite what the API says.
67 /** Identifies if a selection intersects with a given buffer range. */
68 intersectsBufferRange(bufferRange: RangeLike): boolean;
69
70 /** Identifies if a selection intersects with another selection. */
71 intersectsWith(otherSelection: Selection): boolean;
72
73 // Modifying the selected range
74 /** Clears the selection, moving the marker to the head. */
75 clear(options?: { autoscroll?: boolean | undefined }): void;
76
77 /** Selects the text from the current cursor position to a given screen position. */
78 selectToScreenPosition(position: PointCompatible): void;
79
80 /** Selects the text from the current cursor position to a given buffer position. */
81 selectToBufferPosition(position: PointCompatible): void;
82
83 /** Selects the text one position right of the cursor. */
84 selectRight(columnCount?: number): void;
85
86 /** Selects the text one position left of the cursor. */
87 selectLeft(columnCount?: number): void;
88
89 /** Selects all the text one position above the cursor. */
90 selectUp(rowCount?: number): void;
91
92 /** Selects all the text one position below the cursor. */
93 selectDown(rowCount?: number): void;
94
95 /**
96 * Selects all the text from the current cursor position to the top of the
97 * buffer.
98 */
99 selectToTop(): void;
100
101 /**
102 * Selects all the text from the current cursor position to the bottom of
103 * the buffer.
104 */
105 selectToBottom(): void;
106
107 /** Selects all the text in the buffer. */
108 selectAll(): void;
109
110 /**
111 * Selects all the text from the current cursor position to the beginning of
112 * the line.
113 */
114 selectToBeginningOfLine(): void;
115
116 /**
117 * Selects all the text from the current cursor position to the first character
118 * of the line.
119 */
120 selectToFirstCharacterOfLine(): void;
121
122 /**
123 * Selects all the text from the current cursor position to the end of the
124 * screen line.
125 */
126 selectToEndOfLine(): void;
127
128 /**
129 * Selects all the text from the current cursor position to the end of the
130 * buffer line.
131 */
132 selectToEndOfBufferLine(): void;
133
134 /**
135 * Selects all the text from the current cursor position to the beginning
136 * of the word.
137 */
138 selectToBeginningOfWord(): void;
139
140 /** Selects all the text from the current cursor position to the end of the word. */
141 selectToEndOfWord(): void;
142
143 /**
144 * Selects all the text from the current cursor position to the beginning of
145 * the next word.
146 */
147 selectToBeginningOfNextWord(): void;
148
149 /** Selects text to the previous word boundary. */
150 selectToPreviousWordBoundary(): void;
151
152 /** Selects text to the next word boundary. */
153 selectToNextWordBoundary(): void;
154
155 /** Selects text to the previous subword boundary. */
156 selectToPreviousSubwordBoundary(): void;
157
158 /** Selects text to the next subword boundary. */
159 selectToNextSubwordBoundary(): void;
160
161 /**
162 * Selects all the text from the current cursor position to the beginning of
163 * the next paragraph.
164 */
165 selectToBeginningOfNextParagraph(): void;
166
167 /**
168 * Selects all the text from the current cursor position to the beginning of
169 * the previous paragraph.
170 */
171 selectToBeginningOfPreviousParagraph(): void;
172
173 /** Modifies the selection to encompass the current word. */
174 selectWord(): void;
175
176 /**
177 * Expands the newest selection to include the entire word on which the
178 * cursors rests.
179 */
180 expandOverWord(): void;
181
182 /** Selects an entire line in the buffer. */
183 selectLine(row: number): void;
184
185 /**
186 * Expands the newest selection to include the entire line on which the cursor
187 * currently rests.
188 * It also includes the newline character.
189 */
190 expandOverLine(): void;
191
192 // Modifying the selected text
193 /** Replaces text at the current selection. */
194 insertText(text: string, options?: TextInsertionOptions & ReadonlyEditOptions): void;
195
196 /**
197 * Removes the first character before the selection if the selection is empty
198 * otherwise it deletes the selection.
199 */
200 backspace(options?: ReadonlyEditOptions): void;
201
202 /**
203 * Removes the selection or, if nothing is selected, then all characters from
204 * the start of the selection back to the previous word boundary.
205 */
206 deleteToPreviousWordBoundary(options?: ReadonlyEditOptions): void;
207
208 /**
209 * Removes the selection or, if nothing is selected, then all characters from
210 * the start of the selection up to the next word boundary.
211 */
212 deleteToNextWordBoundary(options?: ReadonlyEditOptions): void;
213
214 /**
215 * Removes from the start of the selection to the beginning of the current
216 * word if the selection is empty otherwise it deletes the selection.
217 */
218 deleteToBeginningOfWord(options?: ReadonlyEditOptions): void;
219
220 /**
221 * Removes from the beginning of the line which the selection begins on all
222 * the way through to the end of the selection.
223 */
224 deleteToBeginningOfLine(options?: ReadonlyEditOptions): void;
225
226 /**
227 * Removes the selection or the next character after the start of the selection
228 * if the selection is empty.
229 */
230 delete(options?: ReadonlyEditOptions): void;
231
232 /**
233 * If the selection is empty, removes all text from the cursor to the end of
234 * the line. If the cursor is already at the end of the line, it removes the following
235 * newline. If the selection isn't empty, only deletes the contents of the selection.
236 */
237 deleteToEndOfLine(options?: ReadonlyEditOptions): void;
238
239 /**
240 * Removes the selection or all characters from the start of the selection to
241 * the end of the current word if nothing is selected.
242 */
243 deleteToEndOfWord(options?: ReadonlyEditOptions): void;
244
245 /**
246 * Removes the selection or all characters from the start of the selection to
247 * the end of the current word if nothing is selected.
248 */
249 deleteToBeginningOfSubword(options?: ReadonlyEditOptions): void;
250
251 /**
252 * Removes the selection or all characters from the start of the selection to
253 * the end of the current word if nothing is selected.
254 */
255 deleteToEndOfSubword(options?: ReadonlyEditOptions): void;
256
257 /** Removes only the selected text. */
258 deleteSelectedText(options?: ReadonlyEditOptions): void;
259
260 /**
261 * Removes the line at the beginning of the selection if the selection is empty
262 * unless the selection spans multiple lines in which case all lines are removed.
263 */
264 deleteLine(options?: ReadonlyEditOptions): void;
265
266 /**
267 * Joins the current line with the one below it. Lines will be separated by a single space.
268 * If there selection spans more than one line, all the lines are joined together.
269 */
270 joinLines(options?: ReadonlyEditOptions): void;
271
272 /** Removes one level of indent from the currently selected rows. */
273 outdentSelectedRows(options?: ReadonlyEditOptions): void;
274
275 /**
276 * Sets the indentation level of all selected rows to values suggested by the
277 * relevant grammars.
278 */
279 autoIndentSelectedRows(options?: ReadonlyEditOptions): void;
280
281 /**
282 * Wraps the selected lines in comments if they aren't currently part of a comment.
283 * Removes the comment if they are currently wrapped in a comment.
284 */
285 toggleLineComments(options?: ReadonlyEditOptions): void;
286
287 /** Cuts the selection until the end of the screen line. */
288 cutToEndOfLine(maintainClipboard?: boolean, options?: ReadonlyEditOptions): void;
289
290 /** Cuts the selection until the end of the buffer line. */
291 cutToEndOfBufferLine(maintainClipboard?: boolean, options?: ReadonlyEditOptions): void;
292
293 /** Copies the selection to the clipboard and then deletes it. */
294 cut(maintainClipboard?: boolean, fullLine?: boolean, options?: ReadonlyEditOptions): void;
295
296 /** Copies the current selection to the clipboard. */
297 copy(maintainClipboard?: boolean, fullLine?: boolean): void;
298
299 /** Creates a fold containing the current selection. */
300 fold(): void;
301
302 /** If the selection spans multiple rows, indent all of them. */
303 indentSelectedRows(options?: ReadonlyEditOptions): void;
304
305 // Managing multiple selections
306 /** Moves the selection down one row. */
307 addSelectionBelow(): void;
308
309 /** Moves the selection up one row. */
310 addSelectionAbove(): void;
311
312 /**
313 * Combines the given selection into this selection and then destroys the
314 * given selection.
315 */
316 merge(otherSelection: Selection, options?: { preserveFolds?: boolean | undefined; autoscroll?: boolean | undefined }): void;
317
318 // Comparing to other selections
319 /**
320 * Compare this selection's buffer range to another selection's buffer range.
321 * See Range::compare for more details.
322 */
323 compare(otherSelection: Selection): number;
324}
325
326export interface SelectionChangedEvent {
327 oldBufferRange: Range;
328 oldScreenRange: Range;
329 newBufferRange: Range;
330 newScreenRange: Range;
331 selection: Selection;
332}