simpleServices.ts 9.0 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 {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/common/configurationService';
A
Alex Dima 已提交
14
import {IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, ITextEditorModel, Position} from 'vs/platform/editor/common/editor';
15
import {AbstractExtensionService, ActivatedExtension} from 'vs/platform/extensions/common/abstractExtensionService';
A
Alex Dima 已提交
16
import {IExtensionDescription} from 'vs/platform/extensions/common/extensions';
17
import {ICommandService, ICommandHandler} from 'vs/platform/commands/common/commands';
18
import {KeybindingService2} from 'vs/platform/keybinding/browser/keybindingServiceImpl';
A
Alex Dima 已提交
19
import {IOSupport} from 'vs/platform/keybinding/common/keybindingResolver';
20
import {IKeybindingItem} from 'vs/platform/keybinding/common/keybinding';
21
import {IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
A
Alex Dima 已提交
22
import {IConfirmation, IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
23
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
24 25
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser';
26
import {Selection} from 'vs/editor/common/core/selection';
27
import {IEventService} from 'vs/platform/event/common/event';
E
Erich Gamma 已提交
28 29 30 31 32 33 34

export class SimpleEditor implements IEditor {

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

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

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

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

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

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

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

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

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

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

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

	private editor:SimpleEditor;
	private openEditorDelegate:IOpenEditorDelegate;

	constructor() {
		this.openEditorDelegate = null;
	}

A
Alex Dima 已提交
85
	public setEditor(editor:editorCommon.IEditor): void {
E
Erich Gamma 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		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 已提交
103
	private doOpenEditor(editor:editorCommon.ICommonCodeEditor, data:IResourceInput): IEditor {
E
Erich Gamma 已提交
104 105 106 107 108 109 110 111
		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 已提交
112
					if (schema === Schemas.http || schema === Schemas.https) {
E
Erich Gamma 已提交
113 114 115 116 117 118 119 120 121 122
						// This is a fully qualified http or https URL
						window.open(data.resource.toString());
						return this.editor;
					}
				}
			}
			return null;
		}


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

		return this.editor;
	}

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

		return model;
	}

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

		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 {
167
	public _serviceBrand: any;
E
Erich Gamma 已提交
168 169 170 171 172 173 174

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

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

		switch(sev) {
			case Severity.Error:
A
Alex Dima 已提交
175
				console.error(toErrorMessage(message, true));
E
Erich Gamma 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
				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 已提交
200 201 202
}

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

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

A
Alex Dima 已提交
208
	constructor(
209
		contextKeyService: IContextKeyService,
A
Alex Dima 已提交
210 211 212 213
		commandService: ICommandService,
		messageService: IMessageService,
		domNode: HTMLElement
	) {
214
		super(contextKeyService, commandService, messageService);
215

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

		this._beginListening(domNode);
E
Erich Gamma 已提交
220 221
	}

222
	public addDynamicKeybinding(keybinding: number, handler:ICommandHandler, when:string, commandId:string = null): string {
E
Erich Gamma 已提交
223
		if (commandId === null) {
A
Alex Dima 已提交
224
			commandId = 'DYNAMIC_' + (++StandaloneKeybindingService2.LAST_GENERATED_ID);
E
Erich Gamma 已提交
225
		}
226
		var parsedContext = IOSupport.readKeybindingWhen(when);
E
Erich Gamma 已提交
227 228 229
		this._dynamicKeybindings.push({
			keybinding: keybinding,
			command: commandId,
230
			when: parsedContext,
E
Erich Gamma 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
			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 已提交
248
export class SimpleExtensionService extends AbstractExtensionService<ActivatedExtension> {
E
Erich Gamma 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

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

270
	protected _createFailedExtension(): ActivatedExtension {
271 272 273
		throw new Error('unexpected');
	}

274
	protected _actualActivateExtension(extensionDescription: IExtensionDescription): TPromise<ActivatedExtension> {
275 276 277
		throw new Error('unexpected');
	}

E
Erich Gamma 已提交
278
}
279 280 281

export class SimpleConfigurationService extends ConfigurationService {

282 283 284 285 286
	constructor(contextService: IWorkspaceContextService, eventService: IEventService) {
		super(contextService, eventService);
		this.initialize();
	}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	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
		});
	}

310 311 312 313
	setUserConfiguration(key: any, value: any) : Thenable<void> {
		return TPromise.as(null);
	}

314
}