Move SelectionService into browser (+strict)
This commit is contained in:
Vendored
+2
-2
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
export interface ISelectionRedrawRequestEvent {
|
||||
start: [number, number];
|
||||
end: [number, number];
|
||||
start: [number, number] | undefined;
|
||||
end: [number, number] | undefined;
|
||||
columnSelectMode: boolean;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,492 @@
|
||||
/**
|
||||
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { assert } from 'chai';
|
||||
import { SelectionService, SelectionMode } from './SelectionService';
|
||||
import { SelectionModel } from 'browser/selection/SelectionModel';
|
||||
import { IBufferLine } from 'common/Types';
|
||||
import { MockBufferService, MockOptionsService, MockCoreService } from 'common/TestUtils.test';
|
||||
import { BufferLine } from 'common/buffer/BufferLine';
|
||||
import { IBufferService, IOptionsService } from 'common/services/Services';
|
||||
import { MockCharSizeService, MockMouseService } from 'browser/TestUtils.test';
|
||||
import { CellData } from 'common/buffer/CellData';
|
||||
import { IBuffer } from 'common/buffer/Types';
|
||||
import { isMSWindows } from '../../../out/common/Platform';
|
||||
|
||||
class TestSelectionService extends SelectionService {
|
||||
constructor(
|
||||
bufferService: IBufferService,
|
||||
optionsService: IOptionsService
|
||||
) {
|
||||
super(() => {}, null!, null!, new MockCharSizeService(10, 10), bufferService, new MockCoreService(), new MockMouseService(), optionsService);
|
||||
}
|
||||
|
||||
public get model(): SelectionModel { return this._model; }
|
||||
|
||||
public set selectionMode(mode: SelectionMode) { this._activeSelectionMode = mode; }
|
||||
|
||||
public selectLineAt(line: number): void { this._selectLineAt(line); }
|
||||
public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords, true); }
|
||||
public areCoordsInSelection(coords: [number, number], start: [number, number], end: [number, number]): boolean { return this._areCoordsInSelection(coords, start, end); }
|
||||
|
||||
// Disable DOM interaction
|
||||
public enable(): void {}
|
||||
public disable(): void {}
|
||||
public refresh(): void {}
|
||||
}
|
||||
|
||||
describe('SelectionService', () => {
|
||||
let buffer: IBuffer;
|
||||
let bufferService: IBufferService;
|
||||
let optionsService: IOptionsService;
|
||||
let selectionService: TestSelectionService;
|
||||
|
||||
beforeEach(() => {
|
||||
optionsService = new MockOptionsService();
|
||||
bufferService = new MockBufferService(20, 20, optionsService);
|
||||
buffer = bufferService.buffer;
|
||||
selectionService = new TestSelectionService(bufferService, optionsService);
|
||||
});
|
||||
|
||||
function stringToRow(text: string): IBufferLine {
|
||||
const result = new BufferLine(text.length);
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
result.setCell(i, CellData.fromCharData([0, text.charAt(i), 1, text.charCodeAt(i)]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function stringArrayToRow(chars: string[]): IBufferLine {
|
||||
const line = new BufferLine(chars.length);
|
||||
chars.map((c, idx) => line.setCell(idx, CellData.fromCharData([0, c, 1, c.charCodeAt(0)])));
|
||||
return line;
|
||||
}
|
||||
|
||||
describe('_selectWordAt', () => {
|
||||
it('should expand selection for normal width chars', () => {
|
||||
buffer.lines.set(0, stringToRow('foo bar'));
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, 'bar');
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, 'bar');
|
||||
selectionService.selectWordAt([6, 0]);
|
||||
assert.equal(selectionService.selectionText, 'bar');
|
||||
});
|
||||
it('should expand selection for whitespace', () => {
|
||||
buffer.lines.set(0, stringToRow('a b'));
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, 'b');
|
||||
});
|
||||
it('should expand selection for wide characters', () => {
|
||||
// Wide characters use a special format
|
||||
const data: [number, string, number, number][] = [
|
||||
[0, '中', 2, '中'.charCodeAt(0)],
|
||||
[0, '', 0, 0],
|
||||
[0, '文', 2, '文'.charCodeAt(0)],
|
||||
[0, '', 0, 0],
|
||||
[0, ' ', 1, ' '.charCodeAt(0)],
|
||||
[0, 'a', 1, 'a'.charCodeAt(0)],
|
||||
[0, '中', 2, '中'.charCodeAt(0)],
|
||||
[0, '', 0, 0],
|
||||
[0, '文', 2, '文'.charCodeAt(0)],
|
||||
[0, '', 0, ''.charCodeAt(0)],
|
||||
[0, 'b', 1, 'b'.charCodeAt(0)],
|
||||
[0, ' ', 1, ' '.charCodeAt(0)],
|
||||
[0, 'f', 1, 'f'.charCodeAt(0)],
|
||||
[0, 'o', 1, 'o'.charCodeAt(0)],
|
||||
[0, 'o', 1, 'o'.charCodeAt(0)]
|
||||
];
|
||||
const line = new BufferLine(data.length);
|
||||
for (let i = 0; i < data.length; ++i) line.setCell(i, CellData.fromCharData(data[i]));
|
||||
buffer.lines.set(0, line);
|
||||
// Ensure wide characters take up 2 columns
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, '中文');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '中文');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, '中文');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, '中文');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
// Ensure wide characters work when wrapped in normal width characters
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([6, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([7, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([8, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([9, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([10, 0]);
|
||||
assert.equal(selectionService.selectionText, 'a中文b');
|
||||
selectionService.selectWordAt([11, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
// Ensure normal width characters work fine in a line containing wide characters
|
||||
selectionService.selectWordAt([12, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
selectionService.selectWordAt([13, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
selectionService.selectWordAt([14, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foo');
|
||||
});
|
||||
it('should select up to non-path characters that are commonly adjacent to paths', () => {
|
||||
buffer.lines.set(0, stringToRow('(cd)[ef]{gh}\'ij"'));
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, '(cd');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd)');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, '[ef');
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef');
|
||||
selectionService.selectWordAt([6, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef');
|
||||
selectionService.selectWordAt([7, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef]');
|
||||
selectionService.selectWordAt([8, 0]);
|
||||
assert.equal(selectionService.selectionText, '{gh');
|
||||
selectionService.selectWordAt([9, 0]);
|
||||
assert.equal(selectionService.selectionText, 'gh');
|
||||
selectionService.selectWordAt([10, 0]);
|
||||
assert.equal(selectionService.selectionText, 'gh');
|
||||
selectionService.selectWordAt([11, 0]);
|
||||
assert.equal(selectionService.selectionText, 'gh}');
|
||||
selectionService.selectWordAt([12, 0]);
|
||||
assert.equal(selectionService.selectionText, '\'ij');
|
||||
selectionService.selectWordAt([13, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ij');
|
||||
selectionService.selectWordAt([14, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ij');
|
||||
selectionService.selectWordAt([15, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ij"');
|
||||
});
|
||||
it('should expand upwards or downards for wrapped lines', () => {
|
||||
buffer.lines.set(0, stringToRow(' foo'));
|
||||
buffer.lines.set(1, stringToRow('bar '));
|
||||
buffer.lines.get(1)!.isWrapped = true;
|
||||
selectionService.selectWordAt([1, 1]);
|
||||
assert.equal(selectionService.selectionText, 'foobar');
|
||||
selectionService.model.clearSelection();
|
||||
selectionService.selectWordAt([18, 0]);
|
||||
assert.equal(selectionService.selectionText, 'foobar');
|
||||
});
|
||||
it('should expand both upwards and downwards for word wrapped over many lines', () => {
|
||||
const expectedText = 'fooaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccbar';
|
||||
buffer.lines.set(0, stringToRow(' foo'));
|
||||
buffer.lines.set(1, stringToRow('aaaaaaaaaaaaaaaaaaaa'));
|
||||
buffer.lines.set(2, stringToRow('bbbbbbbbbbbbbbbbbbbb'));
|
||||
buffer.lines.set(3, stringToRow('cccccccccccccccccccc'));
|
||||
buffer.lines.set(4, stringToRow('bar '));
|
||||
buffer.lines.get(1)!.isWrapped = true;
|
||||
buffer.lines.get(2)!.isWrapped = true;
|
||||
buffer.lines.get(3)!.isWrapped = true;
|
||||
buffer.lines.get(4)!.isWrapped = true;
|
||||
selectionService.selectWordAt([18, 0]);
|
||||
assert.equal(selectionService.selectionText, expectedText);
|
||||
selectionService.model.clearSelection();
|
||||
selectionService.selectWordAt([10, 1]);
|
||||
assert.equal(selectionService.selectionText, expectedText);
|
||||
selectionService.model.clearSelection();
|
||||
selectionService.selectWordAt([10, 2]);
|
||||
assert.equal(selectionService.selectionText, expectedText);
|
||||
selectionService.model.clearSelection();
|
||||
selectionService.selectWordAt([10, 3]);
|
||||
assert.equal(selectionService.selectionText, expectedText);
|
||||
selectionService.model.clearSelection();
|
||||
selectionService.selectWordAt([1, 4]);
|
||||
assert.equal(selectionService.selectionText, expectedText);
|
||||
});
|
||||
describe('emoji', () => {
|
||||
it('should treat a single emoji as a word when wrapped in spaces', () => {
|
||||
buffer.lines.set(0, stringToRow(' ⚽ a')); // The a is here to prevent the space being trimmed in selectionText
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
});
|
||||
it('should treat multiple emojis as a word when wrapped in spaces', () => {
|
||||
buffer.lines.set(0, stringToRow(' ⚽⚽ a')); // The a is here to prevent the space being trimmed in selectionText
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽⚽');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽⚽');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
});
|
||||
it('should treat emojis using the zero-width-joiner as a single word', () => {
|
||||
// Note that the first 3 emojis include the invisible ZWJ char
|
||||
buffer.lines.set(0, stringArrayToRow([
|
||||
' ', '👨', '👩', '👧', '👦', ' ', 'a'
|
||||
])); // The a is here to prevent the space being trimmed in selectionText
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
// ZWJ emojis do not combine in the terminal so the family emoji used here consumed 4 cells
|
||||
// The selection text should retain ZWJ chars despite not combining on the terminal
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '👨👩👧👦');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, '👨👩👧👦');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, '👨👩👧👦');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, '👨👩👧👦');
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
});
|
||||
it('should treat emojis and characters joined together as a word', () => {
|
||||
buffer.lines.set(0, stringToRow(' ⚽ab cd⚽ ef⚽gh')); // The a is here to prevent the space being trimmed in selectionText
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽ab');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽ab');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, '⚽ab');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd⚽');
|
||||
selectionService.selectWordAt([6, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd⚽');
|
||||
selectionService.selectWordAt([7, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd⚽');
|
||||
selectionService.selectWordAt([8, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([9, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef⚽gh');
|
||||
selectionService.selectWordAt([10, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef⚽gh');
|
||||
selectionService.selectWordAt([11, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef⚽gh');
|
||||
selectionService.selectWordAt([12, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef⚽gh');
|
||||
selectionService.selectWordAt([13, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef⚽gh');
|
||||
});
|
||||
it('should treat complex emojis and characters joined together as a word', () => {
|
||||
// This emoji is the flag for England and is made up of: 1F3F4 E0067 E0062 E0065 E006E E0067 E007F
|
||||
buffer.lines.set(0, stringArrayToRow([
|
||||
' ', '🏴', 'a', 'b', ' ', 'c', 'd', '🏴', ' ', 'e', 'f', '🏴', 'g', 'h', ' ', 'a'
|
||||
])); // The a is here to prevent the space being trimmed in selectionText
|
||||
selectionService.selectWordAt([0, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([1, 0]);
|
||||
assert.equal(selectionService.selectionText, '🏴ab');
|
||||
selectionService.selectWordAt([2, 0]);
|
||||
assert.equal(selectionService.selectionText, '🏴ab');
|
||||
selectionService.selectWordAt([3, 0]);
|
||||
assert.equal(selectionService.selectionText, '🏴ab');
|
||||
selectionService.selectWordAt([4, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([5, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd🏴');
|
||||
selectionService.selectWordAt([6, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd🏴');
|
||||
selectionService.selectWordAt([7, 0]);
|
||||
assert.equal(selectionService.selectionText, 'cd🏴');
|
||||
selectionService.selectWordAt([8, 0]);
|
||||
assert.equal(selectionService.selectionText, ' ');
|
||||
selectionService.selectWordAt([9, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef🏴gh');
|
||||
selectionService.selectWordAt([10, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef🏴gh');
|
||||
selectionService.selectWordAt([11, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef🏴gh');
|
||||
selectionService.selectWordAt([12, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef🏴gh');
|
||||
selectionService.selectWordAt([13, 0]);
|
||||
assert.equal(selectionService.selectionText, 'ef🏴gh');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_selectLineAt', () => {
|
||||
it('should select the entire line', () => {
|
||||
buffer.lines.set(0, stringToRow('foo bar'));
|
||||
selectionService.selectLineAt(0);
|
||||
assert.equal(selectionService.selectionText, 'foo bar', 'The selected text is correct');
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 0]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 0], 'The actual selection spans the entire column');
|
||||
});
|
||||
it('should select the entire wrapped line', () => {
|
||||
buffer.lines.set(0, stringToRow('foo'));
|
||||
const line2 = stringToRow('bar');
|
||||
line2.isWrapped = true;
|
||||
buffer.lines.set(1, line2);
|
||||
selectionService.selectLineAt(0);
|
||||
assert.equal(selectionService.selectionText, 'foobar', 'The selected text is correct');
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 0]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 1], 'The actual selection spans the entire column');
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectAll', () => {
|
||||
it('should select the entire buffer, beyond the viewport', () => {
|
||||
bufferService.resize(20, 5);
|
||||
buffer.lines.set(0, stringToRow('1'));
|
||||
buffer.lines.set(1, stringToRow('2'));
|
||||
buffer.lines.set(2, stringToRow('3'));
|
||||
buffer.lines.set(3, stringToRow('4'));
|
||||
buffer.lines.set(4, stringToRow('5'));
|
||||
selectionService.selectAll();
|
||||
console.log(selectionService.selectionText.length);
|
||||
console.log(isMSWindows);
|
||||
assert.equal(selectionService.selectionText, '1\n2\n3\n4\n5');
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectLines', () => {
|
||||
it('should select a single line', () => {
|
||||
buffer.lines.length = 3;
|
||||
buffer.lines.set(0, stringToRow('1'));
|
||||
buffer.lines.set(1, stringToRow('2'));
|
||||
buffer.lines.set(2, stringToRow('3'));
|
||||
selectionService.selectLines(1, 1);
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 1]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 1]);
|
||||
});
|
||||
it('should select multiple lines', () => {
|
||||
buffer.lines.length = 5;
|
||||
buffer.lines.set(0, stringToRow('1'));
|
||||
buffer.lines.set(1, stringToRow('2'));
|
||||
buffer.lines.set(2, stringToRow('3'));
|
||||
buffer.lines.set(3, stringToRow('4'));
|
||||
buffer.lines.set(4, stringToRow('5'));
|
||||
selectionService.selectLines(1, 3);
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 1]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 3]);
|
||||
});
|
||||
it('should select the to the start when requesting a negative row', () => {
|
||||
buffer.lines.length = 2;
|
||||
buffer.lines.set(0, stringToRow('1'));
|
||||
buffer.lines.set(1, stringToRow('2'));
|
||||
selectionService.selectLines(-1, 0);
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 0]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 0]);
|
||||
});
|
||||
it('should select the to the end when requesting beyond the final row', () => {
|
||||
buffer.lines.length = 2;
|
||||
buffer.lines.set(0, stringToRow('1'));
|
||||
buffer.lines.set(1, stringToRow('2'));
|
||||
selectionService.selectLines(1, 2);
|
||||
assert.deepEqual(selectionService.model.finalSelectionStart, [0, 1]);
|
||||
assert.deepEqual(selectionService.model.finalSelectionEnd, [bufferService.cols, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasSelection', () => {
|
||||
it('should return whether there is a selection', () => {
|
||||
selectionService.model.selectionStart = [0, 0];
|
||||
selectionService.model.selectionStartLength = 0;
|
||||
assert.equal(selectionService.hasSelection, false);
|
||||
selectionService.model.selectionEnd = [0, 0];
|
||||
assert.equal(selectionService.hasSelection, false);
|
||||
selectionService.model.selectionEnd = [1, 0];
|
||||
assert.equal(selectionService.hasSelection, true);
|
||||
selectionService.model.selectionEnd = [0, 1];
|
||||
assert.equal(selectionService.hasSelection, true);
|
||||
selectionService.model.selectionEnd = [1, 1];
|
||||
assert.equal(selectionService.hasSelection, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('column selection', () => {
|
||||
it('should select a column of text', () => {
|
||||
buffer.lines.length = 3;
|
||||
buffer.lines.set(0, stringToRow('abcdefghij'));
|
||||
buffer.lines.set(1, stringToRow('klmnopqrst'));
|
||||
buffer.lines.set(2, stringToRow('uvwxyz'));
|
||||
|
||||
selectionService.selectionMode = SelectionMode.COLUMN;
|
||||
selectionService.model.selectionStart = [2, 0];
|
||||
selectionService.model.selectionEnd = [4, 2];
|
||||
|
||||
assert.equal(selectionService.selectionText, 'cd\nmn\nwx');
|
||||
});
|
||||
|
||||
it('should select a column of text without chopping up double width characters', () => {
|
||||
buffer.lines.length = 3;
|
||||
buffer.lines.set(0, stringToRow('a'));
|
||||
buffer.lines.set(1, stringToRow('語'));
|
||||
buffer.lines.set(2, stringToRow('b'));
|
||||
|
||||
selectionService.selectionMode = SelectionMode.COLUMN;
|
||||
selectionService.model.selectionStart = [0, 0];
|
||||
selectionService.model.selectionEnd = [1, 2];
|
||||
|
||||
assert.equal(selectionService.selectionText, 'a\n語\nb');
|
||||
});
|
||||
|
||||
it('should select a column of text with single character emojis', () => {
|
||||
buffer.lines.length = 3;
|
||||
buffer.lines.set(0, stringToRow('a'));
|
||||
buffer.lines.set(1, stringToRow('☃'));
|
||||
buffer.lines.set(2, stringToRow('c'));
|
||||
|
||||
selectionService.selectionMode = SelectionMode.COLUMN;
|
||||
selectionService.model.selectionStart = [0, 0];
|
||||
selectionService.model.selectionEnd = [1, 2];
|
||||
|
||||
assert.equal(selectionService.selectionText, 'a\n☃\nc');
|
||||
});
|
||||
|
||||
it('should select a column of text with double character emojis', () => {
|
||||
// TODO the case this is testing works for me in the demo webapp,
|
||||
// but doing it programmatically fails.
|
||||
buffer.lines.length = 3;
|
||||
buffer.lines.set(0, stringToRow('a '));
|
||||
buffer.lines.set(1, stringArrayToRow(['😁', ' ']));
|
||||
buffer.lines.set(2, stringToRow('c '));
|
||||
|
||||
selectionService.selectionMode = SelectionMode.COLUMN;
|
||||
selectionService.model.selectionStart = [0, 0];
|
||||
selectionService.model.selectionEnd = [1, 2];
|
||||
|
||||
assert.equal(selectionService.selectionText, 'a\n😁\nc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('_areCoordsInSelection', () => {
|
||||
it('should return whether coords are in the selection', () => {
|
||||
assert.isFalse(selectionService.areCoordsInSelection([0, 0], [2, 0], [2, 1]));
|
||||
assert.isFalse(selectionService.areCoordsInSelection([1, 0], [2, 0], [2, 1]));
|
||||
assert.isTrue(selectionService.areCoordsInSelection([2, 0], [2, 0], [2, 1]));
|
||||
assert.isTrue(selectionService.areCoordsInSelection([10, 0], [2, 0], [2, 1]));
|
||||
assert.isTrue(selectionService.areCoordsInSelection([0, 1], [2, 0], [2, 1]));
|
||||
assert.isTrue(selectionService.areCoordsInSelection([1, 1], [2, 0], [2, 1]));
|
||||
assert.isFalse(selectionService.areCoordsInSelection([2, 1], [2, 0], [2, 1]));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,949 @@
|
||||
/**
|
||||
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { ISelectionRedrawRequestEvent } from 'browser/selection/Types';
|
||||
import { IBuffer } from 'common/buffer/Types';
|
||||
import { IBufferLine, IDisposable } from 'common/Types';
|
||||
import * as Browser from 'common/Platform';
|
||||
import { SelectionModel } from 'browser/selection/SelectionModel';
|
||||
import { CellData } from 'common/buffer/CellData';
|
||||
import { EventEmitter, IEvent } from 'common/EventEmitter';
|
||||
import { ICharSizeService, IMouseService, ISelectionService } from 'browser/services/Services';
|
||||
import { IBufferService, IOptionsService, ICoreService } from 'common/services/Services';
|
||||
import { getCoordsRelativeToElement } from 'browser/input/Mouse';
|
||||
import { moveToCellSequence } from 'browser/input/MoveToCell';
|
||||
|
||||
/**
|
||||
* The number of pixels the mouse needs to be above or below the viewport in
|
||||
* order to scroll at the maximum speed.
|
||||
*/
|
||||
const DRAG_SCROLL_MAX_THRESHOLD = 50;
|
||||
|
||||
/**
|
||||
* The maximum scrolling speed
|
||||
*/
|
||||
const DRAG_SCROLL_MAX_SPEED = 15;
|
||||
|
||||
/**
|
||||
* The number of milliseconds between drag scroll updates.
|
||||
*/
|
||||
const DRAG_SCROLL_INTERVAL = 50;
|
||||
|
||||
/**
|
||||
* The maximum amount of time that can have elapsed for an alt click to move the
|
||||
* cursor.
|
||||
*/
|
||||
const ALT_CLICK_MOVE_CURSOR_TIME = 500;
|
||||
|
||||
const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160);
|
||||
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.
|
||||
*/
|
||||
export const enum SelectionMode {
|
||||
NORMAL,
|
||||
WORD,
|
||||
LINE,
|
||||
COLUMN
|
||||
}
|
||||
|
||||
/**
|
||||
* A class that manages the selection of the terminal. With help from
|
||||
* SelectionModel, SelectionService handles with all logic associated with
|
||||
* dealing with the selection, including handling mouse interaction, wide
|
||||
* characters and fetching the actual text within the selection. Rendering is
|
||||
* not handled by the SelectionService but the onRedrawRequest event is fired
|
||||
* when the selection is ready to be redrawn (on an animation frame).
|
||||
*/
|
||||
export class SelectionService implements ISelectionService {
|
||||
protected _model: SelectionModel;
|
||||
|
||||
/**
|
||||
* The amount to scroll every drag scroll update (depends on how far the mouse
|
||||
* drag is above or below the terminal).
|
||||
*/
|
||||
private _dragScrollAmount: number = 0;
|
||||
|
||||
/**
|
||||
* The current selection mode.
|
||||
*/
|
||||
protected _activeSelectionMode: SelectionMode;
|
||||
|
||||
/**
|
||||
* A setInterval timer that is active while the mouse is down whose callback
|
||||
* scrolls the viewport when necessary.
|
||||
*/
|
||||
private _dragScrollIntervalTimer: number | undefined;
|
||||
|
||||
/**
|
||||
* The animation frame ID used for refreshing the selection.
|
||||
*/
|
||||
private _refreshAnimationFrame: number | undefined;
|
||||
|
||||
/**
|
||||
* Whether selection is enabled.
|
||||
*/
|
||||
private _enabled = true;
|
||||
|
||||
private _mouseMoveListener: EventListener;
|
||||
private _mouseUpListener: EventListener;
|
||||
private _trimListener: IDisposable;
|
||||
private _workCell: CellData = new CellData();
|
||||
|
||||
private _mouseDownTimeStamp: number = 0;
|
||||
|
||||
private _onLinuxMouseSelection = new EventEmitter<string>();
|
||||
public get onLinuxMouseSelection(): IEvent<string> { return this._onLinuxMouseSelection.event; }
|
||||
private _onRedrawRequest = new EventEmitter<ISelectionRedrawRequestEvent>();
|
||||
public get onRedrawRequest(): IEvent<ISelectionRedrawRequestEvent> { return this._onRedrawRequest.event; }
|
||||
private _onSelectionChange = new EventEmitter<void>();
|
||||
public get onSelectionChange(): IEvent<void> { return this._onSelectionChange.event; }
|
||||
|
||||
constructor(
|
||||
private readonly _scrollLines: (amount: number, suppressEvent: boolean) => void,
|
||||
private readonly _element: HTMLElement,
|
||||
private readonly _screenElement: HTMLElement,
|
||||
private readonly _charSizeService: ICharSizeService,
|
||||
private readonly _bufferService: IBufferService,
|
||||
private readonly _coreService: ICoreService,
|
||||
private readonly _mouseService: IMouseService,
|
||||
private readonly _optionsService: IOptionsService
|
||||
) {
|
||||
// Init listeners
|
||||
this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event);
|
||||
this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event);
|
||||
this._coreService.onUserInput(() => {
|
||||
if (this.hasSelection) {
|
||||
this.clearSelection();
|
||||
}
|
||||
});
|
||||
this._trimListener = this._bufferService.buffer.lines.onTrim(amount => this._onTrim(amount));
|
||||
this._bufferService.buffers.onBufferActivate(e => this._onBufferActivate(e));
|
||||
|
||||
this.enable();
|
||||
|
||||
this._model = new SelectionModel(this._bufferService);
|
||||
this._activeSelectionMode = SelectionMode.NORMAL;
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this._removeMouseDownListeners();
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.clearSelection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the selection manager. This is useful for when terminal mouse
|
||||
* are enabled.
|
||||
*/
|
||||
public disable(): void {
|
||||
this.clearSelection();
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the selection manager.
|
||||
*/
|
||||
public enable(): void {
|
||||
this._enabled = true;
|
||||
}
|
||||
|
||||
public get selectionStart(): [number, number] | undefined { return this._model.finalSelectionStart; }
|
||||
public get selectionEnd(): [number, number] | undefined { return this._model.finalSelectionEnd; }
|
||||
|
||||
/**
|
||||
* Gets whether there is an active text selection.
|
||||
*/
|
||||
public get hasSelection(): boolean {
|
||||
const start = this._model.finalSelectionStart;
|
||||
const end = this._model.finalSelectionEnd;
|
||||
if (!start || !end) {
|
||||
return false;
|
||||
}
|
||||
return start[0] !== end[0] || start[1] !== end[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the text currently selected.
|
||||
*/
|
||||
public get selectionText(): string {
|
||||
const start = this._model.finalSelectionStart;
|
||||
const end = this._model.finalSelectionEnd;
|
||||
if (!start || !end) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const buffer = this._bufferService.buffer;
|
||||
const result: string[] = [];
|
||||
|
||||
if (this._activeSelectionMode === SelectionMode.COLUMN) {
|
||||
// Ignore zero width selections
|
||||
if (start[0] === end[0]) {
|
||||
return '';
|
||||
}
|
||||
|
||||
for (let i = start[1]; i <= end[1]; i++) {
|
||||
const lineText = buffer.translateBufferLineToString(i, true, start[0], end[0]);
|
||||
result.push(lineText);
|
||||
}
|
||||
} else {
|
||||
// Get first row
|
||||
const startRowEndCol = start[1] === end[1] ? end[0] : undefined;
|
||||
result.push(buffer.translateBufferLineToString(start[1], true, start[0], startRowEndCol));
|
||||
|
||||
// Get middle rows
|
||||
for (let i = start[1] + 1; i <= end[1] - 1; i++) {
|
||||
const bufferLine = buffer.lines.get(i);
|
||||
const lineText = buffer.translateBufferLineToString(i, true);
|
||||
if (bufferLine!.isWrapped) {
|
||||
result[result.length - 1] += lineText;
|
||||
} else {
|
||||
result.push(lineText);
|
||||
}
|
||||
}
|
||||
|
||||
// Get final row
|
||||
if (start[1] !== end[1]) {
|
||||
const bufferLine = buffer.lines.get(end[1]);
|
||||
const lineText = buffer.translateBufferLineToString(end[1], true, 0, end[0]);
|
||||
if (bufferLine!.isWrapped) {
|
||||
result[result.length - 1] += lineText;
|
||||
} else {
|
||||
result.push(lineText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Format string by replacing non-breaking space chars with regular spaces
|
||||
// and joining the array into a multi-line string.
|
||||
const formattedResult = result.map(line => {
|
||||
return line.replace(ALL_NON_BREAKING_SPACE_REGEX, ' ');
|
||||
}).join(Browser.isMSWindows ? '\r\n' : '\n');
|
||||
|
||||
return formattedResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the current terminal selection.
|
||||
*/
|
||||
public clearSelection(): void {
|
||||
this._model.clearSelection();
|
||||
this._removeMouseDownListeners();
|
||||
this.refresh();
|
||||
this._onSelectionChange.fire();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a refresh, redrawing the selection on the next opportunity.
|
||||
* @param isLinuxMouseSelection Whether the selection should be registered as a new
|
||||
* selection on Linux.
|
||||
*/
|
||||
public refresh(isLinuxMouseSelection?: boolean): void {
|
||||
// Queue the refresh for the renderer
|
||||
if (!this._refreshAnimationFrame) {
|
||||
this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh());
|
||||
}
|
||||
|
||||
// If the platform is Linux and the refresh call comes from a mouse event,
|
||||
// we need to update the selection for middle click to paste selection.
|
||||
if (Browser.isLinux && isLinuxMouseSelection) {
|
||||
const selectionText = this.selectionText;
|
||||
if (selectionText.length) {
|
||||
this._onLinuxMouseSelection.fire(this.selectionText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires the refresh event, causing consumers to pick it up and redraw the
|
||||
* selection state.
|
||||
*/
|
||||
private _refresh(): void {
|
||||
this._refreshAnimationFrame = undefined;
|
||||
this._onRedrawRequest.fire({
|
||||
start: this._model.finalSelectionStart,
|
||||
end: this._model.finalSelectionEnd,
|
||||
columnSelectMode: this._activeSelectionMode === SelectionMode.COLUMN
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current click was inside the current selection
|
||||
* @param event The mouse event
|
||||
*/
|
||||
public isClickInSelection(event: MouseEvent): boolean {
|
||||
const coords = this._getMouseBufferCoords(event);
|
||||
const start = this._model.finalSelectionStart;
|
||||
const end = this._model.finalSelectionEnd;
|
||||
|
||||
if (!start || !end || !coords) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this._areCoordsInSelection(coords, start, end);
|
||||
}
|
||||
|
||||
protected _areCoordsInSelection(coords: [number, number], start: [number, number], end: [number, number]): boolean {
|
||||
return (coords[1] > start[1] && coords[1] < end[1]) ||
|
||||
(start[1] === end[1] && coords[1] === start[1] && coords[0] >= start[0] && coords[0] < end[0]) ||
|
||||
(start[1] < end[1] && coords[1] === end[1] && coords[0] < end[0]) ||
|
||||
(start[1] < end[1] && coords[1] === start[1] && coords[0] >= start[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects word at the current mouse event coordinates.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
public selectWordAtCursor(event: MouseEvent): void {
|
||||
const coords = this._getMouseBufferCoords(event);
|
||||
if (coords) {
|
||||
this._selectWordAt(coords, false);
|
||||
this._model.selectionEnd = undefined;
|
||||
this.refresh(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects all text within the terminal.
|
||||
*/
|
||||
public selectAll(): void {
|
||||
this._model.isSelectAllActive = true;
|
||||
this.refresh();
|
||||
this._onSelectionChange.fire();
|
||||
}
|
||||
|
||||
public selectLines(start: number, end: number): void {
|
||||
this._model.clearSelection();
|
||||
start = Math.max(start, 0);
|
||||
end = Math.min(end, this._bufferService.buffer.lines.length - 1);
|
||||
this._model.selectionStart = [0, start];
|
||||
this._model.selectionEnd = [this._bufferService.cols, end];
|
||||
this.refresh();
|
||||
this._onSelectionChange.fire();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the buffer being trimmed, adjust the selection position.
|
||||
* @param amount The amount the buffer is being trimmed.
|
||||
*/
|
||||
private _onTrim(amount: number): void {
|
||||
const needsRefresh = this._model.onTrim(amount);
|
||||
if (needsRefresh) {
|
||||
this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the 0-based [x, y] buffer coordinates of the current mouse event.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _getMouseBufferCoords(event: MouseEvent): [number, number] | undefined {
|
||||
const coords = this._mouseService.getCoords(event, this._screenElement, this._bufferService.cols, this._bufferService.rows, true);
|
||||
if (!coords) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Convert to 0-based
|
||||
coords[0]--;
|
||||
coords[1]--;
|
||||
|
||||
// Convert viewport coords to buffer coords
|
||||
coords[1] += this._bufferService.buffer.ydisp;
|
||||
return coords;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount the viewport should be scrolled based on how far out of the
|
||||
* terminal the mouse is.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _getMouseEventScrollAmount(event: MouseEvent): number {
|
||||
let offset = getCoordsRelativeToElement(event, this._screenElement)[1];
|
||||
const terminalHeight = this._bufferService.rows * Math.ceil(this._charSizeService.height * this._optionsService.options.lineHeight);
|
||||
if (offset >= 0 && offset <= terminalHeight) {
|
||||
return 0;
|
||||
}
|
||||
if (offset > terminalHeight) {
|
||||
offset -= terminalHeight;
|
||||
}
|
||||
|
||||
offset = Math.min(Math.max(offset, -DRAG_SCROLL_MAX_THRESHOLD), DRAG_SCROLL_MAX_THRESHOLD);
|
||||
offset /= DRAG_SCROLL_MAX_THRESHOLD;
|
||||
return (offset / Math.abs(offset)) + Math.round(offset * (DRAG_SCROLL_MAX_SPEED - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the selection manager should force selection, regardless of
|
||||
* whether the terminal is in mouse events mode.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
public shouldForceSelection(event: MouseEvent): boolean {
|
||||
if (Browser.isMac) {
|
||||
return event.altKey && this._optionsService.options.macOptionClickForcesSelection;
|
||||
}
|
||||
|
||||
return event.shiftKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles te mousedown event, setting up for a new selection.
|
||||
* @param event The mousedown event.
|
||||
*/
|
||||
public onMouseDown(event: MouseEvent): void {
|
||||
this._mouseDownTimeStamp = event.timeStamp;
|
||||
// If we have selection, we want the context menu on right click even if the
|
||||
// terminal is in mouse mode.
|
||||
if (event.button === 2 && this.hasSelection) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only action the primary button
|
||||
if (event.button !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow selection when using a specific modifier key, even when disabled
|
||||
if (!this._enabled) {
|
||||
if (!this.shouldForceSelection(event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't send the mouse down event to the current process, we want to select
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
// Tell the browser not to start a regular selection
|
||||
event.preventDefault();
|
||||
|
||||
// Reset drag scroll state
|
||||
this._dragScrollAmount = 0;
|
||||
|
||||
if (this._enabled && event.shiftKey) {
|
||||
this._onIncrementalClick(event);
|
||||
} else {
|
||||
if (event.detail === 1) {
|
||||
this._onSingleClick(event);
|
||||
} else if (event.detail === 2) {
|
||||
this._onDoubleClick(event);
|
||||
} else if (event.detail === 3) {
|
||||
this._onTripleClick(event);
|
||||
}
|
||||
}
|
||||
|
||||
this._addMouseDownListeners();
|
||||
this.refresh(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds listeners when mousedown is triggered.
|
||||
*/
|
||||
private _addMouseDownListeners(): void {
|
||||
// Listen on the document so that dragging outside of viewport works
|
||||
if (this._screenElement.ownerDocument) {
|
||||
this._screenElement.ownerDocument.addEventListener('mousemove', this._mouseMoveListener);
|
||||
this._screenElement.ownerDocument.addEventListener('mouseup', this._mouseUpListener);
|
||||
}
|
||||
this._dragScrollIntervalTimer = window.setInterval(() => this._dragScroll(), DRAG_SCROLL_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the listeners that are registered when mousedown is triggered.
|
||||
*/
|
||||
private _removeMouseDownListeners(): void {
|
||||
if (this._screenElement.ownerDocument) {
|
||||
this._screenElement.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener);
|
||||
this._screenElement.ownerDocument.removeEventListener('mouseup', this._mouseUpListener);
|
||||
}
|
||||
clearInterval(this._dragScrollIntervalTimer);
|
||||
this._dragScrollIntervalTimer = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an incremental click, setting the selection end position to the mouse
|
||||
* position.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _onIncrementalClick(event: MouseEvent): void {
|
||||
if (this._model.selectionStart) {
|
||||
this._model.selectionEnd = this._getMouseBufferCoords(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a single click, resetting relevant state and setting the selection
|
||||
* start position.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _onSingleClick(event: MouseEvent): void {
|
||||
this._model.selectionStartLength = 0;
|
||||
this._model.isSelectAllActive = false;
|
||||
this._activeSelectionMode = this.shouldColumnSelect(event) ? SelectionMode.COLUMN : SelectionMode.NORMAL;
|
||||
|
||||
// Initialize the new selection
|
||||
this._model.selectionStart = this._getMouseBufferCoords(event);
|
||||
if (!this._model.selectionStart) {
|
||||
return;
|
||||
}
|
||||
this._model.selectionEnd = undefined;
|
||||
|
||||
// Ensure the line exists
|
||||
const line = this._bufferService.buffer.lines.get(this._model.selectionStart[1]);
|
||||
if (!line) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Return early if the click event is not in the buffer (eg. in scroll bar)
|
||||
if (line.length >= this._model.selectionStart[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the mouse is over the second half of a wide character, adjust the
|
||||
// selection to cover the whole character
|
||||
if (line.hasWidth(this._model.selectionStart[0]) === 0) {
|
||||
this._model.selectionStart[0]++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a double click, selecting the current work.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _onDoubleClick(event: MouseEvent): void {
|
||||
const coords = this._getMouseBufferCoords(event);
|
||||
if (coords) {
|
||||
this._activeSelectionMode = SelectionMode.WORD;
|
||||
this._selectWordAt(coords, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a triple click, selecting the current line and activating line
|
||||
* select mode.
|
||||
* @param event The mouse event.
|
||||
*/
|
||||
private _onTripleClick(event: MouseEvent): void {
|
||||
const coords = this._getMouseBufferCoords(event);
|
||||
if (coords) {
|
||||
this._activeSelectionMode = SelectionMode.LINE;
|
||||
this._selectLineAt(coords[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the selection manager should operate in column select mode
|
||||
* @param event the mouse or keyboard event
|
||||
*/
|
||||
public shouldColumnSelect(event: KeyboardEvent | MouseEvent): boolean {
|
||||
return event.altKey && !(Browser.isMac && this._optionsService.options.macOptionClickForcesSelection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the mousemove event when the mouse button is down, recording the
|
||||
* end of the selection and refreshing the selection.
|
||||
* @param event The mousemove event.
|
||||
*/
|
||||
private _onMouseMove(event: MouseEvent): void {
|
||||
// If the mousemove listener is active it means that a selection is
|
||||
// currently being made, we should stop propagation to prevent mouse events
|
||||
// to be sent to the pty.
|
||||
event.stopImmediatePropagation();
|
||||
|
||||
// Something went wrong
|
||||
if (!this._model.selectionStart) {
|
||||
throw new Error('Selection start position was not set before mousemove event');
|
||||
}
|
||||
|
||||
// Record the previous position so we know whether to redraw the selection
|
||||
// at the end.
|
||||
const previousSelectionEnd = this._model.selectionEnd ? [this._model.selectionEnd[0], this._model.selectionEnd[1]] : null;
|
||||
|
||||
// Set the initial selection end based on the mouse coordinates
|
||||
this._model.selectionEnd = this._getMouseBufferCoords(event);
|
||||
if (!this._model.selectionEnd) {
|
||||
this.refresh(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Select the entire line if line select mode is active.
|
||||
if (this._activeSelectionMode === SelectionMode.LINE) {
|
||||
if (this._model.selectionEnd[1] < this._model.selectionStart[1]) {
|
||||
this._model.selectionEnd[0] = 0;
|
||||
} else {
|
||||
this._model.selectionEnd[0] = this._bufferService.cols;
|
||||
}
|
||||
} else if (this._activeSelectionMode === SelectionMode.WORD) {
|
||||
this._selectToWordAt(this._model.selectionEnd);
|
||||
}
|
||||
|
||||
// Determine the amount of scrolling that will happen.
|
||||
this._dragScrollAmount = this._getMouseEventScrollAmount(event);
|
||||
|
||||
// If the cursor was above or below the viewport, make sure it's at the
|
||||
// start or end of the viewport respectively. This should only happen when
|
||||
// NOT in column select mode.
|
||||
if (this._activeSelectionMode !== SelectionMode.COLUMN) {
|
||||
if (this._dragScrollAmount > 0) {
|
||||
this._model.selectionEnd[0] = this._bufferService.cols;
|
||||
} else if (this._dragScrollAmount < 0) {
|
||||
this._model.selectionEnd[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If the character is a wide character include the cell to the right in the
|
||||
// selection. Note that selections at the very end of the line will never
|
||||
// have a character.
|
||||
const buffer = this._bufferService.buffer;
|
||||
if (this._model.selectionEnd[1] < buffer.lines.length) {
|
||||
const line = buffer.lines.get(this._model.selectionEnd[1]);
|
||||
if (line && line.hasWidth(this._model.selectionEnd[0]) === 0) {
|
||||
this._model.selectionEnd[0]++;
|
||||
}
|
||||
}
|
||||
|
||||
// Only draw here if the selection changes.
|
||||
if (!previousSelectionEnd ||
|
||||
previousSelectionEnd[0] !== this._model.selectionEnd[0] ||
|
||||
previousSelectionEnd[1] !== this._model.selectionEnd[1]) {
|
||||
this.refresh(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback that occurs every DRAG_SCROLL_INTERVAL ms that does the
|
||||
* scrolling of the viewport.
|
||||
*/
|
||||
private _dragScroll(): void {
|
||||
if (!this._model.selectionEnd || !this._model.selectionStart) {
|
||||
return;
|
||||
}
|
||||
if (this._dragScrollAmount) {
|
||||
this._scrollLines(this._dragScrollAmount, false);
|
||||
// Re-evaluate selection
|
||||
// If the cursor was above or below the viewport, make sure it's at the
|
||||
// start or end of the viewport respectively. This should only happen when
|
||||
// NOT in column select mode.
|
||||
const buffer = this._bufferService.buffer;
|
||||
if (this._dragScrollAmount > 0) {
|
||||
if (this._activeSelectionMode !== SelectionMode.COLUMN) {
|
||||
this._model.selectionEnd[0] = this._bufferService.cols;
|
||||
}
|
||||
this._model.selectionEnd[1] = Math.min(buffer.ydisp + this._bufferService.rows, buffer.lines.length - 1);
|
||||
} else {
|
||||
if (this._activeSelectionMode !== SelectionMode.COLUMN) {
|
||||
this._model.selectionEnd[0] = 0;
|
||||
}
|
||||
this._model.selectionEnd[1] = buffer.ydisp;
|
||||
}
|
||||
this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the mouseup event, removing the mousedown listeners.
|
||||
* @param event The mouseup event.
|
||||
*/
|
||||
private _onMouseUp(event: MouseEvent): void {
|
||||
const timeElapsed = event.timeStamp - this._mouseDownTimeStamp;
|
||||
|
||||
this._removeMouseDownListeners();
|
||||
|
||||
if (this.selectionText.length <= 1 && timeElapsed < ALT_CLICK_MOVE_CURSOR_TIME) {
|
||||
if (event.altKey) {
|
||||
const coordinates = this._mouseService.getCoords(
|
||||
event,
|
||||
this._element,
|
||||
this._bufferService.cols,
|
||||
this._bufferService.rows,
|
||||
false
|
||||
);
|
||||
if (coordinates && coordinates[0] !== undefined && coordinates[1] !== undefined) {
|
||||
const sequence = moveToCellSequence(coordinates[0] - 1, coordinates[1] - 1, this._bufferService, this._coreService.decPrivateModes.applicationCursorKeys);
|
||||
this._coreService.triggerDataEvent(sequence, true);
|
||||
}
|
||||
}
|
||||
} else if (this.hasSelection) {
|
||||
this._onSelectionChange.fire();
|
||||
}
|
||||
}
|
||||
|
||||
private _onBufferActivate(e: {activeBuffer: IBuffer, inactiveBuffer: IBuffer}): void {
|
||||
this.clearSelection();
|
||||
// Only adjust the selection on trim, shiftElements is rarely used (only in
|
||||
// reverseIndex) and delete in a splice is only ever used when the same
|
||||
// number of elements was just added. Given this is could actually be
|
||||
// beneficial to leave the selection as is for these cases.
|
||||
if (this._trimListener) {
|
||||
this._trimListener.dispose();
|
||||
}
|
||||
this._trimListener = e.activeBuffer.lines.onTrim(amount => this._onTrim(amount));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a viewport column to the character index on the buffer line, the
|
||||
* latter takes into account wide characters.
|
||||
* @param coords The coordinates to find the 2 index for.
|
||||
*/
|
||||
private _convertViewportColToCharacterIndex(bufferLine: IBufferLine, coords: [number, number]): number {
|
||||
let charIndex = coords[0];
|
||||
for (let i = 0; coords[0] >= i; i++) {
|
||||
const length = bufferLine.loadCell(i, this._workCell).getChars().length;
|
||||
if (this._workCell.getWidth() === 0) {
|
||||
// Wide characters aren't included in the line string so decrement the
|
||||
// index so the index is back on the wide character.
|
||||
charIndex--;
|
||||
} else if (length > 1 && coords[0] !== i) {
|
||||
// Emojis take up multiple characters, so adjust accordingly. For these
|
||||
// we don't want ot include the character at the column as we're
|
||||
// returning the start index in the string, not the end index.
|
||||
charIndex += length - 1;
|
||||
}
|
||||
}
|
||||
return charIndex;
|
||||
}
|
||||
|
||||
public setSelection(col: number, row: number, length: number): void {
|
||||
this._model.clearSelection();
|
||||
this._removeMouseDownListeners();
|
||||
this._model.selectionStart = [col, row];
|
||||
this._model.selectionStartLength = length;
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets positional information for the word at the coordinated specified.
|
||||
* @param coords The coordinates to get the word at.
|
||||
*/
|
||||
private _getWordAt(coords: [number, number], allowWhitespaceOnlySelection: boolean, followWrappedLinesAbove: boolean = true, followWrappedLinesBelow: boolean = true): IWordPosition | undefined {
|
||||
// Ensure coords are within viewport (eg. not within scroll bar)
|
||||
if (coords[0] >= this._bufferService.cols) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const buffer = this._bufferService.buffer;
|
||||
const bufferLine = buffer.lines.get(coords[1]);
|
||||
if (!bufferLine) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const line = buffer.translateBufferLineToString(coords[1], false);
|
||||
|
||||
// Get actual index, taking into consideration wide characters
|
||||
let startIndex = this._convertViewportColToCharacterIndex(bufferLine, coords);
|
||||
let endIndex = startIndex;
|
||||
|
||||
// Record offset to be used later
|
||||
const charOffset = coords[0] - startIndex;
|
||||
let leftWideCharCount = 0;
|
||||
let rightWideCharCount = 0;
|
||||
let leftLongCharOffset = 0;
|
||||
let rightLongCharOffset = 0;
|
||||
|
||||
if (line.charAt(startIndex) === ' ') {
|
||||
// Expand until non-whitespace is hit
|
||||
while (startIndex > 0 && line.charAt(startIndex - 1) === ' ') {
|
||||
startIndex--;
|
||||
}
|
||||
while (endIndex < line.length && line.charAt(endIndex + 1) === ' ') {
|
||||
endIndex++;
|
||||
}
|
||||
} else {
|
||||
// Expand until whitespace is hit. This algorithm works by scanning left
|
||||
// and right from the starting position, keeping both the index format
|
||||
// (line) and the column format (bufferLine) in sync. When a wide
|
||||
// character is hit, it is recorded and the column index is adjusted.
|
||||
let startCol = coords[0];
|
||||
let endCol = coords[0];
|
||||
|
||||
// Consider the initial position, skip it and increment the wide char
|
||||
// variable
|
||||
if (bufferLine.getWidth(startCol) === 0) {
|
||||
leftWideCharCount++;
|
||||
startCol--;
|
||||
}
|
||||
if (bufferLine.getWidth(endCol) === 2) {
|
||||
rightWideCharCount++;
|
||||
endCol++;
|
||||
}
|
||||
|
||||
// Adjust the end index for characters whose length are > 1 (emojis)
|
||||
const length = bufferLine.getString(endCol).length;
|
||||
if (length > 1) {
|
||||
rightLongCharOffset += length - 1;
|
||||
endIndex += length - 1;
|
||||
}
|
||||
|
||||
// Expand the string in both directions until a space is hit
|
||||
while (startCol > 0 && startIndex > 0 && !this._isCharWordSeparator(bufferLine.loadCell(startCol - 1, this._workCell))) {
|
||||
bufferLine.loadCell(startCol - 1, this._workCell);
|
||||
const length = this._workCell.getChars().length;
|
||||
if (this._workCell.getWidth() === 0) {
|
||||
// If the next character is a wide char, record it and skip the column
|
||||
leftWideCharCount++;
|
||||
startCol--;
|
||||
} else if (length > 1) {
|
||||
// If the next character's string is longer than 1 char (eg. emoji),
|
||||
// adjust the index
|
||||
leftLongCharOffset += length - 1;
|
||||
startIndex -= length - 1;
|
||||
}
|
||||
startIndex--;
|
||||
startCol--;
|
||||
}
|
||||
while (endCol < bufferLine.length && endIndex + 1 < line.length && !this._isCharWordSeparator(bufferLine.loadCell(endCol + 1, this._workCell))) {
|
||||
bufferLine.loadCell(endCol + 1, this._workCell);
|
||||
const length = this._workCell.getChars().length;
|
||||
if (this._workCell.getWidth() === 2) {
|
||||
// If the next character is a wide char, record it and skip the column
|
||||
rightWideCharCount++;
|
||||
endCol++;
|
||||
} else if (length > 1) {
|
||||
// If the next character's string is longer than 1 char (eg. emoji),
|
||||
// adjust the index
|
||||
rightLongCharOffset += length - 1;
|
||||
endIndex += length - 1;
|
||||
}
|
||||
endIndex++;
|
||||
endCol++;
|
||||
}
|
||||
}
|
||||
|
||||
// Incremenet the end index so it is at the start of the next character
|
||||
endIndex++;
|
||||
|
||||
// Calculate the start _column_, converting the the string indexes back to
|
||||
// column coordinates.
|
||||
let start =
|
||||
startIndex // The index of the selection's start char in the line string
|
||||
+ charOffset // The difference between the initial char's column and index
|
||||
- leftWideCharCount // The number of wide chars left of the initial char
|
||||
+ leftLongCharOffset; // The number of additional chars left of the initial char added by columns with strings longer than 1 (emojis)
|
||||
|
||||
// Calculate the length in _columns_, converting the the string indexes back
|
||||
// to column coordinates.
|
||||
let length = Math.min(this._bufferService.cols, // Disallow lengths larger than the terminal cols
|
||||
endIndex // The index of the selection's end char in the line string
|
||||
- startIndex // The index of the selection's start char in the line string
|
||||
+ leftWideCharCount // The number of wide chars left of the initial char
|
||||
+ rightWideCharCount // The number of wide chars right of the initial char (inclusive)
|
||||
- leftLongCharOffset // The number of additional chars left of the initial char added by columns with strings longer than 1 (emojis)
|
||||
- rightLongCharOffset); // The number of additional chars right of the initial char (inclusive) added by columns with strings longer than 1 (emojis)
|
||||
|
||||
if (!allowWhitespaceOnlySelection && line.slice(startIndex, endIndex).trim() === '') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Recurse upwards if the line is wrapped and the word wraps to the above line
|
||||
if (followWrappedLinesAbove) {
|
||||
if (start === 0 && bufferLine.getCodePoint(0) !== 32 /*' '*/) {
|
||||
const previousBufferLine = buffer.lines.get(coords[1] - 1);
|
||||
if (previousBufferLine && bufferLine.isWrapped && previousBufferLine.getCodePoint(this._bufferService.cols - 1) !== 32 /*' '*/) {
|
||||
const previousLineWordPosition = this._getWordAt([this._bufferService.cols - 1, coords[1] - 1], false, true, false);
|
||||
if (previousLineWordPosition) {
|
||||
const offset = this._bufferService.cols - previousLineWordPosition.start;
|
||||
start -= offset;
|
||||
length += offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse downwards if the line is wrapped and the word wraps to the next line
|
||||
if (followWrappedLinesBelow) {
|
||||
if (start + length === this._bufferService.cols && bufferLine.getCodePoint(this._bufferService.cols - 1) !== 32 /*' '*/) {
|
||||
const nextBufferLine = buffer.lines.get(coords[1] + 1);
|
||||
if (nextBufferLine && nextBufferLine.isWrapped && nextBufferLine.getCodePoint(0) !== 32 /*' '*/) {
|
||||
const nextLineWordPosition = this._getWordAt([0, coords[1] + 1], false, false, true);
|
||||
if (nextLineWordPosition) {
|
||||
length += nextLineWordPosition.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { start, length };
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the word at the coordinates specified.
|
||||
* @param coords The coordinates to get the word at.
|
||||
* @param allowWhitespaceOnlySelection If whitespace should be selected
|
||||
*/
|
||||
protected _selectWordAt(coords: [number, number], allowWhitespaceOnlySelection: boolean): void {
|
||||
const wordPosition = this._getWordAt(coords, allowWhitespaceOnlySelection);
|
||||
if (wordPosition) {
|
||||
// Adjust negative start value
|
||||
while (wordPosition.start < 0) {
|
||||
wordPosition.start += this._bufferService.cols;
|
||||
coords[1]--;
|
||||
}
|
||||
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, true);
|
||||
if (wordPosition) {
|
||||
let endRow = coords[1];
|
||||
|
||||
// Adjust negative start value
|
||||
while (wordPosition.start < 0) {
|
||||
wordPosition.start += this._bufferService.cols;
|
||||
endRow--;
|
||||
}
|
||||
|
||||
// Adjust wrapped length value, this only needs to happen when values are reversed as in that
|
||||
// case we're interested in the start of the word, not the end
|
||||
if (!this._model.areSelectionValuesReversed()) {
|
||||
while (wordPosition.start + wordPosition.length > this._bufferService.cols) {
|
||||
wordPosition.length -= this._bufferService.cols;
|
||||
endRow++;
|
||||
}
|
||||
}
|
||||
|
||||
this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : wordPosition.start + wordPosition.length, endRow];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether the character is considered a word separator by the select
|
||||
* word logic.
|
||||
* @param char The character to check.
|
||||
*/
|
||||
private _isCharWordSeparator(cell: CellData): boolean {
|
||||
// Zero width characters are never separators as they are always to the
|
||||
// right of wide characters
|
||||
if (cell.getWidth() === 0) {
|
||||
return false;
|
||||
}
|
||||
return this._optionsService.options.wordSeparator.indexOf(cell.getChars()) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the line specified.
|
||||
* @param line The line index.
|
||||
*/
|
||||
protected _selectLineAt(line: number): void {
|
||||
const wrappedRange = this._bufferService.buffer.getWrappedRangeForLine(line);
|
||||
this._model.selectionStart = [0, wrappedRange.first];
|
||||
this._model.selectionEnd = [this._bufferService.cols, wrappedRange.last];
|
||||
this._model.selectionStartLength = 0;
|
||||
}
|
||||
}
|
||||
Vendored
+3
-3
@@ -51,11 +51,11 @@ export interface IRenderService {
|
||||
export interface ISelectionService {
|
||||
readonly selectionText: string;
|
||||
readonly hasSelection: boolean;
|
||||
readonly selectionStart: [number, number];
|
||||
readonly selectionEnd: [number, number];
|
||||
readonly selectionStart: [number, number] | undefined;
|
||||
readonly selectionEnd: [number, number] | undefined;
|
||||
|
||||
readonly onLinuxMouseSelection: IEvent<string>;
|
||||
readonly onRedrawRequest: IEvent<ISelectionRedrawRequestEvent>
|
||||
readonly onRedrawRequest: IEvent<ISelectionRedrawRequestEvent>;
|
||||
readonly onSelectionChange: IEvent<void>;
|
||||
|
||||
disable(): void;
|
||||
|
||||
Reference in New Issue
Block a user