Skip to content

Commit e77a0ec

Browse files
authored
return 0 instead of -1 for no result find count (#3874)
1 parent 1bf2ccc commit e77a0ec

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

addons/xterm-addon-search/src/SearchAddon.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class SearchAddon implements ITerminalAddon {
5555
private _cachedSearchTerm: string | undefined;
5656
private _selectedDecoration: IDecoration | undefined;
5757
private _resultDecorations: Map<number, IDecoration[]> | undefined;
58-
private _searchResults: Map<string, ISearchResult> | undefined;
58+
private _searchResults: Map<string, ISearchResult> | undefined;
5959
private _onDataDisposable: IDisposable | undefined;
6060
private _onResizeDisposable: IDisposable | undefined;
6161
private _lastSearchOptions: ISearchOptions | undefined;
@@ -72,7 +72,7 @@ export class SearchAddon implements ITerminalAddon {
7272

7373
private _resultIndex: number | undefined;
7474

75-
private readonly _onDidChangeResults = new EventEmitter<{resultIndex: number, resultCount: number} | undefined>();
75+
private readonly _onDidChangeResults = new EventEmitter<{ resultIndex: number, resultCount: number } | undefined>();
7676
public readonly onDidChangeResults = this._onDidChangeResults.event;
7777

7878
public activate(terminal: Terminal): void {
@@ -87,9 +87,9 @@ export class SearchAddon implements ITerminalAddon {
8787
}
8888
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
8989
this._highlightTimeout = setTimeout(() => {
90-
this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
91-
this._resultIndex = this._searchResults ? this._searchResults.size -1 : -1;
92-
this._onDidChangeResults.fire({ resultIndex: this._searchResults ? this._searchResults.size - 1 : -1, resultCount: this._searchResults ? this._searchResults.size : -1 });
90+
this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
91+
this._resultIndex = this._searchResults ? this._searchResults.size - 1 : -1;
92+
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults?.size ?? -1 });
9393
}, 200);
9494
}
9595
}
@@ -324,10 +324,8 @@ export class SearchAddon implements ITerminalAddon {
324324

325325
private _fireResults(term: string, found: boolean, searchOptions?: ISearchOptions): boolean {
326326
if (searchOptions?.decorations) {
327-
if (found && this._resultIndex !== undefined && this._searchResults?.size) {
327+
if (this._resultIndex !== undefined && this._searchResults?.size !== undefined) {
328328
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size });
329-
} else if (this._resultIndex === -1) {
330-
this._onDidChangeResults.fire({ resultIndex: -1, resultCount: -1 });
331329
} else {
332330
this._onDidChangeResults.fire(undefined);
333331
}
@@ -693,7 +691,7 @@ export class SearchAddon implements ITerminalAddon {
693691
}
694692

695693
if (!noScroll) {
696-
// If it is not in the viewport then we scroll else it just gets selected
694+
// If it is not in the viewport then we scroll else it just gets selected
697695
if (result.row >= (terminal.buffer.active.viewportY + terminal.rows) || result.row < terminal.buffer.active.viewportY) {
698696
let scroll = result.row - terminal.buffer.active.viewportY;
699697
scroll -= Math.floor(terminal.rows / 2);

addons/xterm-addon-search/test/SearchAddon.api.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ let page: Page;
1616
const width = 800;
1717
const height = 600;
1818

19-
describe('Search Tests', function(): void {
20-
before(async function(): Promise<any> {
19+
describe('Search Tests', function (): void {
20+
before(async function (): Promise<any> {
2121
browser = await launchBrowser();
2222
page = await (await browser.newContext()).newPage();
2323
await page.setViewportSize({ width, height });
@@ -155,15 +155,15 @@ describe('Search Tests', function(): void {
155155
assert.deepStrictEqual(await page.evaluate('window.calls'), [
156156
{ resultCount: 1, resultIndex: 0 },
157157
{ resultCount: 2, resultIndex: 0 },
158-
{ resultCount: -1, resultIndex: -1 }
158+
{ resultCount: 0, resultIndex: -1 }
159159
]);
160160
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
161161
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
162162
assert.strictEqual(await page.evaluate(`window.search.findNext('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
163163
assert.deepStrictEqual(await page.evaluate('window.calls'), [
164164
{ resultCount: 1, resultIndex: 0 },
165165
{ resultCount: 2, resultIndex: 0 },
166-
{ resultCount: -1, resultIndex: -1 },
166+
{ resultCount: 0, resultIndex: -1 },
167167
{ resultCount: 3, resultIndex: 0 },
168168
{ resultCount: 3, resultIndex: 1 },
169169
{ resultCount: 3, resultIndex: 2 }
@@ -203,7 +203,7 @@ describe('Search Tests', function(): void {
203203
{ resultCount: 2, resultIndex: 0 },
204204
{ resultCount: 2, resultIndex: 0 },
205205
{ resultCount: 2, resultIndex: 1 },
206-
{ resultCount: -1, resultIndex: -1 }
206+
{ resultCount: 0, resultIndex: -1 }
207207
]);
208208
});
209209
});
@@ -239,15 +239,15 @@ describe('Search Tests', function(): void {
239239
assert.deepStrictEqual(await page.evaluate('window.calls'), [
240240
{ resultCount: 1, resultIndex: 0 },
241241
{ resultCount: 2, resultIndex: 1 },
242-
{ resultCount: -1, resultIndex: -1 }
242+
{ resultCount: 0, resultIndex: -1 }
243243
]);
244244
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
245245
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
246246
assert.strictEqual(await page.evaluate(`window.search.findPrevious('c', { decorations: { activeMatchColorOverviewRuler: '#ff0000' } })`), true);
247247
assert.deepStrictEqual(await page.evaluate('window.calls'), [
248248
{ resultCount: 1, resultIndex: 0 },
249249
{ resultCount: 2, resultIndex: 1 },
250-
{ resultCount: -1, resultIndex: -1 },
250+
{ resultCount: 0, resultIndex: -1 },
251251
{ resultCount: 3, resultIndex: 2 },
252252
{ resultCount: 3, resultIndex: 1 },
253253
{ resultCount: 3, resultIndex: 0 }
@@ -287,7 +287,7 @@ describe('Search Tests', function(): void {
287287
{ resultCount: 2, resultIndex: 1 },
288288
{ resultCount: 2, resultIndex: 1 },
289289
{ resultCount: 2, resultIndex: 0 },
290-
{ resultCount: -1, resultIndex: -1 }
290+
{ resultCount: 0, resultIndex: -1 }
291291
]);
292292
});
293293
});

addons/xterm-addon-search/typings/xterm-addon-search.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ declare module 'xterm-addon-search' {
121121
/**
122122
* When decorations are enabled, fires when
123123
* the search results change.
124-
* @returns -1 if there are no matches and
125-
* @returns undefined when the threshold of 1k results
124+
* @returns -1 for resultIndex for a resultCount of 0
125+
* and @returns undefined when the threshold of 1k results
126126
* is exceeded and decorations are disposed of.
127127
*/
128128
readonly onDidChangeResults: IEvent<{ resultIndex: number, resultCount: number } | undefined>;

0 commit comments

Comments
 (0)