openFileHandler.ts 5.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

B
Benjamin Pasero 已提交
7
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9 10 11 12
import nls = require('vs/nls');
import paths = require('vs/base/common/paths');
import labels = require('vs/base/common/labels');
import URI from 'vs/base/common/uri';
import {IRange} from 'vs/editor/common/editorCommon';
13
import {IAutoFocus} from 'vs/base/parts/quickopen/common/quickOpen';
B
Benjamin Pasero 已提交
14
import {QuickOpenEntry, QuickOpenModel} from 'vs/base/parts/quickopen/browser/quickOpenModel';
E
Erich Gamma 已提交
15 16
import {QuickOpenHandler, EditorQuickOpenEntry} from 'vs/workbench/browser/quickopen';
import {QueryBuilder} from 'vs/workbench/parts/search/common/searchQuery';
17
import {EditorInput, getOutOfWorkspaceEditorResources, IWorkbenchEditorConfiguration} from 'vs/workbench/common/editor';
18
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
19 20
import {IResourceInput} from 'vs/platform/editor/common/editor';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
E
Erich Gamma 已提交
21 22
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
C
chrmarti 已提交
23
import {IQueryOptions, ISearchService, ISearchStats} from 'vs/platform/search/common/search';
E
Erich Gamma 已提交
24 25 26 27 28 29 30 31
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';

export class FileEntry extends EditorQuickOpenEntry {
	private name: string;
	private description: string;
	private resource: URI;
	private range: IRange;

32 33 34 35
	constructor(
		name: string,
		description: string,
		resource: URI,
E
Erich Gamma 已提交
36 37
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IInstantiationService private instantiationService: IInstantiationService,
38
		@IConfigurationService private configurationService: IConfigurationService,
E
Erich Gamma 已提交
39 40 41 42 43 44
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
		super(editorService);

		this.resource = resource;
		this.name = name;
45
		this.description = description;
E
Erich Gamma 已提交
46 47 48 49 50 51
	}

	public getLabel(): string {
		return this.name;
	}

52 53 54 55
	public getAriaLabel(): string {
		return nls.localize('entryAriaLabel', "{0}, file picker", this.getLabel());
	}

E
Erich Gamma 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	public getDescription(): string {
		return this.description;
	}

	public getIcon(): string {
		return 'file';
	}

	public getResource(): URI {
		return this.resource;
	}

	public setRange(range: IRange): void {
		this.range = range;
	}

72 73
	public getInput(): IResourceInput | EditorInput {
		let input: IResourceInput = {
E
Erich Gamma 已提交
74
			resource: this.resource,
75
			options: {
76
				pinned: !this.configurationService.getConfiguration<IWorkbenchEditorConfiguration>().workbench.editor.enablePreviewFromQuickOpen
77
			}
E
Erich Gamma 已提交
78 79 80
		};

		if (this.range) {
81
			input.options.selection = this.range;
E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91
		}

		return input;
	}
}

export class OpenFileHandler extends QuickOpenHandler {
	private queryBuilder: QueryBuilder;

	constructor(
92
		@IEditorGroupService private editorGroupService: IEditorGroupService,
E
Erich Gamma 已提交
93 94
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
B
Benjamin Pasero 已提交
95
		@ISearchService private searchService: ISearchService
E
Erich Gamma 已提交
96 97 98 99 100 101 102
	) {
		super();

		this.queryBuilder = this.instantiationService.createInstance(QueryBuilder);
	}

	public getResults(searchValue: string): TPromise<QuickOpenModel> {
C
chrmarti 已提交
103 104 105 106 107
		return this.getResultsWithStats(searchValue)
			.then(result => result[0]);
	}

	public getResultsWithStats(searchValue: string): TPromise<[QuickOpenModel, ISearchStats]> {
E
Erich Gamma 已提交
108
		searchValue = searchValue.trim();
C
chrmarti 已提交
109
		let promise: TPromise<[QuickOpenEntry[], ISearchStats]>;
E
Erich Gamma 已提交
110 111 112

		// Respond directly to empty search
		if (!searchValue) {
C
chrmarti 已提交
113
			promise = TPromise.as(<[QuickOpenEntry[], ISearchStats]>[[], undefined]);
E
Erich Gamma 已提交
114 115 116 117
		} else {
			promise = this.doFindResults(searchValue);
		}

C
chrmarti 已提交
118
		return promise.then(result => [new QuickOpenModel(result[0]), result[1]]);
E
Erich Gamma 已提交
119 120
	}

C
chrmarti 已提交
121
	private doFindResults(searchValue: string): TPromise<[QuickOpenEntry[], ISearchStats]> {
122
		const query: IQueryOptions = {
123
			folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
124
			extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
125
			filePattern: searchValue
126
		};
E
Erich Gamma 已提交
127

128
		return this.searchService.search(this.queryBuilder.file(query)).then((complete) => {
129
			let results: QuickOpenEntry[] = [];
130 131 132 133 134
			for (let i = 0; i < complete.results.length; i++) {
				let fileMatch = complete.results[i];

				let label = paths.basename(fileMatch.resource.fsPath);
				let description = labels.getPathLabel(paths.dirname(fileMatch.resource.fsPath), this.contextService);
135

136
				results.push(this.instantiationService.createInstance(FileEntry, label, description, fileMatch.resource));
137 138
			}

C
chrmarti 已提交
139
			return [results, complete.stats];
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
		});
	}

	public getGroupLabel(): string {
		return nls.localize('searchResults', "search results");
	}

	public getAutoFocus(searchValue: string): IAutoFocus {
		return {
			autoFocusFirstEntry: true
		};
	}
}