Merge pull request #716 from Tyriar/697_select_to_word

Select to the word at the mouse after double click
This commit is contained in:
Daniel Imms
2017-06-19 07:56:35 -07:00
committed by GitHub
3 changed files with 54 additions and 18 deletions
+50 -12
View File
@@ -51,6 +51,23 @@ const LINE_DATA_WIDTH_INDEX = 2;
const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160); const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160);
const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g'); const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g');
/**
* Represents a position of a word on a line.
*/
interface IWordPosition {
start: number;
length: number;
}
/**
* A selection mode, this drives how the selection behaves on mouse move.
*/
enum SelectionMode {
NORMAL,
WORD,
LINE
}
/** /**
* A class that manages the selection of the terminal. With help from * A class that manages the selection of the terminal. With help from
* SelectionModel, SelectionManager handles with all logic associated with * SelectionModel, SelectionManager handles with all logic associated with
@@ -86,9 +103,9 @@ export class SelectionManager extends EventEmitter {
private _clickCount: number; private _clickCount: number;
/** /**
* Whether line select mode is active, this occurs after a triple click. * The current selection mode.
*/ */
private _isLineSelectModeActive: boolean; private _activeSelectionMode: SelectionMode;
/** /**
* A setInterval timer that is active while the mouse is down whose callback * A setInterval timer that is active while the mouse is down whose callback
@@ -118,7 +135,7 @@ export class SelectionManager extends EventEmitter {
this._model = new SelectionModel(_terminal); this._model = new SelectionModel(_terminal);
this._lastMouseDownTime = 0; this._lastMouseDownTime = 0;
this._isLineSelectModeActive = false; this._activeSelectionMode = SelectionMode.NORMAL;
} }
/** /**
@@ -417,7 +434,7 @@ export class SelectionManager extends EventEmitter {
private _onSingleClick(event: MouseEvent): void { private _onSingleClick(event: MouseEvent): void {
this._model.selectionStartLength = 0; this._model.selectionStartLength = 0;
this._model.isSelectAllActive = false; this._model.isSelectAllActive = false;
this._isLineSelectModeActive = false; this._activeSelectionMode = SelectionMode.NORMAL;
this._model.selectionStart = this._getMouseBufferCoords(event); this._model.selectionStart = this._getMouseBufferCoords(event);
if (this._model.selectionStart) { if (this._model.selectionStart) {
this._model.selectionEnd = null; this._model.selectionEnd = null;
@@ -437,6 +454,7 @@ export class SelectionManager extends EventEmitter {
private _onDoubleClick(event: MouseEvent): void { private _onDoubleClick(event: MouseEvent): void {
const coords = this._getMouseBufferCoords(event); const coords = this._getMouseBufferCoords(event);
if (coords) { if (coords) {
this._activeSelectionMode = SelectionMode.WORD;
this._selectWordAt(coords); this._selectWordAt(coords);
} }
} }
@@ -449,7 +467,7 @@ export class SelectionManager extends EventEmitter {
private _onTripleClick(event: MouseEvent): void { private _onTripleClick(event: MouseEvent): void {
const coords = this._getMouseBufferCoords(event); const coords = this._getMouseBufferCoords(event);
if (coords) { if (coords) {
this._isLineSelectModeActive = true; this._activeSelectionMode = SelectionMode.LINE;
this._selectLineAt(coords[1]); this._selectLineAt(coords[1]);
} }
} }
@@ -494,12 +512,14 @@ export class SelectionManager extends EventEmitter {
this._model.selectionEnd = this._getMouseBufferCoords(event); this._model.selectionEnd = this._getMouseBufferCoords(event);
// Select the entire line if line select mode is active. // Select the entire line if line select mode is active.
if (this._isLineSelectModeActive) { if (this._activeSelectionMode === SelectionMode.LINE) {
if (this._model.selectionEnd[1] < this._model.selectionStart[1]) { if (this._model.selectionEnd[1] < this._model.selectionStart[1]) {
this._model.selectionEnd[0] = 0; this._model.selectionEnd[0] = 0;
} else { } else {
this._model.selectionEnd[0] = this._terminal.cols; this._model.selectionEnd[0] = this._terminal.cols;
} }
} else if (this._activeSelectionMode === SelectionMode.WORD) {
this._selectToWordAt(this._model.selectionEnd);
} }
// Determine the amount of scrolling that will happen. // Determine the amount of scrolling that will happen.
@@ -573,11 +593,10 @@ export class SelectionManager extends EventEmitter {
} }
/** /**
* Selects the word at the coordinates specified. Words are defined as all * Gets positional information for the word at the coordinated specified.
* non-whitespace characters.
* @param coords The coordinates to get the word at. * @param coords The coordinates to get the word at.
*/ */
protected _selectWordAt(coords: [number, number]): void { private _getWordAt(coords: [number, number]): IWordPosition {
const bufferLine = this._buffer.get(coords[1]); const bufferLine = this._buffer.get(coords[1]);
const line = this._translateBufferLineToString(bufferLine, false); const line = this._translateBufferLineToString(bufferLine, false);
@@ -636,9 +655,28 @@ export class SelectionManager extends EventEmitter {
} }
} }
// Record the resulting selection const start = startIndex + charOffset - leftWideCharCount;
this._model.selectionStart = [startIndex + charOffset - leftWideCharCount, coords[1]]; const length = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols);
this._model.selectionStartLength = Math.min(endIndex - startIndex + leftWideCharCount + rightWideCharCount + 1/*include endIndex char*/, this._terminal.cols); return {start, length};
}
/**
* Selects the word at the coordinates specified.
* @param coords The coordinates to get the word at.
*/
protected _selectWordAt(coords: [number, number]): void {
const wordPosition = this._getWordAt(coords);
this._model.selectionStart = [wordPosition.start, coords[1]];
this._model.selectionStartLength = wordPosition.length;
}
/**
* Sets the selection end to the word at the coordinated specified.
* @param coords The coordinates to get the word at.
*/
private _selectToWordAt(coords: [number, number]): void {
const wordPosition = this._getWordAt(coords);
this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]];
} }
/** /**
+1 -3
View File
@@ -11,8 +11,6 @@ class TestSelectionModel extends SelectionModel {
) { ) {
super(terminal); super(terminal);
} }
public areSelectionValuesReversed(): boolean { return this._areSelectionValuesReversed(); }
} }
describe('SelectionManager', () => { describe('SelectionManager', () => {
@@ -39,7 +37,7 @@ describe('SelectionManager', () => {
}); });
}); });
describe('_areSelectionValuesReversed', () => { describe('areSelectionValuesReversed', () => {
it('should return true when the selection end is before selection start', () => { it('should return true when the selection end is before selection start', () => {
model.selectionStart = [1, 0]; model.selectionStart = [1, 0];
model.selectionEnd = [0, 0]; model.selectionEnd = [0, 0];
+3 -3
View File
@@ -59,7 +59,7 @@ export class SelectionModel {
return this.selectionStart; return this.selectionStart;
} }
return this._areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart; return this.areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
} }
/** /**
@@ -76,7 +76,7 @@ export class SelectionModel {
} }
// Use the selection start if the end doesn't exist or they're reversed // Use the selection start if the end doesn't exist or they're reversed
if (!this.selectionEnd || this._areSelectionValuesReversed()) { if (!this.selectionEnd || this.areSelectionValuesReversed()) {
return [this.selectionStart[0] + this.selectionStartLength, this.selectionStart[1]]; return [this.selectionStart[0] + this.selectionStartLength, this.selectionStart[1]];
} }
@@ -93,7 +93,7 @@ export class SelectionModel {
/** /**
* Returns whether the selection start and end are reversed. * Returns whether the selection start and end are reversed.
*/ */
protected _areSelectionValuesReversed(): boolean { public areSelectionValuesReversed(): boolean {
const start = this.selectionStart; const start = this.selectionStart;
const end = this.selectionEnd; const end = this.selectionEnd;
return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]); return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]);