From 6e54f3c423a03228cd1112651059bb0dedd668c0 Mon Sep 17 00:00:00 2001 From: elaihau Date: Tue, 26 Feb 2019 08:44:03 -0500 Subject: [PATCH] support ignored globs & limit in file search & plugin-ext - in current Theia, passing "ignored globs" or "limit" to plugins-api from plugins does not work, as the data is dropped before reaching the file search service. This change ensures the data goes through and handled by the service. Signed-off-by: elaihau --- .../src/node/file-search-service-impl.spec.ts | 8 ++++ .../src/node/file-search-service-impl.ts | 38 ++++++++++++++----- .../src/main/browser/workspace-main.ts | 9 ++++- .../plugin-ext/src/plugin/plugin-context.ts | 4 +- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/packages/file-search/src/node/file-search-service-impl.spec.ts b/packages/file-search/src/node/file-search-service-impl.spec.ts index 67be5de1ab600..3cf913de7417a 100644 --- a/packages/file-search/src/node/file-search-service-impl.spec.ts +++ b/packages/file-search/src/node/file-search-service-impl.spec.ts @@ -78,4 +78,12 @@ describe('search-service', function () { expect(matches).to.be.not.undefined; expect(matches.length).to.eq(2); }); + + it('should ignore globs passed through the search options', async () => { + const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources/subdir1/sub2')).toString(); + + const matches = await service.find('foo', { rootUris: [rootUri], defaultIgnorePatterns: ['foo'] }); + expect(matches).to.be.not.undefined; + expect(matches.length).to.eq(0); + }); }); diff --git a/packages/file-search/src/node/file-search-service-impl.ts b/packages/file-search/src/node/file-search-service-impl.ts index fcd28b5c3056d..5c9d30183b67e 100644 --- a/packages/file-search/src/node/file-search-service-impl.ts +++ b/packages/file-search/src/node/file-search-service-impl.ts @@ -37,20 +37,14 @@ export class FileSearchServiceImpl implements FileSearchService { limit: Number.MAX_SAFE_INTEGER, useGitIgnore: true, defaultIgnorePatterns: [ - '^.git$' + '.git' ], ...options }; - const args: string[] = [ - '--files', - '--sort-files', - ]; - if (!options.useGitIgnore) { - args.push('-uu'); - } + const process = this.rawProcessFactory({ command: rgPath, - args: [...args, ...opts.rootUris.map(r => FileUri.fsPath(r))] + args: this.getSearchArgs(opts) }); const result: string[] = []; const fuzzyMatches: string[] = []; @@ -94,4 +88,30 @@ export class FileSearchServiceImpl implements FileSearchService { return resultDeferred.promise; } + private getSearchArgs(options: FileSearchService.Options): string[] { + const args: string[] = [ + '--sort-files' + ]; + if (!options.useGitIgnore) { + args.push('-uu'); + } + if (options && options.defaultIgnorePatterns) { + options.defaultIgnorePatterns.filter(p => p !== '') + .forEach(ignore => { + if (!ignore.endsWith('*')) { + ignore = `${ignore}*`; + } + if (!ignore.startsWith('*')) { + ignore = `!*${ignore}`; + } else { + ignore = `!${ignore}`; + } + args.push('--glob'); + args.push(ignore); + }); + } + args.push('--files'); + args.push(...options.rootUris.map(r => FileUri.fsPath(r))); + return args; + } } diff --git a/packages/plugin-ext/src/main/browser/workspace-main.ts b/packages/plugin-ext/src/main/browser/workspace-main.ts index c964ded16bb74..bb13b50d59e8c 100644 --- a/packages/plugin-ext/src/main/browser/workspace-main.ts +++ b/packages/plugin-ext/src/main/browser/workspace-main.ts @@ -154,7 +154,14 @@ export class WorkspaceMainImpl implements WorkspaceMain { async $startFileSearch(includePattern: string, excludePatternOrDisregardExcludes?: string | false, maxResults?: number, token?: theia.CancellationToken): Promise { - const uriStrs = await this.fileSearchService.find(includePattern, { rootUris: this.roots.map(r => r.uri) }); + const opts: FileSearchService.Options = { rootUris: this.roots.map(r => r.uri) }; + if (typeof excludePatternOrDisregardExcludes === 'string') { + opts.defaultIgnorePatterns = [excludePatternOrDisregardExcludes]; + } + if (typeof maxResults === 'number') { + opts.limit = maxResults; + } + const uriStrs = await this.fileSearchService.find(includePattern, opts); return uriStrs.map(uriStr => Uri.parse(uriStr)); } diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index dd224054cdc3e..4895440ec1354 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -355,7 +355,7 @@ export function createAPIFactory( }; const workspace: typeof theia.workspace = { - get rootPath(): string | undefined { + get rootPath(): string | undefined { return workspaceExt.rootPath; }, get workspaceFolders(): theia.WorkspaceFolder[] | undefined { @@ -418,7 +418,7 @@ export function createAPIFactory( return workspaceExt.createFileSystemWatcher(globPattern, ignoreCreateEvents, ignoreChangeEvents, ignoreDeleteEvents); }, findFiles(include: theia.GlobPattern, exclude?: theia.GlobPattern | undefined, maxResults?: number, token?: CancellationToken): PromiseLike { - return workspaceExt.findFiles(include, undefined, maxResults, token); + return workspaceExt.findFiles(include, exclude, maxResults, token); }, applyEdit(edit: theia.WorkspaceEdit): PromiseLike { return editors.applyWorkspaceEdit(edit);