simpleServices.ts 21.3 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 12
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';
import { OS, isLinux, isMacintosh } 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, LabelRules, RegisterFormatterEvent } 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';
E
Erich Gamma 已提交
45

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
81 82 83 84 85 86 87 88 89 90
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);
	}
}

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

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

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

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

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

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

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

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

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

		return model;
	}
}

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

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

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

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

153
export class SimpleDialogService implements IDialogService {
154 155

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

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

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

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

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

180 181 182 183
export class SimpleNotificationService implements INotificationService {

	public _serviceBrand: any;

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

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

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

211
		return SimpleNotificationService.NO_OP;
212
	}
213

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

A
Alex Dima 已提交
219 220
export class StandaloneCommandService implements ICommandService {
	_serviceBrand: any;
221

A
Alex Dima 已提交
222
	private readonly _instantiationService: IInstantiationService;
223 224
	private _dynamicCommands: { [id: string]: ICommand; };

225
	private readonly _onWillExecuteCommand = new Emitter<ICommandEvent>();
226 227
	public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;

A
Alex Dima 已提交
228 229
	constructor(instantiationService: IInstantiationService) {
		this._instantiationService = instantiationService;
230 231 232
		this._dynamicCommands = Object.create(null);
	}

J
Johannes Rieken 已提交
233 234
	public addCommand(command: ICommand): IDisposable {
		const { id } = command;
235
		this._dynamicCommands[id] = command;
236 237 238
		return toDisposable(() => {
			delete this._dynamicCommands[id];
		});
239 240
	}

241
	public executeCommand<T>(id: string, ...args: any[]): Promise<T> {
A
Alex Dima 已提交
242 243
		const command = (CommandsRegistry.getCommand(id) || this._dynamicCommands[id]);
		if (!command) {
244
			return Promise.reject(new Error(`command '${id}' not found`));
A
Alex Dima 已提交
245 246 247
		}

		try {
248
			this._onWillExecuteCommand.fire({ commandId: id });
249
			const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler, ...args]) as T;
250
			return Promise.resolve(result);
A
Alex Dima 已提交
251
		} catch (err) {
252
			return Promise.reject(err);
A
Alex Dima 已提交
253
		}
254 255 256
	}
}

257
export class StandaloneKeybindingService extends AbstractKeybindingService {
A
Alex Dima 已提交
258
	private _cachedResolver: KeybindingResolver | null;
E
Erich Gamma 已提交
259 260
	private _dynamicKeybindings: IKeybindingItem[];

A
Alex Dima 已提交
261
	constructor(
262
		contextKeyService: IContextKeyService,
A
Alex Dima 已提交
263
		commandService: ICommandService,
264
		telemetryService: ITelemetryService,
265
		notificationService: INotificationService,
A
Alex Dima 已提交
266 267
		domNode: HTMLElement
	) {
268
		super(contextKeyService, commandService, telemetryService, notificationService);
269

270
		this._cachedResolver = null;
E
Erich Gamma 已提交
271
		this._dynamicKeybindings = [];
272

273
		this._register(dom.addDisposableListener(domNode, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
274
			let keyEvent = new StandardKeyboardEvent(e);
275
			let shouldPreventDefault = this._dispatch(keyEvent, keyEvent.target);
276 277 278 279
			if (shouldPreventDefault) {
				keyEvent.preventDefault();
			}
		}));
E
Erich Gamma 已提交
280 281
	}

A
Alex Dima 已提交
282 283 284 285 286 287
	public addDynamicKeybinding(commandId: string, _keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | null): IDisposable {
		const keybinding = createKeybinding(_keybinding, OS);
		if (!keybinding) {
			throw new Error(`Invalid keybinding`);
		}

288 289
		let toDispose: IDisposable[] = [];

E
Erich Gamma 已提交
290
		this._dynamicKeybindings.push({
A
Alex Dima 已提交
291
			keybinding: keybinding,
E
Erich Gamma 已提交
292
			command: commandId,
293
			when: when,
E
Erich Gamma 已提交
294 295 296
			weight1: 1000,
			weight2: 0
		});
297

298 299 300 301 302 303 304
		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;
305 306
				}
			}
307
		}));
308

309 310
		let commandService = this._commandService;
		if (commandService instanceof StandaloneCommandService) {
J
Johannes Rieken 已提交
311 312
			toDispose.push(commandService.addCommand({
				id: commandId,
313
				handler: handler
314
			}));
315 316 317
		} else {
			throw new Error('Unknown command service!');
		}
C
Christof Marti 已提交
318
		this.updateResolver({ source: KeybindingSource.Default });
319

320
		return combinedDisposable(toDispose);
E
Erich Gamma 已提交
321 322
	}

323 324 325 326 327 328 329
	private updateResolver(event: IKeybindingEvent): void {
		this._cachedResolver = null;
		this._onDidUpdateKeybindings.fire(event);
	}

	protected _getResolver(): KeybindingResolver {
		if (!this._cachedResolver) {
330 331
			const defaults = this._toNormalizedKeybindingItems(KeybindingsRegistry.getDefaultKeybindings(), true);
			const overrides = this._toNormalizedKeybindingItems(this._dynamicKeybindings, false);
332
			this._cachedResolver = new KeybindingResolver(defaults, overrides);
333 334 335 336
		}
		return this._cachedResolver;
	}

337 338 339 340
	protected _documentHasFocus(): boolean {
		return document.hasFocus();
	}

341 342
	private _toNormalizedKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
		let result: ResolvedKeybindingItem[] = [], resultLen = 0;
343 344 345
		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 已提交
346
			const keybinding = item.keybinding;
347

348 349 350 351 352
			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);
353 354
				for (const resolvedKeybinding of resolvedKeybindings) {
					result[resultLen++] = new ResolvedKeybindingItem(resolvedKeybinding, item.command, item.commandArgs, when, isDefault);
355 356
				}
			}
357 358 359 360 361
		}

		return result;
	}

362 363
	public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {
		return [new USLayoutResolvedKeybinding(keybinding, OS)];
A
Alex Dima 已提交
364 365
	}

366 367 368 369 370 371 372 373
	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
		let keybinding = new SimpleKeybinding(
			keyboardEvent.ctrlKey,
			keyboardEvent.shiftKey,
			keyboardEvent.altKey,
			keyboardEvent.metaKey,
			keyboardEvent.keyCode
		);
374
		return new USLayoutResolvedKeybinding(keybinding, OS);
375
	}
376 377 378 379

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

382 383 384 385 386 387 388
function isConfigurationOverrides(thing: any): thing is IConfigurationOverrides {
	return thing
		&& typeof thing === 'object'
		&& (!thing.overrideIdentifier || typeof thing.overrideIdentifier === 'string')
		&& (!thing.resource || thing.resource instanceof URI);
}

389
export class SimpleConfigurationService implements IConfigurationService {
390

391 392
	_serviceBrand: any;

393
	private _onDidChangeConfiguration = new Emitter<IConfigurationChangeEvent>();
394
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent> = this._onDidChangeConfiguration.event;
395

396
	private _configuration: Configuration;
397

398
	constructor() {
399
		this._configuration = new Configuration(new DefaultConfigurationModel(), new ConfigurationModel());
400 401
	}

402
	private configuration(): Configuration {
403
		return this._configuration;
404 405
	}

406 407 408 409 410
	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 已提交
411
		const section = typeof arg1 === 'string' ? arg1 : undefined;
412 413
		const overrides = isConfigurationOverrides(arg1) ? arg1 : isConfigurationOverrides(arg2) ? arg2 : {};
		return this.configuration().getValue(section, overrides, null);
414 415
	}

S
Sandeep Somavarapu 已提交
416
	public updateValue(key: string, value: any, arg3?: any, arg4?: any): Promise<void> {
417
		this.configuration().updateValue(key, value);
A
Alex Dima 已提交
418
		return Promise.resolve();
B
Benjamin Pasero 已提交
419
	}
B
Benjamin Pasero 已提交
420

S
Sandeep Somavarapu 已提交
421
	public inspect<C>(key: string, options: IConfigurationOverrides = {}): {
422 423
		default: C,
		user: C,
M
Matt Bierner 已提交
424 425
		workspace?: C,
		workspaceFolder?: C
426 427
		value: C,
	} {
428
		return this.configuration().inspect<C>(key, options, null);
B
Benjamin Pasero 已提交
429
	}
430

431
	public keys() {
S
Sandeep Somavarapu 已提交
432
		return this.configuration().keys(null);
433 434
	}

S
Sandeep Somavarapu 已提交
435
	public reloadConfiguration(): Promise<void> {
R
Rob Lourens 已提交
436
		return Promise.resolve(undefined);
437
	}
S
Sandeep Somavarapu 已提交
438

A
Alex Dima 已提交
439
	public getConfigurationData(): IConfigurationData | null {
S
Sandeep Somavarapu 已提交
440 441
		return null;
	}
442
}
A
Alex Dima 已提交
443

444 445 446 447
export class SimpleResourceConfigurationService implements ITextResourceConfigurationService {

	_serviceBrand: any;

448 449
	public readonly onDidChangeConfiguration: Event<IConfigurationChangeEvent>;
	private readonly _onDidChangeConfigurationEmitter = new Emitter();
450 451

	constructor(private configurationService: SimpleConfigurationService) {
452 453
		this.configurationService.onDidChangeConfiguration((e) => {
			this._onDidChangeConfigurationEmitter.fire(e);
454
		});
455 456
	}

457 458 459
	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 已提交
460
		const position: IPosition | null = Pos.isIPosition(arg2) ? arg2 : null;
R
Rob Lourens 已提交
461
		const section: string | undefined = position ? (typeof arg3 === 'string' ? arg3 : undefined) : (typeof arg2 === 'string' ? arg2 : undefined);
A
Alex Dima 已提交
462 463 464
		if (typeof section === 'undefined') {
			return this.configurationService.getValue<T>();
		}
465
		return this.configurationService.getValue<T>(section);
466 467 468
	}
}

S
Sandeep Somavarapu 已提交
469 470 471 472 473
export class SimpleResourcePropertiesService implements ITextResourcePropertiesService {

	_serviceBrand: any;

	constructor(
474
		@IConfigurationService private readonly configurationService: IConfigurationService,
S
Sandeep Somavarapu 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488
	) {
	}

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

489 490 491 492 493
export class StandaloneTelemetryService implements ITelemetryService {
	_serviceBrand: void;

	public isOptedIn = false;

494
	public publicLog(eventName: string, data?: any): Promise<void> {
R
Rob Lourens 已提交
495
		return Promise.resolve(undefined);
496 497
	}

498
	public getTelemetryInfo(): Promise<ITelemetryInfo> {
A
Alex Dima 已提交
499
		throw new Error(`Not available`);
500 501
	}
}
502 503 504 505 506

export class SimpleWorkspaceContextService implements IWorkspaceContextService {

	public _serviceBrand: any;

507
	private static SCHEME = 'inmemory';
508

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

512
	private readonly _onDidChangeWorkspaceFolders = new Emitter<IWorkspaceFoldersChangeEvent>();
513
	public readonly onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent> = this._onDidChangeWorkspaceFolders.event;
514

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

518
	private readonly workspace: IWorkspace;
519

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

B
Benjamin Pasero 已提交
525
	public getWorkspace(): IWorkspace {
526
		return this.workspace;
527 528
	}

529
	public getWorkbenchState(): WorkbenchState {
530 531
		if (this.workspace) {
			if (this.workspace.configuration) {
532
				return WorkbenchState.WORKSPACE;
533
			}
534
			return WorkbenchState.FOLDER;
535
		}
536
		return WorkbenchState.EMPTY;
537 538
	}

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

543
	public isInsideWorkspace(resource: URI): boolean {
544
		return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME;
545 546
	}

547
	public isCurrentWorkspace(workspaceIdentifier: ISingleFolderWorkspaceIdentifier | IWorkspaceIdentifier): boolean {
548 549
		return true;
	}
550
}
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

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]);
		}
	});
}
568 569 570 571 572 573 574 575

export class SimpleBulkEditService implements IBulkEditService {
	_serviceBrand: any;

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

576
	apply(workspaceEdit: WorkspaceEdit, options: IBulkEditOptions): Promise<IBulkEditResult> {
577 578 579

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

A
Alex Dima 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593
		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));
594 595 596 597 598 599 600 601 602 603 604
			}
		}

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

605
		return Promise.resolve({
606 607 608 609 610
			selection: undefined,
			ariaSummary: localize('summary', 'Made {0} edits in {1} files', totalEdits, totalFiles)
		});
	}
}
A
Alex Dima 已提交
611

I
isidor 已提交
612
export class SimpleUriLabelService implements ILabelService {
A
Alex Dima 已提交
613 614
	_serviceBrand: any;

615
	private readonly _onDidRegisterFormatter = new Emitter<RegisterFormatterEvent>();
616
	public readonly onDidRegisterFormatter: Event<RegisterFormatterEvent> = this._onDidRegisterFormatter.event;
A
Alex Dima 已提交
617

618
	public getUriLabel(resource: URI, options?: { relative?: boolean, forceNoTildify?: boolean }): string {
A
Alex Dima 已提交
619 620 621 622 623 624
		if (resource.scheme === 'file') {
			return resource.fsPath;
		}
		return resource.path;
	}

I
isidor 已提交
625 626
	public getWorkspaceLabel(workspace: IWorkspaceIdentifier | URI | IWorkspace, options?: { verbose: boolean; }): string {
		return '';
627 628
	}

629
	public registerFormatter(selector: string, formatter: LabelRules): IDisposable {
A
Alex Dima 已提交
630 631
		throw new Error('Not implemented');
	}
I
isidor 已提交
632 633 634 635

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