Merge pull request #3831 from Tyriar/vscode148109
Test search event, fix returning -1 for no results
This commit is contained in:
@@ -134,7 +134,7 @@ export class SearchAddon implements ITerminalAddon {
|
|||||||
}
|
}
|
||||||
this._lastSearchOptions = searchOptions;
|
this._lastSearchOptions = searchOptions;
|
||||||
if (searchOptions?.decorations) {
|
if (searchOptions?.decorations) {
|
||||||
if (this._resultIndex !== undefined || this._cachedSearchTerm && term !== this._cachedSearchTerm) {
|
if (this._resultIndex !== undefined || this._cachedSearchTerm === undefined || term !== this._cachedSearchTerm) {
|
||||||
this._highlightAllMatches(term, searchOptions);
|
this._highlightAllMatches(term, searchOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -288,7 +288,9 @@ export class SearchAddon implements ITerminalAddon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this._searchResults) {
|
if (this._searchResults) {
|
||||||
if (this._resultIndex === undefined) {
|
if (this._searchResults.size === 0) {
|
||||||
|
this._resultIndex = -1;
|
||||||
|
} else if (this._resultIndex === undefined) {
|
||||||
this._resultIndex = 0;
|
this._resultIndex = 0;
|
||||||
} else {
|
} else {
|
||||||
this._resultIndex++;
|
this._resultIndex++;
|
||||||
@@ -312,8 +314,10 @@ export class SearchAddon implements ITerminalAddon {
|
|||||||
throw new Error('Cannot use addon until it has been loaded');
|
throw new Error('Cannot use addon until it has been loaded');
|
||||||
}
|
}
|
||||||
this._lastSearchOptions = searchOptions;
|
this._lastSearchOptions = searchOptions;
|
||||||
if (searchOptions?.decorations && (this._resultIndex !== undefined || term !== this._cachedSearchTerm)) {
|
if (searchOptions?.decorations) {
|
||||||
this._highlightAllMatches(term, searchOptions);
|
if (this._resultIndex !== undefined || this._cachedSearchTerm === undefined || term !== this._cachedSearchTerm) {
|
||||||
|
this._highlightAllMatches(term, searchOptions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this._fireResults(term, this._findPreviousAndSelect(term, searchOptions), searchOptions);
|
return this._fireResults(term, this._findPreviousAndSelect(term, searchOptions), searchOptions);
|
||||||
}
|
}
|
||||||
@@ -408,12 +412,14 @@ export class SearchAddon implements ITerminalAddon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this._searchResults) {
|
if (this._searchResults) {
|
||||||
if (this._resultIndex === undefined || this._resultIndex < 0) {
|
if (this._searchResults.size === 0) {
|
||||||
this._resultIndex = this._searchResults?.size - 1;
|
this._resultIndex = -1;
|
||||||
|
} else if (this._resultIndex === undefined || this._resultIndex < 0) {
|
||||||
|
this._resultIndex = this._searchResults.size - 1;
|
||||||
} else {
|
} else {
|
||||||
this._resultIndex--;
|
this._resultIndex--;
|
||||||
if (this._resultIndex === -1) {
|
if (this._resultIndex === -1) {
|
||||||
this._resultIndex = this._searchResults?.size - 1;
|
this._resultIndex = this._searchResults.size - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
import { assert } from 'chai';
|
import { assert } from 'chai';
|
||||||
import { readFile } from 'fs';
|
import { readFile } from 'fs';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { openTerminal, writeSync, launchBrowser } from '../../../out-test/api/TestUtils';
|
import { openTerminal, writeSync, launchBrowser, timeout } from '../../../out-test/api/TestUtils';
|
||||||
import { Browser, Page } from 'playwright';
|
import { Browser, Page } from 'playwright';
|
||||||
|
|
||||||
const APP = 'http://127.0.0.1:3001/test';
|
const APP = 'http://127.0.0.1:3001/test';
|
||||||
@@ -23,8 +23,6 @@ describe('Search Tests', function(): void {
|
|||||||
await page.setViewportSize({ width, height });
|
await page.setViewportSize({ width, height });
|
||||||
await page.goto(APP);
|
await page.goto(APP);
|
||||||
await openTerminal(page);
|
await openTerminal(page);
|
||||||
await page.evaluate(`window.search = new SearchAddon();`);
|
|
||||||
await page.evaluate(`window.term.loadAddon(window.search);`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
after(() => {
|
after(() => {
|
||||||
@@ -32,7 +30,12 @@ describe('Search Tests', function(): void {
|
|||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await page.evaluate(`window.term.reset()`);
|
await page.evaluate(`
|
||||||
|
window.term.reset()
|
||||||
|
window.search?.dispose();
|
||||||
|
window.search = new SearchAddon();
|
||||||
|
window.term.loadAddon(window.search);
|
||||||
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Simple Search', async () => {
|
it('Simple Search', async () => {
|
||||||
@@ -120,6 +123,176 @@ describe('Search Tests', function(): void {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('onDidChangeResults', async () => {
|
||||||
|
describe('findNext', () => {
|
||||||
|
it('should not fire unless the decorations option is set', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc');
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('a')`), true);
|
||||||
|
assert.strictEqual(await page.evaluate('window.calls.length'), 0);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('b', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate('window.calls.length'), 1);
|
||||||
|
});
|
||||||
|
it('should fire with correct event values', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc bc c');
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('a', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('b', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('d', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), false);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 }
|
||||||
|
]);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 },
|
||||||
|
{ resultCount: 3, resultIndex: 0 },
|
||||||
|
{ resultCount: 3, resultIndex: 1 },
|
||||||
|
{ resultCount: 3, resultIndex: 2 }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
it('should fire with correct event values (incremental)', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc aabc');
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findNext('a', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findNext('ab', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findNext('abc', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findNext('abc', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findNext('abcd', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), false);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('findPrevious', () => {
|
||||||
|
it('should not fire unless the decorations option is set', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc');
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('a')`), true);
|
||||||
|
assert.strictEqual(await page.evaluate('window.calls.length'), 0);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('b', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate('window.calls.length'), 1);
|
||||||
|
});
|
||||||
|
it('should fire with correct event values', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc bc c');
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('a', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('b', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 }
|
||||||
|
]);
|
||||||
|
await timeout(2000);
|
||||||
|
assert.strictEqual(await page.evaluate(`debugger; window.search.findPrevious('d', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), false);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 }
|
||||||
|
]);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 1, resultIndex: 0 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 },
|
||||||
|
{ resultCount: 3, resultIndex: 2 },
|
||||||
|
{ resultCount: 3, resultIndex: 1 },
|
||||||
|
{ resultCount: 3, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
it('should fire with correct event values (incremental)', async () => {
|
||||||
|
await page.evaluate(`
|
||||||
|
window.calls = [];
|
||||||
|
window.search.onDidChangeResults(e => window.calls.push(e));
|
||||||
|
`);
|
||||||
|
await writeSync(page, 'abc aabc');
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findPrevious('a', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 2 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findPrevious('ab', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 2 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findPrevious('abc', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 2 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findPrevious('abc', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 2 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 }
|
||||||
|
]);
|
||||||
|
assert.deepStrictEqual(await page.evaluate(`window.search.findPrevious('abcd', { incremental: true, decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), false);
|
||||||
|
assert.deepStrictEqual(await page.evaluate('window.calls'), [
|
||||||
|
{ resultCount: 3, resultIndex: 2 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: 2, resultIndex: 1 },
|
||||||
|
{ resultCount: 2, resultIndex: 0 },
|
||||||
|
{ resultCount: -1, resultIndex: -1 }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Regression tests', () => {
|
describe('Regression tests', () => {
|
||||||
describe('#2444 wrapped line content not being found', () => {
|
describe('#2444 wrapped line content not being found', () => {
|
||||||
let fixture: string;
|
let fixture: string;
|
||||||
|
|||||||
+18
-2
@@ -94,7 +94,8 @@ const terminalContainer = document.getElementById('terminal-container');
|
|||||||
const actionElements = {
|
const actionElements = {
|
||||||
find: <HTMLInputElement>document.querySelector('#find'),
|
find: <HTMLInputElement>document.querySelector('#find'),
|
||||||
findNext: <HTMLInputElement>document.querySelector('#find-next'),
|
findNext: <HTMLInputElement>document.querySelector('#find-next'),
|
||||||
findPrevious: <HTMLInputElement>document.querySelector('#find-previous')
|
findPrevious: <HTMLInputElement>document.querySelector('#find-previous'),
|
||||||
|
findResults: document.querySelector('#find-results')
|
||||||
};
|
};
|
||||||
const paddingElement = <HTMLInputElement>document.getElementById('padding');
|
const paddingElement = <HTMLInputElement>document.getElementById('padding');
|
||||||
|
|
||||||
@@ -398,9 +399,12 @@ function initAddons(term: TerminalType): void {
|
|||||||
if (!addon.canChange) {
|
if (!addon.canChange) {
|
||||||
checkbox.disabled = true;
|
checkbox.disabled = true;
|
||||||
}
|
}
|
||||||
if(name === 'unicode11' && checkbox.checked) {
|
if (name === 'unicode11' && checkbox.checked) {
|
||||||
term.unicode.activeVersion = '11';
|
term.unicode.activeVersion = '11';
|
||||||
}
|
}
|
||||||
|
if (name === 'search' && checkbox.checked) {
|
||||||
|
addon.instance.onDidChangeResults(e => updateFindResults(e));
|
||||||
|
}
|
||||||
addDomListener(checkbox, 'change', () => {
|
addDomListener(checkbox, 'change', () => {
|
||||||
if (checkbox.checked) {
|
if (checkbox.checked) {
|
||||||
addon.instance = new addon.ctor();
|
addon.instance = new addon.ctor();
|
||||||
@@ -411,6 +415,8 @@ function initAddons(term: TerminalType): void {
|
|||||||
}, 0);
|
}, 0);
|
||||||
} else if (name === 'unicode11') {
|
} else if (name === 'unicode11') {
|
||||||
term.unicode.activeVersion = '11';
|
term.unicode.activeVersion = '11';
|
||||||
|
} else if (name === 'search') {
|
||||||
|
addon.instance.onDidChangeResults(e => updateFindResults(e));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (name === 'webgl') {
|
if (name === 'webgl') {
|
||||||
@@ -439,6 +445,16 @@ function initAddons(term: TerminalType): void {
|
|||||||
container.appendChild(fragment);
|
container.appendChild(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateFindResults(e: { resultIndex: number, resultCount: number } | undefined) {
|
||||||
|
let content: string;
|
||||||
|
if (e === undefined) {
|
||||||
|
content = 'undefined';
|
||||||
|
} else {
|
||||||
|
content = `index: ${e.resultIndex}, count: ${e.resultCount}`;
|
||||||
|
}
|
||||||
|
actionElements.findResults.textContent = content;
|
||||||
|
}
|
||||||
|
|
||||||
function addDomListener(element: HTMLElement, type: string, handler: (...args: any[]) => any): void {
|
function addDomListener(element: HTMLElement, type: string, handler: (...args: any[]) => any): void {
|
||||||
element.addEventListener(type, handler);
|
element.addEventListener(type, handler);
|
||||||
term._core.register({ dispose: () => element.removeEventListener(type, handler) });
|
term._core.register({ dispose: () => element.removeEventListener(type, handler) });
|
||||||
|
|||||||
@@ -40,6 +40,7 @@
|
|||||||
<div style= "display:flex; flex-direction:column;">
|
<div style= "display:flex; flex-direction:column;">
|
||||||
<label>Find next <input id="find-next"/></label>
|
<label>Find next <input id="find-next"/></label>
|
||||||
<label>Find previous <input id="find-previous"/></label>
|
<label>Find previous <input id="find-previous"/></label>
|
||||||
|
<div>Results: <span id="find-results"></span></div>
|
||||||
<label><input type="checkbox" id="regex"/>Use regex</label>
|
<label><input type="checkbox" id="regex"/>Use regex</label>
|
||||||
<label><input type="checkbox" id="case-sensitive"/>Case sensitive</label>
|
<label><input type="checkbox" id="case-sensitive"/>Case sensitive</label>
|
||||||
<label><input type="checkbox" id="whole-word"/>Whole word</label>
|
<label><input type="checkbox" id="whole-word"/>Whole word</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user