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

A
Alex Dima 已提交
6 7 8 9 10 11
import { localize } from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { Emitter, Event } from 'vs/base/common/event';
import { Keybinding, ResolvedKeybinding, SimpleKeybinding, createKeybinding } from 'vs/base/common/keyCodes';
import { IDisposable, IReference, ImmortalReference, combinedDisposable, toDisposable } from 'vs/base/common/lifecycle';
12
import { OS, isLinux, isMacintosh, AccessibilitySupport } from 'vs/base/common/platform';
E
Erich Gamma 已提交
13
import Severity from 'vs/base/common/severity';
14
import { URI } from 'vs/base/common/uri';
15
import { ICodeEditor, IDiffEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
16 17 18 19
import { IBulkEditOptions, IBulkEditResult, IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { isDiffEditorConfigurationKey, isEditorConfigurationKey } from 'vs/editor/common/config/commonEditorConfig';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { IPosition, Position as Pos } from 'vs/editor/common/core/position';
20
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
21
import * as editorCommon from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
22
import { ITextModel } from 'vs/editor/common/model';
A
Alex Dima 已提交
23
import { TextEdit, WorkspaceEdit, isResourceTextEdit } from 'vs/editor/common/modes';
24
import { IModelService } from 'vs/editor/common/services/modelService';
A
Alex Dima 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
import { ITextEditorModel, ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService';
import { ITextResourceConfigurationService, ITextResourcePropertiesService } from 'vs/editor/common/services/resourceConfiguration';
import { CommandsRegistry, ICommand, ICommandEvent, ICommandHandler, ICommandService } from 'vs/platform/commands/common/commands';
import { IConfigurationChangeEvent, IConfigurationData, IConfigurationOverrides, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { Configuration, ConfigurationModel, DefaultConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IConfirmation, IConfirmationResult, IDialogOptions, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
import { IKeybindingEvent, IKeyboardEvent, KeybindingSource } from 'vs/platform/keybinding/common/keybinding';
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
import { IKeybindingItem, KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
39
import { ILabelService, ResourceLabelFormatter } from 'vs/platform/label/common/label';
A
Alex Dima 已提交
40 41 42 43 44
import { INotification, INotificationHandle, INotificationService, IPromptChoice, IPromptOptions, NoOpNotification } from 'vs/platform/notification/common/notification';
import { IProgressRunner, IProgressService } from 'vs/platform/progress/common/progress';
import { ITelemetryInfo, ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IWorkspace, IWorkspaceContextService, IWorkspaceFolder, IWorkspaceFoldersChangeEvent, WorkbenchState, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
45
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
E
Erich Gamma 已提交
46

J
Johannes Rieken 已提交
47
export class SimpleModel implements ITextEditorModel {
E
Erich Gamma 已提交
48

A
Alex Dima 已提交
49
	private model: ITextModel;
M
Matt Bierner 已提交
50
	private readonly _onDispose: Emitter<void>;
E
Erich Gamma 已提交
51

A
Alex Dima 已提交
52
	constructor(model: ITextModel) {
E
Erich Gamma 已提交
53
		this.model = model;
54 55 56 57 58
		this._onDispose = new Emitter<void>();
	}

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

J
Johannes Rieken 已提交
61
	public load(): Promise<SimpleModel> {
62
		return Promise.resolve(this);
63 64
	}

A
Alex Dima 已提交
65
	public get textEditorModel(): ITextModel {
E
Erich Gamma 已提交
66 67
		return this.model;
	}
68

69 70 71 72
	public isReadonly(): boolean {
		return false;
	}

73 74 75
	public dispose(): void {
		this._onDispose.fire();
	}
E
Erich Gamma 已提交
76 77 78
}

export interface IOpenEditorDelegate {
J
Johannes Rieken 已提交
79
	(url: string): boolean;
E
Erich Gamma 已提交
80 81
}

B
Benjamin Pasero 已提交
82 83 84 85 86 87 88 89 90 91
function withTypedEditor<T>(widget: editorCommon.IEditor, codeEditorCallback: (editor: ICodeEditor) => T, diffEditorCallback: (editor: IDiffEditor) => T): T {
	if (isCodeEditor(widget)) {
		// Single Editor
		return codeEditorCallback(<ICodeEditor>widget);
	} else {
		// Diff Editor
		return diffEditorCallback(<IDiffEditor>widget);
	}
}

92
export class SimpleEditorModelResolverService implements ITextModelService {
93 94
	public _serviceBrand: any;

B
Benjamin Pasero 已提交
95
	private editor: editorCommon.IEditor;
96 97

	public setEditor(editor: editorCommon.IEditor): void {
B
Benjamin Pasero 已提交
98
		this.editor = editor;
99 100
	}

101
	public createModelReference(resource: URI): Promise<IReference<ITextEditorModel>> {
A
Alex Dima 已提交
102
		let model: ITextModel | null = withTypedEditor(this.editor,
103 104 105 106 107
			(editor) => this.findModel(editor, resource),
			(diffEditor) => this.findModel(diffEditor.getOriginalEditor(), resource) || this.findModel(diffEditor.getModifiedEditor(), resource)
		);

		if (!model) {
A
Alex Dima 已提交
108
			return Promise.reject(new Error(`Model not found`));
109 110
		}

111
		return Promise.resolve(new ImmortalReference(new SimpleModel(model)));
112 113 114 115 116 117 118 119
	}

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

120 121 122 123
	public hasTextModelContentProvider(scheme: string): boolean {
		return false;
	}

A
Alex Dima 已提交
124
	private findModel(editor: ICodeEditor, resource: URI): ITextModel | null {
125
		let model = editor.getModel();
A
Alex Dima 已提交
126
		if (model && model.uri.toString() !== resource.toString()) {
127 128 129 130 131 132 133
			return null;
		}

		return model;
	}
}

134 135 136
export class SimpleProgressService implements IProgressService {
	_serviceBrand: any;

J
Johannes Rieken 已提交
137 138 139 140
	private static NULL_PROGRESS_RUNNER: IProgressRunner = {
		done: () => { },
		total: () => { },
		worked: () => { }
141 142
	};

143
	show(infinite: true, delay?: number): IProgressRunner;
144 145 146 147 148
	show(total: number, delay?: number): IProgressRunner;
	show(): IProgressRunner {
		return SimpleProgressService.NULL_PROGRESS_RUNNER;
	}

J
Johannes Rieken 已提交
149
	showWhile(promise: Promise<any>, delay?: number): Promise<void> {
R
Rob Lourens 已提交
150
		return Promise.resolve(undefined);
151 152 153
	}
}

154
export class SimpleDialogService implements IDialogService {
155 156

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

J
Johannes Rieken 已提交
158
	public confirm(confirmation: IConfirmation): Promise<IConfirmationResult> {
159 160 161 162 163 164 165 166
		return this.doConfirm(confirmation).then(confirmed => {
			return {
				confirmed,
				checkboxChecked: false // unsupported
			} as IConfirmationResult;
		});
	}

J
Johannes Rieken 已提交
167
	private doConfirm(confirmation: IConfirmation): Promise<boolean> {
A
Alex Dima 已提交
168
		let messageText = confirmation.message;
E
Erich Gamma 已提交
169 170 171 172
		if (confirmation.detail) {
			messageText = messageText + '\n\n' + confirmation.detail;
		}

A
Alex Dima 已提交
173
		return Promise.resolve(window.confirm(messageText));
E
Erich Gamma 已提交
174
	}
175

J
Johannes Rieken 已提交
176
	public show(severity: Severity, message: string, buttons: string[], options?: IDialogOptions): Promise<number> {
A
Alex Dima 已提交
177
		return Promise.resolve(0);
178
	}
A
Alex Dima 已提交
179 180
}

181 182 183 184
export class SimpleNotificationService implements INotificationService {

	public _serviceBrand: any;

185
	private static readonly NO_OP: INotificationHandle = new NoOpNotification();
186

B
Benjamin Pasero 已提交
187 188 189 190 191 192 193 194 195
	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 {
196 197 198
		return this.notify({ severity: Severity.Error, message: error });
	}

199 200
	public notify(notification: INotification): INotificationHandle {
		switch (notification.severity) {
201
			case Severity.Error:
202
				console.error(notification.message);
203 204
				break;
			case Severity.Warning:
205
				console.warn(notification.message);
206 207
				break;
			default:
208
				console.log(notification.message);
209 210 211
				break;
		}

212
		return SimpleNotificationService.NO_OP;
213
	}
214

B
Benjamin Pasero 已提交
215
	public prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
216
		return SimpleNotificationService.NO_OP;
217
	}
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
export class BrowserAccessibilityService implements IAccessibilityService {
	_serviceBrand: any;

	private _accessibilitySupport = AccessibilitySupport.Unknown;
	private readonly _onDidChangeAccessibilitySupport = new Emitter<void>();
	readonly onDidChangeAccessibilitySupport: Event<void> = this._onDidChangeAccessibilitySupport.event;

	alwaysUnderlineAccessKeys(): Promise<boolean> {
		return Promise.resolve(false);
	}

	setAccessibilitySupport(accessibilitySupport: AccessibilitySupport): void {
		if (this._accessibilitySupport === accessibilitySupport) {
			return;
		}

		this._accessibilitySupport = accessibilitySupport;
		this._onDidChangeAccessibilitySupport.fire();
	}

	getAccessibilitySupport(): AccessibilitySupport {
		return this._accessibilitySupport;
	}
}

A
Alex Dima 已提交
245 246
export class StandaloneCommandService implements ICommandService {
	_serviceBrand: any;
247

A
Alex Dima 已提交
248
	private readonly _instantiationService: IInstantiationService;
249 250
	private _dynamicCommands: { [id: string]: ICommand; };

251
	private readonly _onWillExecuteCommand = new Emitter<ICommandEvent>();
252 253
	public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;

A
Alex Dima 已提交
254 255
	constructor(instantiationService: IInstantiationService) {
		this._instantiationService = instantiationService;
256 257 258
		this._dynamicCommands = Object.create(null);
	}

J
Johannes Rieken 已提交
259 260
	public addCommand(command: ICommand): IDisposable {
		const { id } = command;
261
		this._dynamicCommands[id] = command;
262 263 264
		return toDisposable(() => {
			delete this._dynamicCommands[id];
		});
265 266
	}

267
	public executeCommand<T>(id: string, ...args: any[]): Promise<T> {
A
Alex Dima 已提交
268 269
		const command = (CommandsRegistry.getCommand(id) || this._dynamicCommands[id]);
		if (!command) {
270
			return Promise.reject(new Error(`command '${id}' not found`));
A
Alex Dima 已提交
271 272 273
		}

		try {
274
			this._onWillExecuteCommand.fire({ commandId: id });
275
			const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler, ...args]) as T;
276
			return Promise.resolve(result);
A
Alex Dima 已提交
277
		} catch (err) {
278
			return Promise.reject(err);
A
Alex Dima 已提交
279
		}
280 281 282
	}
}

283
export class StandaloneKeybindingService extends AbstractKeybindingService {
A
Alex Dima 已提交
284
	private _cachedResolver: KeybindingResolver | null;
E
Erich Gamma 已提交
285 286
	private _dynamicKeybindings: IKeybindingItem[];

A
Alex Dima 已提交
287
	constructor(
288
		contextKeyService: IContextKeyService,
A
Alex Dima 已提交
289
		commandService: ICommandService,
290
		telemetryService: ITelemetryService,
291
		notificationService: INotificationService,
A
Alex Dima 已提交
292 293
		domNode: HTMLElement
	) {
294
		super(contextKeyService, commandService, telemetryService, notificationService);
295

296
		this._cachedResolver = null;
E
Erich Gamma 已提交
297
		this._dynamicKeybindings = [];
298

299
		this._register(dom.addDisposableListener(domNode, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
300
			let keyEvent = new StandardKeyboardEvent(e);
301
			let shouldPreventDefault = this._dispatch(keyEvent, keyEvent.target);
302 303 304 305
			if (shouldPreventDefault) {
				keyEvent.preventDefault();
			}
		}));
E
Erich Gamma 已提交
306 307
	}

A
Alex Dima 已提交
308 309 310 311 312 313
	public addDynamicKeybinding(commandId: string, _keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | null): IDisposable {
		const keybinding = createKeybinding(_keybinding, OS);
		if (!keybinding) {
			throw new Error(`Invalid keybinding`);
		}

314 315
		let toDispose: IDisposable[] = [];

E
Erich Gamma 已提交
316
		this._dynamicKeybindings.push({
A
Alex Dima 已提交
317
			keybinding: keybinding,
E
Erich Gamma 已提交
318
			command: commandId,
319
			when: when,
E
Erich Gamma 已提交
320 321 322
			weight1: 1000,
			weight2: 0
		});
323

324 325 326 327 328 329 330
		toDispose.push(toDisposable(() => {
			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;
331 332
				}
			}
333
		}));
334

335 336
		let commandService = this._commandService;
		if (commandService instanceof StandaloneCommandService) {
J
Johannes Rieken 已提交
337 338
			toDispose.push(commandService.addCommand({
				id: commandId,
339
				handler: handler
340
			}));
341 342 343
		} else {
			throw new Error('Unknown command service!');
		}
C
Christof Marti 已提交
344
		this.updateResolver({ source: KeybindingSource.Default });
345

346
		return combinedDisposable(toDispose);
E
Erich Gamma 已提交
347 348
	}

349 350 351 352 353 354 355
	private updateResolver(event: IKeybindingEvent): void {
		this._cachedResolver = null;
		this._onDidUpdateKeybindings.fire(event);
	}

	protected _getResolver(): KeybindingResolver {
		if (!this._cachedResolver) {
356 357
			const defaults = this._toNormalizedKeybindingItems(KeybindingsRegistry.getDefaultKeybindings(), true);
			const overrides = this._toNormalizedKeybindingItems(this._dynamicKeybindings, false);
358
			this._cachedResolver = new KeybindingResolver(defaults, overrides);
359 360 361 362
		}
		return this._cachedResolver;
	}

363 364 365 366
	protected _documentHasFocus(): boolean {
		return document.hasFocus();
	}

367 368
	private _toNormalizedKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
369
		for (const item of items) {
370
			const when = (item.when ? item.when.normalize() : null);
A
Alex Dima 已提交
371
			const keybinding = item.keybinding;
372

373 374 375 376 377
			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);
378 379
				for (const resolvedKeybinding of resolvedKeybindings) {
					result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybinding, item.command, item.commandArgs, when, isDefault);
380 381
				}
			}
382 383 384 385 386
		}

		return result;
	}

387 388
	public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {
		return [new USLayoutResolvedKeybinding(keybinding, OS)];
A
Alex Dima 已提交
389 390
	}

391 392 393 394 395 396 397
	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
		let keybinding = new SimpleKeybinding(
			keyboardEvent.ctrlKey,
			keyboardEvent.shiftKey,
			keyboardEvent.altKey,
			keyboardEvent.metaKey,
			keyboardEvent.keyCode
398
		).toChord();
399
		return new USLayoutResolvedKeybinding(keybinding, OS);
400
	}
401 402 403 404

	public resolveUserBinding(userBinding: string): ResolvedKeybinding[] {
		return [];
	}
405 406 407 408

	public _dumpDebugInfo(): string {
		return '';
	}
E
Erich Gamma 已提交
409 410
}

411 412 413 414 415 416 417
function isConfigurationOverrides(thing: any): thing is IConfigurationOverrides {
	return thing
		&& typeof thing === 'object'
		&& (!thing.overrideIdentifier || typeof thing.overrideIdentifier === 'string')
		&& (!thing.resource || thing.resource instanceof URI);
}

418
export class SimpleConfigurationService implements IConfigurationService {
419

420 421
	_serviceBrand: any;

422
	private _onDidChangeConfiguration = new Emitter<IConfigurationChangeEvent>();
423
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent> = this._onDidChangeConfiguration.event;
424

425
	private _configuration: Configuration;
426

427
	constructor() {
428
		this._configuration = new Configuration(new DefaultConfigurationModel(), new ConfigurationModel());
429 430
	}

431
	private configuration(): Configuration {
432
		return this._configuration;
433 434
	}

435 436 437 438 439
	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 {
R
Rob Lourens 已提交
440
		const section = typeof arg1 === 'string' ? arg1 : undefined;
441
		const overrides = isConfigurationOverrides(arg1) ? arg1 : isConfigurationOverrides(arg2) ? arg2 : {};
442
		return this.configuration().getValue(section, overrides, undefined);
443 444
	}

S
Sandeep Somavarapu 已提交
445
	public updateValue(key: string, value: any, arg3?: any, arg4?: any): Promise<void> {
446
		this.configuration().updateValue(key, value);
A
Alex Dima 已提交
447
		return Promise.resolve();
B
Benjamin Pasero 已提交
448
	}
B
Benjamin Pasero 已提交
449

S
Sandeep Somavarapu 已提交
450
	public inspect<C>(key: string, options: IConfigurationOverrides = {}): {
451 452
		default: C,
		user: C,
M
Matt Bierner 已提交
453 454
		workspace?: C,
		workspaceFolder?: C
455 456
		value: C,
	} {
457
		return this.configuration().inspect<C>(key, options, undefined);
B
Benjamin Pasero 已提交
458
	}
459

460
	public keys() {
461
		return this.configuration().keys(undefined);
462 463
	}

S
Sandeep Somavarapu 已提交
464
	public reloadConfiguration(): Promise<void> {
R
Rob Lourens 已提交
465
		return Promise.resolve(undefined);
466
	}
S
Sandeep Somavarapu 已提交
467

A
Alex Dima 已提交
468
	public getConfigurationData(): IConfigurationData | null {
S
Sandeep Somavarapu 已提交
469 470
		return null;
	}
471
}
A
Alex Dima 已提交
472

473 474 475 476
export class SimpleResourceConfigurationService implements ITextResourceConfigurationService {

	_serviceBrand: any;

477 478
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent>;
	private readonly _onDidChangeConfigurationEmitter = new Emitter();
479 480

	constructor(private configurationService: SimpleConfigurationService) {
481 482
		this.configurationService.onDidChangeConfiguration((e) => {
			this._onDidChangeConfigurationEmitter.fire(e);
483
		});
484 485
	}

486 487 488
	getValue<T>(resource: URI, section?: string): T;
	getValue<T>(resource: URI, position?: IPosition, section?: string): T;
	getValue<T>(resource: any, arg2?: any, arg3?: any) {
A
Alex Dima 已提交
489
		const position: IPosition | null = Pos.isIPosition(arg2) ? arg2 : null;
R
Rob Lourens 已提交
490
		const section: string | undefined = position ? (typeof arg3 === 'string' ? arg3 : undefined) : (typeof arg2 === 'string' ? arg2 : undefined);
A
Alex Dima 已提交
491 492 493
		if (typeof section === 'undefined') {
			return this.configurationService.getValue<T>();
		}
494
		return this.configurationService.getValue<T>(section);
495 496 497
	}
}

S
Sandeep Somavarapu 已提交
498 499 500 501 502
export class SimpleResourcePropertiesService implements ITextResourcePropertiesService {

	_serviceBrand: any;

	constructor(
503
		@IConfigurationService private readonly configurationService: IConfigurationService,
S
Sandeep Somavarapu 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517
	) {
	}

	getEOL(resource: URI): string {
		const filesConfiguration = this.configurationService.getValue<{ eol: string }>('files');
		if (filesConfiguration && filesConfiguration.eol) {
			if (filesConfiguration.eol !== 'auto') {
				return filesConfiguration.eol;
			}
		}
		return (isLinux || isMacintosh) ? '\n' : '\r\n';
	}
}

518 519 520 521 522
export class StandaloneTelemetryService implements ITelemetryService {
	_serviceBrand: void;

	public isOptedIn = false;

523
	public publicLog(eventName: string, data?: any): Promise<void> {
R
Rob Lourens 已提交
524
		return Promise.resolve(undefined);
525 526
	}

527
	public getTelemetryInfo(): Promise<ITelemetryInfo> {
A
Alex Dima 已提交
528
		throw new Error(`Not available`);
529 530
	}
}
531 532 533 534 535

export class SimpleWorkspaceContextService implements IWorkspaceContextService {

	public _serviceBrand: any;

536
	private static SCHEME = 'inmemory';
537

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

541
	private readonly _onDidChangeWorkspaceFolders = new Emitter<IWorkspaceFoldersChangeEvent>();
542
	public readonly onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent> = this._onDidChangeWorkspaceFolders.event;
543

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

547
	private readonly workspace: IWorkspace;
548

549
	constructor() {
550
		const resource = URI.from({ scheme: SimpleWorkspaceContextService.SCHEME, authority: 'model', path: '/' });
I
isidor 已提交
551
		this.workspace = { id: '4064f6ec-cb38-4ad0-af64-ee6467e63c82', folders: [new WorkspaceFolder({ uri: resource, name: '', index: 0 })] };
552 553
	}

554 555 556 557
	getCompleteWorkspace(): Promise<IWorkspace> {
		return Promise.resolve(this.getWorkspace());
	}

B
Benjamin Pasero 已提交
558
	public getWorkspace(): IWorkspace {
559
		return this.workspace;
560 561
	}

562
	public getWorkbenchState(): WorkbenchState {
563 564
		if (this.workspace) {
			if (this.workspace.configuration) {
565
				return WorkbenchState.WORKSPACE;
566
			}
567
			return WorkbenchState.FOLDER;
568
		}
569
		return WorkbenchState.EMPTY;
570 571
	}

A
Alex Dima 已提交
572 573
	public getWorkspaceFolder(resource: URI): IWorkspaceFolder | null {
		return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME ? this.workspace.folders[0] : null;
574 575
	}

576
	public isInsideWorkspace(resource: URI): boolean {
577
		return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME;
578 579
	}

580
	public isCurrentWorkspace(workspaceIdentifier: ISingleFolderWorkspaceIdentifier | IWorkspaceIdentifier): boolean {
581 582
		return true;
	}
583
}
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

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]);
		}
	});
}
601 602 603 604 605 606 607 608

export class SimpleBulkEditService implements IBulkEditService {
	_serviceBrand: any;

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

M
Matt Bierner 已提交
609
	apply(workspaceEdit: WorkspaceEdit, options?: IBulkEditOptions): Promise<IBulkEditResult> {
610 611 612

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

A
Alex Dima 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626
		if (workspaceEdit.edits) {
			for (let edit of workspaceEdit.edits) {
				if (!isResourceTextEdit(edit)) {
					return Promise.reject(new Error('bad edit - only text edits are supported'));
				}
				let model = this._modelService.getModel(edit.resource);
				if (!model) {
					return Promise.reject(new Error('bad edit - model not found'));
				}
				let array = edits.get(model);
				if (!array) {
					array = [];
				}
				edits.set(model, array.concat(edit.edits));
627 628 629 630 631 632 633 634 635 636 637
			}
		}

		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;
		});

638
		return Promise.resolve({
639 640 641 642 643
			selection: undefined,
			ariaSummary: localize('summary', 'Made {0} edits in {1} files', totalEdits, totalFiles)
		});
	}
}
A
Alex Dima 已提交
644

I
isidor 已提交
645
export class SimpleUriLabelService implements ILabelService {
A
Alex Dima 已提交
646 647
	_serviceBrand: any;

648
	private readonly _onDidRegisterFormatter = new Emitter<void>();
649
	public readonly onDidChangeFormatters: Event<void> = this._onDidRegisterFormatter.event;
A
Alex Dima 已提交
650

651
	public getUriLabel(resource: URI, options?: { relative?: boolean, forceNoTildify?: boolean }): string {
A
Alex Dima 已提交
652 653 654 655 656 657
		if (resource.scheme === 'file') {
			return resource.fsPath;
		}
		return resource.path;
	}

I
isidor 已提交
658 659
	public getWorkspaceLabel(workspace: IWorkspaceIdentifier | URI | IWorkspace, options?: { verbose: boolean; }): string {
		return '';
660 661
	}

662 663 664 665
	public getSeparator(scheme: string, authority?: string): '/' | '\\' {
		return '/';
	}

666
	public registerFormatter(formatter: ResourceLabelFormatter): IDisposable {
A
Alex Dima 已提交
667 668
		throw new Error('Not implemented');
	}
I
isidor 已提交
669 670 671 672

	public getHostLabel(): string {
		return '';
	}
A
Alex Dima 已提交
673
}