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 10
import {toErrorMessage} from 'vs/base/common/errors';
import {EventEmitter} from 'vs/base/common/eventEmitter';
import {IDisposable} from 'vs/base/common/lifecycle';
import {Schemas} from 'vs/base/common/network';
E
Erich Gamma 已提交
11
import Severity from 'vs/base/common/severity';
12
import URI from 'vs/base/common/uri';
A
Alex Dima 已提交
13
import {TPromise} from 'vs/base/common/winjs.base';
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';
A
Alex Dima 已提交
18 19 20 21
import {KeybindingService} from 'vs/platform/keybinding/browser/keybindingServiceImpl';
import {IOSupport} from 'vs/platform/keybinding/common/keybindingResolver';
import {ICommandHandler, ICommandsMap, IKeybindingItem} from 'vs/platform/keybinding/common/keybindingService';
import {IConfirmation, IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
22 23 24
import {BaseRequestService} from 'vs/platform/request/common/baseRequestService';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
25 26
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser';
E
Erich Gamma 已提交
27 28 29 30 31 32 33

export class SimpleEditor implements IEditor {

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

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

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

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

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

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

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

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

A
Alex Dima 已提交
65
	public get textEditorModel():editorCommon.IModel {
E
Erich Gamma 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		return this.model;
	}
}

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

export class SimpleEditorService implements IEditorService {
	public serviceId = IEditorService;

	private editor:SimpleEditor;
	private openEditorDelegate:IOpenEditorDelegate;

	constructor() {
		this.openEditorDelegate = null;
	}

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


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

		return this.editor;
	}

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

		return model;
	}

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

		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 {
	public serviceId = IMessageService;

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

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

		switch(sev) {
			case Severity.Error:
A
Alex Dima 已提交
174
				console.error(toErrorMessage(message, true));
E
Erich Gamma 已提交
175 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
	public setStatusMessage(message: string, autoDisposeAfter:number = -1): IDisposable {
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213
		return {
			dispose: () => { /* Nothing to do here */ }
		};
	}
}

export class SimpleEditorRequestService extends BaseRequestService {

	constructor(contextService: IWorkspaceContextService, telemetryService?: ITelemetryService) {
		super(contextService, telemetryService);
	}
}

A
Alex Dima 已提交
214
export class StandaloneKeybindingService extends KeybindingService {
E
Erich Gamma 已提交
215 216 217 218 219 220
	private static LAST_GENERATED_ID = 0;

	private _dynamicKeybindings: IKeybindingItem[];
	private _dynamicCommands: ICommandsMap;

	constructor(domNode: HTMLElement) {
221
		super();
222

E
Erich Gamma 已提交
223 224
		this._dynamicKeybindings = [];
		this._dynamicCommands = Object.create(null);
225 226

		this._beginListening(domNode);
E
Erich Gamma 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	}

	public addDynamicKeybinding(keybinding: number, handler:ICommandHandler, context:string, commandId:string = null): string {
		if (commandId === null) {
			commandId = 'DYNAMIC_' + (++StandaloneKeybindingService.LAST_GENERATED_ID);
		}
		var parsedContext = IOSupport.readKeybindingContexts(context);
		this._dynamicKeybindings.push({
			keybinding: keybinding,
			command: commandId,
			context: parsedContext,
			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];
	}
}

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

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

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

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

E
Erich Gamma 已提交
285
}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

export class SimpleConfigurationService extends ConfigurationService {

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

}