Handle optional maxResults correctly

This commit is contained in:
roblou 2016-11-30 15:14:59 -08:00
parent 67567420af
commit 80a0b11735
3 changed files with 11 additions and 11 deletions

View file

@ -83,7 +83,7 @@ export class Engine implements ISearchEngine<ISerializedFileMatch> {
const worker = this.workers[this.nextWorker];
this.nextWorker = (this.nextWorker + 1) % this.workers.length;
const maxResults = this.config.maxResults - this.numResults;
const maxResults = this.config.maxResults && (this.config.maxResults - this.numResults);
worker.search({ absolutePaths: batch, maxResults }).then(result => {
if (!result || this.limitReached || this.isCanceled) {
return unwind(batchBytes);
@ -95,7 +95,7 @@ export class Engine implements ISearchEngine<ISerializedFileMatch> {
onResult(m);
});
if (this.numResults >= this.config.maxResults) {
if (this.config.maxResults && this.numResults >= this.config.maxResults) {
// It's possible to go over maxResults like this, but it's much simpler than trying to extract the exact number
// of file matches, line matches, and matches within a line to == maxResults.
this.limitReached = true;

View file

@ -48,14 +48,14 @@ export class SearchWorker implements ISearchWorker {
search(args: ISearchWorkerSearchArgs): TPromise<ISearchWorkerSearchResult> {
// Queue this search to run after the current one
return this.nextSearch = this.nextSearch
.then(() => searchBatch(args.absolutePaths, this.contentPattern, args.maxResults, this.fileEncoding));
.then(() => searchBatch(args.absolutePaths, this.contentPattern, this.fileEncoding, args.maxResults));
}
}
/**
* Searches some number of the given paths concurrently, and starts searches in other paths when those complete.
*/
function searchBatch(absolutePaths: string[], contentPattern: RegExp, maxResults: number, fileEncoding: string): TPromise<ISearchWorkerSearchResult> {
function searchBatch(absolutePaths: string[], contentPattern: RegExp, fileEncoding: string, maxResults?: number): TPromise<ISearchWorkerSearchResult> {
if (isCanceled) {
return TPromise.wrap(null);
}
@ -69,7 +69,7 @@ function searchBatch(absolutePaths: string[], contentPattern: RegExp, maxResults
// Search in the given path, and when it's finished, search in the next path in absolutePaths
const startSearchInFile = (absolutePath: string): TPromise<void> => {
return searchInFile(absolutePath, contentPattern, maxResults - result.numMatches, fileEncoding).then(fileResult => {
return searchInFile(absolutePath, contentPattern, fileEncoding, maxResults && (maxResults - result.numMatches)).then(fileResult => {
// Finish early if search is canceled
if (isCanceled) {
return;
@ -94,7 +94,7 @@ function searchBatch(absolutePaths: string[], contentPattern: RegExp, maxResults
let batchPromises: TPromise<void>[] = [];
for (let i = 0; i < SearchWorker.CONCURRENT_SEARCH_PATHS && i < absolutePaths.length; i++) {
batchPromises.push(startSearchInFile(absolutePaths[i]));
batchPromises.push(startSearchInFile(absolutePaths.shift()));
}
TPromise.join(batchPromises).then(() => {
@ -109,7 +109,7 @@ interface IFileSearchResult {
limitReached?: boolean;
}
function searchInFile(absolutePath: string, contentPattern: RegExp, maxResults: number, fileEncoding: string): TPromise<IFileSearchResult> {
function searchInFile(absolutePath: string, contentPattern: RegExp, fileEncoding: string, maxResults?: number): TPromise<IFileSearchResult> {
let fileMatch: FileMatch = null;
let limitReached = false;
let numMatches = 0;

View file

@ -5,10 +5,10 @@
'use strict';
import { PPromise, TPromise } from 'vs/base/common/winjs.base';
import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { ISerializedFileMatch } from '../search';
import { IProgress, ILineMatch, IPatternInfo, ISearchStats } from 'vs/platform/search/common/search';
import { IPatternInfo } from 'vs/platform/search/common/search';
import { SearchWorker } from './searchWorker';
export interface ISearchWorkerConfig {
@ -19,7 +19,7 @@ export interface ISearchWorkerConfig {
export interface ISearchWorkerSearchArgs {
absolutePaths: string[];
maxResults: number;
maxResults?: number;
}
export interface ISearchWorkerSearchResult {
@ -46,7 +46,7 @@ export class SearchWorkerChannel implements ISearchWorkerChannel {
}
call(command: string, arg?: any): TPromise<any> {
switch(command) {
switch (command) {
case 'initialize': return TPromise.wrap(this.worker.initialize(arg));
case 'search': return this.worker.search(arg);
case 'cancel': return this.worker.cancel();