openFileHandler.ts 10.8 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';

J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
8 9 10 11
import * as errors from 'vs/base/common/errors';
import * as nls from 'vs/nls';
import * as paths from 'vs/base/common/paths';
import * as labels from 'vs/base/common/labels';
12
import * as objects from 'vs/base/common/objects';
13
import { defaultGenerator } from 'vs/base/common/idGenerator';
E
Erich Gamma 已提交
14
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
15
import * as resources from 'vs/base/common/resources';
16
import { IIconLabelValueOptions } from 'vs/base/browser/ui/iconLabel/iconLabel';
J
Johannes Rieken 已提交
17 18 19
import { IModeService } from 'vs/editor/common/services/modeService';
import { getIconClasses } from 'vs/workbench/browser/labels';
import { IModelService } from 'vs/editor/common/services/modelService';
20
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
J
Johannes Rieken 已提交
21 22 23
import { IAutoFocus } from 'vs/base/parts/quickopen/common/quickOpen';
import { QuickOpenEntry, QuickOpenModel } from 'vs/base/parts/quickopen/browser/quickOpenModel';
import { QuickOpenHandler, EditorQuickOpenEntry } from 'vs/workbench/browser/quickopen';
24
import { QueryBuilder } from 'vs/workbench/parts/search/common/queryBuilder';
B
Benjamin Pasero 已提交
25
import { EditorInput, IWorkbenchEditorConfiguration } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
26 27 28
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
29
import { IQueryOptions, ISearchService, ISearchStats, ISearchQuery } from 'vs/platform/search/common/search';
30
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
31 32
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IRange } from 'vs/editor/common/core/range';
33
import { getOutOfWorkspaceEditorResources } from 'vs/workbench/parts/search/common/search';
34
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
B
Benjamin Pasero 已提交
35 36
import { prepareQuery, IPreparedQuery } from 'vs/base/parts/quickopen/common/quickOpenScorer';
import { IFileService } from 'vs/platform/files/common/files';
E
Erich Gamma 已提交
37

38 39
export class FileQuickOpenModel extends QuickOpenModel {

40
	constructor(entries: QuickOpenEntry[], public stats?: ISearchStats) {
41 42 43 44
		super(entries);
	}
}

E
Erich Gamma 已提交
45 46 47
export class FileEntry extends EditorQuickOpenEntry {
	private range: IRange;

48
	constructor(
49 50 51 52
		private resource: URI,
		private name: string,
		private description: string,
		private icon: string,
53
		@IEditorService editorService: IEditorService,
B
Benjamin Pasero 已提交
54
		@IModeService private modeService: IModeService,
55
		@IModelService private modelService: IModelService,
56
		@IConfigurationService private configurationService: IConfigurationService,
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64 65
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
		super(editorService);
	}

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

66
	public getLabelOptions(): IIconLabelValueOptions {
B
Benjamin Pasero 已提交
67
		return {
68
			extraClasses: getIconClasses(this.modelService, this.modeService, this.resource)
B
Benjamin Pasero 已提交
69 70 71
		};
	}

72 73 74 75
	public getAriaLabel(): string {
		return nls.localize('entryAriaLabel', "{0}, file picker", this.getLabel());
	}

E
Erich Gamma 已提交
76 77 78 79 80
	public getDescription(): string {
		return this.description;
	}

	public getIcon(): string {
81
		return this.icon;
E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91
	}

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

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

92 93
	public mergeWithEditorHistory(): boolean {
		return true;
94 95
	}

96
	public getInput(): IResourceInput | EditorInput {
97
		const input: IResourceInput = {
E
Erich Gamma 已提交
98
			resource: this.resource,
99
			options: {
100
				pinned: !this.configurationService.getValue<IWorkbenchEditorConfiguration>().workbench.editor.enablePreviewFromQuickOpen
101
			}
E
Erich Gamma 已提交
102 103 104
		};

		if (this.range) {
105
			input.options.selection = this.range;
E
Erich Gamma 已提交
106 107 108 109 110 111
		}

		return input;
	}
}

112
export interface IOpenFileOptions {
B
Benjamin Pasero 已提交
113
	forceUseIcons: boolean;
114
}
115

116 117
export class OpenFileHandler extends QuickOpenHandler {
	private options: IOpenFileOptions;
E
Erich Gamma 已提交
118
	private queryBuilder: QueryBuilder;
119
	private cacheState: CacheState;
E
Erich Gamma 已提交
120 121

	constructor(
122
		@IEditorService private editorService: IEditorService,
E
Erich Gamma 已提交
123
		@IInstantiationService private instantiationService: IInstantiationService,
124
		@IWorkbenchThemeService private themeService: IWorkbenchThemeService,
E
Erich Gamma 已提交
125
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
126
		@ISearchService private searchService: ISearchService,
B
Benjamin Pasero 已提交
127 128
		@IEnvironmentService private environmentService: IEnvironmentService,
		@IFileService private fileService: IFileService
E
Erich Gamma 已提交
129 130 131 132 133 134
	) {
		super();

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

135 136 137 138
	public setOptions(options: IOpenFileOptions) {
		this.options = options;
	}

139
	public getResults(searchValue: string, maxSortedResults?: number): TPromise<FileQuickOpenModel> {
B
Benjamin Pasero 已提交
140
		const query = prepareQuery(searchValue);
E
Erich Gamma 已提交
141 142

		// Respond directly to empty search
B
Benjamin Pasero 已提交
143
		if (!query.value) {
144
			return TPromise.as(new FileQuickOpenModel([]));
E
Erich Gamma 已提交
145 146
		}

147
		// Untildify file pattern
B
Benjamin Pasero 已提交
148
		query.value = labels.untildify(query.value, this.environmentService.userHome);
149

150
		// Do find results
B
Benjamin Pasero 已提交
151
		return this.doFindResults(query, this.cacheState.cacheKey, maxSortedResults);
E
Erich Gamma 已提交
152 153
	}

B
Benjamin Pasero 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
	private doFindResults(query: IPreparedQuery, cacheKey?: string, maxSortedResults?: number): TPromise<FileQuickOpenModel> {
		return this.doResolveQueryOptions(query, cacheKey, maxSortedResults).then(queryOptions => {
			let iconClass: string;
			if (this.options && this.options.forceUseIcons && !this.themeService.getFileIconTheme()) {
				iconClass = 'file'; // only use a generic file icon if we are forced to use an icon and have no icon theme set otherwise
			}

			return this.searchService.search(this.queryBuilder.file(this.contextService.getWorkspace().folders.map(folder => folder.uri), queryOptions)).then(complete => {
				const results: QuickOpenEntry[] = [];
				for (let i = 0; i < complete.results.length; i++) {
					const fileMatch = complete.results[i];

					const label = paths.basename(fileMatch.resource.fsPath);
167
					const description = labels.getPathLabel(resources.dirname(fileMatch.resource), this.environmentService, this.contextService);
B
Benjamin Pasero 已提交
168 169 170 171 172 173 174 175 176 177 178

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

				return new FileQuickOpenModel(results, complete.stats);
			});
		});
	}

	private doResolveQueryOptions(query: IPreparedQuery, cacheKey?: string, maxSortedResults?: number): TPromise<IQueryOptions> {
		const queryOptions: IQueryOptions = {
179
			extraFileResources: getOutOfWorkspaceEditorResources(this.editorService, this.contextService),
B
Benjamin Pasero 已提交
180 181
			filePattern: query.value,
			cacheKey
182
		};
183

184
		if (typeof maxSortedResults === 'number') {
B
Benjamin Pasero 已提交
185 186
			queryOptions.maxResults = maxSortedResults;
			queryOptions.sortByScore = true;
187
		}
E
Erich Gamma 已提交
188

B
Benjamin Pasero 已提交
189 190 191 192 193 194
		let queryIsAbsoluteFilePromise: TPromise<URI>;
		if (paths.isAbsolute(query.original)) {
			const resource = URI.file(query.original);
			queryIsAbsoluteFilePromise = this.fileService.resolveFile(resource).then(stat => stat.isDirectory ? void 0 : resource, error => void 0);
		} else {
			queryIsAbsoluteFilePromise = TPromise.as(null);
B
Benjamin Pasero 已提交
195 196
		}

B
Benjamin Pasero 已提交
197 198 199 200 201
		return queryIsAbsoluteFilePromise.then(resource => {
			if (resource) {
				// if the original search value is an existing file on disk, add it to the
				// extra file resources to consider (fixes https://github.com/Microsoft/vscode/issues/42726)
				queryOptions.extraFileResources.push(resource);
202 203
			}

B
Benjamin Pasero 已提交
204
			return queryOptions;
E
Erich Gamma 已提交
205 206 207
		});
	}

208 209 210 211
	public hasShortResponseTime(): boolean {
		return this.isCacheLoaded;
	}

212
	public onOpen(): void {
213 214
		this.cacheState = new CacheState(cacheKey => this.cacheQuery(cacheKey), query => this.searchService.search(query), cacheKey => this.searchService.clearCache(cacheKey), this.cacheState);
		this.cacheState.load();
215 216
	}

217 218
	private cacheQuery(cacheKey: string): ISearchQuery {
		const options: IQueryOptions = {
219
			extraFileResources: getOutOfWorkspaceEditorResources(this.editorService, this.contextService),
220 221 222
			filePattern: '',
			cacheKey: cacheKey,
			maxResults: 0,
223
			sortByScore: true,
224
		};
225

226
		const folderResources = this.contextService.getWorkspace().folders.map(folder => folder.uri);
R
Rob Lourens 已提交
227
		const query = this.queryBuilder.file(folderResources, options);
228

229 230 231 232 233
		return query;
	}

	public get isCacheLoaded(): boolean {
		return this.cacheState && this.cacheState.isLoaded;
234 235
	}

E
Erich Gamma 已提交
236 237 238 239 240 241 242 243 244
	public getGroupLabel(): string {
		return nls.localize('searchResults', "search results");
	}

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

247 248 249 250 251 252 253
enum LoadingPhase {
	Created = 1,
	Loading,
	Loaded,
	Errored,
	Disposed
}
254

255 256 257 258
/**
 * Exported for testing.
 */
export class CacheState {
259

260
	private _cacheKey = defaultGenerator.nextId();
261
	private query: ISearchQuery;
262

263
	private loadingPhase = LoadingPhase.Created;
264 265
	private promise: TPromise<void>;

266
	constructor(cacheQuery: (cacheKey: string) => ISearchQuery, private doLoad: (query: ISearchQuery) => TPromise<any>, private doDispose: (cacheKey: string) => TPromise<void>, private previous: CacheState) {
267 268 269 270 271 272 273 274 275 276 277 278
		this.query = cacheQuery(this._cacheKey);
		if (this.previous) {
			const current = objects.assign({}, this.query, { cacheKey: null });
			const previous = objects.assign({}, this.previous.query, { cacheKey: null });
			if (!objects.equals(current, previous)) {
				this.previous.dispose();
				this.previous = null;
			}
		}
	}

	public get cacheKey(): string {
279
		return this.loadingPhase === LoadingPhase.Loaded || !this.previous ? this._cacheKey : this.previous.cacheKey;
280 281 282
	}

	public get isLoaded(): boolean {
283 284 285 286 287 288 289
		const isLoaded = this.loadingPhase === LoadingPhase.Loaded;
		return isLoaded || !this.previous ? isLoaded : this.previous.isLoaded;
	}

	public get isUpdating(): boolean {
		const isUpdating = this.loadingPhase === LoadingPhase.Loading;
		return isUpdating || !this.previous ? isUpdating : this.previous.isUpdating;
290 291 292
	}

	public load(): void {
293 294 295 296
		if (this.isUpdating) {
			return;
		}
		this.loadingPhase = LoadingPhase.Loading;
297 298
		this.promise = this.doLoad(this.query)
			.then(() => {
299
				this.loadingPhase = LoadingPhase.Loaded;
300 301 302 303 304
				if (this.previous) {
					this.previous.dispose();
					this.previous = null;
				}
			}, err => {
305
				this.loadingPhase = LoadingPhase.Errored;
306
				errors.onUnexpectedError(err);
307 308 309 310
			});
	}

	public dispose(): void {
311 312 313 314 315 316 317 318 319 320 321
		if (this.promise) {
			this.promise.then(null, () => { })
				.then(() => {
					this.loadingPhase = LoadingPhase.Disposed;
					return this.doDispose(this._cacheKey);
				}).then(null, err => {
					errors.onUnexpectedError(err);
				});
		} else {
			this.loadingPhase = LoadingPhase.Disposed;
		}
322 323 324 325 326
		if (this.previous) {
			this.previous.dispose();
			this.previous = null;
		}
	}
327
}