]>
Commit | Line | Data |
---|---|---|
70fda994 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | ||
5 | import { CharMeasure } from './utils/CharMeasure'; | |
6 | import { CircularList } from './utils/CircularList'; | |
b594407c | 7 | import { EventEmitter } from './EventEmitter'; |
70fda994 | 8 | import * as Mouse from './utils/Mouse'; |
ad3ae67e | 9 | import { ITerminal } from './Interfaces'; |
f7d6ab5f | 10 | import { SelectionModel } from './SelectionModel'; |
70fda994 | 11 | |
0dc3dd03 DI |
12 | /** |
13 | * The number of pixels the mouse needs to be above or below the viewport in | |
14 | * order to scroll at the maximum speed. | |
15 | */ | |
b8129910 | 16 | const DRAG_SCROLL_MAX_THRESHOLD = 50; |
0dc3dd03 DI |
17 | |
18 | /** | |
19 | * The maximum scrolling speed | |
20 | */ | |
b8129910 | 21 | const DRAG_SCROLL_MAX_SPEED = 15; |
0dc3dd03 DI |
22 | |
23 | /** | |
24 | * The number of milliseconds between drag scroll updates. | |
25 | */ | |
b8129910 | 26 | const DRAG_SCROLL_INTERVAL = 50; |
0dc3dd03 | 27 | |
9f271de8 | 28 | /** |
f380153f DI |
29 | * The amount of time before mousedown events are no longer stacked to create |
30 | * double/triple click events. | |
9f271de8 DI |
31 | */ |
32 | const CLEAR_MOUSE_DOWN_TIME = 400; | |
33 | ||
f380153f DI |
34 | /** |
35 | * The number of pixels in each direction that the mouse must move before | |
36 | * mousedown events are no longer stacked to create double/triple click events. | |
37 | */ | |
38 | const CLEAR_MOUSE_DISTANCE = 10; | |
39 | ||
2621be81 DI |
40 | // TODO: Move these constants elsewhere, they belong in a buffer or buffer |
41 | // data/line class. | |
54e7f65d DI |
42 | const LINE_DATA_CHAR_INDEX = 1; |
43 | const LINE_DATA_WIDTH_INDEX = 2; | |
44 | ||
b594407c | 45 | export class SelectionManager extends EventEmitter { |
fd91c5e1 | 46 | protected _model: SelectionModel; |
9f271de8 DI |
47 | |
48 | /** | |
49 | * The amount to scroll every drag scroll update (depends on how far the mouse | |
50 | * drag is above or below the terminal). | |
51 | */ | |
0dc3dd03 | 52 | private _dragScrollAmount: number; |
70fda994 | 53 | |
9f271de8 DI |
54 | /** |
55 | * The last time the mousedown event fired, this is used to track double and | |
56 | * triple clicks. | |
57 | */ | |
58 | private _lastMouseDownTime: number; | |
59 | ||
f380153f DI |
60 | /** |
61 | * The last position the mouse was clicked [x, y]. | |
62 | */ | |
63 | private _lastMousePosition: [number, number]; | |
64 | ||
2621be81 DI |
65 | /** |
66 | * The number of clicks of the mousedown event. This is used to keep track of | |
67 | * double and triple clicks. | |
68 | */ | |
9f271de8 DI |
69 | private _clickCount: number; |
70 | ||
2621be81 DI |
71 | /** |
72 | * Whether line select mode is active, this occurs after a triple click. | |
73 | */ | |
5bc11121 DI |
74 | private _isLineSelectModeActive: boolean; |
75 | ||
d0b603d0 DI |
76 | /** |
77 | * A setInterval timer that is active while the mouse is down whose callback | |
78 | * scrolls the viewport when necessary. | |
79 | */ | |
80 | private _dragScrollIntervalTimer: NodeJS.Timer; | |
81 | ||
ab40908f | 82 | private _bufferTrimListener: any; |
70fda994 | 83 | private _mouseMoveListener: EventListener; |
ab40908f DI |
84 | private _mouseDownListener: EventListener; |
85 | private _mouseUpListener: EventListener; | |
70fda994 | 86 | |
ad3ae67e DI |
87 | constructor( |
88 | private _terminal: ITerminal, | |
89 | private _buffer: CircularList<any>, | |
90 | private _rowContainer: HTMLElement, | |
ad3ae67e DI |
91 | private _charMeasure: CharMeasure |
92 | ) { | |
b594407c | 93 | super(); |
ab40908f DI |
94 | this._initListeners(); |
95 | this.enable(); | |
9f271de8 | 96 | |
f7d6ab5f | 97 | this._model = new SelectionModel(_terminal); |
9f271de8 | 98 | this._lastMouseDownTime = 0; |
5bc11121 | 99 | this._isLineSelectModeActive = false; |
70fda994 DI |
100 | } |
101 | ||
d0b603d0 DI |
102 | /** |
103 | * Initializes listener variables. | |
104 | */ | |
ab40908f DI |
105 | private _initListeners() { |
106 | this._bufferTrimListener = (amount: number) => this._onTrim(amount); | |
70fda994 | 107 | this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event); |
ab40908f DI |
108 | this._mouseDownListener = event => this._onMouseDown(<MouseEvent>event); |
109 | this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event); | |
ab40908f | 110 | } |
70fda994 | 111 | |
ab40908f DI |
112 | /** |
113 | * Disables the selection manager. This is useful for when terminal mouse | |
114 | * are enabled. | |
115 | */ | |
116 | public disable() { | |
f7d6ab5f DI |
117 | this._model.selectionStart = null; |
118 | this._model.selectionEnd = null; | |
ab40908f DI |
119 | this.refresh(); |
120 | this._buffer.off('trim', this._bufferTrimListener); | |
121 | this._rowContainer.removeEventListener('mousedown', this._mouseDownListener); | |
b8129910 | 122 | this._removeMouseDownListeners(); |
ab40908f DI |
123 | } |
124 | ||
125 | /** | |
126 | * Enable the selection manager. | |
127 | */ | |
128 | public enable() { | |
f380153f DI |
129 | // Only adjust the selection on trim, shiftElements is rarely used (only in |
130 | // reverseIndex) and delete in a splice is only ever used when the same | |
131 | // number of elements was just added. Given this is could actually be | |
132 | // beneficial to leave the selection as is for these cases. | |
ab40908f DI |
133 | this._buffer.on('trim', this._bufferTrimListener); |
134 | this._rowContainer.addEventListener('mousedown', this._mouseDownListener); | |
70fda994 DI |
135 | } |
136 | ||
9f271de8 DI |
137 | /** |
138 | * Gets the text currently selected. | |
139 | */ | |
70fda994 | 140 | public get selectionText(): string { |
f7d6ab5f DI |
141 | const start = this._model.finalSelectionStart; |
142 | const end = this._model.finalSelectionEnd; | |
e29ab294 | 143 | if (!start || !end) { |
293ae18a | 144 | return ''; |
70fda994 | 145 | } |
43c796a7 | 146 | |
43c796a7 | 147 | // Get first row |
293ae18a | 148 | const startRowEndCol = start[1] === end[1] ? end[0] : null; |
32b34cbe | 149 | let result: string[] = []; |
ec61f3ac | 150 | result.push(this._translateBufferLineToString(this._buffer.get(start[1]), true, start[0], startRowEndCol)); |
43c796a7 DI |
151 | |
152 | // Get middle rows | |
293ae18a | 153 | for (let i = start[1] + 1; i <= end[1] - 1; i++) { |
ec61f3ac | 154 | result.push(this._translateBufferLineToString(this._buffer.get(i), true)); |
32b34cbe | 155 | } |
43c796a7 DI |
156 | |
157 | // Get final row | |
293ae18a | 158 | if (start[1] !== end[1]) { |
ec61f3ac | 159 | result.push(this._translateBufferLineToString(this._buffer.get(end[1]), true, 0, end[0])); |
32b34cbe | 160 | } |
2b243182 | 161 | |
32b34cbe DI |
162 | return result.join('\n'); |
163 | } | |
164 | ||
d0b603d0 DI |
165 | /** |
166 | * Translates a buffer line to a string, with optional start and end columns. | |
167 | * Wide characters will count as two columns in the resulting string. This | |
168 | * function is useful for getting the actual text underneath the raw selection | |
169 | * position. | |
170 | * @param line The line being translated. | |
171 | * @param trimRight Whether to trim whitespace to the right. | |
172 | * @param startCol The column to start at. | |
173 | * @param endCol The column to end at. | |
174 | */ | |
ec61f3ac | 175 | private _translateBufferLineToString(line: any, trimRight: boolean, startCol: number = 0, endCol: number = null): string { |
32b34cbe | 176 | // TODO: This function should live in a buffer or buffer line class |
ec61f3ac DI |
177 | |
178 | // Get full line | |
179 | let lineString = ''; | |
54e7f65d DI |
180 | let widthAdjustedStartCol = startCol; |
181 | let widthAdjustedEndCol = endCol; | |
ec61f3ac | 182 | for (let i = 0; i < line.length; i++) { |
54e7f65d DI |
183 | const char = line[i]; |
184 | lineString += char[LINE_DATA_CHAR_INDEX]; | |
185 | // Adjust start and end cols for wide characters if they affect their | |
186 | // column indexes | |
187 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { | |
188 | if (startCol >= i) { | |
189 | widthAdjustedStartCol--; | |
190 | } | |
191 | if (endCol >= i) { | |
192 | widthAdjustedEndCol--; | |
193 | } | |
194 | } | |
ec61f3ac DI |
195 | } |
196 | ||
54e7f65d DI |
197 | // Calculate the final end col by trimming whitespace on the right of the |
198 | // line if needed. | |
d3865ad2 | 199 | let finalEndCol = widthAdjustedEndCol || line.length; |
ec61f3ac DI |
200 | if (trimRight) { |
201 | const rightWhitespaceIndex = lineString.search(/\s+$/); | |
a53a0acd DI |
202 | if (rightWhitespaceIndex !== -1) { |
203 | finalEndCol = Math.min(finalEndCol, rightWhitespaceIndex); | |
204 | } | |
ec61f3ac | 205 | // Return the empty string if only trimmed whitespace is selected |
54e7f65d | 206 | if (finalEndCol <= widthAdjustedStartCol) { |
ec61f3ac DI |
207 | return ''; |
208 | } | |
209 | } | |
210 | ||
54e7f65d | 211 | return lineString.substring(widthAdjustedStartCol, finalEndCol); |
70fda994 DI |
212 | } |
213 | ||
207c4cf9 DI |
214 | /** |
215 | * Redraws the selection. | |
216 | */ | |
217 | public refresh(): void { | |
f7d6ab5f | 218 | this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd }); |
25152e44 DI |
219 | } |
220 | ||
221 | /** | |
222 | * Selects all text within the terminal. | |
223 | */ | |
224 | public selectAll(): void { | |
f7d6ab5f | 225 | this._model.isSelectAllActive = true; |
25152e44 | 226 | this.refresh(); |
207c4cf9 DI |
227 | } |
228 | ||
229 | /** | |
230 | * Handle the buffer being trimmed, adjust the selection position. | |
231 | * @param amount The amount the buffer is being trimmed. | |
232 | */ | |
70fda994 | 233 | private _onTrim(amount: number) { |
f7d6ab5f DI |
234 | const needsRefresh = this._model.onTrim(amount); |
235 | if (needsRefresh) { | |
207c4cf9 | 236 | this.refresh(); |
207c4cf9 | 237 | } |
70fda994 DI |
238 | } |
239 | ||
d0b603d0 DI |
240 | /** |
241 | * Gets the 0-based [x, y] buffer coordinates of the current mouse event. | |
242 | * @param event The mouse event. | |
243 | */ | |
0dc3dd03 DI |
244 | private _getMouseBufferCoords(event: MouseEvent): [number, number] { |
245 | const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows); | |
ad3ae67e DI |
246 | // Convert to 0-based |
247 | coords[0]--; | |
248 | coords[1]--; | |
249 | // Convert viewport coords to buffer coords | |
250 | coords[1] += this._terminal.ydisp; | |
251 | return coords; | |
b36d8780 DI |
252 | } |
253 | ||
d0b603d0 DI |
254 | /** |
255 | * Gets the amount the viewport should be scrolled based on how far out of the | |
256 | * terminal the mouse is. | |
257 | * @param event The mouse event. | |
258 | */ | |
0dc3dd03 DI |
259 | private _getMouseEventScrollAmount(event: MouseEvent): number { |
260 | let offset = Mouse.getCoordsRelativeToElement(event, this._rowContainer)[1]; | |
261 | const terminalHeight = this._terminal.rows * this._charMeasure.height; | |
262 | if (offset >= 0 && offset <= terminalHeight) { | |
263 | return 0; | |
264 | } | |
265 | if (offset > terminalHeight) { | |
266 | offset -= terminalHeight; | |
267 | } | |
268 | ||
269 | offset = Math.min(Math.max(offset, -DRAG_SCROLL_MAX_THRESHOLD), DRAG_SCROLL_MAX_THRESHOLD); | |
270 | offset /= DRAG_SCROLL_MAX_THRESHOLD; | |
271 | return (offset / Math.abs(offset)) + Math.round(offset * (DRAG_SCROLL_MAX_SPEED - 1)); | |
272 | } | |
273 | ||
e63fdf58 DI |
274 | /** |
275 | * Handles te mousedown event, setting up for a new selection. | |
276 | * @param event The mousedown event. | |
277 | */ | |
70fda994 | 278 | private _onMouseDown(event: MouseEvent) { |
0dc3dd03 DI |
279 | // Only action the primary button |
280 | if (event.button !== 0) { | |
281 | return; | |
282 | } | |
283 | ||
b8129910 DI |
284 | // Reset drag scroll state |
285 | this._dragScrollAmount = 0; | |
286 | ||
f380153f | 287 | this._setMouseClickCount(event); |
9f271de8 | 288 | |
db8ded2a DI |
289 | if (event.shiftKey) { |
290 | this._onShiftClick(event); | |
291 | } else { | |
292 | if (this._clickCount === 1) { | |
293 | this._onSingleClick(event); | |
294 | } else if (this._clickCount === 2) { | |
295 | this._onDoubleClick(event); | |
296 | } else if (this._clickCount === 3) { | |
297 | this._onTripleClick(event); | |
298 | } | |
9f271de8 | 299 | } |
e29ab294 | 300 | |
b8129910 DI |
301 | this._addMouseDownListeners(); |
302 | this.refresh(); | |
303 | } | |
304 | ||
305 | /** | |
306 | * Adds listeners when mousedown is triggered. | |
307 | */ | |
308 | private _addMouseDownListeners(): void { | |
e29ab294 DI |
309 | // Listen on the document so that dragging outside of viewport works |
310 | this._rowContainer.ownerDocument.addEventListener('mousemove', this._mouseMoveListener); | |
311 | this._rowContainer.ownerDocument.addEventListener('mouseup', this._mouseUpListener); | |
2621be81 | 312 | this._dragScrollIntervalTimer = setInterval(() => this._dragScroll(), DRAG_SCROLL_INTERVAL); |
b8129910 DI |
313 | } |
314 | ||
315 | /** | |
316 | * Removes the listeners that are registered when mousedown is triggered. | |
317 | */ | |
318 | private _removeMouseDownListeners(): void { | |
319 | this._rowContainer.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener); | |
320 | this._rowContainer.ownerDocument.removeEventListener('mouseup', this._mouseUpListener); | |
321 | clearInterval(this._dragScrollIntervalTimer); | |
322 | this._dragScrollIntervalTimer = null; | |
9f271de8 DI |
323 | } |
324 | ||
d0b603d0 DI |
325 | /** |
326 | * Performs a shift click, setting the selection end position to the mouse | |
327 | * position. | |
328 | * @param event The mouse event. | |
329 | */ | |
db8ded2a DI |
330 | private _onShiftClick(event: MouseEvent): void { |
331 | if (this._model.selectionStart) { | |
332 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
333 | } | |
334 | } | |
335 | ||
d0b603d0 DI |
336 | /** |
337 | * Performs a single click, resetting relevant state and setting the selection | |
338 | * start position. | |
339 | * @param event The mouse event. | |
340 | */ | |
9f271de8 | 341 | private _onSingleClick(event: MouseEvent): void { |
f7d6ab5f DI |
342 | this._model.selectionStartLength = 0; |
343 | this._model.isSelectAllActive = false; | |
5bc11121 | 344 | this._isLineSelectModeActive = false; |
f7d6ab5f DI |
345 | this._model.selectionStart = this._getMouseBufferCoords(event); |
346 | if (this._model.selectionStart) { | |
347 | this._model.selectionEnd = null; | |
54e7f65d DI |
348 | // If the mouse is over the second half of a wide character, adjust the |
349 | // selection to cover the whole character | |
350 | const char = this._buffer.get(this._model.selectionStart[1])[this._model.selectionStart[0]]; | |
351 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { | |
352 | this._model.selectionStart[0]++; | |
353 | } | |
70fda994 DI |
354 | } |
355 | } | |
356 | ||
d0b603d0 DI |
357 | /** |
358 | * Performs a double click, selecting the current work. | |
359 | * @param event The mouse event. | |
360 | */ | |
9f271de8 DI |
361 | private _onDoubleClick(event: MouseEvent): void { |
362 | const coords = this._getMouseBufferCoords(event); | |
363 | if (coords) { | |
364 | this._selectWordAt(coords); | |
365 | } | |
366 | } | |
367 | ||
d0b603d0 DI |
368 | /** |
369 | * Performs a triple click, selecting the current line and activating line | |
370 | * select mode. | |
371 | * @param event The mouse event. | |
372 | */ | |
9f271de8 DI |
373 | private _onTripleClick(event: MouseEvent): void { |
374 | const coords = this._getMouseBufferCoords(event); | |
375 | if (coords) { | |
5bc11121 | 376 | this._isLineSelectModeActive = true; |
9f271de8 DI |
377 | this._selectLineAt(coords[1]); |
378 | } | |
379 | } | |
380 | ||
d0b603d0 DI |
381 | /** |
382 | * Sets the number of clicks for the current mousedown event based on the time | |
383 | * and position of the last mousedown event. | |
384 | * @param event The mouse event. | |
385 | */ | |
f380153f | 386 | private _setMouseClickCount(event: MouseEvent): void { |
9f271de8 | 387 | let currentTime = (new Date()).getTime(); |
f380153f | 388 | if (currentTime - this._lastMouseDownTime > CLEAR_MOUSE_DOWN_TIME || this._distanceFromLastMousePosition(event) > CLEAR_MOUSE_DISTANCE) { |
9f271de8 | 389 | this._clickCount = 0; |
d3865ad2 DI |
390 | } |
391 | this._lastMouseDownTime = currentTime; | |
f380153f | 392 | this._lastMousePosition = [event.pageX, event.pageY]; |
9f271de8 | 393 | this._clickCount++; |
f380153f | 394 | } |
9f271de8 | 395 | |
d0b603d0 DI |
396 | /** |
397 | * Gets the maximum number of pixels in each direction the mouse has moved. | |
398 | * @param event The mouse event. | |
399 | */ | |
f380153f DI |
400 | private _distanceFromLastMousePosition(event: MouseEvent): number { |
401 | const result = Math.max( | |
402 | Math.abs(this._lastMousePosition[0] - event.pageX), | |
403 | Math.abs(this._lastMousePosition[1] - event.pageY)); | |
404 | return result; | |
9f271de8 DI |
405 | } |
406 | ||
e63fdf58 DI |
407 | /** |
408 | * Handles the mousemove event when the mouse button is down, recording the | |
409 | * end of the selection and refreshing the selection. | |
410 | * @param event The mousemove event. | |
411 | */ | |
70fda994 | 412 | private _onMouseMove(event: MouseEvent) { |
2621be81 DI |
413 | // Record the previous position so we know whether to redraw the selection |
414 | // at the end. | |
415 | const previousSelectionEnd = this._model.selectionEnd ? [this._model.selectionEnd[0], this._model.selectionEnd[1]] : null; | |
416 | ||
417 | // Set the initial selection end based on the mouse coordinates | |
418 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
419 | ||
420 | // Select the entire line if line select mode is active. | |
5bc11121 | 421 | if (this._isLineSelectModeActive) { |
5bc11121 DI |
422 | if (this._model.selectionEnd[1] < this._model.selectionStart[1]) { |
423 | this._model.selectionEnd[0] = 0; | |
424 | } else { | |
425 | this._model.selectionEnd[0] = this._terminal.cols; | |
426 | } | |
5bc11121 DI |
427 | } |
428 | ||
2621be81 | 429 | // Determine the amount of scrolling that will happen. |
0dc3dd03 | 430 | this._dragScrollAmount = this._getMouseEventScrollAmount(event); |
2621be81 | 431 | |
0dc3dd03 | 432 | // If the cursor was above or below the viewport, make sure it's at the |
2621be81 | 433 | // start or end of the viewport respectively. |
0dc3dd03 | 434 | if (this._dragScrollAmount > 0) { |
f7d6ab5f | 435 | this._model.selectionEnd[0] = this._terminal.cols - 1; |
0dc3dd03 | 436 | } else if (this._dragScrollAmount < 0) { |
f7d6ab5f | 437 | this._model.selectionEnd[0] = 0; |
0dc3dd03 | 438 | } |
54e7f65d DI |
439 | |
440 | // If the character is a wide character include the cell to the right in the | |
5bc11121 DI |
441 | // selection. Note that selections at the very end of the line will never |
442 | // have a character. | |
71477874 DI |
443 | if (this._model.selectionEnd[1] < this._buffer.length) { |
444 | const char = this._buffer.get(this._model.selectionEnd[1])[this._model.selectionEnd[0]]; | |
445 | if (char && char[2] === 0) { | |
446 | this._model.selectionEnd[0]++; | |
447 | } | |
54e7f65d DI |
448 | } |
449 | ||
2621be81 DI |
450 | // Only draw here if the selection changes. |
451 | if (!previousSelectionEnd || | |
452 | previousSelectionEnd[0] !== this._model.selectionEnd[0] || | |
453 | previousSelectionEnd[1] !== this._model.selectionEnd[1]) { | |
454 | this.refresh(); | |
455 | } | |
70fda994 DI |
456 | } |
457 | ||
d0b603d0 DI |
458 | /** |
459 | * The callback that occurs every DRAG_SCROLL_INTERVAL ms that does the | |
460 | * scrolling of the viewport. | |
461 | */ | |
0dc3dd03 DI |
462 | private _dragScroll() { |
463 | if (this._dragScrollAmount) { | |
464 | this._terminal.scrollDisp(this._dragScrollAmount, false); | |
465 | // Re-evaluate selection | |
466 | if (this._dragScrollAmount > 0) { | |
f7d6ab5f | 467 | this._model.selectionEnd = [this._terminal.cols - 1, this._terminal.ydisp + this._terminal.rows]; |
0dc3dd03 | 468 | } else { |
f7d6ab5f | 469 | this._model.selectionEnd = [0, this._terminal.ydisp]; |
0dc3dd03 DI |
470 | } |
471 | this.refresh(); | |
472 | } | |
473 | } | |
474 | ||
e63fdf58 | 475 | /** |
b8129910 | 476 | * Handles the mouseup event, removing the mousedown listeners. |
e63fdf58 DI |
477 | * @param event The mouseup event. |
478 | */ | |
70fda994 | 479 | private _onMouseUp(event: MouseEvent) { |
b8129910 | 480 | this._removeMouseDownListeners(); |
70fda994 | 481 | } |
597c6939 | 482 | |
cb6533f3 DI |
483 | /** |
484 | * Converts a viewport column to the character index on the buffer line, the | |
485 | * latter takes into account wide characters. | |
d3865ad2 | 486 | * @param coords The coordinates to find the 2 index for. |
cb6533f3 | 487 | */ |
11cec31d | 488 | private _convertViewportColToCharacterIndex(bufferLine: any, coords: [number, number]): number { |
cb6533f3 | 489 | let charIndex = coords[0]; |
d9991b25 | 490 | for (let i = 0; coords[0] >= i; i++) { |
11cec31d | 491 | const char = bufferLine[i]; |
cb6533f3 DI |
492 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { |
493 | charIndex--; | |
494 | } | |
495 | } | |
496 | return charIndex; | |
497 | } | |
498 | ||
597c6939 DI |
499 | /** |
500 | * Selects the word at the coordinates specified. Words are defined as all | |
501 | * non-whitespace characters. | |
502 | * @param coords The coordinates to get the word at. | |
503 | */ | |
a53a0acd | 504 | protected _selectWordAt(coords: [number, number]): void { |
11cec31d DI |
505 | const bufferLine = this._buffer.get(coords[1]); |
506 | const line = this._translateBufferLineToString(bufferLine, false); | |
cb6533f3 DI |
507 | |
508 | // Get actual index, taking into consideration wide characters | |
11cec31d | 509 | let endIndex = this._convertViewportColToCharacterIndex(bufferLine, coords); |
cb6533f3 | 510 | let startIndex = endIndex; |
cb6533f3 DI |
511 | |
512 | // Record offset to be used later | |
513 | const charOffset = coords[0] - startIndex; | |
11cec31d DI |
514 | let leftWideCharCount = 0; |
515 | let rightWideCharCount = 0; | |
cb6533f3 | 516 | |
cb6533f3 | 517 | if (line.charAt(startIndex) === ' ') { |
11cec31d | 518 | // Expand until non-whitespace is hit |
cb6533f3 DI |
519 | while (startIndex > 0 && line.charAt(startIndex - 1) === ' ') { |
520 | startIndex--; | |
521 | } | |
522 | while (endIndex < line.length && line.charAt(endIndex + 1) === ' ') { | |
523 | endIndex++; | |
524 | } | |
cb6533f3 | 525 | } else { |
11cec31d DI |
526 | // Expand until whitespace is hit. This algorithm works by scanning left |
527 | // and right from the starting position, keeping both the index format | |
528 | // (line) and the column format (bufferLine) in sync. When a wide | |
529 | // character is hit, it is recorded and the column index is adjusted. | |
d9991b25 DI |
530 | let startCol = coords[0]; |
531 | let endCol = coords[0]; | |
532 | // Consider the initial position, skip it and increment the wide char | |
533 | // variable | |
11cec31d | 534 | if (bufferLine[startCol][LINE_DATA_WIDTH_INDEX] === 0) { |
d9991b25 DI |
535 | leftWideCharCount++; |
536 | startCol--; | |
537 | } | |
11cec31d | 538 | if (bufferLine[endCol][LINE_DATA_WIDTH_INDEX] === 2) { |
d9991b25 DI |
539 | rightWideCharCount++; |
540 | endCol++; | |
541 | } | |
cb6533f3 DI |
542 | // Expand the string in both directions until a space is hit |
543 | while (startIndex > 0 && line.charAt(startIndex - 1) !== ' ') { | |
11cec31d DI |
544 | if (bufferLine[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { |
545 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
546 | leftWideCharCount++; |
547 | startCol--; | |
cb6533f3 DI |
548 | } |
549 | startIndex--; | |
d9991b25 | 550 | startCol--; |
cb6533f3 | 551 | } |
d3865ad2 | 552 | while (endIndex + 1 < line.length && line.charAt(endIndex + 1) !== ' ') { |
11cec31d DI |
553 | if (bufferLine[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { |
554 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
555 | rightWideCharCount++; |
556 | endCol++; | |
cb6533f3 DI |
557 | } |
558 | endIndex++; | |
d9991b25 | 559 | endCol++; |
cb6533f3 | 560 | } |
597c6939 | 561 | } |
11cec31d DI |
562 | |
563 | // Record the resulting selection | |
d9991b25 | 564 | this._model.selectionStart = [startIndex + charOffset - leftWideCharCount, coords[1]]; |
f61d852f | 565 | this._model.selectionStartLength = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols); |
597c6939 | 566 | } |
9f271de8 | 567 | |
d0b603d0 DI |
568 | /** |
569 | * Selects the line specified. | |
570 | * @param line The line index. | |
571 | */ | |
fd91c5e1 | 572 | protected _selectLineAt(line: number): void { |
f7d6ab5f DI |
573 | this._model.selectionStart = [0, line]; |
574 | this._model.selectionStartLength = this._terminal.cols; | |
9f271de8 | 575 | } |
70fda994 | 576 | } |