mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
SearchProvider - split out separate TextSearchProvider #47058
This commit is contained in:
parent
9cc1fc914d
commit
9999dac541
|
@ -13,12 +13,13 @@ export function activate(): void {
|
|||
const outputChannel = vscode.window.createOutputChannel('search-rg');
|
||||
const provider = new RipgrepSearchProvider(outputChannel);
|
||||
vscode.workspace.registerSearchProvider('file', provider);
|
||||
vscode.workspace.registerTextSearchProvider('file', provider);
|
||||
}
|
||||
}
|
||||
|
||||
type SearchEngine = RipgrepFileSearchEngine | RipgrepTextSearchEngine;
|
||||
|
||||
class RipgrepSearchProvider implements vscode.SearchProvider {
|
||||
class RipgrepSearchProvider implements vscode.SearchProvider, vscode.TextSearchProvider {
|
||||
private cachedProvider: CachedSearchProvider;
|
||||
private inProgress: Set<SearchEngine> = new Set();
|
||||
|
||||
|
|
39
src/vs/vscode.proposed.d.ts
vendored
39
src/vs/vscode.proposed.d.ts
vendored
|
@ -160,6 +160,25 @@ declare module 'vscode' {
|
|||
preview: TextSearchResultPreview;
|
||||
}
|
||||
|
||||
// interface FileIndexProvider {
|
||||
// provideFileIndex(options: FileSearchOptions, token: CancellationToken): Thenable<Uri[]>
|
||||
// }
|
||||
|
||||
// interface FileSearchProvider {
|
||||
// provideFileSearchResults(query: FileSear, options, token): Thenable<Uri[]>
|
||||
// }
|
||||
|
||||
interface TextSearchProvider {
|
||||
/**
|
||||
* Provide results that match the given text pattern.
|
||||
* @param query The parameters for this query.
|
||||
* @param options A set of options to consider while searching.
|
||||
* @param progress A progress callback that must be invoked for all results.
|
||||
* @param token A cancellation token.
|
||||
*/
|
||||
provideTextSearchResults?(query: TextSearchQuery, options: TextSearchOptions, progress: Progress<TextSearchResult>, token: CancellationToken): Thenable<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A SearchProvider provides search results for files or text in files. It can be invoked by quickopen, the search viewlet, and other extensions.
|
||||
*/
|
||||
|
@ -178,15 +197,6 @@ declare module 'vscode' {
|
|||
* @param cacheKey The same key that was passed as `query.cacheKey`.
|
||||
*/
|
||||
clearCache?(cacheKey: string): void;
|
||||
|
||||
/**
|
||||
* Provide results that match the given text pattern.
|
||||
* @param query The parameters for this query.
|
||||
* @param options A set of options to consider while searching.
|
||||
* @param progress A progress callback that must be invoked for all results.
|
||||
* @param token A cancellation token.
|
||||
*/
|
||||
provideTextSearchResults?(query: TextSearchQuery, options: TextSearchOptions, progress: Progress<TextSearchResult>, token: CancellationToken): Thenable<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -243,6 +253,17 @@ declare module 'vscode' {
|
|||
*/
|
||||
export function registerSearchProvider(scheme: string, provider: SearchProvider): Disposable;
|
||||
|
||||
/**
|
||||
* Register a text search provider.
|
||||
*
|
||||
* Only one provider can be registered per scheme.
|
||||
*
|
||||
* @param scheme The provider will be invoked for workspace folders that have this file scheme.
|
||||
* @param provider The provider.
|
||||
* @return A [disposable](#Disposable) that unregisters this provider when being disposed.
|
||||
*/
|
||||
export function registerTextSearchProvider(scheme: string, provider: TextSearchProvider): Disposable;
|
||||
|
||||
|
||||
/**
|
||||
* Search text in files across all [workspace folders](#workspace.workspaceFolders) in the workspace.
|
||||
|
|
|
@ -585,6 +585,9 @@ export function createApiFactory(
|
|||
registerSearchProvider: proposedApiFunction(extension, (scheme, provider) => {
|
||||
return extHostSearch.registerSearchProvider(scheme, provider);
|
||||
}),
|
||||
registerTextSearchProvider: proposedApiFunction(extension, (scheme, provider) => {
|
||||
return extHostSearch.registerTextSearchProvider(scheme, provider);
|
||||
}),
|
||||
registerDocumentCommentProvider: proposedApiFunction(extension, (provider: vscode.DocumentCommentProvider) => {
|
||||
return exthostCommentProviders.registerDocumentCommentProvider(provider);
|
||||
}),
|
||||
|
|
|
@ -25,6 +25,7 @@ export class ExtHostSearch implements ExtHostSearchShape {
|
|||
|
||||
private readonly _proxy: MainThreadSearchShape;
|
||||
private readonly _searchProvider = new Map<number, vscode.SearchProvider>();
|
||||
private readonly _textSearchProvider = new Map<number, vscode.TextSearchProvider>();
|
||||
private _handlePool: number = 0;
|
||||
|
||||
private _fileSearchManager: FileSearchManager;
|
||||
|
@ -51,6 +52,16 @@ export class ExtHostSearch implements ExtHostSearchShape {
|
|||
});
|
||||
}
|
||||
|
||||
registerTextSearchProvider(scheme: string, provider: vscode.TextSearchProvider) {
|
||||
const handle = this._handlePool++;
|
||||
this._textSearchProvider.set(handle, provider);
|
||||
this._proxy.$registerSearchProvider(handle, this._transformScheme(scheme));
|
||||
return toDisposable(() => {
|
||||
this._searchProvider.delete(handle);
|
||||
this._proxy.$unregisterProvider(handle);
|
||||
});
|
||||
}
|
||||
|
||||
$provideFileSearchResults(handle: number, session: number, rawQuery: IRawSearchQuery): TPromise<ISearchCompleteStats> {
|
||||
const provider = this._searchProvider.get(handle);
|
||||
if (!provider.provideFileSearchResults) {
|
||||
|
@ -74,7 +85,7 @@ export class ExtHostSearch implements ExtHostSearchShape {
|
|||
}
|
||||
|
||||
$provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, rawQuery: IRawSearchQuery): TPromise<ISearchCompleteStats> {
|
||||
const provider = this._searchProvider.get(handle);
|
||||
const provider = this._textSearchProvider.get(handle);
|
||||
if (!provider.provideTextSearchResults) {
|
||||
return TPromise.as(undefined);
|
||||
}
|
||||
|
@ -363,7 +374,7 @@ class TextSearchEngine {
|
|||
private resultCount = 0;
|
||||
private isCanceled: boolean;
|
||||
|
||||
constructor(private pattern: IPatternInfo, private config: ISearchQuery, private provider: vscode.SearchProvider, private _extfs: typeof extfs) {
|
||||
constructor(private pattern: IPatternInfo, private config: ISearchQuery, private provider: vscode.TextSearchProvider, private _extfs: typeof extfs) {
|
||||
}
|
||||
|
||||
public cancel(): void {
|
||||
|
|
|
@ -32,6 +32,10 @@ class MockMainThreadSearch implements MainThreadSearchShape {
|
|||
this.lastHandle = handle;
|
||||
}
|
||||
|
||||
$registerTextSearchProvider(handle: number, scheme: string): void {
|
||||
this.lastHandle = handle;
|
||||
}
|
||||
|
||||
$unregisterProvider(handle: number): void {
|
||||
}
|
||||
|
||||
|
@ -53,6 +57,11 @@ class MockMainThreadSearch implements MainThreadSearchShape {
|
|||
let mockExtfs: Partial<typeof extfs>;
|
||||
|
||||
suite('ExtHostSearch', () => {
|
||||
async function registerTestTextSearchProvider(provider: vscode.TextSearchProvider, scheme = 'file'): Promise<void> {
|
||||
disposables.push(extHostSearch.registerTextSearchProvider(scheme, provider));
|
||||
await rpcProtocol.sync();
|
||||
}
|
||||
|
||||
async function registerTestSearchProvider(provider: vscode.SearchProvider, scheme = 'file'): Promise<void> {
|
||||
disposables.push(extHostSearch.registerSearchProvider(scheme, provider));
|
||||
await rpcProtocol.sync();
|
||||
|
@ -734,7 +743,7 @@ suite('ExtHostSearch', () => {
|
|||
}
|
||||
|
||||
test('no results', async () => {
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
return TPromise.wrap(null);
|
||||
}
|
||||
|
@ -751,7 +760,7 @@ suite('ExtHostSearch', () => {
|
|||
makeTextResult(rootFolderA, 'file2.ts')
|
||||
];
|
||||
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
return TPromise.wrap(null);
|
||||
|
@ -764,7 +773,7 @@ suite('ExtHostSearch', () => {
|
|||
});
|
||||
|
||||
test('all provider calls get global include/excludes', async () => {
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
assert.equal(options.includes.length, 1);
|
||||
assert.equal(options.excludes.length, 1);
|
||||
|
@ -793,7 +802,7 @@ suite('ExtHostSearch', () => {
|
|||
});
|
||||
|
||||
test('global/local include/excludes combined', async () => {
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
if (options.folder.toString() === rootFolderA.toString()) {
|
||||
assert.deepEqual(options.includes.sort(), ['*.ts', 'foo']);
|
||||
|
@ -834,7 +843,7 @@ suite('ExtHostSearch', () => {
|
|||
});
|
||||
|
||||
test('include/excludes resolved correctly', async () => {
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
assert.deepEqual(options.includes.sort(), ['*.jsx', '*.ts']);
|
||||
assert.deepEqual(options.excludes.sort(), []);
|
||||
|
@ -871,7 +880,7 @@ suite('ExtHostSearch', () => {
|
|||
});
|
||||
|
||||
test('provider fail', async () => {
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
throw new Error('Provider fail');
|
||||
}
|
||||
|
@ -902,7 +911,7 @@ suite('ExtHostSearch', () => {
|
|||
makeTextResult(rootFolderA, 'file1.ts')
|
||||
];
|
||||
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
return TPromise.wrap(null);
|
||||
|
@ -946,7 +955,7 @@ suite('ExtHostSearch', () => {
|
|||
}
|
||||
};
|
||||
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
let reportedResults;
|
||||
if (options.folder.fsPath === rootFolderA.fsPath) {
|
||||
|
@ -1010,7 +1019,7 @@ suite('ExtHostSearch', () => {
|
|||
makeTextResult(rootFolderA, 'file1.ts')
|
||||
];
|
||||
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
return TPromise.wrap(null);
|
||||
|
@ -1040,7 +1049,7 @@ suite('ExtHostSearch', () => {
|
|||
];
|
||||
|
||||
let wasCanceled = false;
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
token.onCancellationRequested(() => wasCanceled = true);
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
|
@ -1072,7 +1081,7 @@ suite('ExtHostSearch', () => {
|
|||
];
|
||||
|
||||
let wasCanceled = false;
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
token.onCancellationRequested(() => wasCanceled = true);
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
|
@ -1103,7 +1112,7 @@ suite('ExtHostSearch', () => {
|
|||
];
|
||||
|
||||
let wasCanceled = false;
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
token.onCancellationRequested(() => wasCanceled = true);
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
|
@ -1129,7 +1138,7 @@ suite('ExtHostSearch', () => {
|
|||
|
||||
test('multiroot max results', async () => {
|
||||
let cancels = 0;
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
token.onCancellationRequested(() => cancels++);
|
||||
return new TPromise(r => process.nextTick(r))
|
||||
|
@ -1166,7 +1175,7 @@ suite('ExtHostSearch', () => {
|
|||
makeTextResult(fancySchemeFolderA, 'file3.ts')
|
||||
];
|
||||
|
||||
await registerTestSearchProvider({
|
||||
await registerTestTextSearchProvider({
|
||||
provideTextSearchResults(query: vscode.TextSearchQuery, options: vscode.TextSearchOptions, progress: vscode.Progress<vscode.TextSearchResult>, token: vscode.CancellationToken): Thenable<void> {
|
||||
providedResults.forEach(r => progress.report(r));
|
||||
return TPromise.wrap(null);
|
||||
|
|
Loading…
Reference in a new issue