search.ts 4.1 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
9
import { IExpression } from 'vs/base/common/glob';
10
import { IProgress, ILineMatch, IPatternInfo, ISearchStats } from 'vs/platform/search/common/search';
C
Christof Marti 已提交
11
import { ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
12
import { Event } from 'vs/base/common/event';
J
Joao Moreno 已提交
13

R
Rob Lourens 已提交
14 15 16
export interface IFolderSearch {
	folder: string;
	excludePattern?: IExpression;
17
	includePattern?: IExpression;
R
Rob Lourens 已提交
18
	fileEncoding?: string;
19
	disregardIgnoreFiles?: boolean;
R
Rob Lourens 已提交
20 21
}

J
Joao Moreno 已提交
22
export interface IRawSearch {
R
Rob Lourens 已提交
23
	folderQueries: IFolderSearch[];
24
	ignoreSymlinks?: boolean;
J
Joao Moreno 已提交
25 26
	extraFiles?: string[];
	filePattern?: string;
B
Benjamin Pasero 已提交
27 28
	excludePattern?: IExpression;
	includePattern?: IExpression;
J
Joao Moreno 已提交
29 30
	contentPattern?: IPatternInfo;
	maxResults?: number;
31
	exists?: boolean;
32 33
	sortByScore?: boolean;
	cacheKey?: string;
J
Joao Moreno 已提交
34
	maxFilesize?: number;
35
	useRipgrep?: boolean;
R
Rob Lourens 已提交
36
	disregardIgnoreFiles?: boolean;
J
Joao Moreno 已提交
37 38
}

C
Christof Marti 已提交
39 40 41 42 43
export interface ITelemetryEvent {
	eventName: string;
	data: ITelemetryData;
}

J
Joao Moreno 已提交
44
export interface IRawSearchService {
45 46
	fileSearch(search: IRawSearch): Event<ISerializedSearchProgressItem | ISerializedSearchComplete>;
	textSearch(search: IRawSearch): Event<ISerializedSearchProgressItem | ISerializedSearchComplete>;
47
	clearCache(cacheKey: string): TPromise<void>;
48
	readonly onTelemetry: Event<ITelemetryEvent>;
J
Joao Moreno 已提交
49 50
}

51
export interface IRawFileMatch {
C
Christof Marti 已提交
52
	base?: string;
53
	relativePath: string;
54
	basename: string;
C
Christof Marti 已提交
55
	size?: number;
56 57 58
}

export interface ISearchEngine<T> {
59
	search: (onResult: (matches: T) => void, onProgress: (progress: IProgress) => void, done: (error: Error, complete: ISerializedSearchSuccess) => void) => void;
J
Joao Moreno 已提交
60 61 62
	cancel: () => void;
}

63 64
export interface ISerializedSearchSuccess {
	type: 'success';
J
Joao Moreno 已提交
65
	limitHit: boolean;
C
chrmarti 已提交
66
	stats: ISearchStats;
J
Joao Moreno 已提交
67 68
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
export interface ISerializedSearchError {
	type: 'error';
	error: any;
}

export type ISerializedSearchComplete = ISerializedSearchSuccess | ISerializedSearchError;

export function isSerializedSearchComplete(arg: ISerializedSearchProgressItem | ISerializedSearchComplete): arg is ISerializedSearchComplete {
	if ((arg as any).type === 'error') {
		return true;
	} else if ((arg as any).type === 'success') {
		return true;
	} else {
		return false;
	}
}

export function isSerializedSearchSuccess(arg: ISerializedSearchComplete): arg is ISerializedSearchSuccess {
	return arg.type === 'success';
}

J
Joao Moreno 已提交
90
export interface ISerializedFileMatch {
91
	path: string;
J
Joao Moreno 已提交
92
	lineMatches?: ILineMatch[];
93
	numMatches?: number;
J
Joao Moreno 已提交
94 95
}

C
chrmarti 已提交
96
// Type of the possible values for progress calls from the engine
97
export type ISerializedSearchProgressItem = ISerializedFileMatch | ISerializedFileMatch[] | IProgress;
R
Rob Lourens 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
export type IFileSearchProgressItem = IRawFileMatch | IRawFileMatch[] | IProgress;


export class FileMatch implements ISerializedFileMatch {
	path: string;
	lineMatches: LineMatch[];

	constructor(path: string) {
		this.path = path;
		this.lineMatches = [];
	}

	addMatch(lineMatch: LineMatch): void {
		this.lineMatches.push(lineMatch);
	}

	serialize(): ISerializedFileMatch {
		let lineMatches: ILineMatch[] = [];
		let numMatches = 0;

		for (let i = 0; i < this.lineMatches.length; i++) {
			numMatches += this.lineMatches[i].offsetAndLengths.length;
			lineMatches.push(this.lineMatches[i].serialize());
		}

		return {
			path: this.path,
			lineMatches,
			numMatches
		};
	}
}

export class LineMatch implements ILineMatch {
	preview: string;
	lineNumber: number;
	offsetAndLengths: number[][];

	constructor(preview: string, lineNumber: number) {
		this.preview = preview.replace(/(\r|\n)*$/, '');
		this.lineNumber = lineNumber;
		this.offsetAndLengths = [];
	}

	addMatch(offset: number, length: number): void {
		this.offsetAndLengths.push([offset, length]);
	}

	serialize(): ILineMatch {
		const result = {
			preview: this.preview,
			lineNumber: this.lineNumber,
			offsetAndLengths: this.offsetAndLengths
		};

		return result;
	}
}