simpleServices.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';

A
Alex Dima 已提交
7 8 9
import {toErrorMessage} from 'vs/base/common/errors';
import {EventEmitter} from 'vs/base/common/eventEmitter';
import {Schemas} from 'vs/base/common/network';
E
Erich Gamma 已提交
10
import Severity from 'vs/base/common/severity';
11
import URI from 'vs/base/common/uri';
A
Alex Dima 已提交
12
import {TPromise} from 'vs/base/common/winjs.base';
13
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
14
import {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/common/configurationService';
A
Alex Dima 已提交
15
import {IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, ITextEditorModel, Position} from 'vs/platform/editor/common/editor';
16
import {AbstractExtensionService, ActivatedExtension} from 'vs/platform/extensions/common/abstractExtensionService';
A
Alex Dima 已提交
17
import {IExtensionDescription} from 'vs/platform/extensions/common/extensions';
18
import {ICommandService, ICommandHandler} from 'vs/platform/commands/common/commands';
19 20
import {KeybindingService2} from 'vs/platform/keybinding/browser/keybindingServiceImpl';
import {KeybindingService} from 'vs/platform/contextkey/browser/contextKeyService';
A
Alex Dima 已提交
21
import {IOSupport} from 'vs/platform/keybinding/common/keybindingResolver';
22 23
import {IKeybindingItem} from 'vs/platform/keybinding/common/keybinding';
import {IKeybindingService} from 'vs/platform/contextkey/common/contextkey';
A
Alex Dima 已提交
24
import {IConfirmation, IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
25
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
26 27
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser';
28
import {Selection} from 'vs/editor/common/core/selection';
29
import {IEventService} from 'vs/platform/event/common/event';
E
Erich Gamma 已提交
30 31 32 33 34 35 36

export class SimpleEditor implements IEditor {

	public input:IEditorInput;
	public options:IEditorOptions;
	public position:Position;

A
Alex Dima 已提交
37
	public _widget:editorCommon.IEditor;
E
Erich Gamma 已提交
38

A
Alex Dima 已提交
39
	constructor(editor:editorCommon.IEditor) {
E
Erich Gamma 已提交
40 41 42 43
		this._widget = editor;
	}

	public getId():string { return 'editor'; }
A
Alex Dima 已提交
44
	public getControl():editorCommon.IEditor { return this._widget; }
45
	public getSelection():Selection { return this._widget.getSelection(); }
E
Erich Gamma 已提交
46 47
	public focus():void { this._widget.focus(); }

A
Alex Dima 已提交
48 49
	public withTypedEditor<T>(codeEditorCallback:(editor:ICodeEditor)=>T, diffEditorCallback:(editor:IDiffEditor)=>T): T {
		if (this._widget.getEditorType() === editorCommon.EditorType.ICodeEditor) {
E
Erich Gamma 已提交
50
			// Single Editor
A
Alex Dima 已提交
51
			return codeEditorCallback(<ICodeEditor>this._widget);
E
Erich Gamma 已提交
52 53
		} else {
			// Diff Editor
A
Alex Dima 已提交
54
			return diffEditorCallback(<IDiffEditor>this._widget);
E
Erich Gamma 已提交
55 56 57 58
		}
	}
}

A
Alex Dima 已提交
59
export class SimpleModel extends EventEmitter implements ITextEditorModel  {
E
Erich Gamma 已提交
60

A
Alex Dima 已提交
61
	private model:editorCommon.IModel;
E
Erich Gamma 已提交
62

A
Alex Dima 已提交
63
	constructor(model:editorCommon.IModel) {
E
Erich Gamma 已提交
64 65 66 67
		super();
		this.model = model;
	}

A
Alex Dima 已提交
68
	public get textEditorModel():editorCommon.IModel {
E
Erich Gamma 已提交
69 70 71 72 73 74 75 76 77
		return this.model;
	}
}

export interface IOpenEditorDelegate {
	(url:string): boolean;
}

export class SimpleEditorService implements IEditorService {
78
	public _serviceBrand: any;
E
Erich Gamma 已提交
79 80 81 82 83 84 85 86

	private editor:SimpleEditor;
	private openEditorDelegate:IOpenEditorDelegate;

	constructor() {
		this.openEditorDelegate = null;
	}

A
Alex Dima 已提交
87
	public setEditor(editor:editorCommon.IEditor): void {
E
Erich Gamma 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		this.editor = new SimpleEditor(editor);
	}

	public setOpenEditorDelegate(openEditorDelegate:IOpenEditorDelegate): void {
		this.openEditorDelegate = openEditorDelegate;
	}

	public openEditor(typedData:IResourceInput, sideBySide?:boolean): TPromise<IEditor> {
		return TPromise.as(this.editor.withTypedEditor(
			(editor) => this.doOpenEditor(editor, typedData),
			(diffEditor) => (
				this.doOpenEditor(diffEditor.getOriginalEditor(), typedData) ||
				this.doOpenEditor(diffEditor.getModifiedEditor(), typedData)
			)
		));
	}

A
Alex Dima 已提交
105
	private doOpenEditor(editor:editorCommon.ICommonCodeEditor, data:IResourceInput): IEditor {
E
Erich Gamma 已提交
106 107 108 109 110 111 112 113
		var model = this.findModel(editor, data);
		if (!model) {
			if (data.resource) {
				if (this.openEditorDelegate) {
					this.openEditorDelegate(data.resource.toString());
					return null;
				} else {
					var schema = data.resource.scheme;
A
Alex Dima 已提交
114
					if (schema === Schemas.http || schema === Schemas.https) {
E
Erich Gamma 已提交
115 116 117 118 119 120 121 122 123 124
						// This is a fully qualified http or https URL
						window.open(data.resource.toString());
						return this.editor;
					}
				}
			}
			return null;
		}


A
Alex Dima 已提交
125
		var selection = <editorCommon.IRange>data.options.selection;
E
Erich Gamma 已提交
126 127 128
		if (selection) {
			if (typeof selection.endLineNumber === 'number' && typeof selection.endColumn === 'number') {
				editor.setSelection(selection);
A
Alex Dima 已提交
129
				editor.revealRangeInCenter(selection);
E
Erich Gamma 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142
			} else {
				var pos = {
					lineNumber: selection.startLineNumber,
					column: selection.startColumn
				};
				editor.setPosition(pos);
				editor.revealPositionInCenter(pos);
			}
		}

		return this.editor;
	}

A
Alex Dima 已提交
143
	private findModel(editor:editorCommon.ICommonCodeEditor, data:IResourceInput): editorCommon.IModel {
E
Erich Gamma 已提交
144
		var model = editor.getModel();
145
		if(model.uri.toString() !== data.resource.toString()) {
E
Erich Gamma 已提交
146 147 148 149 150 151 152
			return null;
		}

		return model;
	}

	public resolveEditorModel(typedData: IResourceInput, refresh?: boolean): TPromise<ITextEditorModel> {
A
Alex Dima 已提交
153
		var model: editorCommon.IModel;
E
Erich Gamma 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

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

		if (!model) {
			return TPromise.as(null);
		}

		return TPromise.as(new SimpleModel(model));
	}
}

export class SimpleMessageService implements IMessageService {
169
	public _serviceBrand: any;
E
Erich Gamma 已提交
170 171 172 173 174 175 176

	private static Empty = function() { /* nothing */};

	public show(sev:Severity, message:any):()=>void {

		switch(sev) {
			case Severity.Error:
A
Alex Dima 已提交
177
				console.error(toErrorMessage(message, true));
E
Erich Gamma 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
				break;
			case Severity.Warning:
				console.warn(message);
				break;
			default:
				console.log(message);
				break;
		}

		return SimpleMessageService.Empty;
	}

	public hideAll():void {
		// No-op
	}

	public confirm(confirmation:IConfirmation):boolean {
		var messageText = confirmation.message;
		if (confirmation.detail) {
			messageText = messageText + '\n\n' + confirmation.detail;
		}

		return window.confirm(messageText);
	}
A
Alex Dima 已提交
202 203 204
}

export class StandaloneKeybindingService2 extends KeybindingService2 {
E
Erich Gamma 已提交
205 206 207
	private static LAST_GENERATED_ID = 0;

	private _dynamicKeybindings: IKeybindingItem[];
208
	private _dynamicCommands: { [id: string]: ICommandHandler };
E
Erich Gamma 已提交
209

A
Alex Dima 已提交
210 211 212 213 214 215 216
	constructor(
		keybindingService: IKeybindingService,
		commandService: ICommandService,
		messageService: IMessageService,
		domNode: HTMLElement
	) {
		super(keybindingService, commandService, messageService);
217

E
Erich Gamma 已提交
218 219
		this._dynamicKeybindings = [];
		this._dynamicCommands = Object.create(null);
220 221

		this._beginListening(domNode);
E
Erich Gamma 已提交
222 223
	}

224
	public addDynamicKeybinding(keybinding: number, handler:ICommandHandler, when:string, commandId:string = null): string {
E
Erich Gamma 已提交
225
		if (commandId === null) {
A
Alex Dima 已提交
226
			commandId = 'DYNAMIC_' + (++StandaloneKeybindingService2.LAST_GENERATED_ID);
E
Erich Gamma 已提交
227
		}
228
		var parsedContext = IOSupport.readKeybindingWhen(when);
E
Erich Gamma 已提交
229 230 231
		this._dynamicKeybindings.push({
			keybinding: keybinding,
			command: commandId,
232
			when: parsedContext,
E
Erich Gamma 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
			weight1: 1000,
			weight2: 0
		});
		this._dynamicCommands[commandId] = handler;
		this.updateResolver();
		return commandId;
	}

	protected _getExtraKeybindings(isFirstTime:boolean): IKeybindingItem[] {
		return this._dynamicKeybindings;
	}

	protected _getCommandHandler(commandId:string): ICommandHandler {
		return super._getCommandHandler(commandId) || this._dynamicCommands[commandId];
	}
}

A
Alex Dima 已提交
250 251 252 253 254 255
export class StandaloneKeybindingService extends KeybindingService {
	constructor(configurationService: IConfigurationService) {
		super(configurationService);
	}
}

A
Alex Dima 已提交
256
export class SimpleExtensionService extends AbstractExtensionService<ActivatedExtension> {
E
Erich Gamma 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

	constructor() {
		super(true);
	}

	protected _showMessage(severity:Severity, msg:string): void {
		switch (severity) {
			case Severity.Error:
				console.error(msg);
				break;
			case Severity.Warning:
				console.warn(msg);
				break;
			case Severity.Info:
				console.info(msg);
				break;
			default:
				console.log(msg);
		}
	}
277

278
	protected _createFailedExtension(): ActivatedExtension {
279 280 281
		throw new Error('unexpected');
	}

282
	protected _actualActivateExtension(extensionDescription: IExtensionDescription): TPromise<ActivatedExtension> {
283 284 285
		throw new Error('unexpected');
	}

E
Erich Gamma 已提交
286
}
287 288 289

export class SimpleConfigurationService extends ConfigurationService {

290 291 292 293 294
	constructor(contextService: IWorkspaceContextService, eventService: IEventService) {
		super(contextService, eventService);
		this.initialize();
	}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	protected resolveContents(resources: URI[]): TPromise<IContent[]> {
		return TPromise.as(resources.map((resource) => {
			return {
				resource: resource,
				value: ''
			};
		}));
	}

	protected resolveContent(resource: URI): TPromise<IContent> {
		return TPromise.as({
			resource: resource,
			value: ''
		});
	}

	protected resolveStat(resource: URI): TPromise<IStat> {
		return TPromise.as({
			resource: resource,
			isDirectory: false
		});
	}

318 319 320 321
	setUserConfiguration(key: any, value: any) : Thenable<void> {
		return TPromise.as(null);
	}

322
}