search.ts 3.5 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 { PPromise, 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';
J
Joao Moreno 已提交
12

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

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

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

J
Joao Moreno 已提交
43 44 45
export interface IRawSearchService {
	fileSearch(search: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem>;
	textSearch(search: IRawSearch): PPromise<ISerializedSearchComplete, ISerializedSearchProgressItem>;
46
	clearCache(cacheKey: string): TPromise<void>;
C
Christof Marti 已提交
47
	fetchTelemetry(): PPromise<void, ITelemetryEvent>;
J
Joao Moreno 已提交
48 49
}

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

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

export interface ISerializedSearchComplete {
	limitHit: boolean;
C
chrmarti 已提交
64
	stats: ISearchStats;
J
Joao Moreno 已提交
65 66 67
}

export interface ISerializedFileMatch {
68
	path: string;
J
Joao Moreno 已提交
69
	lineMatches?: ILineMatch[];
70
	numMatches?: number;
J
Joao Moreno 已提交
71 72
}

C
chrmarti 已提交
73
// Type of the possible values for progress calls from the engine
74
export type ISerializedSearchProgressItem = ISerializedFileMatch | ISerializedFileMatch[] | IProgress;
R
Rob Lourens 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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
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;
	}
}