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