]>
Commit | Line | Data |
---|---|---|
70fda994 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | ||
dc165175 DI |
5 | import * as Mouse from './utils/Mouse'; |
6 | import * as Browser from './utils/Browser'; | |
70fda994 DI |
7 | import { CharMeasure } from './utils/CharMeasure'; |
8 | import { CircularList } from './utils/CircularList'; | |
b594407c | 9 | import { EventEmitter } from './EventEmitter'; |
59f75555 | 10 | import { ITerminal, ICircularList } from './Interfaces'; |
f7d6ab5f | 11 | import { SelectionModel } from './SelectionModel'; |
b9571307 | 12 | import { translateBufferLineToString } from './utils/BufferLine'; |
70fda994 | 13 | |
0dc3dd03 DI |
14 | /** |
15 | * The number of pixels the mouse needs to be above or below the viewport in | |
16 | * order to scroll at the maximum speed. | |
17 | */ | |
b8129910 | 18 | const DRAG_SCROLL_MAX_THRESHOLD = 50; |
0dc3dd03 DI |
19 | |
20 | /** | |
21 | * The maximum scrolling speed | |
22 | */ | |
b8129910 | 23 | const DRAG_SCROLL_MAX_SPEED = 15; |
0dc3dd03 DI |
24 | |
25 | /** | |
26 | * The number of milliseconds between drag scroll updates. | |
27 | */ | |
b8129910 | 28 | const DRAG_SCROLL_INTERVAL = 50; |
0dc3dd03 | 29 | |
7f2e94c3 DI |
30 | /** |
31 | * A string containing all characters that are considered word separated by the | |
32 | * double click to select work logic. | |
33 | */ | |
ca03f892 | 34 | const WORD_SEPARATORS = ' ()[]{}\'"'; |
7f2e94c3 | 35 | |
2621be81 DI |
36 | // TODO: Move these constants elsewhere, they belong in a buffer or buffer |
37 | // data/line class. | |
54e7f65d DI |
38 | const LINE_DATA_CHAR_INDEX = 1; |
39 | const LINE_DATA_WIDTH_INDEX = 2; | |
40 | ||
2012c8c9 DI |
41 | const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160); |
42 | const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g'); | |
43 | ||
320fb55d DI |
44 | /** |
45 | * Represents a position of a word on a line. | |
46 | */ | |
4b170ca4 DI |
47 | interface IWordPosition { |
48 | start: number; | |
49 | length: number; | |
50 | } | |
51 | ||
320fb55d DI |
52 | /** |
53 | * A selection mode, this drives how the selection behaves on mouse move. | |
54 | */ | |
55 | enum SelectionMode { | |
56 | NORMAL, | |
57 | WORD, | |
58 | LINE | |
59 | } | |
60 | ||
6075498c DI |
61 | /** |
62 | * A class that manages the selection of the terminal. With help from | |
63 | * SelectionModel, SelectionManager handles with all logic associated with | |
64 | * dealing with the selection, including handling mouse interaction, wide | |
65 | * characters and fetching the actual text within the selection. Rendering is | |
66 | * not handled by the SelectionManager but a 'refresh' event is fired when the | |
67 | * selection is ready to be redrawn. | |
68 | */ | |
b594407c | 69 | export class SelectionManager extends EventEmitter { |
fd91c5e1 | 70 | protected _model: SelectionModel; |
9f271de8 DI |
71 | |
72 | /** | |
73 | * The amount to scroll every drag scroll update (depends on how far the mouse | |
74 | * drag is above or below the terminal). | |
75 | */ | |
0dc3dd03 | 76 | private _dragScrollAmount: number; |
70fda994 | 77 | |
2621be81 | 78 | /** |
320fb55d | 79 | * The current selection mode. |
2621be81 | 80 | */ |
320fb55d | 81 | private _activeSelectionMode: SelectionMode; |
5bc11121 | 82 | |
d0b603d0 DI |
83 | /** |
84 | * A setInterval timer that is active while the mouse is down whose callback | |
85 | * scrolls the viewport when necessary. | |
86 | */ | |
87 | private _dragScrollIntervalTimer: NodeJS.Timer; | |
88 | ||
b81c165b DI |
89 | /** |
90 | * The animation frame ID used for refreshing the selection. | |
91 | */ | |
13c401cb DI |
92 | private _refreshAnimationFrame: number; |
93 | ||
80a83ff8 JM |
94 | /** |
95 | * Whether selection is enabled. | |
96 | */ | |
97 | private _enabled = true; | |
98 | ||
70fda994 | 99 | private _mouseMoveListener: EventListener; |
ab40908f | 100 | private _mouseUpListener: EventListener; |
70fda994 | 101 | |
ad3ae67e DI |
102 | constructor( |
103 | private _terminal: ITerminal, | |
59f75555 | 104 | private _buffer: ICircularList<[number, string, number][]>, |
ad3ae67e | 105 | private _rowContainer: HTMLElement, |
ad3ae67e DI |
106 | private _charMeasure: CharMeasure |
107 | ) { | |
b594407c | 108 | super(); |
ab40908f DI |
109 | this._initListeners(); |
110 | this.enable(); | |
9f271de8 | 111 | |
f7d6ab5f | 112 | this._model = new SelectionModel(_terminal); |
320fb55d | 113 | this._activeSelectionMode = SelectionMode.NORMAL; |
70fda994 DI |
114 | } |
115 | ||
d0b603d0 DI |
116 | /** |
117 | * Initializes listener variables. | |
118 | */ | |
ab40908f | 119 | private _initListeners() { |
70fda994 | 120 | this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event); |
ab40908f | 121 | this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event); |
80a83ff8 JM |
122 | |
123 | this._rowContainer.addEventListener('mousedown', event => this._onMouseDown(<MouseEvent>event)); | |
124 | ||
125 | // Only adjust the selection on trim, shiftElements is rarely used (only in | |
126 | // reverseIndex) and delete in a splice is only ever used when the same | |
127 | // number of elements was just added. Given this is could actually be | |
128 | // beneficial to leave the selection as is for these cases. | |
129 | this._buffer.on('trim', (amount: number) => this._onTrim(amount)); | |
ab40908f | 130 | } |
70fda994 | 131 | |
ab40908f DI |
132 | /** |
133 | * Disables the selection manager. This is useful for when terminal mouse | |
134 | * are enabled. | |
135 | */ | |
136 | public disable() { | |
4405f5e1 | 137 | this.clearSelection(); |
80a83ff8 | 138 | this._enabled = false; |
ab40908f DI |
139 | } |
140 | ||
141 | /** | |
142 | * Enable the selection manager. | |
143 | */ | |
144 | public enable() { | |
80a83ff8 | 145 | this._enabled = true; |
70fda994 DI |
146 | } |
147 | ||
9a86eeb3 DI |
148 | /** |
149 | * Sets the active buffer, this should be called when the alt buffer is | |
150 | * switched in or out. | |
151 | * @param buffer The active buffer. | |
152 | */ | |
59f75555 | 153 | public setBuffer(buffer: ICircularList<[number, string, number][]>): void { |
9a86eeb3 | 154 | this._buffer = buffer; |
645a5920 | 155 | this.clearSelection(); |
9a86eeb3 DI |
156 | } |
157 | ||
280b698a DI |
158 | public get selectionStart(): [number, number] { return this._model.finalSelectionStart; } |
159 | public get selectionEnd(): [number, number] { return this._model.finalSelectionEnd; } | |
160 | ||
1343b83f DI |
161 | /** |
162 | * Gets whether there is an active text selection. | |
163 | */ | |
164 | public get hasSelection(): boolean { | |
7b469407 DI |
165 | const start = this._model.finalSelectionStart; |
166 | const end = this._model.finalSelectionEnd; | |
167 | if (!start || !end) { | |
168 | return false; | |
169 | } | |
170 | return start[0] !== end[0] || start[1] !== end[1]; | |
1343b83f DI |
171 | } |
172 | ||
9f271de8 DI |
173 | /** |
174 | * Gets the text currently selected. | |
175 | */ | |
70fda994 | 176 | public get selectionText(): string { |
f7d6ab5f DI |
177 | const start = this._model.finalSelectionStart; |
178 | const end = this._model.finalSelectionEnd; | |
e29ab294 | 179 | if (!start || !end) { |
293ae18a | 180 | return ''; |
70fda994 | 181 | } |
43c796a7 | 182 | |
43c796a7 | 183 | // Get first row |
293ae18a | 184 | const startRowEndCol = start[1] === end[1] ? end[0] : null; |
32b34cbe | 185 | let result: string[] = []; |
b9571307 | 186 | result.push(translateBufferLineToString(this._buffer.get(start[1]), true, start[0], startRowEndCol)); |
43c796a7 DI |
187 | |
188 | // Get middle rows | |
293ae18a | 189 | for (let i = start[1] + 1; i <= end[1] - 1; i++) { |
346b5177 | 190 | const bufferLine = this._buffer.get(i); |
53602775 | 191 | const lineText = translateBufferLineToString(bufferLine, true); |
59f75555 | 192 | if ((<any>bufferLine).isWrapped) { |
346b5177 DI |
193 | result[result.length - 1] += lineText; |
194 | } else { | |
195 | result.push(lineText); | |
196 | } | |
32b34cbe | 197 | } |
43c796a7 DI |
198 | |
199 | // Get final row | |
293ae18a | 200 | if (start[1] !== end[1]) { |
346b5177 | 201 | const bufferLine = this._buffer.get(end[1]); |
53602775 | 202 | const lineText = translateBufferLineToString(bufferLine, true, 0, end[0]); |
59f75555 | 203 | if ((<any>bufferLine).isWrapped) { |
346b5177 DI |
204 | result[result.length - 1] += lineText; |
205 | } else { | |
206 | result.push(lineText); | |
207 | } | |
32b34cbe | 208 | } |
2b243182 | 209 | |
2012c8c9 DI |
210 | // Format string by replacing non-breaking space chars with regular spaces |
211 | // and joining the array into a multi-line string. | |
212 | const formattedResult = result.map(line => { | |
213 | return line.replace(ALL_NON_BREAKING_SPACE_REGEX, ' '); | |
cfe22e6f | 214 | }).join(Browser.isMSWindows ? '\r\n' : '\n'); |
2012c8c9 DI |
215 | |
216 | return formattedResult; | |
32b34cbe DI |
217 | } |
218 | ||
5d33727a DI |
219 | /** |
220 | * Clears the current terminal selection. | |
221 | */ | |
222 | public clearSelection(): void { | |
4405f5e1 | 223 | this._model.clearSelection(); |
5d33727a DI |
224 | this._removeMouseDownListeners(); |
225 | this.refresh(); | |
226 | } | |
227 | ||
207c4cf9 | 228 | /** |
13c401cb | 229 | * Queues a refresh, redrawing the selection on the next opportunity. |
63d63c27 DI |
230 | * @param isNewSelection Whether the selection should be registered as a new |
231 | * selection on Linux. | |
207c4cf9 | 232 | */ |
63d63c27 | 233 | public refresh(isNewSelection?: boolean): void { |
8811d96a | 234 | // Queue the refresh for the renderer |
13c401cb DI |
235 | if (!this._refreshAnimationFrame) { |
236 | this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh()); | |
237 | } | |
8811d96a | 238 | |
dc165175 DI |
239 | // If the platform is Linux and the refresh call comes from a mouse event, |
240 | // we need to update the selection for middle click to paste selection. | |
63d63c27 | 241 | if (Browser.isLinux && isNewSelection) { |
dc165175 DI |
242 | const selectionText = this.selectionText; |
243 | if (selectionText.length) { | |
244 | this.emit('newselection', this.selectionText); | |
245 | } | |
8811d96a | 246 | } |
13c401cb DI |
247 | } |
248 | ||
249 | /** | |
250 | * Fires the refresh event, causing consumers to pick it up and redraw the | |
251 | * selection state. | |
252 | */ | |
253 | private _refresh(): void { | |
254 | this._refreshAnimationFrame = null; | |
f7d6ab5f | 255 | this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd }); |
25152e44 DI |
256 | } |
257 | ||
258 | /** | |
259 | * Selects all text within the terminal. | |
260 | */ | |
261 | public selectAll(): void { | |
f7d6ab5f | 262 | this._model.isSelectAllActive = true; |
25152e44 | 263 | this.refresh(); |
207c4cf9 DI |
264 | } |
265 | ||
266 | /** | |
267 | * Handle the buffer being trimmed, adjust the selection position. | |
268 | * @param amount The amount the buffer is being trimmed. | |
269 | */ | |
70fda994 | 270 | private _onTrim(amount: number) { |
f7d6ab5f DI |
271 | const needsRefresh = this._model.onTrim(amount); |
272 | if (needsRefresh) { | |
207c4cf9 | 273 | this.refresh(); |
207c4cf9 | 274 | } |
70fda994 DI |
275 | } |
276 | ||
d0b603d0 DI |
277 | /** |
278 | * Gets the 0-based [x, y] buffer coordinates of the current mouse event. | |
279 | * @param event The mouse event. | |
280 | */ | |
0dc3dd03 | 281 | private _getMouseBufferCoords(event: MouseEvent): [number, number] { |
b05814e6 | 282 | const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows, true); |
e56f710c DI |
283 | if (!coords) { |
284 | return null; | |
285 | } | |
286 | ||
ad3ae67e DI |
287 | // Convert to 0-based |
288 | coords[0]--; | |
289 | coords[1]--; | |
290 | // Convert viewport coords to buffer coords | |
bbafdd3d | 291 | coords[1] += this._terminal.buffer.ydisp; |
ad3ae67e | 292 | return coords; |
b36d8780 DI |
293 | } |
294 | ||
d0b603d0 DI |
295 | /** |
296 | * Gets the amount the viewport should be scrolled based on how far out of the | |
297 | * terminal the mouse is. | |
298 | * @param event The mouse event. | |
299 | */ | |
0dc3dd03 DI |
300 | private _getMouseEventScrollAmount(event: MouseEvent): number { |
301 | let offset = Mouse.getCoordsRelativeToElement(event, this._rowContainer)[1]; | |
302 | const terminalHeight = this._terminal.rows * this._charMeasure.height; | |
303 | if (offset >= 0 && offset <= terminalHeight) { | |
304 | return 0; | |
305 | } | |
306 | if (offset > terminalHeight) { | |
307 | offset -= terminalHeight; | |
308 | } | |
309 | ||
310 | offset = Math.min(Math.max(offset, -DRAG_SCROLL_MAX_THRESHOLD), DRAG_SCROLL_MAX_THRESHOLD); | |
311 | offset /= DRAG_SCROLL_MAX_THRESHOLD; | |
312 | return (offset / Math.abs(offset)) + Math.round(offset * (DRAG_SCROLL_MAX_SPEED - 1)); | |
313 | } | |
314 | ||
e63fdf58 DI |
315 | /** |
316 | * Handles te mousedown event, setting up for a new selection. | |
317 | * @param event The mousedown event. | |
318 | */ | |
70fda994 | 319 | private _onMouseDown(event: MouseEvent) { |
80a83ff8 JM |
320 | // Ignore this event when selection is disabled |
321 | if (!this._enabled) { | |
322 | if (!event.altKey) { | |
323 | return; | |
324 | } else { | |
325 | ||
326 | // Don't send the mouse down event to the current process | |
327 | event.stopPropagation(); | |
328 | } | |
329 | } | |
330 | ||
0dc3dd03 DI |
331 | // Only action the primary button |
332 | if (event.button !== 0) { | |
333 | return; | |
334 | } | |
335 | ||
2a0f44b0 DI |
336 | // Tell the browser not to start a regular selection |
337 | event.preventDefault(); | |
338 | ||
b8129910 DI |
339 | // Reset drag scroll state |
340 | this._dragScrollAmount = 0; | |
341 | ||
db8ded2a DI |
342 | if (event.shiftKey) { |
343 | this._onShiftClick(event); | |
344 | } else { | |
9bd52820 | 345 | if (event.detail === 1) { |
80a83ff8 | 346 | this._onSingleClick(event); |
9bd52820 | 347 | } else if (event.detail === 2) { |
80a83ff8 | 348 | this._onDoubleClick(event); |
9bd52820 | 349 | } else if (event.detail === 3) { |
80a83ff8 | 350 | this._onTripleClick(event); |
db8ded2a | 351 | } |
9f271de8 | 352 | } |
e29ab294 | 353 | |
b8129910 | 354 | this._addMouseDownListeners(); |
8811d96a | 355 | this.refresh(true); |
b8129910 DI |
356 | } |
357 | ||
358 | /** | |
359 | * Adds listeners when mousedown is triggered. | |
360 | */ | |
361 | private _addMouseDownListeners(): void { | |
e29ab294 DI |
362 | // Listen on the document so that dragging outside of viewport works |
363 | this._rowContainer.ownerDocument.addEventListener('mousemove', this._mouseMoveListener); | |
364 | this._rowContainer.ownerDocument.addEventListener('mouseup', this._mouseUpListener); | |
2621be81 | 365 | this._dragScrollIntervalTimer = setInterval(() => this._dragScroll(), DRAG_SCROLL_INTERVAL); |
b8129910 DI |
366 | } |
367 | ||
368 | /** | |
369 | * Removes the listeners that are registered when mousedown is triggered. | |
370 | */ | |
371 | private _removeMouseDownListeners(): void { | |
372 | this._rowContainer.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener); | |
373 | this._rowContainer.ownerDocument.removeEventListener('mouseup', this._mouseUpListener); | |
374 | clearInterval(this._dragScrollIntervalTimer); | |
375 | this._dragScrollIntervalTimer = null; | |
9f271de8 DI |
376 | } |
377 | ||
d0b603d0 DI |
378 | /** |
379 | * Performs a shift click, setting the selection end position to the mouse | |
380 | * position. | |
381 | * @param event The mouse event. | |
382 | */ | |
db8ded2a DI |
383 | private _onShiftClick(event: MouseEvent): void { |
384 | if (this._model.selectionStart) { | |
385 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
386 | } | |
387 | } | |
388 | ||
d0b603d0 DI |
389 | /** |
390 | * Performs a single click, resetting relevant state and setting the selection | |
391 | * start position. | |
392 | * @param event The mouse event. | |
393 | */ | |
9f271de8 | 394 | private _onSingleClick(event: MouseEvent): void { |
f7d6ab5f DI |
395 | this._model.selectionStartLength = 0; |
396 | this._model.isSelectAllActive = false; | |
320fb55d | 397 | this._activeSelectionMode = SelectionMode.NORMAL; |
e56f710c DI |
398 | |
399 | // Initialize the new selection | |
f7d6ab5f | 400 | this._model.selectionStart = this._getMouseBufferCoords(event); |
e56f710c DI |
401 | if (!this._model.selectionStart) { |
402 | return; | |
403 | } | |
404 | this._model.selectionEnd = null; | |
405 | ||
406 | // Ensure the line exists | |
407 | const line = this._buffer.get(this._model.selectionStart[1]); | |
408 | if (!line) { | |
409 | return; | |
410 | } | |
411 | ||
412 | // If the mouse is over the second half of a wide character, adjust the | |
413 | // selection to cover the whole character | |
414 | const char = line[this._model.selectionStart[0]]; | |
415 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { | |
416 | this._model.selectionStart[0]++; | |
70fda994 DI |
417 | } |
418 | } | |
419 | ||
d0b603d0 DI |
420 | /** |
421 | * Performs a double click, selecting the current work. | |
422 | * @param event The mouse event. | |
423 | */ | |
9f271de8 DI |
424 | private _onDoubleClick(event: MouseEvent): void { |
425 | const coords = this._getMouseBufferCoords(event); | |
426 | if (coords) { | |
320fb55d | 427 | this._activeSelectionMode = SelectionMode.WORD; |
9f271de8 DI |
428 | this._selectWordAt(coords); |
429 | } | |
430 | } | |
431 | ||
d0b603d0 DI |
432 | /** |
433 | * Performs a triple click, selecting the current line and activating line | |
434 | * select mode. | |
435 | * @param event The mouse event. | |
436 | */ | |
9f271de8 DI |
437 | private _onTripleClick(event: MouseEvent): void { |
438 | const coords = this._getMouseBufferCoords(event); | |
439 | if (coords) { | |
320fb55d | 440 | this._activeSelectionMode = SelectionMode.LINE; |
9f271de8 DI |
441 | this._selectLineAt(coords[1]); |
442 | } | |
443 | } | |
444 | ||
e63fdf58 DI |
445 | /** |
446 | * Handles the mousemove event when the mouse button is down, recording the | |
447 | * end of the selection and refreshing the selection. | |
448 | * @param event The mousemove event. | |
449 | */ | |
70fda994 | 450 | private _onMouseMove(event: MouseEvent) { |
2621be81 DI |
451 | // Record the previous position so we know whether to redraw the selection |
452 | // at the end. | |
453 | const previousSelectionEnd = this._model.selectionEnd ? [this._model.selectionEnd[0], this._model.selectionEnd[1]] : null; | |
454 | ||
455 | // Set the initial selection end based on the mouse coordinates | |
456 | this._model.selectionEnd = this._getMouseBufferCoords(event); | |
e56f710c DI |
457 | if (!this._model.selectionEnd) { |
458 | this.refresh(true); | |
459 | return; | |
460 | } | |
2621be81 DI |
461 | |
462 | // Select the entire line if line select mode is active. | |
320fb55d | 463 | if (this._activeSelectionMode === SelectionMode.LINE) { |
5bc11121 DI |
464 | if (this._model.selectionEnd[1] < this._model.selectionStart[1]) { |
465 | this._model.selectionEnd[0] = 0; | |
466 | } else { | |
467 | this._model.selectionEnd[0] = this._terminal.cols; | |
468 | } | |
320fb55d | 469 | } else if (this._activeSelectionMode === SelectionMode.WORD) { |
4b170ca4 | 470 | this._selectToWordAt(this._model.selectionEnd); |
5bc11121 DI |
471 | } |
472 | ||
2621be81 | 473 | // Determine the amount of scrolling that will happen. |
0dc3dd03 | 474 | this._dragScrollAmount = this._getMouseEventScrollAmount(event); |
2621be81 | 475 | |
0dc3dd03 | 476 | // If the cursor was above or below the viewport, make sure it's at the |
2621be81 | 477 | // start or end of the viewport respectively. |
0dc3dd03 | 478 | if (this._dragScrollAmount > 0) { |
f7d6ab5f | 479 | this._model.selectionEnd[0] = this._terminal.cols - 1; |
0dc3dd03 | 480 | } else if (this._dragScrollAmount < 0) { |
f7d6ab5f | 481 | this._model.selectionEnd[0] = 0; |
0dc3dd03 | 482 | } |
54e7f65d DI |
483 | |
484 | // If the character is a wide character include the cell to the right in the | |
5bc11121 DI |
485 | // selection. Note that selections at the very end of the line will never |
486 | // have a character. | |
71477874 DI |
487 | if (this._model.selectionEnd[1] < this._buffer.length) { |
488 | const char = this._buffer.get(this._model.selectionEnd[1])[this._model.selectionEnd[0]]; | |
489 | if (char && char[2] === 0) { | |
490 | this._model.selectionEnd[0]++; | |
491 | } | |
54e7f65d DI |
492 | } |
493 | ||
2621be81 DI |
494 | // Only draw here if the selection changes. |
495 | if (!previousSelectionEnd || | |
80a83ff8 JM |
496 | previousSelectionEnd[0] !== this._model.selectionEnd[0] || |
497 | previousSelectionEnd[1] !== this._model.selectionEnd[1]) { | |
dc165175 | 498 | this.refresh(true); |
2621be81 | 499 | } |
70fda994 DI |
500 | } |
501 | ||
d0b603d0 DI |
502 | /** |
503 | * The callback that occurs every DRAG_SCROLL_INTERVAL ms that does the | |
504 | * scrolling of the viewport. | |
505 | */ | |
0dc3dd03 DI |
506 | private _dragScroll() { |
507 | if (this._dragScrollAmount) { | |
508 | this._terminal.scrollDisp(this._dragScrollAmount, false); | |
509 | // Re-evaluate selection | |
510 | if (this._dragScrollAmount > 0) { | |
bbafdd3d | 511 | this._model.selectionEnd = [this._terminal.cols - 1, this._terminal.buffer.ydisp + this._terminal.rows]; |
0dc3dd03 | 512 | } else { |
bbafdd3d | 513 | this._model.selectionEnd = [0, this._terminal.buffer.ydisp]; |
0dc3dd03 DI |
514 | } |
515 | this.refresh(); | |
516 | } | |
517 | } | |
518 | ||
e63fdf58 | 519 | /** |
b8129910 | 520 | * Handles the mouseup event, removing the mousedown listeners. |
e63fdf58 DI |
521 | * @param event The mouseup event. |
522 | */ | |
70fda994 | 523 | private _onMouseUp(event: MouseEvent) { |
b8129910 | 524 | this._removeMouseDownListeners(); |
70fda994 | 525 | } |
597c6939 | 526 | |
cb6533f3 DI |
527 | /** |
528 | * Converts a viewport column to the character index on the buffer line, the | |
529 | * latter takes into account wide characters. | |
d3865ad2 | 530 | * @param coords The coordinates to find the 2 index for. |
cb6533f3 | 531 | */ |
11cec31d | 532 | private _convertViewportColToCharacterIndex(bufferLine: any, coords: [number, number]): number { |
cb6533f3 | 533 | let charIndex = coords[0]; |
d9991b25 | 534 | for (let i = 0; coords[0] >= i; i++) { |
11cec31d | 535 | const char = bufferLine[i]; |
cb6533f3 DI |
536 | if (char[LINE_DATA_WIDTH_INDEX] === 0) { |
537 | charIndex--; | |
538 | } | |
539 | } | |
540 | return charIndex; | |
541 | } | |
542 | ||
8d12881a DI |
543 | public setSelection(col: number, row: number, length: number): void { |
544 | this._model.clearSelection(); | |
545 | this._removeMouseDownListeners(); | |
8d12881a DI |
546 | this._model.selectionStart = [col, row]; |
547 | this._model.selectionStartLength = length; | |
548 | this.refresh(); | |
549 | } | |
550 | ||
597c6939 | 551 | /** |
4b170ca4 | 552 | * Gets positional information for the word at the coordinated specified. |
597c6939 DI |
553 | * @param coords The coordinates to get the word at. |
554 | */ | |
4b170ca4 | 555 | private _getWordAt(coords: [number, number]): IWordPosition { |
11cec31d | 556 | const bufferLine = this._buffer.get(coords[1]); |
e3ded611 DI |
557 | if (!bufferLine) { |
558 | return null; | |
559 | } | |
560 | ||
b9571307 | 561 | const line = translateBufferLineToString(bufferLine, false); |
cb6533f3 DI |
562 | |
563 | // Get actual index, taking into consideration wide characters | |
11cec31d | 564 | let endIndex = this._convertViewportColToCharacterIndex(bufferLine, coords); |
cb6533f3 | 565 | let startIndex = endIndex; |
cb6533f3 DI |
566 | |
567 | // Record offset to be used later | |
568 | const charOffset = coords[0] - startIndex; | |
11cec31d DI |
569 | let leftWideCharCount = 0; |
570 | let rightWideCharCount = 0; | |
cb6533f3 | 571 | |
cb6533f3 | 572 | if (line.charAt(startIndex) === ' ') { |
11cec31d | 573 | // Expand until non-whitespace is hit |
cb6533f3 DI |
574 | while (startIndex > 0 && line.charAt(startIndex - 1) === ' ') { |
575 | startIndex--; | |
576 | } | |
577 | while (endIndex < line.length && line.charAt(endIndex + 1) === ' ') { | |
578 | endIndex++; | |
579 | } | |
cb6533f3 | 580 | } else { |
11cec31d DI |
581 | // Expand until whitespace is hit. This algorithm works by scanning left |
582 | // and right from the starting position, keeping both the index format | |
583 | // (line) and the column format (bufferLine) in sync. When a wide | |
584 | // character is hit, it is recorded and the column index is adjusted. | |
d9991b25 DI |
585 | let startCol = coords[0]; |
586 | let endCol = coords[0]; | |
587 | // Consider the initial position, skip it and increment the wide char | |
588 | // variable | |
11cec31d | 589 | if (bufferLine[startCol][LINE_DATA_WIDTH_INDEX] === 0) { |
d9991b25 DI |
590 | leftWideCharCount++; |
591 | startCol--; | |
592 | } | |
11cec31d | 593 | if (bufferLine[endCol][LINE_DATA_WIDTH_INDEX] === 2) { |
d9991b25 DI |
594 | rightWideCharCount++; |
595 | endCol++; | |
596 | } | |
cb6533f3 | 597 | // Expand the string in both directions until a space is hit |
7f2e94c3 | 598 | while (startIndex > 0 && !this._isCharWordSeparator(line.charAt(startIndex - 1))) { |
11cec31d DI |
599 | if (bufferLine[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { |
600 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
601 | leftWideCharCount++; |
602 | startCol--; | |
cb6533f3 DI |
603 | } |
604 | startIndex--; | |
d9991b25 | 605 | startCol--; |
cb6533f3 | 606 | } |
7f2e94c3 | 607 | while (endIndex + 1 < line.length && !this._isCharWordSeparator(line.charAt(endIndex + 1))) { |
11cec31d DI |
608 | if (bufferLine[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { |
609 | // If the next character is a wide char, record it and skip the column | |
d9991b25 DI |
610 | rightWideCharCount++; |
611 | endCol++; | |
cb6533f3 DI |
612 | } |
613 | endIndex++; | |
d9991b25 | 614 | endCol++; |
cb6533f3 | 615 | } |
597c6939 | 616 | } |
11cec31d | 617 | |
4b170ca4 DI |
618 | const start = startIndex + charOffset - leftWideCharCount; |
619 | const length = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols); | |
80a83ff8 | 620 | return { start, length }; |
4b170ca4 DI |
621 | } |
622 | ||
623 | /** | |
624 | * Selects the word at the coordinates specified. | |
625 | * @param coords The coordinates to get the word at. | |
626 | */ | |
627 | protected _selectWordAt(coords: [number, number]): void { | |
628 | const wordPosition = this._getWordAt(coords); | |
e3ded611 DI |
629 | if (wordPosition) { |
630 | this._model.selectionStart = [wordPosition.start, coords[1]]; | |
631 | this._model.selectionStartLength = wordPosition.length; | |
632 | } | |
4b170ca4 DI |
633 | } |
634 | ||
635 | /** | |
636 | * Sets the selection end to the word at the coordinated specified. | |
637 | * @param coords The coordinates to get the word at. | |
638 | */ | |
639 | private _selectToWordAt(coords: [number, number]): void { | |
640 | const wordPosition = this._getWordAt(coords); | |
e3ded611 DI |
641 | if (wordPosition) { |
642 | this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]]; | |
643 | } | |
597c6939 | 644 | } |
9f271de8 | 645 | |
7f2e94c3 DI |
646 | /** |
647 | * Gets whether the character is considered a word separator by the select | |
648 | * word logic. | |
649 | * @param char The character to check. | |
650 | */ | |
651 | private _isCharWordSeparator(char: string): boolean { | |
652 | return WORD_SEPARATORS.indexOf(char) >= 0; | |
597c6939 | 653 | } |
9f271de8 | 654 | |
d0b603d0 DI |
655 | /** |
656 | * Selects the line specified. | |
657 | * @param line The line index. | |
658 | */ | |
fd91c5e1 | 659 | protected _selectLineAt(line: number): void { |
f7d6ab5f DI |
660 | this._model.selectionStart = [0, line]; |
661 | this._model.selectionStartLength = this._terminal.cols; | |
9f271de8 | 662 | } |
70fda994 | 663 | } |