openFileHandler.ts 9.4 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';
8
import errors = require('vs/base/common/errors');
E
Erich Gamma 已提交
9 10 11
import nls = require('vs/nls');
import paths = require('vs/base/common/paths');
import labels = require('vs/base/common/labels');
12
import * as objects from 'vs/base/common/objects';
13
import uuid = require('vs/base/common/uuid');
E
Erich Gamma 已提交
14
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
15
import {IIconLabelOptions} from 'vs/base/browser/ui/iconLabel/iconLabel';
E
Erich Gamma 已提交
16
import {IRange} from 'vs/editor/common/editorCommon';
B
Benjamin Pasero 已提交
17 18
import {IModeService} from 'vs/editor/common/services/modeService';
import {getIconClasses} from 'vs/workbench/browser/labels';
19
import {IModelService} from 'vs/editor/common/services/modelService';
B
Benjamin Pasero 已提交
20
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
21
import {IAutoFocus} from 'vs/base/parts/quickopen/common/quickOpen';
B
Benjamin Pasero 已提交
22
import {QuickOpenEntry, QuickOpenModel} from 'vs/base/parts/quickopen/browser/quickOpenModel';
E
Erich Gamma 已提交
23 24
import {QuickOpenHandler, EditorQuickOpenEntry} from 'vs/workbench/browser/quickopen';
import {QueryBuilder} from 'vs/workbench/parts/search/common/searchQuery';
25
import {EditorInput, getOutOfWorkspaceEditorResources, IWorkbenchEditorConfiguration} from 'vs/workbench/common/editor';
26
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
27 28
import {IResourceInput} from 'vs/platform/editor/common/editor';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
E
Erich Gamma 已提交
29 30
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
31
import {IQueryOptions, ISearchService, ISearchStats, ISearchQuery} from 'vs/platform/search/common/search';
E
Erich Gamma 已提交
32 33
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';

34 35
export class FileQuickOpenModel extends QuickOpenModel {

36
	constructor(entries: QuickOpenEntry[], public stats?: ISearchStats) {
37 38 39 40
		super(entries);
	}
}

E
Erich Gamma 已提交
41 42 43
export class FileEntry extends EditorQuickOpenEntry {
	private range: IRange;

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

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

B
Benjamin Pasero 已提交
62 63
	public getLabelOptions(): IIconLabelOptions {
		return {
64
			extraClasses: getIconClasses(this.modelService, this.modeService, this.resource)
B
Benjamin Pasero 已提交
65 66 67
		};
	}

68 69 70 71
	public getAriaLabel(): string {
		return nls.localize('entryAriaLabel', "{0}, file picker", this.getLabel());
	}

E
Erich Gamma 已提交
72 73 74 75 76
	public getDescription(): string {
		return this.description;
	}

	public getIcon(): string {
77
		return this.icon;
E
Erich Gamma 已提交
78 79 80 81 82 83 84 85 86 87
	}

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

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

88 89 90 91
	public isFile(): boolean {
		return true; // TODO@Ben debt with editor history merging
	}

92
	public getInput(): IResourceInput | EditorInput {
93
		const input: IResourceInput = {
E
Erich Gamma 已提交
94
			resource: this.resource,
95
			options: {
96
				pinned: !this.configurationService.getConfiguration<IWorkbenchEditorConfiguration>().workbench.editor.enablePreviewFromQuickOpen
97
			}
E
Erich Gamma 已提交
98 99 100
		};

		if (this.range) {
101
			input.options.selection = this.range;
E
Erich Gamma 已提交
102 103 104 105 106 107
		}

		return input;
	}
}

108
export interface IOpenFileOptions {
B
Benjamin Pasero 已提交
109
	forceUseIcons: boolean;
110
}
111

112 113
export class OpenFileHandler extends QuickOpenHandler {
	private options: IOpenFileOptions;
E
Erich Gamma 已提交
114
	private queryBuilder: QueryBuilder;
115
	private cacheState: CacheState;
E
Erich Gamma 已提交
116 117

	constructor(
118
		@IEditorGroupService private editorGroupService: IEditorGroupService,
E
Erich Gamma 已提交
119
		@IInstantiationService private instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
120
		@IThemeService private themeService: IThemeService,
E
Erich Gamma 已提交
121
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
B
Benjamin Pasero 已提交
122
		@ISearchService private searchService: ISearchService
E
Erich Gamma 已提交
123 124 125 126 127 128
	) {
		super();

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

129 130 131 132
	public setOptions(options: IOpenFileOptions) {
		this.options = options;
	}

133
	public getResults(searchValue: string, maxSortedResults?: number): TPromise<FileQuickOpenModel> {
E
Erich Gamma 已提交
134 135 136 137
		searchValue = searchValue.trim();

		// Respond directly to empty search
		if (!searchValue) {
138
			return TPromise.as(new FileQuickOpenModel([]));
E
Erich Gamma 已提交
139 140
		}

141 142
		// Do find results
		return this.doFindResults(searchValue, this.cacheState.cacheKey, maxSortedResults);
E
Erich Gamma 已提交
143 144
	}

145
	private doFindResults(searchValue: string, cacheKey?: string, maxSortedResults?: number): TPromise<FileQuickOpenModel> {
146
		const query: IQueryOptions = {
147
			folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
148
			extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
149 150
			filePattern: searchValue,
			cacheKey: cacheKey
151
		};
152

153
		if (typeof maxSortedResults === 'number') {
154 155 156
			query.maxResults = maxSortedResults;
			query.sortByScore = true;
		}
E
Erich Gamma 已提交
157

B
Benjamin Pasero 已提交
158 159 160 161 162
		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
		}

163
		return this.searchService.search(this.queryBuilder.file(query)).then((complete) => {
164
			const results: QuickOpenEntry[] = [];
165
			for (let i = 0; i < complete.results.length; i++) {
166
				const fileMatch = complete.results[i];
167

168 169
				const label = paths.basename(fileMatch.resource.fsPath);
				const description = labels.getPathLabel(paths.dirname(fileMatch.resource.fsPath), this.contextService);
170

B
Benjamin Pasero 已提交
171
				results.push(this.instantiationService.createInstance(FileEntry, fileMatch.resource, label, description, iconClass));
172 173
			}

174
			return new FileQuickOpenModel(results, complete.stats);
E
Erich Gamma 已提交
175 176 177
		});
	}

178 179 180 181
	public hasShortResponseTime(): boolean {
		return this.isCacheLoaded;
	}

182
	public onOpen(): void {
183 184
		this.cacheState = new CacheState(cacheKey => this.cacheQuery(cacheKey), query => this.searchService.search(query), cacheKey => this.searchService.clearCache(cacheKey), this.cacheState);
		this.cacheState.load();
185 186
	}

187 188 189
	private cacheQuery(cacheKey: string): ISearchQuery {
		const options: IQueryOptions = {
			folderResources: this.contextService.getWorkspace() ? [this.contextService.getWorkspace().resource] : [],
B
Benjamin Pasero 已提交
190
			extraFileResources: getOutOfWorkspaceEditorResources(this.editorGroupService, this.contextService),
191 192 193 194 195
			filePattern: '',
			cacheKey: cacheKey,
			maxResults: 0,
			sortByScore: true
		};
196

197 198
		const query = this.queryBuilder.file(options);
		this.searchService.extendQuery(query);
199

200 201 202 203 204
		return query;
	}

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

E
Erich Gamma 已提交
207 208 209 210 211 212 213 214 215
	public getGroupLabel(): string {
		return nls.localize('searchResults', "search results");
	}

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

218 219 220 221 222 223 224
enum LoadingPhase {
	Created = 1,
	Loading,
	Loaded,
	Errored,
	Disposed
}
225

226 227 228 229
/**
 * Exported for testing.
 */
export class CacheState {
230 231

	private _cacheKey = uuid.generateUuid();
232
	private query: ISearchQuery;
233

234
	private loadingPhase = LoadingPhase.Created;
235 236
	private promise: TPromise<void>;

237
	constructor(cacheQuery: (cacheKey: string) => ISearchQuery, private doLoad: (query: ISearchQuery) => TPromise<any>, private doDispose: (cacheKey: string) => TPromise<void>, private previous: CacheState) {
238 239 240 241 242 243 244 245 246 247 248 249
		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 {
250
		return this.loadingPhase === LoadingPhase.Loaded || !this.previous ? this._cacheKey : this.previous.cacheKey;
251 252 253
	}

	public get isLoaded(): boolean {
254 255 256 257 258 259 260
		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;
261 262 263
	}

	public load(): void {
264 265 266 267
		if (this.isUpdating) {
			return;
		}
		this.loadingPhase = LoadingPhase.Loading;
268 269
		this.promise = this.doLoad(this.query)
			.then(() => {
270
				this.loadingPhase = LoadingPhase.Loaded;
271 272 273 274 275
				if (this.previous) {
					this.previous.dispose();
					this.previous = null;
				}
			}, err => {
276
				this.loadingPhase = LoadingPhase.Errored;
277
				errors.onUnexpectedError(err);
278 279 280 281
			});
	}

	public dispose(): void {
282 283 284 285 286 287 288 289 290 291 292
		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;
		}
293 294 295 296 297
		if (this.previous) {
			this.previous.dispose();
			this.previous = null;
		}
	}
E
Erich Gamma 已提交
298
}