simpleServices.ts 8.3 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';
A
Alex Dima 已提交
12 13 14 15 16 17 18 19
import {TPromise} from 'vs/base/common/winjs.base';
import {IEditor, IEditorInput, IEditorOptions, IEditorService, IResourceInput, ITextEditorModel, Position} from 'vs/platform/editor/common/editor';
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';
import {AbstractPluginService, ActivatedPlugin} from 'vs/platform/plugins/common/abstractPluginService';
import {IPluginDescription} from 'vs/platform/plugins/common/plugins';
E
Erich Gamma 已提交
20 21 22
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 已提交
23 24
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser';
E
Erich Gamma 已提交
25 26 27 28 29 30 31

export class SimpleEditor implements IEditor {

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

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

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

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

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

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

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

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

A
Alex Dima 已提交
63
	public get textEditorModel():editorCommon.IModel {
E
Erich Gamma 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		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 已提交
82
	public setEditor(editor:editorCommon.IEditor): void {
E
Erich Gamma 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		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 已提交
100
	private doOpenEditor(editor:editorCommon.ICommonCodeEditor, data:IResourceInput): IEditor {
E
Erich Gamma 已提交
101 102 103 104 105 106 107 108
		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 已提交
109
					if (schema === Schemas.http || schema === Schemas.https) {
E
Erich Gamma 已提交
110 111 112 113 114 115 116 117 118 119
						// This is a fully qualified http or https URL
						window.open(data.resource.toString());
						return this.editor;
					}
				}
			}
			return null;
		}


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

		return this.editor;
	}

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

		return model;
	}

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

		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 已提交
172
				console.error(toErrorMessage(message, true));
E
Erich Gamma 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
				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 已提交
198
	public setStatusMessage(message: string, autoDisposeAfter:number = -1): IDisposable {
E
Erich Gamma 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
		return {
			dispose: () => { /* Nothing to do here */ }
		};
	}
}

export class SimpleEditorRequestService extends BaseRequestService {

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

A
Alex Dima 已提交
212
export class StandaloneKeybindingService extends KeybindingService {
E
Erich Gamma 已提交
213 214 215 216 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 245 246 247 248 249
	private static LAST_GENERATED_ID = 0;

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

	constructor(domNode: HTMLElement) {
		this._dynamicKeybindings = [];
		this._dynamicCommands = Object.create(null);
		super(domNode);
	}

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

250
export class SimplePluginService extends AbstractPluginService<ActivatedPlugin> {
E
Erich Gamma 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

	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);
		}
	}
271 272 273 274

	public deactivate(pluginId:string): void {
		// nothing to do
	}
275 276 277 278 279 280 281 282 283

	protected _createFailedPlugin(): ActivatedPlugin {
		throw new Error('unexpected');
	}

	protected _actualActivatePlugin(pluginDescription: IPluginDescription): TPromise<ActivatedPlugin> {
		throw new Error('unexpected');
	}

E
Erich Gamma 已提交
284
}