Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search additional roots #10448

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/api-tests/src/file-search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('file-search', function () {
assert.equal(filterAndRange, quickFileOpenService['filterAndRangeDefault']);
});

it('should update when searching', () => {
quickFileOpenService['getPicks']('a:2:1', new CancellationTokenSource().token); // perform a mock search.
it('should update when searching', async () => {
await quickFileOpenService['getPicks']('a:2:1', new CancellationTokenSource().token); // perform a mock search.
const filterAndRange = quickFileOpenService['filterAndRange'];
assert.equal(filterAndRange.filter, 'a');
assert.deepEqual(filterAndRange.range, { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } });
Expand Down
1 change: 1 addition & 0 deletions packages/file-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@theia/filesystem": "1.20.0",
"@theia/monaco": "1.20.0",
"@theia/process": "1.20.0",
"@theia/variable-resolver": "1.20.0",
"@theia/workspace": "1.20.0",
"vscode-ripgrep": "^1.2.4"
},
Expand Down
19 changes: 15 additions & 4 deletions packages/file-search/src/browser/quick-file-open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { inject, injectable, optional, postConstruct } from '@theia/core/shared/inversify';
import { OpenerService, KeybindingRegistry, QuickAccessRegistry, QuickAccessProvider, CommonCommands } from '@theia/core/lib/browser';
import { OpenerService, KeybindingRegistry, QuickAccessRegistry, QuickAccessProvider, CommonCommands, PreferenceService } from '@theia/core/lib/browser';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import URI from '@theia/core/lib/common/uri';
import { FileSearchService, WHITESPACE_QUERY_SEPARATOR } from '../common/file-search-service';
Expand All @@ -27,6 +27,7 @@ import { MessageService } from '@theia/core/lib/common/message-service';
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
import { EditorOpenerOptions, EditorWidget, Position, Range } from '@theia/editor/lib/browser';
import { findMatches, QuickInputService, QuickPickItem, QuickPicks } from '@theia/core/lib/browser/quick-input/quick-input-service';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';

export const quickFileOpen = Command.toDefaultLocalizedCommand({
id: 'file-search.openFile',
Expand Down Expand Up @@ -66,6 +67,10 @@ export class QuickFileOpenService implements QuickAccessProvider {
protected readonly messageService: MessageService;
@inject(FileSystemPreferences)
protected readonly fsPreferences: FileSystemPreferences;
@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;
@inject(VariableResolverService)
protected readonly variableResolverService: VariableResolverService;

registerQuickAccessProvider(): void {
this.quickAccessRegistry.registerQuickAccessProvider({
Expand Down Expand Up @@ -153,9 +158,14 @@ export class QuickFileOpenService implements QuickAccessProvider {
return undefined;
}

async getPicks(filter: string, token: CancellationToken): Promise<QuickPicks> {
const roots = this.workspaceService.tryGetRoots();
async getRoots(): Promise<string[]> {
const workspaceRoots = this.workspaceService.tryGetRoots().map(r => r.resource.toString());
const additionalSearchRoots: string[] = this.preferenceService.get('search.additionalRoots') ?? [];
const resolvedAdditionalSearchRoots = await this.variableResolverService.resolveArray(additionalSearchRoots);
return [...workspaceRoots, ...resolvedAdditionalSearchRoots];
}

async getPicks(filter: string, token: CancellationToken): Promise<QuickPicks> {
this.filterAndRange = this.splitFilterAndRange(filter);
const fileFilter = this.filterAndRange.filter;

Expand All @@ -176,6 +186,7 @@ export class QuickFileOpenService implements QuickAccessProvider {
}
}

const roots = await this.getRoots();
if (fileFilter.length > 0) {
const handler = async (results: string[]) => {
if (token.isCancellationRequested || results.length <= 0) {
Expand Down Expand Up @@ -207,7 +218,7 @@ export class QuickFileOpenService implements QuickAccessProvider {
};

return this.fileSearchService.find(fileFilter, {
rootUris: roots.map(r => r.resource.toString()),
rootUris: roots,
fuzzyMatch: true,
limit: 200,
useGitIgnore: this.hideIgnoredFiles,
Expand Down
3 changes: 3 additions & 0 deletions packages/file-search/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"path": "../process"
},
{
"path": "../variable-resolver"
},
{
"path": "../workspace"
}
Expand Down
1 change: 1 addition & 0 deletions packages/search-in-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@theia/filesystem": "1.20.0",
"@theia/navigator": "1.20.0",
"@theia/process": "1.20.0",
"@theia/variable-resolver": "1.20.0",
"@theia/workspace": "1.20.0",
"minimatch": "^3.0.4",
"vscode-ripgrep": "^1.2.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export const searchInWorkspacePreferencesSchema: PreferenceSchema = {
description: nls.localizeByDefault('Controls whether to follow symlinks while searching.'),
default: true,
type: 'boolean',
},
'search.additionalRoots': {
description: nls.localize('theia/search-in-workspace/additionalRoots', 'Directories outside the current workspace that should be included in searches.'),
default: [],
type: 'array',
}
}
};
Expand All @@ -70,6 +75,7 @@ export class SearchInWorkspaceConfiguration {
'search.searchOnEditorModification': boolean;
'search.smartCase': boolean;
'search.followSymlinks': boolean;
'search.additionalRoots': string[];
}

export const SearchInWorkspacePreferenceContribution = Symbol('SearchInWorkspacePreferenceContribution');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
} from '../common/search-in-workspace-interface';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { ILogger } from '@theia/core';
import { SearchInWorkspacePreferences } from './search-in-workspace-preferences';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';

/**
* Class that will receive the search results from the server. This is separate
Expand Down Expand Up @@ -72,6 +74,8 @@ export class SearchInWorkspaceService implements SearchInWorkspaceClient {
@inject(SearchInWorkspaceClientImpl) protected readonly client: SearchInWorkspaceClientImpl;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(ILogger) protected readonly logger: ILogger;
@inject(SearchInWorkspacePreferences) protected readonly searchInWorkspacePreferences: SearchInWorkspacePreferences;
@inject(VariableResolverService) protected readonly variableResolverService: VariableResolverService;

@postConstruct()
protected init(): void {
Expand Down Expand Up @@ -113,8 +117,15 @@ export class SearchInWorkspaceService implements SearchInWorkspaceClient {
throw new Error('Search failed: no workspace root.');
}

const roots = await this.workspaceService.roots;
return this.doSearch(what, roots.map(r => r.resource.toString()), callbacks, opts);
const roots = await this.getRoots();
return this.doSearch(what, roots, callbacks, opts);
}

async getRoots(): Promise<string[]> {
const workspaceRoots = (await this.workspaceService.roots).map(r => r.resource.toString());
const additionalSearchRoots = this.searchInWorkspacePreferences.get('search.additionalRoots');
const resolvedAdditionalSearchRoots = await this.variableResolverService.resolveArray(additionalSearchRoots);
return [...workspaceRoots, ...resolvedAdditionalSearchRoots];
}

protected async doSearch(what: string, rootsUris: string[], callbacks: SearchInWorkspaceCallbacks, opts?: SearchInWorkspaceOptions): Promise<number> {
Expand Down
3 changes: 3 additions & 0 deletions packages/search-in-workspace/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"path": "../process"
},
{
"path": "../variable-resolver"
},
{
"path": "../workspace"
}
Expand Down