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

quick palette fixes #4549

Merged
merged 4 commits into from
Mar 15, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Breaking changes:
- [editor] computation of resource context keys moved to core [#4531](https://github.com/theia-ide/theia/pull/4531)
- [plugin] support multiple windows per a backend [#4509](https://github.com/theia-ide/theia/issues/4509)
- Some plugin bindings are scoped per a connection now. Clients, who contribute/rebind these bindings, will need to scope them per a connection as well.
- [quick-open] disable separate fuzzy matching by default [#4549](https://github.com/theia-ide/theia/pull/4549)

## v0.4.0
- [application-manager] added support for pre-load HTML templates
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/browser/quick-open/quick-open-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ import { MessageType } from '../../common/message-service-protocol';

export type QuickOpenOptions = Partial<QuickOpenOptions.Resolved>;
export namespace QuickOpenOptions {
export interface FuzzyMatchOptions {
/**
* Default: `false`
*/
enableSeparateSubstringMatching?: boolean
}
export interface Resolved {
readonly prefix: string;
readonly placeholder: string;
readonly ignoreFocusOut: boolean;

readonly fuzzyMatchLabel: boolean;
readonly fuzzyMatchDetail: boolean;
readonly fuzzyMatchDescription: boolean;
readonly fuzzyMatchLabel: boolean | FuzzyMatchOptions;
readonly fuzzyMatchDetail: boolean | FuzzyMatchOptions;
readonly fuzzyMatchDescription: boolean | FuzzyMatchOptions;
readonly fuzzySort: boolean;

/** The amount of first symbols to be ignored by quick open widget (e.g. don't affect matching). */
Expand Down
66 changes: 41 additions & 25 deletions packages/file-search/src/browser/quick-file-open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ export class QuickFileOpenService implements QuickOpenModel, QuickOpenHandler {
}
return {
placeholder,
fuzzyMatchLabel: true,
fuzzyMatchDescription: true,
fuzzyMatchLabel: {
enableSeparateSubstringMatching: true
},
fuzzyMatchDescription: {
enableSeparateSubstringMatching: true
},
showItemsWithoutHighlight: true,
onClose: () => {
this.isOpen = false;
Expand Down Expand Up @@ -137,52 +141,64 @@ export class QuickFileOpenService implements QuickOpenModel, QuickOpenHandler {
private cancelIndicator = new CancellationTokenSource();

public async onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): Promise<void> {
this.cancelIndicator.cancel();
this.cancelIndicator = new CancellationTokenSource();
const token = this.cancelIndicator.token;

const roots = this.workspaceService.tryGetRoots();
if (roots.length === 0) {
return;
}

this.currentLookFor = lookFor;
this.cancelIndicator.cancel();
this.cancelIndicator = new CancellationTokenSource();

const token = this.cancelIndicator.token;
const alreadyCollected = new Set<string>();
const recentlyUsedItems: QuickOpenItem[] = [];

const locations = [...this.navigationLocationService.locations()].reverse();
for (const location of locations) {
const uriString = location.uri.toString();
if (location.uri.scheme === 'file' && !alreadyCollected.has(uriString) && fuzzy.test(lookFor, uriString)) {
recentlyUsedItems.push(await this.toItem(location.uri, { groupLabel: recentlyUsedItems.length === 0 ? 'recently opened' : undefined, showBorder: false }));
const item = await this.toItem(location.uri, { groupLabel: recentlyUsedItems.length === 0 ? 'recently opened' : undefined, showBorder: false });
if (token.isCancellationRequested) {
return;
}
recentlyUsedItems.push(item);
alreadyCollected.add(uriString);
}
}
if (lookFor.length > 0) {
const handler = async (results: string[]) => {
if (!token.isCancellationRequested) {
const fileSearchResultItems: QuickOpenItem[] = [];
for (const fileUri of results) {
if (!alreadyCollected.has(fileUri)) {
fileSearchResultItems.push(await this.toItem(fileUri));
alreadyCollected.add(fileUri);
if (token.isCancellationRequested) {
return;
}
const fileSearchResultItems: QuickOpenItem[] = [];
for (const fileUri of results) {
if (!alreadyCollected.has(fileUri)) {
const item = await this.toItem(fileUri);
if (token.isCancellationRequested) {
return;
}
fileSearchResultItems.push(item);
alreadyCollected.add(fileUri);
}
}

// Create a copy of the file search results and sort.
const sortedResults = fileSearchResultItems.slice();
sortedResults.sort((a, b) => this.compareItems(a, b));

// Extract the first element, and re-add it to the array with the group label.
const first = sortedResults[0];
sortedResults.shift();
if (first) {
sortedResults.unshift(await this.toItem(first.getUri()!, { groupLabel: 'file results', showBorder: true }));
// Create a copy of the file search results and sort.
const sortedResults = fileSearchResultItems.slice();
sortedResults.sort((a, b) => this.compareItems(a, b));

// Extract the first element, and re-add it to the array with the group label.
const first = sortedResults[0];
sortedResults.shift();
if (first) {
const item = await this.toItem(first.getUri()!, { groupLabel: 'file results', showBorder: true });
if (token.isCancellationRequested) {
return;
}

// Return the recently used items, followed by the search results.
acceptor([...recentlyUsedItems, ...sortedResults]);
sortedResults.unshift(item);
}
// Return the recently used items, followed by the search results.
acceptor([...recentlyUsedItems, ...sortedResults]);
};
this.fileSearchService.find(lookFor, {
rootUris: roots.map(r => r.uri),
Expand Down
1 change: 1 addition & 0 deletions packages/file-search/src/common/file-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export namespace FileSearchService {
useGitIgnore?: boolean
/** when `undefined`, no excludes will apply, when empty array, default excludes will apply */
defaultIgnorePatterns?: string[]
includePatterns?: string[]
}
}
57 changes: 44 additions & 13 deletions packages/file-search/src/node/file-search-service-impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
********************************************************************************/

import { expect } from 'chai';
import * as assert from 'assert';
import * as path from 'path';
import { FileSearchServiceImpl } from './file-search-service-impl';
import { FileUri } from '@theia/core/lib/node';
import { Container, ContainerModule } from 'inversify';
import { CancellationTokenSource } from '@theia/core';
import { bindLogger } from '@theia/core/lib/node/logger-backend-module';
import processBackendModule from '@theia/process/lib/node/process-backend-module';
import URI from '@theia/core/lib/common/uri';

// tslint:disable:no-unused-expression

Expand Down Expand Up @@ -51,15 +53,14 @@ describe('search-service', function () {
expect(testFile).to.be.not.undefined;
});

it('shall respect nested .gitignore');
// const service = testContainer.get(FileSearchServiceImpl);
// const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources')).toString();
// const matches = await service.find('foo', { rootUri, fuzzyMatch: false });
it.skip('shall respect nested .gitignore', async () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources')).toString();
const matches = await service.find('foo', { rootUris: [rootUri], fuzzyMatch: false });

// expect(matches.find(match => match.endsWith('subdir1/sub-bar/foo.txt'))).to.be.undefined;
// expect(matches.find(match => match.endsWith('subdir1/sub2/foo.txt'))).to.be.not.undefined;
// expect(matches.find(match => match.endsWith('subdir1/foo.txt'))).to.be.not.undefined;
// });
expect(matches.find(match => match.endsWith('subdir1/sub-bar/foo.txt'))).to.be.undefined;
expect(matches.find(match => match.endsWith('subdir1/sub2/foo.txt'))).to.be.not.undefined;
expect(matches.find(match => match.endsWith('subdir1/foo.txt'))).to.be.not.undefined;
});

it('shall cancel searches', async () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../../../..')).toString();
Expand All @@ -83,19 +84,19 @@ describe('search-service', function () {
it('should support file searches with globs', async () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources/subdir1/sub2')).toString();

const matches = await service.find('**/*oo.*', { rootUris: [rootUri] });
const matches = await service.find('', { rootUris: [rootUri], includePatterns: ['**/*oo.*'] });
expect(matches).to.be.not.undefined;
expect(matches.length).to.eq(1);
});

it('should support file searches with globs without the prefixed or trailing star (*)', async () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources/subdir1/sub2')).toString();

const trailingMatches = await service.find('*oo', { rootUris: [rootUri] });
const trailingMatches = await service.find('', { rootUris: [rootUri], includePatterns: ['*oo'] });
expect(trailingMatches).to.be.not.undefined;
expect(trailingMatches.length).to.eq(1);

const prefixedMatches = await service.find('oo*', { rootUris: [rootUri] });
const prefixedMatches = await service.find('', { rootUris: [rootUri], includePatterns: ['oo*'] });
expect(prefixedMatches).to.be.not.undefined;
expect(prefixedMatches.length).to.eq(1);
});
Expand All @@ -105,17 +106,47 @@ describe('search-service', function () {
it('should ignore strings passed through the search options', async () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../test-resources/subdir1/sub2')).toString();

const matches = await service.find('**/*oo.*', { rootUris: [rootUri], defaultIgnorePatterns: ['foo'] });
const matches = await service.find('', { rootUris: [rootUri], includePatterns: ['**/*oo.*'], defaultIgnorePatterns: ['foo'] });
expect(matches).to.be.not.undefined;
expect(matches.length).to.eq(0);
});

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('**/*oo.*', { rootUris: [rootUri], defaultIgnorePatterns: ['*fo*'] });
const matches = await service.find('', { rootUris: [rootUri], includePatterns: ['**/*oo.*'], defaultIgnorePatterns: ['*fo*'] });
expect(matches).to.be.not.undefined;
expect(matches.length).to.eq(0);
});
});

describe('irrelevant absolute results', () => {
const rootUri = FileUri.create(path.resolve(__dirname, '../../../..'));

it('not fuzzy', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is a repro for the bogus workspace search results.

        const rootUri = FileUri.create(path.resolve(__dirname, '../../../..'));

        it.only('not fuzzy', async () => {
            const searchPattern = rootUri.path.dir.base;
            const matches = await service.find(searchPattern, { rootUris: [rootUri.toString()], fuzzyMatch: false, useGitIgnore: true, limit: 200 });
            for (const match of matches) {
                const relativUri = rootUri.relative(new URI(match));
                if (relativUri) {
                    const relativMatch = relativUri.toString();
                    assert.notEqual(relativMatch.indexOf(searchPattern), -1, relativMatch);
                }
            }
        });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elaihau, ☝️ the workspace search pattern is in the path of the bogus search results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it.only nice feature, did not know it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just don't forget to remove it ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see it is for vscode extension, i don't think API was modeled properly, there should be a new explicit option for included globs and it should not affect quick file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akosyakov this is one example: https://github.com/Microsoft/vscode/blob/master/extensions/npm/src/tasks.ts#L101

npm uses the service to find all packages.json, excluding those from node_modules.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@elaihau elaihau Mar 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a new explicit option for included globs and it should not affect quick file

i was thinking about the same thing. but after seeing this interface i chose to support the string match & glob match from the same function without having that option. https://github.com/Microsoft/vscode/blob/master/src/vs/workbench/services/search/common/search.ts#L79

export interface IFileQueryProps<U extends UriComponents> extends ICommonQueryProps<U> {
	type: QueryType.File;
	filePattern?: string;

	/**
	 * If true no results will be returned. Instead `limitHit` will indicate if at least one result exists or not.
	 * Currently does not work with queries including a 'siblings clause'.
	 */
	exists?: boolean;
	sortByScore?: boolean;
	cacheKey?: string;
}

maybe you are right, with the new option defined we would have finer grained control over what the service does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elaihau yes, searchPattern and includePatterns are not the same, i've separated them

const searchPattern = rootUri.path.dir.base;
const matches = await service.find(searchPattern, { rootUris: [rootUri.toString()], fuzzyMatch: false, useGitIgnore: true, limit: 200 });
for (const match of matches) {
const relativUri = rootUri.relative(new URI(match));
assert.notEqual(relativUri, undefined);
const relativMatch = relativUri!.toString();
assert.notEqual(relativMatch.indexOf(searchPattern), -1, relativMatch);
}
});

it('fuzzy', async () => {
const matches = await service.find('shell', { rootUris: [rootUri.toString()], fuzzyMatch: true, useGitIgnore: true, limit: 200 });
for (const match of matches) {
const relativUri = rootUri.relative(new URI(match));
assert.notEqual(relativUri, undefined);
const relativMatch = relativUri!.toString();
let position = 0;
for (const ch of 'shell') {
position = relativMatch.indexOf(ch, position);
assert.notEqual(position, -1, relativMatch);
}
}
});
});

});
Loading