forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainThreadSearch.ts
More file actions
174 lines (142 loc) · 6.18 KB
/
mainThreadSearch.ts
File metadata and controls
174 lines (142 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
65
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken } from 'vs/base/common/cancellation';
import { DisposableStore, dispose, IDisposable } from 'vs/base/common/lifecycle';
import { URI, UriComponents } from 'vs/base/common/uri';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { IFileMatch, IFileQuery, IRawFileMatch2, ISearchComplete, ISearchCompleteStats, ISearchProgressItem, ISearchResultProvider, ISearchService, ITextQuery, QueryType, SearchProviderType } from 'vs/workbench/services/search/common/search';
import { ExtHostContext, ExtHostSearchShape, MainContext, MainThreadSearchShape } from '../common/extHost.protocol';
@extHostNamedCustomer(MainContext.MainThreadSearch)
export class MainThreadSearch implements MainThreadSearchShape {
private readonly _proxy: ExtHostSearchShape;
private readonly _searchProvider = new Map<number, RemoteSearchProvider>();
constructor(
extHostContext: IExtHostContext,
@ISearchService private readonly _searchService: ISearchService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@IConfigurationService _configurationService: IConfigurationService,
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostSearch);
this._proxy.$enableExtensionHostSearch();
}
dispose(): void {
this._searchProvider.forEach(value => value.dispose());
this._searchProvider.clear();
}
$registerTextSearchProvider(handle: number, scheme: string): void {
this._searchProvider.set(handle, new RemoteSearchProvider(this._searchService, SearchProviderType.text, scheme, handle, this._proxy));
}
$registerFileSearchProvider(handle: number, scheme: string): void {
this._searchProvider.set(handle, new RemoteSearchProvider(this._searchService, SearchProviderType.file, scheme, handle, this._proxy));
}
$unregisterProvider(handle: number): void {
dispose(this._searchProvider.get(handle));
this._searchProvider.delete(handle);
}
$handleFileMatch(handle: number, session: number, data: UriComponents[]): void {
const provider = this._searchProvider.get(handle);
if (!provider) {
throw new Error('Got result for unknown provider');
}
provider.handleFindMatch(session, data);
}
$handleTextMatch(handle: number, session: number, data: IRawFileMatch2[]): void {
const provider = this._searchProvider.get(handle);
if (!provider) {
throw new Error('Got result for unknown provider');
}
provider.handleFindMatch(session, data);
}
$handleTelemetry(eventName: string, data: any): void {
this._telemetryService.publicLog(eventName, data);
}
}
class SearchOperation {
private static _idPool = 0;
constructor(
readonly progress?: (match: IFileMatch) => any,
readonly id: number = ++SearchOperation._idPool,
readonly matches = new Map<string, IFileMatch>()
) {
//
}
addMatch(match: IFileMatch): void {
const existingMatch = this.matches.get(match.resource.toString());
if (existingMatch) {
// TODO@rob clean up text/file result types
// If a file search returns the same file twice, we would enter this branch.
// It's possible that could happen, #90813
if (existingMatch.results && match.results) {
existingMatch.results.push(...match.results);
}
} else {
this.matches.set(match.resource.toString(), match);
}
this.progress?.(match);
}
}
class RemoteSearchProvider implements ISearchResultProvider, IDisposable {
private readonly _registrations = new DisposableStore();
private readonly _searches = new Map<number, SearchOperation>();
constructor(
searchService: ISearchService,
type: SearchProviderType,
private readonly _scheme: string,
private readonly _handle: number,
private readonly _proxy: ExtHostSearchShape
) {
this._registrations.add(searchService.registerSearchResultProvider(this._scheme, type, this));
}
dispose(): void {
this._registrations.dispose();
}
fileSearch(query: IFileQuery, token: CancellationToken = CancellationToken.None): Promise<ISearchComplete> {
return this.doSearch(query, undefined, token);
}
textSearch(query: ITextQuery, onProgress?: (p: ISearchProgressItem) => void, token: CancellationToken = CancellationToken.None): Promise<ISearchComplete> {
return this.doSearch(query, onProgress, token);
}
doSearch(query: ITextQuery | IFileQuery, onProgress?: (p: ISearchProgressItem) => void, token: CancellationToken = CancellationToken.None): Promise<ISearchComplete> {
if (!query.folderQueries.length) {
throw new Error('Empty folderQueries');
}
const search = new SearchOperation(onProgress);
this._searches.set(search.id, search);
const searchP = query.type === QueryType.File
? this._proxy.$provideFileSearchResults(this._handle, search.id, query, token)
: this._proxy.$provideTextSearchResults(this._handle, search.id, query, token);
return Promise.resolve(searchP).then((result: ISearchCompleteStats) => {
this._searches.delete(search.id);
return { results: Array.from(search.matches.values()), stats: result.stats, limitHit: result.limitHit, messages: result.messages };
}, err => {
this._searches.delete(search.id);
return Promise.reject(err);
});
}
clearCache(cacheKey: string): Promise<void> {
return Promise.resolve(this._proxy.$clearCache(cacheKey));
}
handleFindMatch(session: number, dataOrUri: Array<UriComponents | IRawFileMatch2>): void {
const searchOp = this._searches.get(session);
if (!searchOp) {
// ignore...
return;
}
dataOrUri.forEach(result => {
if ((<IRawFileMatch2>result).results) {
searchOp.addMatch({
resource: URI.revive((<IRawFileMatch2>result).resource),
results: (<IRawFileMatch2>result).results
});
} else {
searchOp.addMatch({
resource: URI.revive(<UriComponents>result)
});
}
});
}
}