rawSearchService.ts 2.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import fs = require('fs');

import gracefulFs = require('graceful-fs');
gracefulFs.gracefulify(fs);

import {PPromise} from 'vs/base/common/winjs.base';
import glob = require('vs/base/common/glob');
import {IProgress, ILineMatch, IPatternInfo} from 'vs/platform/search/common/search';
import {FileWalker, Engine as FileSearchEngine} from 'vs/workbench/services/search/node/fileSearch';
import {Engine as TextSearchEngine} from 'vs/workbench/services/search/node/textSearch';

export interface IRawSearch {
	rootPaths: string[];
21
	filePattern?: string;
E
Erich Gamma 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	excludePattern?: glob.IExpression;
	includePattern?: glob.IExpression;
	contentPattern?: IPatternInfo;
	maxResults?: number;
	fileEncoding?: string;
}

export interface IRawSearchService {
	fileSearch(search: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem>;
	textSearch(search: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem>;
}

export interface ISearchEngine {
	search: (onResult: (match: ISerializedFileMatch) => void, onProgress: (progress: IProgress) => void, done: (error: Error, isLimitHit: boolean) => void) => void;
	cancel: () => void;
}

export interface ISerializedSearchComplete {
	limitHit: boolean;
}

export interface ISerializedFileMatch {
	path?: string;
	lineMatches?: ILineMatch[];
}

export interface ISerializedSearchProgressItem extends ISerializedFileMatch, IProgress {
	// Marker interface to indicate the possible values for progress calls from the engine
}

export class SearchService implements IRawSearchService {

	public fileSearch(config: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem> {
		let engine = new FileSearchEngine(config);

		return this.doSearch(engine);
	}

	public textSearch(config: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem> {
		let engine = new TextSearchEngine(config, new FileWalker({
			rootPaths: config.rootPaths,
			includePattern: config.includePattern,
			excludePattern: config.excludePattern,
65
			filePattern: config.filePattern
E
Erich Gamma 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		}));

		return this.doSearch(engine);
	}

	private doSearch(engine: ISearchEngine): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem> {
		return new PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem>((c, e, p) => {
			engine.search((match) => {
				if (match) {
					p(match);
				}
			}, (progress) => {
				p(progress);
			}, (error, isLimitHit) => {
				if (error) {
					e(error);
				} else {
					c({
						limitHit: isLimitHit
					});
				}
			});
		}, () => engine.cancel());
	}
}