From 221db55680ce07a6357d5b503075036b09efe42a Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Thu, 26 May 2022 10:32:53 -0700 Subject: [PATCH 1/2] Add find result count/index to demo Part of #148109 --- demo/client.ts | 20 ++++++++++++++++++-- demo/index.html | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/demo/client.ts b/demo/client.ts index d812ca673d..b67a84a554 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -94,7 +94,8 @@ const terminalContainer = document.getElementById('terminal-container'); const actionElements = { find: document.querySelector('#find'), findNext: document.querySelector('#find-next'), - findPrevious: document.querySelector('#find-previous') + findPrevious: document.querySelector('#find-previous'), + findResults: document.querySelector('#find-results') }; const paddingElement = document.getElementById('padding'); @@ -398,9 +399,12 @@ function initAddons(term: TerminalType): void { if (!addon.canChange) { checkbox.disabled = true; } - if(name === 'unicode11' && checkbox.checked) { + if (name === 'unicode11' && checkbox.checked) { term.unicode.activeVersion = '11'; } + if (name === 'search' && checkbox.checked) { + addon.instance.onDidChangeResults(e => updateFindResults(e)); + } addDomListener(checkbox, 'change', () => { if (checkbox.checked) { addon.instance = new addon.ctor(); @@ -411,6 +415,8 @@ function initAddons(term: TerminalType): void { }, 0); } else if (name === 'unicode11') { term.unicode.activeVersion = '11'; + } else if (name === 'search') { + addon.instance.onDidChangeResults(e => updateFindResults(e)); } } else { if (name === 'webgl') { @@ -439,6 +445,16 @@ function initAddons(term: TerminalType): void { 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 { element.addEventListener(type, handler); term._core.register({ dispose: () => element.removeEventListener(type, handler) }); diff --git a/demo/index.html b/demo/index.html index f05b4e64d7..a460fea16d 100644 --- a/demo/index.html +++ b/demo/index.html @@ -40,6 +40,7 @@

SearchAddon

+
Results:
From b5f53b341dbbf2032ca4e903768008b83a1a07e5 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Thu, 26 May 2022 11:37:54 -0700 Subject: [PATCH 2/2] Test search event, fix returning -1 for no results Fixes #3830 --- addons/xterm-addon-search/src/SearchAddon.ts | 20 +- .../test/SearchAddon.api.ts | 181 +++++++++++++++++- 2 files changed, 190 insertions(+), 11 deletions(-) diff --git a/addons/xterm-addon-search/src/SearchAddon.ts b/addons/xterm-addon-search/src/SearchAddon.ts index 0f356e1446..1c816939a8 100644 --- a/addons/xterm-addon-search/src/SearchAddon.ts +++ b/addons/xterm-addon-search/src/SearchAddon.ts @@ -134,7 +134,7 @@ export class SearchAddon implements ITerminalAddon { } this._lastSearchOptions = searchOptions; 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); } } @@ -288,7 +288,9 @@ export class SearchAddon implements ITerminalAddon { } if (this._searchResults) { - if (this._resultIndex === undefined) { + if (this._searchResults.size === 0) { + this._resultIndex = -1; + } else if (this._resultIndex === undefined) { this._resultIndex = 0; } else { this._resultIndex++; @@ -312,8 +314,10 @@ export class SearchAddon implements ITerminalAddon { throw new Error('Cannot use addon until it has been loaded'); } this._lastSearchOptions = searchOptions; - if (searchOptions?.decorations && (this._resultIndex !== undefined || term !== this._cachedSearchTerm)) { - this._highlightAllMatches(term, searchOptions); + if (searchOptions?.decorations) { + if (this._resultIndex !== undefined || this._cachedSearchTerm === undefined || term !== this._cachedSearchTerm) { + this._highlightAllMatches(term, searchOptions); + } } return this._fireResults(term, this._findPreviousAndSelect(term, searchOptions), searchOptions); } @@ -408,12 +412,14 @@ export class SearchAddon implements ITerminalAddon { } if (this._searchResults) { - if (this._resultIndex === undefined || this._resultIndex < 0) { - this._resultIndex = this._searchResults?.size - 1; + if (this._searchResults.size === 0) { + this._resultIndex = -1; + } else if (this._resultIndex === undefined || this._resultIndex < 0) { + this._resultIndex = this._searchResults.size - 1; } else { this._resultIndex--; if (this._resultIndex === -1) { - this._resultIndex = this._searchResults?.size - 1; + this._resultIndex = this._searchResults.size - 1; } } } diff --git a/addons/xterm-addon-search/test/SearchAddon.api.ts b/addons/xterm-addon-search/test/SearchAddon.api.ts index 7cb15c5da2..e665f54eba 100644 --- a/addons/xterm-addon-search/test/SearchAddon.api.ts +++ b/addons/xterm-addon-search/test/SearchAddon.api.ts @@ -6,7 +6,7 @@ import { assert } from 'chai'; import { readFile } from 'fs'; 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'; 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.goto(APP); await openTerminal(page); - await page.evaluate(`window.search = new SearchAddon();`); - await page.evaluate(`window.term.loadAddon(window.search);`); }); after(() => { @@ -32,7 +30,12 @@ describe('Search Tests', function(): void { }); 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 () => { @@ -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('#2444 wrapped line content not being found', () => { let fixture: string;