Skip to content

Commit

Permalink
Add quick search in workspace feature
Browse files Browse the repository at this point in the history
This patch adds a "quick" search in workspace feature.  It works using
the "quick open" widget.  The default keybinding to open it is
ctrl-shift-F.  The user types in the widget and the search results
appear as they type.  Selecting a search result opens the file at the
location of the file.

The backend uses ripgrep to implement the search.  There is no way to
make ripgrep output a truly unambiguous machine parsable output.  The
best way that people have found is to enable the color output, and use
the ANSI color codes as markers for the fields and matches.

The vision in the future would be to have a proper "search in
directory/workspace" view, which would show the results more
permanently.  I think this quick search in workspace feature is still
useful in the mean time, easy to implement on the UI side, and lays out
the foundation in the backend.

Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
  • Loading branch information
Simon Marchi authored and simark committed Jan 22, 2018
1 parent 467603e commit ab31b86
Show file tree
Hide file tree
Showing 14 changed files with 1,221 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@
"webRoot": "${workspaceRoot}"
}
]
}
}
4 changes: 2 additions & 2 deletions configs/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rules": {
"file-header": [
true,
"[\n\r]+ \\* Copyright \\(C\\) \\d{4} .*[\n\r]+"
"[\n\r]+ \\* Copyright \\(C\\) \\d{4}(-\\d{4})? .*[\n\r]+"
],
"class-name": true,
"comment-format": [
Expand Down Expand Up @@ -64,4 +64,4 @@
"check-type"
]
}
}
}
1 change: 1 addition & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@theia/preferences": "^0.3.2",
"@theia/process": "^0.3.2",
"@theia/python": "^0.3.2",
"@theia/search-in-workspace": "^0.3.2",
"@theia/task": "^0.3.2",
"@theia/terminal": "^0.3.2",
"@theia/typescript": "^0.3.2",
Expand Down
3 changes: 2 additions & 1 deletion examples/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@theia/preferences": "^0.3.2",
"@theia/process": "^0.3.2",
"@theia/python": "^0.3.2",
"@theia/search-in-workspace": "^0.3.2",
"@theia/task": "^0.3.2",
"@theia/terminal": "^0.3.2",
"@theia/typescript": "^0.3.2",
Expand All @@ -45,4 +46,4 @@
"devDependencies": {
"@theia/cli": "^0.3.2"
}
}
}
10 changes: 10 additions & 0 deletions packages/search-in-workspace/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
]
}
46 changes: 46 additions & 0 deletions packages/search-in-workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@theia/search-in-workspace",
"version": "0.3.2",
"description": "Theia - Search in workspace",
"dependencies": {
"@theia/core": "^0.3.2",
"@theia/editor": "^0.3.2",
"vscode-ripgrep": "^0.7.1-patch.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"frontend": "lib/browser/search-in-workspace-frontend-module",
"backend": "lib/node/search-in-workspace-backend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "theiaext build",
"watch": "theiaext watch",
"test": "theiaext test",
"docs": "theiaext docs"
},
"devDependencies": {
"@theia/ext-scripts": "^0.2.0"
}
}
194 changes: 194 additions & 0 deletions packages/search-in-workspace/src/browser/quick-search-in-workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Copyright (C) 2017-2018 Erisson and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import URI from '@theia/core/lib/common/uri';
import { QuickOpenService, QuickOpenModel, QuickOpenItem, QuickOpenItemOptions } from '@theia/core/lib/browser/quick-open/';
import { injectable, inject } from 'inversify';
import { MenuModelRegistry, MenuContribution, CommandContribution, CommandRegistry, KeybindingContribution, KeybindingRegistry, ILogger } from '@theia/core';
import { CommonMenus, QuickOpenMode, OpenerService, open, Highlight, QuickOpenOptions } from '@theia/core/lib/browser';
import { SearchInWorkspaceService } from './search-in-workspace-service';
import { SearchInWorkspaceResult, SearchInWorkspaceOptions } from '../common/search-in-workspace-interface';
import { Range } from '@theia/editor/lib/browser';
import { LabelProvider } from '@theia/core/lib/browser/label-provider';

@injectable()
export class QuickSearchInWorkspace implements QuickOpenModel {
private currentSearchId: number = -1;
protected MAX_RESULTS = 100;

constructor(
@inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService,
@inject(SearchInWorkspaceService) protected readonly searchInWorkspaceService: SearchInWorkspaceService,
@inject(OpenerService) protected readonly openerService: OpenerService,
@inject(LabelProvider) protected readonly labelProvider: LabelProvider,
@inject(ILogger) protected readonly logger: ILogger,
) { }

onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): void {
// If we have a search pending, it's not relevant anymore, cancel it.
this.cancelCurrentSeach();

if (lookFor.length === 0) {
// The user has emptied the search box, call acceptor to
// remove any previously shown results.
acceptor([]);
return;
}

// Options passed to the search service.
const opts: SearchInWorkspaceOptions = {
maxResults: this.MAX_RESULTS,
};

// The array in which we'll keep accumulating search results.
const items: QuickSearchInWorkspaceResultItem[] = [];

this.searchInWorkspaceService.search(lookFor, {

onResult: (searchId: number, result: SearchInWorkspaceResult) => {
// Is this result from a previous search?
if (searchId !== this.currentSearchId) {
return;
}

items.push(new QuickSearchInWorkspaceResultItem(result, this.openerService, this.labelProvider));
},

onDone: (searchId: number, error?: string) => {
if (searchId !== this.currentSearchId) {
this.logger.debug('Search ' + this.currentSearchId + ' has completed, but it\'s not the current search.');
return;
}
this.logger.debug('Search ' + this.currentSearchId + ' has completed and is the current search.');
this.currentSearchId = -1;

if (error) {
this.showFakeResult(error, acceptor);
} else if (items.length !== 0) {
items.sort((a, b) => SearchInWorkspaceResult.compare(a.getResult(), b.getResult()));
acceptor(items);
} else {
this.showFakeResult('No matches :(', acceptor);
}

},
}, opts).then(searchId => {
this.currentSearchId = searchId;
});
}

showFakeResult(label: string, acceptor: (items: QuickOpenItem[]) => void) {
acceptor([
new QuickOpenItem({
label: label,
}),
]);
}

// If we have an ongoing search, cancel it.
cancelCurrentSeach() {
if (this.currentSearchId >= 0) {
this.logger.debug('Cancelling search ' + this.currentSearchId);
this.searchInWorkspaceService.cancel(this.currentSearchId);
this.currentSearchId = -1;
}
}

// Open the quick search in workspace popup.
open() {
const opts: QuickOpenOptions = {
onClose: cancelled => this.cancelCurrentSeach(),
placeholder: 'Search in workspace by regular expression...',
};
this.quickOpenService.open(this, opts);
}
}

class QuickSearchInWorkspaceResultItem extends QuickOpenItem {

private result: SearchInWorkspaceResult;
private openerService: OpenerService;

constructor(result: SearchInWorkspaceResult, openerService: OpenerService, labelProvider: LabelProvider) {
const resultHl: Highlight = {
start: result.character - 1,
end: result.character + result.length - 1,
};

// Show the path relative to the workspace.
const uri = new URI('file://' + result.file);
const file = labelProvider.getName(uri);
const dir = labelProvider.getLongName(uri.parent) + '/';

const filenameHl: Highlight = {
start: 0,
end: file.length,
};

const opts: QuickOpenItemOptions = {
detail: result.lineText,
detailHighlights: [resultHl],
label: `${file}:${result.line} - ${dir}`,
labelHighlights: [filenameHl],
};
super(opts);

this.result = result;
this.openerService = openerService;
}

run(mode: QuickOpenMode): boolean {
if (mode !== QuickOpenMode.OPEN) {
return false;
}

// Search results are 1-based, positions in editors are 0-based.
const line = this.result.line - 1;
const character = this.result.character - 1;
const uri = new URI('file://' + this.result.file);
const r = Range.create(line, character, line, character + this.result.length);
open(this.openerService, uri, { selection: r });

return true;
}

getResult(): SearchInWorkspaceResult {
return this.result;
}
}

const OpenQuickSearchInWorkspaceCommand = {
id: 'QuickSearchInWorkspace.open',
label: "Search in workspace..."
};

@injectable()
export class SearchInWorkspaceContributions implements CommandContribution, MenuContribution, KeybindingContribution {
constructor(
@inject(QuickSearchInWorkspace) protected readonly quickSeachInWorkspace: QuickSearchInWorkspace,
) { }

registerCommands(registry: CommandRegistry): void {
registry.registerCommand(OpenQuickSearchInWorkspaceCommand, {
execute: what => this.quickSeachInWorkspace.open()
});
}

registerMenus(menus: MenuModelRegistry): void {
menus.registerMenuAction(CommonMenus.EDIT_FIND, {
commandId: OpenQuickSearchInWorkspaceCommand.id,
label: OpenQuickSearchInWorkspaceCommand.label,
});
}

registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybinding({
command: OpenQuickSearchInWorkspaceCommand.id,
keybinding: 'ctrlcmd+shift+f',
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2017-2018 Erisson and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { ContainerModule } from "inversify";
import { SearchInWorkspaceService, SearchInWorkspaceClientImpl } from './search-in-workspace-service';
import { SearchInWorkspaceServer } from '../common/search-in-workspace-interface';
import { WebSocketConnectionProvider } from '@theia/core/lib/browser';
import { QuickSearchInWorkspace, SearchInWorkspaceContributions } from './quick-search-in-workspace';
import { CommandContribution, MenuContribution, KeybindingContribution } from "@theia/core";

export default new ContainerModule(bind => {
bind(QuickSearchInWorkspace).toSelf().inSingletonScope();

bind(CommandContribution).to(SearchInWorkspaceContributions).inSingletonScope();
bind(MenuContribution).to(SearchInWorkspaceContributions).inSingletonScope();
bind(KeybindingContribution).to(SearchInWorkspaceContributions).inSingletonScope();

// The object that gets notified of search results.
bind(SearchInWorkspaceClientImpl).toSelf().inSingletonScope();

bind(SearchInWorkspaceService).toSelf().inSingletonScope();

// The object to call methods on the backend.
bind(SearchInWorkspaceServer).toDynamicValue(ctx => {
const client = ctx.container.get(SearchInWorkspaceClientImpl);
return WebSocketConnectionProvider.createProxy(ctx.container, '/search-in-workspace', client);
}).inSingletonScope();
});
Loading

0 comments on commit ab31b86

Please sign in to comment.