diff --git a/src/SelectionManager.test.ts b/src/SelectionManager.test.ts index e0ff6789..eb9322b6 100644 --- a/src/SelectionManager.test.ts +++ b/src/SelectionManager.test.ts @@ -142,6 +142,49 @@ describe('SelectionManager', () => { selectionManager.selectWordAt([14, 0]); assert.equal(selectionManager.selectionText, 'foo'); }); + it('should select up to non-path characters that are commonly adjacent to paths', () => { + buffer.push(stringToRow(':ab:(cd)[ef]{gh}\'ij"')); + selectionManager.selectWordAt([0, 0]); + assert.equal(selectionManager.selectionText, ':ab'); + selectionManager.selectWordAt([1, 0]); + assert.equal(selectionManager.selectionText, 'ab'); + selectionManager.selectWordAt([2, 0]); + assert.equal(selectionManager.selectionText, 'ab'); + selectionManager.selectWordAt([3, 0]); + assert.equal(selectionManager.selectionText, 'ab:'); + selectionManager.selectWordAt([4, 0]); + assert.equal(selectionManager.selectionText, '(cd'); + selectionManager.selectWordAt([5, 0]); + assert.equal(selectionManager.selectionText, 'cd'); + selectionManager.selectWordAt([6, 0]); + assert.equal(selectionManager.selectionText, 'cd'); + selectionManager.selectWordAt([7, 0]); + assert.equal(selectionManager.selectionText, 'cd)'); + selectionManager.selectWordAt([8, 0]); + assert.equal(selectionManager.selectionText, '[ef'); + selectionManager.selectWordAt([9, 0]); + assert.equal(selectionManager.selectionText, 'ef'); + selectionManager.selectWordAt([10, 0]); + assert.equal(selectionManager.selectionText, 'ef'); + selectionManager.selectWordAt([11, 0]); + assert.equal(selectionManager.selectionText, 'ef]'); + selectionManager.selectWordAt([12, 0]); + assert.equal(selectionManager.selectionText, '{gh'); + selectionManager.selectWordAt([13, 0]); + assert.equal(selectionManager.selectionText, 'gh'); + selectionManager.selectWordAt([14, 0]); + assert.equal(selectionManager.selectionText, 'gh'); + selectionManager.selectWordAt([15, 0]); + assert.equal(selectionManager.selectionText, 'gh}'); + selectionManager.selectWordAt([16, 0]); + assert.equal(selectionManager.selectionText, '\'ij'); + selectionManager.selectWordAt([17, 0]); + assert.equal(selectionManager.selectionText, 'ij'); + selectionManager.selectWordAt([18, 0]); + assert.equal(selectionManager.selectionText, 'ij'); + selectionManager.selectWordAt([19, 0]); + assert.equal(selectionManager.selectionText, 'ij"'); + }); }); describe('_selectLineAt', () => { diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index ac0e4290..a769afd1 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -37,6 +37,12 @@ const CLEAR_MOUSE_DOWN_TIME = 400; */ const CLEAR_MOUSE_DISTANCE = 10; +/** + * A string containing all characters that are considered word separated by the + * double click to select work logic. + */ +const WORD_SEPARATORS = ' ()[]{}:\'"'; + // TODO: Move these constants elsewhere, they belong in a buffer or buffer // data/line class. const LINE_DATA_CHAR_INDEX = 1; @@ -326,7 +332,7 @@ export class SelectionManager extends EventEmitter { * @param event The mouse event. */ private _getMouseBufferCoords(event: MouseEvent): [number, number] { - const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows); + const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows, true); // Convert to 0-based coords[0]--; coords[1]--; @@ -629,7 +635,7 @@ export class SelectionManager extends EventEmitter { endCol++; } // Expand the string in both directions until a space is hit - while (startIndex > 0 && line.charAt(startIndex - 1) !== ' ') { + while (startIndex > 0 && !this._isCharWordSeparator(line.charAt(startIndex - 1))) { if (bufferLine[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { // If the next character is a wide char, record it and skip the column leftWideCharCount++; @@ -638,7 +644,7 @@ export class SelectionManager extends EventEmitter { startIndex--; startCol--; } - while (endIndex + 1 < line.length && line.charAt(endIndex + 1) !== ' ') { + while (endIndex + 1 < line.length && !this._isCharWordSeparator(line.charAt(endIndex + 1))) { if (bufferLine[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { // If the next character is a wide char, record it and skip the column rightWideCharCount++; @@ -673,6 +679,15 @@ export class SelectionManager extends EventEmitter { this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]]; } + /** + * Gets whether the character is considered a word separator by the select + * word logic. + * @param char The character to check. + */ + private _isCharWordSeparator(char: string): boolean { + return WORD_SEPARATORS.indexOf(char) >= 0; + } + /** * Selects the line specified. * @param line The line index. diff --git a/src/utils/Mouse.ts b/src/utils/Mouse.ts index 79c5d5c1..a5d72c1e 100644 --- a/src/utils/Mouse.ts +++ b/src/utils/Mouse.ts @@ -30,12 +30,17 @@ export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLEleme * @param event The mouse event. * @param rowContainer The terminal's row container. * @param charMeasure The char measure object used to determine character sizes. + * @param colCount The number of columns in the terminal. + * @param rowCount The number of rows n the terminal. + * @param isSelection Whether the request is for the selection or not. This will + * apply an offset to the x value such that the left half of the cell will + * select that cell and the right half will select the next cell. */ -export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure, colCount: number, rowCount: number): [number, number] { +export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure, colCount: number, rowCount: number, isSelection?: boolean): [number, number] { const coords = getCoordsRelativeToElement(event, rowContainer); - // Convert to cols/rows - coords[0] = Math.ceil(coords[0] / charMeasure.width); + // Convert to cols/rows. + coords[0] = Math.ceil((coords[0] + (isSelection ? charMeasure.width / 2 : 0)) / charMeasure.width); coords[1] = Math.ceil(coords[1] / charMeasure.height); // Ensure coordinates are within the terminal viewport.