simpleServices.ts 22.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 { Schemas } from 'vs/base/common/network';
E
Erich Gamma 已提交
8
import Severity from 'vs/base/common/severity';
9
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
10
import { TPromise } from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
11
import { IConfigurationService, IConfigurationChangeEvent, IConfigurationOverrides, IConfigurationData } from 'vs/platform/configuration/common/configuration';
12
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
13
import { IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, GroupIdentifier } from 'vs/platform/editor/common/editor';
14
import { ICommandService, ICommand, ICommandEvent, ICommandHandler, CommandsRegistry } from 'vs/platform/commands/common/commands';
15 16
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
17
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
18
import { IKeybindingEvent, KeybindingSource, IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
19
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
20
import { IWorkspaceContextService, IWorkspace, WorkbenchState, IWorkspaceFolder, IWorkspaceFoldersChangeEvent, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
21
import * as editorCommon from 'vs/editor/common/editorCommon';
22
import { ICodeEditor, IDiffEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser';
M
Matt Bierner 已提交
23
import { Event, Emitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
24
import { Configuration, DefaultConfigurationModel, ConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
J
Johannes Rieken 已提交
25 26
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IProgressService, IProgressRunner } from 'vs/platform/progress/common/progress';
27
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
28
import { ITextModelService, ITextModelContentProvider, ITextEditorModel } from 'vs/editor/common/services/resolverService';
29
import { IDisposable, IReference, ImmortalReference, combinedDisposable } from 'vs/base/common/lifecycle';
30 31
import * as dom from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
A
Renames  
Alex Dima 已提交
32
import { KeybindingsRegistry, IKeybindingItem } from 'vs/platform/keybinding/common/keybindingsRegistry';
33
import { MenuId, IMenu, IMenuService } from 'vs/platform/actions/common/actions';
A
Alex Dima 已提交
34
import { Menu } from 'vs/platform/actions/common/menu';
35
import { ITelemetryService, ITelemetryInfo } from 'vs/platform/telemetry/common/telemetry';
36
import { ResolvedKeybinding, Keybinding, createKeybinding, SimpleKeybinding } from 'vs/base/common/keyCodes';
37
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
A
Alex Dima 已提交
38
import { OS } from 'vs/base/common/platform';
39
import { IRange, Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
40
import { ITextModel } from 'vs/editor/common/model';
41
import { INotificationService, INotification, INotificationHandle, NoOpNotification, IPromptChoice } from 'vs/platform/notification/common/notification';
42
import { IConfirmation, IConfirmationResult, IDialogService, IDialogOptions } from 'vs/platform/dialogs/common/dialogs';
43
import { IPosition, Position as Pos } from 'vs/editor/common/core/position';
44
import { isEditorConfigurationKey, isDiffEditorConfigurationKey } from 'vs/editor/common/config/commonEditorConfig';
45 46 47 48 49
import { IBulkEditService, IBulkEditOptions, IBulkEditResult } from 'vs/editor/browser/services/bulkEditService';
import { WorkspaceEdit, isResourceTextEdit, TextEdit } from 'vs/editor/common/modes';
import { IModelService } from 'vs/editor/common/services/modelService';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { localize } from 'vs/nls';
E
Erich Gamma 已提交
50 51 52

export class SimpleEditor implements IEditor {

J
Johannes Rieken 已提交
53 54
	public input: IEditorInput;
	public options: IEditorOptions;
55
	public group: GroupIdentifier;
E
Erich Gamma 已提交
56

J
Johannes Rieken 已提交
57
	public _widget: editorCommon.IEditor;
E
Erich Gamma 已提交
58

J
Johannes Rieken 已提交
59
	constructor(editor: editorCommon.IEditor) {
E
Erich Gamma 已提交
60 61 62
		this._widget = editor;
	}

J
Johannes Rieken 已提交
63 64 65 66
	public getId(): string { return 'editor'; }
	public getControl(): editorCommon.IEditor { return this._widget; }
	public focus(): void { this._widget.focus(); }
	public isVisible(): boolean { return true; }
E
Erich Gamma 已提交
67

J
Johannes Rieken 已提交
68
	public withTypedEditor<T>(codeEditorCallback: (editor: ICodeEditor) => T, diffEditorCallback: (editor: IDiffEditor) => T): T {
69
		if (isCodeEditor(this._widget)) {
E
Erich Gamma 已提交
70
			// Single Editor
A
Alex Dima 已提交
71
			return codeEditorCallback(<ICodeEditor>this._widget);
E
Erich Gamma 已提交
72 73
		} else {
			// Diff Editor
A
Alex Dima 已提交
74
			return diffEditorCallback(<IDiffEditor>this._widget);
E
Erich Gamma 已提交
75 76 77 78
		}
	}
}

J
Johannes Rieken 已提交
79
export class SimpleModel implements ITextEditorModel {
E
Erich Gamma 已提交
80

A
Alex Dima 已提交
81
	private model: ITextModel;
M
Matt Bierner 已提交
82
	private readonly _onDispose: Emitter<void>;
E
Erich Gamma 已提交
83

A
Alex Dima 已提交
84
	constructor(model: ITextModel) {
E
Erich Gamma 已提交
85
		this.model = model;
86 87 88 89 90
		this._onDispose = new Emitter<void>();
	}

	public get onDispose(): Event<void> {
		return this._onDispose.event;
E
Erich Gamma 已提交
91 92
	}

93 94 95 96
	public load(): TPromise<SimpleModel> {
		return TPromise.as(this);
	}

A
Alex Dima 已提交
97
	public get textEditorModel(): ITextModel {
E
Erich Gamma 已提交
98 99
		return this.model;
	}
100 101 102 103

	public dispose(): void {
		this._onDispose.fire();
	}
E
Erich Gamma 已提交
104 105 106
}

export interface IOpenEditorDelegate {
J
Johannes Rieken 已提交
107
	(url: string): boolean;
E
Erich Gamma 已提交
108 109 110
}

export class SimpleEditorService implements IEditorService {
111
	public _serviceBrand: any;
E
Erich Gamma 已提交
112

J
Johannes Rieken 已提交
113 114
	private editor: SimpleEditor;
	private openEditorDelegate: IOpenEditorDelegate;
E
Erich Gamma 已提交
115 116 117 118 119

	constructor() {
		this.openEditorDelegate = null;
	}

J
Johannes Rieken 已提交
120
	public setEditor(editor: editorCommon.IEditor): void {
E
Erich Gamma 已提交
121 122 123
		this.editor = new SimpleEditor(editor);
	}

J
Johannes Rieken 已提交
124
	public setOpenEditorDelegate(openEditorDelegate: IOpenEditorDelegate): void {
E
Erich Gamma 已提交
125 126 127
		this.openEditorDelegate = openEditorDelegate;
	}

J
Johannes Rieken 已提交
128
	public openEditor(typedData: IResourceInput, sideBySide?: boolean): TPromise<IEditor> {
E
Erich Gamma 已提交
129 130 131 132 133 134 135 136 137
		return TPromise.as(this.editor.withTypedEditor(
			(editor) => this.doOpenEditor(editor, typedData),
			(diffEditor) => (
				this.doOpenEditor(diffEditor.getOriginalEditor(), typedData) ||
				this.doOpenEditor(diffEditor.getModifiedEditor(), typedData)
			)
		));
	}

138
	private doOpenEditor(editor: ICodeEditor, data: IResourceInput): IEditor {
A
Alex Dima 已提交
139
		let model = this.findModel(editor, data);
E
Erich Gamma 已提交
140 141 142 143 144 145
		if (!model) {
			if (data.resource) {
				if (this.openEditorDelegate) {
					this.openEditorDelegate(data.resource.toString());
					return null;
				} else {
A
Alex Dima 已提交
146
					let schema = data.resource.scheme;
A
Alex Dima 已提交
147
					if (schema === Schemas.http || schema === Schemas.https) {
E
Erich Gamma 已提交
148
						// This is a fully qualified http or https URL
149
						dom.windowOpenNoOpener(data.resource.toString());
E
Erich Gamma 已提交
150 151 152 153 154 155 156
						return this.editor;
					}
				}
			}
			return null;
		}

A
Alex Dima 已提交
157
		let selection = <IRange>data.options.selection;
E
Erich Gamma 已提交
158 159 160
		if (selection) {
			if (typeof selection.endLineNumber === 'number' && typeof selection.endColumn === 'number') {
				editor.setSelection(selection);
161
				editor.revealRangeInCenter(selection, editorCommon.ScrollType.Immediate);
E
Erich Gamma 已提交
162
			} else {
A
Alex Dima 已提交
163
				let pos = {
E
Erich Gamma 已提交
164 165 166 167
					lineNumber: selection.startLineNumber,
					column: selection.startColumn
				};
				editor.setPosition(pos);
168
				editor.revealPositionInCenter(pos, editorCommon.ScrollType.Immediate);
E
Erich Gamma 已提交
169 170 171 172 173 174
			}
		}

		return this.editor;
	}

A
Alex Dima 已提交
175
	private findModel(editor: ICodeEditor, data: IResourceInput): ITextModel {
A
Alex Dima 已提交
176
		let model = editor.getModel();
J
Johannes Rieken 已提交
177
		if (model.uri.toString() !== data.resource.toString()) {
E
Erich Gamma 已提交
178 179 180 181 182 183 184
			return null;
		}

		return model;
	}
}

185
export class SimpleEditorModelResolverService implements ITextModelService {
186 187 188 189 190 191 192 193
	public _serviceBrand: any;

	private editor: SimpleEditor;

	public setEditor(editor: editorCommon.IEditor): void {
		this.editor = new SimpleEditor(editor);
	}

J
Joao Moreno 已提交
194
	public createModelReference(resource: URI): TPromise<IReference<ITextEditorModel>> {
A
Alex Dima 已提交
195
		let model: ITextModel;
196 197 198 199 200 201 202

		model = this.editor.withTypedEditor(
			(editor) => this.findModel(editor, resource),
			(diffEditor) => this.findModel(diffEditor.getOriginalEditor(), resource) || this.findModel(diffEditor.getModifiedEditor(), resource)
		);

		if (!model) {
J
Joao Moreno 已提交
203
			return TPromise.as(new ImmortalReference(null));
204 205
		}

J
Joao Moreno 已提交
206
		return TPromise.as(new ImmortalReference(new SimpleModel(model)));
207 208 209 210 211 212 213 214
	}

	public registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
		return {
			dispose: function () { /* no op */ }
		};
	}

A
Alex Dima 已提交
215
	private findModel(editor: ICodeEditor, resource: URI): ITextModel {
216 217 218 219 220 221 222 223 224
		let model = editor.getModel();
		if (model.uri.toString() !== resource.toString()) {
			return null;
		}

		return model;
	}
}

225 226 227
export class SimpleProgressService implements IProgressService {
	_serviceBrand: any;

J
Johannes Rieken 已提交
228 229 230 231
	private static NULL_PROGRESS_RUNNER: IProgressRunner = {
		done: () => { },
		total: () => { },
		worked: () => { }
232 233 234 235 236 237 238 239 240 241 242 243 244
	};

	show(infinite: boolean, delay?: number): IProgressRunner;
	show(total: number, delay?: number): IProgressRunner;
	show(): IProgressRunner {
		return SimpleProgressService.NULL_PROGRESS_RUNNER;
	}

	showWhile(promise: TPromise<any>, delay?: number): TPromise<void> {
		return null;
	}
}

245
export class SimpleDialogService implements IDialogService {
246 247

	public _serviceBrand: any;
E
Erich Gamma 已提交
248

249 250 251 252 253 254 255 256 257 258
	public confirm(confirmation: IConfirmation): TPromise<IConfirmationResult> {
		return this.doConfirm(confirmation).then(confirmed => {
			return {
				confirmed,
				checkboxChecked: false // unsupported
			} as IConfirmationResult;
		});
	}

	private doConfirm(confirmation: IConfirmation): TPromise<boolean> {
A
Alex Dima 已提交
259
		let messageText = confirmation.message;
E
Erich Gamma 已提交
260 261 262 263
		if (confirmation.detail) {
			messageText = messageText + '\n\n' + confirmation.detail;
		}

264
		return TPromise.wrap(window.confirm(messageText));
E
Erich Gamma 已提交
265
	}
266

267
	public show(severity: Severity, message: string, buttons: string[], options?: IDialogOptions): TPromise<number> {
268 269
		return TPromise.as(0);
	}
A
Alex Dima 已提交
270 271
}

272 273 274 275
export class SimpleNotificationService implements INotificationService {

	public _serviceBrand: any;

276
	private static readonly NO_OP: INotificationHandle = new NoOpNotification();
277

B
Benjamin Pasero 已提交
278 279 280 281 282 283 284 285 286
	public info(message: string): INotificationHandle {
		return this.notify({ severity: Severity.Info, message });
	}

	public warn(message: string): INotificationHandle {
		return this.notify({ severity: Severity.Warning, message });
	}

	public error(error: string | Error): INotificationHandle {
287 288 289
		return this.notify({ severity: Severity.Error, message: error });
	}

290 291
	public notify(notification: INotification): INotificationHandle {
		switch (notification.severity) {
292
			case Severity.Error:
293
				console.error(notification.message);
294 295
				break;
			case Severity.Warning:
296
				console.warn(notification.message);
297 298
				break;
			default:
299
				console.log(notification.message);
300 301 302
				break;
		}

303
		return SimpleNotificationService.NO_OP;
304
	}
305

306 307
	public prompt(severity: Severity, message: string, choices: IPromptChoice[], onCancel?: () => void): INotificationHandle {
		return SimpleNotificationService.NO_OP;
308
	}
309 310
}

A
Alex Dima 已提交
311 312
export class StandaloneCommandService implements ICommandService {
	_serviceBrand: any;
313

A
Alex Dima 已提交
314
	private readonly _instantiationService: IInstantiationService;
315 316
	private _dynamicCommands: { [id: string]: ICommand; };

M
Matt Bierner 已提交
317
	private readonly _onWillExecuteCommand: Emitter<ICommandEvent> = new Emitter<ICommandEvent>();
318 319
	public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;

A
Alex Dima 已提交
320 321
	constructor(instantiationService: IInstantiationService) {
		this._instantiationService = instantiationService;
322 323 324
		this._dynamicCommands = Object.create(null);
	}

J
Johannes Rieken 已提交
325 326
	public addCommand(command: ICommand): IDisposable {
		const { id } = command;
327
		this._dynamicCommands[id] = command;
328 329 330 331 332
		return {
			dispose: () => {
				delete this._dynamicCommands[id];
			}
		};
333 334
	}

A
Alex Dima 已提交
335 336 337
	public executeCommand<T>(id: string, ...args: any[]): TPromise<T> {
		const command = (CommandsRegistry.getCommand(id) || this._dynamicCommands[id]);
		if (!command) {
338
			return TPromise.wrapError<T>(new Error(`command '${id}' not found`));
A
Alex Dima 已提交
339 340 341
		}

		try {
342
			this._onWillExecuteCommand.fire({ commandId: id });
A
Alex Dima 已提交
343 344 345
			const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler].concat(args));
			return TPromise.as(result);
		} catch (err) {
346
			return TPromise.wrapError<T>(err);
A
Alex Dima 已提交
347
		}
348 349 350
	}
}

351 352
export class StandaloneKeybindingService extends AbstractKeybindingService {
	private _cachedResolver: KeybindingResolver;
E
Erich Gamma 已提交
353 354
	private _dynamicKeybindings: IKeybindingItem[];

A
Alex Dima 已提交
355
	constructor(
356
		contextKeyService: IContextKeyService,
A
Alex Dima 已提交
357
		commandService: ICommandService,
358
		telemetryService: ITelemetryService,
359
		notificationService: INotificationService,
A
Alex Dima 已提交
360 361
		domNode: HTMLElement
	) {
362
		super(contextKeyService, commandService, telemetryService, notificationService);
363

364
		this._cachedResolver = null;
E
Erich Gamma 已提交
365
		this._dynamicKeybindings = [];
366

367
		this._register(dom.addDisposableListener(domNode, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
368
			let keyEvent = new StandardKeyboardEvent(e);
369
			let shouldPreventDefault = this._dispatch(keyEvent, keyEvent.target);
370 371 372 373
			if (shouldPreventDefault) {
				keyEvent.preventDefault();
			}
		}));
E
Erich Gamma 已提交
374 375
	}

376
	public addDynamicKeybinding(commandId: string, keybinding: number, handler: ICommandHandler, when: ContextKeyExpr): IDisposable {
377 378
		let toDispose: IDisposable[] = [];

E
Erich Gamma 已提交
379
		this._dynamicKeybindings.push({
A
Renames  
Alex Dima 已提交
380
			keybinding: createKeybinding(keybinding, OS),
E
Erich Gamma 已提交
381
			command: commandId,
382
			when: when,
E
Erich Gamma 已提交
383 384 385
			weight1: 1000,
			weight2: 0
		});
386

387 388 389 390 391 392 393 394 395 396 397 398 399
		toDispose.push({
			dispose: () => {
				for (let i = 0; i < this._dynamicKeybindings.length; i++) {
					let kb = this._dynamicKeybindings[i];
					if (kb.command === commandId) {
						this._dynamicKeybindings.splice(i, 1);
						this.updateResolver({ source: KeybindingSource.Default });
						return;
					}
				}
			}
		});

400 401
		let commandService = this._commandService;
		if (commandService instanceof StandaloneCommandService) {
J
Johannes Rieken 已提交
402 403
			toDispose.push(commandService.addCommand({
				id: commandId,
404
				handler: handler
405
			}));
406 407 408
		} else {
			throw new Error('Unknown command service!');
		}
C
Christof Marti 已提交
409
		this.updateResolver({ source: KeybindingSource.Default });
410

411
		return combinedDisposable(toDispose);
E
Erich Gamma 已提交
412 413
	}

414 415 416 417 418 419 420
	private updateResolver(event: IKeybindingEvent): void {
		this._cachedResolver = null;
		this._onDidUpdateKeybindings.fire(event);
	}

	protected _getResolver(): KeybindingResolver {
		if (!this._cachedResolver) {
421 422
			const defaults = this._toNormalizedKeybindingItems(KeybindingsRegistry.getDefaultKeybindings(), true);
			const overrides = this._toNormalizedKeybindingItems(this._dynamicKeybindings, false);
423
			this._cachedResolver = new KeybindingResolver(defaults, overrides);
424 425 426 427
		}
		return this._cachedResolver;
	}

428 429 430 431
	protected _documentHasFocus(): boolean {
		return document.hasFocus();
	}

432 433
	private _toNormalizedKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
434 435 436
		for (let i = 0, len = items.length; i < len; i++) {
			const item = items[i];
			const when = (item.when ? item.when.normalize() : null);
A
Alex Dima 已提交
437
			const keybinding = item.keybinding;
438

439 440 441 442 443 444 445 446 447
			if (!keybinding) {
				// This might be a removal keybinding item in user settings => accept it
				result[resultLen++] = new ResolvedKeybindingItem(null, item.command, item.commandArgs, when, isDefault);
			} else {
				const resolvedKeybindings = this.resolveKeybinding(keybinding);
				for (let j = 0; j < resolvedKeybindings.length; j++) {
					result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybindings[j], item.command, item.commandArgs, when, isDefault);
				}
			}
448 449 450 451 452
		}

		return result;
	}

453 454
	public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {
		return [new USLayoutResolvedKeybinding(keybinding, OS)];
A
Alex Dima 已提交
455 456
	}

457 458 459 460 461 462 463 464
	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
		let keybinding = new SimpleKeybinding(
			keyboardEvent.ctrlKey,
			keyboardEvent.shiftKey,
			keyboardEvent.altKey,
			keyboardEvent.metaKey,
			keyboardEvent.keyCode
		);
465
		return new USLayoutResolvedKeybinding(keybinding, OS);
466
	}
467 468 469 470

	public resolveUserBinding(userBinding: string): ResolvedKeybinding[] {
		return [];
	}
E
Erich Gamma 已提交
471 472
}

473 474 475 476 477 478 479
function isConfigurationOverrides(thing: any): thing is IConfigurationOverrides {
	return thing
		&& typeof thing === 'object'
		&& (!thing.overrideIdentifier || typeof thing.overrideIdentifier === 'string')
		&& (!thing.resource || thing.resource instanceof URI);
}

480
export class SimpleConfigurationService implements IConfigurationService {
481

482 483
	_serviceBrand: any;

484
	private _onDidChangeConfiguration = new Emitter<IConfigurationChangeEvent>();
485
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent> = this._onDidChangeConfiguration.event;
486

487
	private _configuration: Configuration;
488

489
	constructor() {
490
		this._configuration = new Configuration(new DefaultConfigurationModel(), new ConfigurationModel());
491 492
	}

493
	private configuration(): Configuration {
494
		return this._configuration;
495 496
	}

497 498 499 500 501 502 503 504
	getValue<T>(): T;
	getValue<T>(section: string): T;
	getValue<T>(overrides: IConfigurationOverrides): T;
	getValue<T>(section: string, overrides: IConfigurationOverrides): T;
	getValue(arg1?: any, arg2?: any): any {
		const section = typeof arg1 === 'string' ? arg1 : void 0;
		const overrides = isConfigurationOverrides(arg1) ? arg1 : isConfigurationOverrides(arg2) ? arg2 : {};
		return this.configuration().getValue(section, overrides, null);
505 506
	}

507
	public updateValue(key: string, value: any, arg3?: any, arg4?: any): TPromise<void> {
508
		this.configuration().updateValue(key, value);
509
		return TPromise.as(null);
B
Benjamin Pasero 已提交
510
	}
B
Benjamin Pasero 已提交
511

S
Sandeep Somavarapu 已提交
512
	public inspect<C>(key: string, options: IConfigurationOverrides = {}): {
513 514 515 516 517 518
		default: C,
		user: C,
		workspace: C,
		workspaceFolder: C
		value: C,
	} {
519
		return this.configuration().inspect<C>(key, options, null);
B
Benjamin Pasero 已提交
520
	}
521

522
	public keys() {
S
Sandeep Somavarapu 已提交
523
		return this.configuration().keys(null);
524 525
	}

526 527
	public reloadConfiguration(): TPromise<void> {
		return TPromise.as(null);
528
	}
S
Sandeep Somavarapu 已提交
529

A
Alex Dima 已提交
530
	public getConfigurationData(): IConfigurationData {
S
Sandeep Somavarapu 已提交
531 532
		return null;
	}
533
}
A
Alex Dima 已提交
534

535 536 537 538
export class SimpleResourceConfigurationService implements ITextResourceConfigurationService {

	_serviceBrand: any;

539 540
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent>;
	private readonly _onDidChangeConfigurationEmitter = new Emitter();
541 542

	constructor(private configurationService: SimpleConfigurationService) {
543 544
		this.configurationService.onDidChangeConfiguration((e) => {
			this._onDidChangeConfigurationEmitter.fire(e);
545
		});
546 547
	}

548 549 550 551 552 553
	getValue<T>(resource: URI, section?: string): T;
	getValue<T>(resource: URI, position?: IPosition, section?: string): T;
	getValue<T>(resource: any, arg2?: any, arg3?: any) {
		const position: IPosition = Pos.isIPosition(arg2) ? arg2 : null;
		const section: string = position ? (typeof arg3 === 'string' ? arg3 : void 0) : (typeof arg2 === 'string' ? arg2 : void 0);
		return this.configurationService.getValue<T>(section);
554 555 556
	}
}

A
Alex Dima 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570
export class SimpleMenuService implements IMenuService {

	_serviceBrand: any;

	private readonly _commandService: ICommandService;

	constructor(commandService: ICommandService) {
		this._commandService = commandService;
	}

	public createMenu(id: MenuId, contextKeyService: IContextKeyService): IMenu {
		return new Menu(id, TPromise.as(true), this._commandService, contextKeyService);
	}
}
571 572 573 574 575 576 577

export class StandaloneTelemetryService implements ITelemetryService {
	_serviceBrand: void;

	public isOptedIn = false;

	public publicLog(eventName: string, data?: any): TPromise<void> {
578
		return TPromise.wrap<void>(null);
579 580 581 582 583 584
	}

	public getTelemetryInfo(): TPromise<ITelemetryInfo> {
		return null;
	}
}
585 586 587 588 589

export class SimpleWorkspaceContextService implements IWorkspaceContextService {

	public _serviceBrand: any;

590 591
	private static SCHEME: 'inmemory';

S
Sandeep Somavarapu 已提交
592 593 594
	private readonly _onDidChangeWorkspaceName: Emitter<void> = new Emitter<void>();
	public readonly onDidChangeWorkspaceName: Event<void> = this._onDidChangeWorkspaceName.event;

595 596
	private readonly _onDidChangeWorkspaceFolders: Emitter<IWorkspaceFoldersChangeEvent> = new Emitter<IWorkspaceFoldersChangeEvent>();
	public readonly onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent> = this._onDidChangeWorkspaceFolders.event;
597 598 599

	private readonly _onDidChangeWorkbenchState: Emitter<WorkbenchState> = new Emitter<WorkbenchState>();
	public readonly onDidChangeWorkbenchState: Event<WorkbenchState> = this._onDidChangeWorkbenchState.event;
600

601
	private readonly workspace: IWorkspace;
602

603
	constructor() {
604
		const resource = URI.from({ scheme: SimpleWorkspaceContextService.SCHEME, authority: 'model', path: '/' });
605
		this.workspace = { id: '4064f6ec-cb38-4ad0-af64-ee6467e63c82', folders: [new WorkspaceFolder({ uri: resource, name: '', index: 0 })], name: resource.fsPath };
606 607
	}

B
Benjamin Pasero 已提交
608
	public getWorkspace(): IWorkspace {
609
		return this.workspace;
610 611
	}

612
	public getWorkbenchState(): WorkbenchState {
613 614
		if (this.workspace) {
			if (this.workspace.configuration) {
615
				return WorkbenchState.WORKSPACE;
616
			}
617
			return WorkbenchState.FOLDER;
618
		}
619
		return WorkbenchState.EMPTY;
620 621
	}

S
Sandeep Somavarapu 已提交
622
	public getWorkspaceFolder(resource: URI): IWorkspaceFolder {
S
Sandeep Somavarapu 已提交
623
		return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME ? this.workspace.folders[0] : void 0;
624 625
	}

626
	public isInsideWorkspace(resource: URI): boolean {
627
		return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME;
628 629
	}

630 631 632
	public isCurrentWorkspace(workspaceIdentifier: ISingleFolderWorkspaceIdentifier | IWorkspaceIdentifier): boolean {
		return true;
	}
633
}
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

export function applyConfigurationValues(configurationService: IConfigurationService, source: any, isDiffEditor: boolean): void {
	if (!source) {
		return;
	}
	if (!(configurationService instanceof SimpleConfigurationService)) {
		return;
	}
	Object.keys(source).forEach((key) => {
		if (isEditorConfigurationKey(key)) {
			configurationService.updateValue(`editor.${key}`, source[key]);
		}
		if (isDiffEditor && isDiffEditorConfigurationKey(key)) {
			configurationService.updateValue(`diffEditor.${key}`, source[key]);
		}
	});
}
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

export class SimpleBulkEditService implements IBulkEditService {
	_serviceBrand: any;

	constructor(private readonly _modelService: IModelService) {
		//
	}

	apply(workspaceEdit: WorkspaceEdit, options: IBulkEditOptions): TPromise<IBulkEditResult> {

		let edits = new Map<ITextModel, TextEdit[]>();

		for (let edit of workspaceEdit.edits) {
			if (!isResourceTextEdit(edit)) {
				return TPromise.wrapError(new Error('bad edit - only text edits are supported'));
			}
			let model = this._modelService.getModel(edit.resource);
			if (!model) {
				return TPromise.wrapError(new Error('bad edit - model not found'));
			}
			let array = edits.get(model);
			if (!array) {
				array = [];
			}
			edits.set(model, array.concat(edit.edits));
		}

		let totalEdits = 0;
		let totalFiles = 0;
		edits.forEach((edits, model) => {
			model.applyEdits(edits.map(edit => EditOperation.replaceMove(Range.lift(edit.range), edit.text)));
			totalFiles += 1;
			totalEdits += edits.length;
		});

		return TPromise.as({
			selection: undefined,
			ariaSummary: localize('summary', 'Made {0} edits in {1} files', totalEdits, totalFiles)
		});
	}
}