textEditor.ts 10.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/texteditor';
import {TPromise} from 'vs/base/common/winjs.base';
import {Dimension, Builder} from 'vs/base/browser/builder';
import objects = require('vs/base/common/objects');
import {CodeEditorWidget} from 'vs/editor/browser/widget/codeEditorWidget';
import {IEditorViewState} from 'vs/editor/common/editorCommon';
14
import {OptionsChangeEvent, EventType as WorkbenchEventType, EditorEvent, TextEditorSelectionEvent} from 'vs/workbench/common/events';
E
Erich Gamma 已提交
15
import {Scope} from 'vs/workbench/common/memento';
B
Benjamin Pasero 已提交
16
import {EditorInput, EditorOptions} from 'vs/workbench/common/editor';
E
Erich Gamma 已提交
17 18
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {EditorConfiguration} from 'vs/editor/common/config/commonEditorConfig';
19
import {IEditorSelection, IEditor, EventType, IConfigurationChangedEvent, IModelContentChangedEvent, IModelOptionsChangedEvent, IModelModeChangedEvent, ICursorPositionChangedEvent, IEditorOptions} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
20 21 22
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
import {IFilesConfiguration} from 'vs/platform/files/common/files';
import {Position} from 'vs/platform/editor/common/editor';
M
Martin Aeschlimann 已提交
23
import {IStorageService} from 'vs/platform/storage/common/storage';
24
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
25 26 27 28 29 30
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IMessageService} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IModeService} from 'vs/editor/common/services/modeService';
M
Martin Aeschlimann 已提交
31
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
E
Erich Gamma 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

const EDITOR_VIEW_STATE_PREFERENCE_KEY = 'editorViewState';

/**
 * The base class of editors that leverage the monaco text editor for the editing experience. This class is only intended to
 * be subclassed and not instantiated.
 */
export abstract class BaseTextEditor extends BaseEditor {
	private editorControl: IEditor;
	private _editorContainer: Builder;

	constructor(
		id: string,
		@ITelemetryService telemetryService: ITelemetryService,
		@IInstantiationService private _instantiationService: IInstantiationService,
		@IWorkspaceContextService private _contextService: IWorkspaceContextService,
		@IStorageService private _storageService: IStorageService,
		@IMessageService private _messageService: IMessageService,
		@IConfigurationService private configurationService: IConfigurationService,
		@IEventService private _eventService: IEventService,
		@IWorkbenchEditorService private _editorService: IWorkbenchEditorService,
M
Martin Aeschlimann 已提交
53 54
		@IModeService private _modeService: IModeService,
		@IThemeService private _themeService: IThemeService
E
Erich Gamma 已提交
55 56 57 58
	) {
		super(id, telemetryService);

		this.toUnbind.push(this._eventService.addListener(WorkbenchEventType.WORKBENCH_OPTIONS_CHANGED, (e) => this.onOptionsChanged(e)));
59
		this.toUnbind.push(this.configurationService.onDidUpdateConfiguration(e => this.applyConfiguration(e.config)).dispose);
M
Martin Aeschlimann 已提交
60 61 62

		this.toUnbind.push(_themeService.onDidThemeChange(_ => this.onThemeChanged()).dispose);
}
E
Erich Gamma 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	public get instantiationService(): IInstantiationService {
		return this._instantiationService;
	}

	public get contextService(): IWorkspaceContextService {
		return this._contextService;
	}

	public get storageService(): IStorageService {
		return this._storageService;
	}

	public get messageService() {
		return this._messageService;
	}

	protected applyConfiguration(configuration: IFilesConfiguration): void {

		// Update Editor with configuration and editor settings
		if (this.editorControl) {
			let specificEditorSettings = this.getCodeEditorOptions();
			configuration = objects.clone(configuration); // dont modify original config
			objects.assign(configuration[EditorConfiguration.EDITOR_SECTION], specificEditorSettings);

			EditorConfiguration.apply(configuration, this.editorControl);
		}

		// Update Languages
		this._modeService.configureAllModes(configuration);
	}

	private onOptionsChanged(event: OptionsChangeEvent): void {
		if (this.editorControl) {
			this.editorControl.updateOptions(this.getCodeEditorOptions());
		}
	}

M
Martin Aeschlimann 已提交
101 102
	private onThemeChanged(): void {
		this.editorControl.updateOptions(this.getCodeEditorOptions());
E
Erich Gamma 已提交
103 104 105 106 107 108 109 110
	}

	protected getCodeEditorOptions(): IEditorOptions {
		let baseOptions: IEditorOptions = {
			overviewRulerLanes: 3,
			readOnly: this.contextService.getOptions().readOnly,
			glyphMargin: true,
			lineNumbersMinChars: 3,
M
Martin Aeschlimann 已提交
111
			theme: this._themeService.getTheme()
E
Erich Gamma 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		};

		// Always mixin editor options from the context into our set to allow for override
		return objects.mixin(baseOptions, this.contextService.getOptions().editor);
	}

	public get eventService(): IEventService {
		return this._eventService;
	}

	public get editorService() {
		return this._editorService;
	}

	public get editorContainer(): Builder {
		return this._editorContainer;
	}

	public createEditor(parent: Builder): void {

		// Editor for Text
		this._editorContainer = parent;
		this.editorControl = this.createEditorControl(parent);

136
		// Hook Listener for Selection changes
E
Erich Gamma 已提交
137
		this.toUnbind.push(this.editorControl.addListener(EventType.CursorPositionChanged, (event: ICursorPositionChangedEvent) => {
138 139
			let selection = this.editorControl.getSelection();
			this.eventService.emit(WorkbenchEventType.TEXT_EDITOR_SELECTION_CHANGED, new TextEditorSelectionEvent(selection, this, this.getId(), this.input, null, this.position, event));
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149 150 151
		}));

		// Hook Listener for mode changes
		this.toUnbind.push(this.editorControl.addListener(EventType.ModelModeChanged, (event: IModelModeChangedEvent) => {
			this.eventService.emit(WorkbenchEventType.TEXT_EDITOR_MODE_CHANGED, new EditorEvent(this, this.getId(), this.input, null, this.position, event));
		}));

		// Hook Listener for content changes
		this.toUnbind.push(this.editorControl.addListener(EventType.ModelContentChanged, (event: IModelContentChangedEvent) => {
			this.eventService.emit(WorkbenchEventType.TEXT_EDITOR_CONTENT_CHANGED, new EditorEvent(this, this.getId(), this.input, null, this.position, event));
		}));

152 153 154 155 156
		// Hook Listener for content options changes
		this.toUnbind.push(this.editorControl.addListener(EventType.ModelOptionsChanged, (event: IModelOptionsChangedEvent) => {
			this.eventService.emit(WorkbenchEventType.TEXT_EDITOR_CONTENT_OPTIONS_CHANGED, new EditorEvent(this, this.getId(), this.input, null, this.position, event));
		}));

E
Erich Gamma 已提交
157 158 159 160 161 162
		// Hook Listener for options changes
		this.toUnbind.push(this.editorControl.addListener(EventType.ConfigurationChanged, (event: IConfigurationChangedEvent) => {
			this.eventService.emit(WorkbenchEventType.TEXT_EDITOR_CONFIGURATION_CHANGED, new EditorEvent(this, this.getId(), this.input, null, this.position, event));
		}));

		// Configuration
163
		this.applyConfiguration(this.configurationService.getConfiguration<IFilesConfiguration>());
E
Erich Gamma 已提交
164 165 166 167 168 169 170 171 172 173
	}

	/**
	 * This method creates and returns the text editor control to be used. Subclasses can override to
	 * provide their own editor control that should be used (e.g. a DiffEditor).
	 */
	public createEditorControl(parent: Builder): IEditor {
		return this._instantiationService.createInstance(CodeEditorWidget, parent.getHTMLElement(), this.getCodeEditorOptions());
	}

174 175 176 177 178 179
	public setInput(input: EditorInput, options: EditorOptions): TPromise<void> {
		return super.setInput(input, options).then(() => {
			this.editorControl.updateOptions(this.getCodeEditorOptions()); // support input specific editor options
		});
	}

E
Erich Gamma 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 250 251 252 253 254 255 256 257 258
	public setVisible(visible: boolean, position: Position = null): TPromise<void> {
		let promise = super.setVisible(visible, position);

		// Pass on to Editor
		if (visible) {
			this.editorControl.onVisible();
		} else {
			this.editorControl.onHide();
		}

		return promise;
	}

	public focus(): void {
		this.editorControl.focus();
	}

	public layout(dimension: Dimension): void {

		// Pass on to Editor
		this.editorControl.layout(dimension);
	}

	public getControl(): IEditor {
		return this.editorControl;
	}

	public getSelection(): IEditorSelection {
		return this.editorControl.getSelection();
	}

	/**
	 * Saves the text editor view state under the given key.
	 */
	public saveTextEditorViewState(storageService: IStorageService, key: string): void {
		let editorViewState = this.editorControl.saveViewState();

		const memento = this.getMemento(storageService, Scope.WORKSPACE);
		let editorViewStateMemento = memento[EDITOR_VIEW_STATE_PREFERENCE_KEY];
		if (!editorViewStateMemento) {
			editorViewStateMemento = {};
			memento[EDITOR_VIEW_STATE_PREFERENCE_KEY] = editorViewStateMemento;
		}

		editorViewStateMemento[key] = editorViewState;
	}

	/**
	 * Clears the text editor view state under the given key.
	 */
	public clearTextEditorViewState(storageService: IStorageService, keys: string[]): void {
		const memento = this.getMemento(storageService, Scope.WORKSPACE);
		let editorViewStateMemento = memento[EDITOR_VIEW_STATE_PREFERENCE_KEY];
		if (editorViewStateMemento) {
			keys.forEach((key) => delete editorViewStateMemento[key]);
		}
	}

	/**
	 * Loads the text editor view state for the given key and returns it.
	 */
	public loadTextEditorViewState(storageService: IStorageService, key: string): IEditorViewState {
		const memento = this.getMemento(storageService, Scope.WORKSPACE);
		let editorViewStateMemento = memento[EDITOR_VIEW_STATE_PREFERENCE_KEY];
		if (editorViewStateMemento) {
			return editorViewStateMemento[key];
		}

		return null;
	}

	public dispose(): void {

		// Destroy Editor Control
		this.editorControl.destroy();

		super.dispose();
	}
}