mainThreadEditor.ts 12.4 KB
Newer Older
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';

7
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
8 9 10 11
import Event, { Emitter } from 'vs/base/common/event';
import { IEditor } from 'vs/platform/editor/common/editor';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
12
import { Range, IRange } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
13
import { Selection, ISelection } from 'vs/editor/common/core/selection';
14
import { SnippetController2 } from 'vs/editor/contrib/snippet/snippetController2';
J
Johannes Rieken 已提交
15
import { EndOfLine, TextEditorLineNumbersStyle } from 'vs/workbench/api/node/extHostTypes';
16
import { TextEditorCursorStyle, cursorStyleToString, RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';
17
import { ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
18
import { IResolvedTextEditorConfiguration, ISelectionChangeEvent, ITextEditorConfigurationUpdate, TextEditorRevealType, IApplyEditsOptions, IUndoStopOptions } from 'vs/workbench/api/node/extHost.protocol';
19
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
20
import { IModel, ISingleEditOperation, EndOfLineSequence, IIdentifiedSingleEditOperation, ITextModelUpdateOptions } from 'vs/editor/common/model/model';
21

J
Johannes Rieken 已提交
22
function configurationsEqual(a: IResolvedTextEditorConfiguration, b: IResolvedTextEditorConfiguration) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	if (a && !b || !a && b) {
		return false;
	}
	if (!a && !b) {
		return true;
	}
	return (
		a.tabSize === b.tabSize
		&& a.insertSpaces === b.insertSpaces
	);
}

export interface IFocusTracker {
	onGainedFocus(): void;
	onLostFocus(): void;
}

/**
 * Text Editor that is permanently bound to the same model.
 * It can be bound or not to a CodeEditor.
 */
export class MainThreadTextEditor {

	private _id: string;
47
	private _model: IModel;
48 49
	private _modelService: IModelService;
	private _modelListeners: IDisposable[];
50
	private _codeEditor: ICodeEditor;
51 52 53 54 55 56
	private _focusTracker: IFocusTracker;
	private _codeEditorListeners: IDisposable[];

	private _lastSelection: Selection[];
	private _configuration: IResolvedTextEditorConfiguration;

57
	private _onSelectionChanged: Emitter<ISelectionChangeEvent>;
58 59 60 61
	private _onConfigurationChanged: Emitter<IResolvedTextEditorConfiguration>;

	constructor(
		id: string,
62
		model: IModel,
63
		codeEditor: ICodeEditor,
J
Johannes Rieken 已提交
64
		focusTracker: IFocusTracker,
65 66 67 68 69 70 71 72 73
		modelService: IModelService
	) {
		this._id = id;
		this._model = model;
		this._codeEditor = null;
		this._focusTracker = focusTracker;
		this._modelService = modelService;
		this._codeEditorListeners = [];

74
		this._onSelectionChanged = new Emitter<ISelectionChangeEvent>();
75 76
		this._onConfigurationChanged = new Emitter<IResolvedTextEditorConfiguration>();

J
Johannes Rieken 已提交
77
		this._lastSelection = [new Selection(1, 1, 1, 1)];
78 79 80 81 82 83
		this._modelListeners = [];
		this._modelListeners.push(this._model.onDidChangeOptions((e) => {
			this._setConfiguration(this._readConfiguration(this._model, this._codeEditor));
		}));

		this.setCodeEditor(codeEditor);
84
		this._setConfiguration(this._readConfiguration(this._model, this._codeEditor));
85 86 87 88 89 90 91 92 93 94 95 96 97
	}

	public dispose(): void {
		this._model = null;
		this._modelListeners = dispose(this._modelListeners);
		this._codeEditor = null;
		this._codeEditorListeners = dispose(this._codeEditorListeners);
	}

	public getId(): string {
		return this._id;
	}

98
	public getModel(): IModel {
99 100 101
		return this._model;
	}

102
	public getCodeEditor(): ICodeEditor {
J
Johannes Rieken 已提交
103 104 105
		return this._codeEditor;
	}

106
	public hasCodeEditor(codeEditor: ICodeEditor): boolean {
107 108 109
		return (this._codeEditor === codeEditor);
	}

110
	public setCodeEditor(codeEditor: ICodeEditor): void {
111 112 113 114 115 116 117 118 119 120 121 122 123 124
		if (this.hasCodeEditor(codeEditor)) {
			// Nothing to do...
			return;
		}
		this._codeEditorListeners = dispose(this._codeEditorListeners);

		this._codeEditor = codeEditor;
		if (this._codeEditor) {

			// Catch early the case that this code editor gets a different model set and disassociate from this model
			this._codeEditorListeners.push(this._codeEditor.onDidChangeModel(() => {
				this.setCodeEditor(null);
			}));

125
			let forwardSelection = (event?: ICursorSelectionChangedEvent) => {
126
				this._lastSelection = this._codeEditor.getSelections();
127 128 129 130
				this._onSelectionChanged.fire({
					selections: this._lastSelection,
					source: event && event.source
				});
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
			};
			this._codeEditorListeners.push(this._codeEditor.onDidChangeCursorSelection(forwardSelection));
			if (!Selection.selectionsArrEqual(this._lastSelection, this._codeEditor.getSelections())) {
				forwardSelection();
			}
			this._codeEditorListeners.push(this._codeEditor.onDidFocusEditor(() => {
				this._focusTracker.onGainedFocus();
			}));
			this._codeEditorListeners.push(this._codeEditor.onDidBlurEditor(() => {
				this._focusTracker.onLostFocus();
			}));
			this._codeEditorListeners.push(this._codeEditor.onDidChangeConfiguration(() => {
				this._setConfiguration(this._readConfiguration(this._model, this._codeEditor));
			}));
			this._setConfiguration(this._readConfiguration(this._model, this._codeEditor));
		}
	}

	public isVisible(): boolean {
		return !!this._codeEditor;
	}

153
	public get onSelectionChanged(): Event<ISelectionChangeEvent> {
154 155 156 157 158 159 160 161 162 163 164 165 166 167
		return this._onSelectionChanged.event;
	}

	public get onConfigurationChanged(): Event<IResolvedTextEditorConfiguration> {
		return this._onConfigurationChanged.event;
	}

	public getSelections(): Selection[] {
		if (this._codeEditor) {
			return this._codeEditor.getSelections();
		}
		return this._lastSelection;
	}

A
Alex Dima 已提交
168
	public setSelections(selections: ISelection[]): void {
169 170 171 172 173 174 175 176 177 178 179
		if (this._codeEditor) {
			this._codeEditor.setSelections(selections);
			return;
		}
		this._lastSelection = selections.map(Selection.liftSelection);
	}

	public getConfiguration(): IResolvedTextEditorConfiguration {
		return this._configuration;
	}

J
Johannes Rieken 已提交
180
	private _setIndentConfiguration(newConfiguration: ITextEditorConfigurationUpdate): void {
181 182 183
		if (newConfiguration.tabSize === 'auto' || newConfiguration.insertSpaces === 'auto') {
			// one of the options was set to 'auto' => detect indentation

184
			let creationOpts = this._modelService.getCreationOptions(this._model.getLanguageIdentifier().language, this._model.uri);
185 186 187
			let insertSpaces = creationOpts.insertSpaces;
			let tabSize = creationOpts.tabSize;

188 189
			if (newConfiguration.insertSpaces !== 'auto' && typeof newConfiguration.insertSpaces !== 'undefined') {
				insertSpaces = newConfiguration.insertSpaces;
190
			}
191 192 193

			if (newConfiguration.tabSize !== 'auto' && typeof newConfiguration.tabSize !== 'undefined') {
				tabSize = newConfiguration.tabSize;
194 195 196 197 198 199
			}

			this._model.detectIndentation(insertSpaces, tabSize);
			return;
		}

200
		let newOpts: ITextModelUpdateOptions = {};
201
		if (typeof newConfiguration.insertSpaces !== 'undefined') {
202
			newOpts.insertSpaces = newConfiguration.insertSpaces;
203 204
		}
		if (typeof newConfiguration.tabSize !== 'undefined') {
205
			newOpts.tabSize = newConfiguration.tabSize;
206 207 208 209
		}
		this._model.updateOptions(newOpts);
	}

J
Johannes Rieken 已提交
210
	public setConfiguration(newConfiguration: ITextEditorConfigurationUpdate): void {
211 212
		this._setIndentConfiguration(newConfiguration);

213 214 215 216
		if (!this._codeEditor) {
			return;
		}

217
		if (newConfiguration.cursorStyle) {
218
			let newCursorStyle = cursorStyleToString(newConfiguration.cursorStyle);
219 220 221 222
			this._codeEditor.updateOptions({
				cursorStyle: newCursorStyle
			});
		}
223 224

		if (typeof newConfiguration.lineNumbers !== 'undefined') {
225
			let lineNumbers: 'on' | 'off' | 'relative';
226 227
			switch (newConfiguration.lineNumbers) {
				case TextEditorLineNumbersStyle.On:
228
					lineNumbers = 'on';
229 230 231 232 233
					break;
				case TextEditorLineNumbersStyle.Relative:
					lineNumbers = 'relative';
					break;
				default:
234
					lineNumbers = 'off';
235
			}
236
			this._codeEditor.updateOptions({
237
				lineNumbers: lineNumbers
238 239
			});
		}
240 241
	}

242
	public setDecorations(key: string, ranges: editorCommon.IDecorationOptions[]): void {
243 244 245 246
		if (!this._codeEditor) {
			return;
		}
		this._codeEditor.setDecorations(key, ranges);
247 248 249 250 251 252 253 254 255 256 257
	}

	public setDecorationsFast(key: string, _ranges: number[]): void {
		if (!this._codeEditor) {
			return;
		}
		let ranges: Range[] = [];
		for (let i = 0, len = Math.floor(_ranges.length / 4); i < len; i++) {
			ranges[i] = new Range(_ranges[4 * i], _ranges[4 * i + 1], _ranges[4 * i + 2], _ranges[4 * i + 3]);
		}
		this._codeEditor.setDecorationsFast(key, ranges);
258 259
	}

A
Alex Dima 已提交
260
	public revealRange(range: IRange, revealType: TextEditorRevealType): void {
261 262 263
		if (!this._codeEditor) {
			return;
		}
264 265
		switch (revealType) {
			case TextEditorRevealType.Default:
266
				this._codeEditor.revealRange(range, editorCommon.ScrollType.Smooth);
267 268
				break;
			case TextEditorRevealType.InCenter:
269
				this._codeEditor.revealRangeInCenter(range, editorCommon.ScrollType.Smooth);
270
				break;
271
			case TextEditorRevealType.InCenterIfOutsideViewport:
272
				this._codeEditor.revealRangeInCenterIfOutsideViewport(range, editorCommon.ScrollType.Smooth);
273 274
				break;
			case TextEditorRevealType.AtTop:
275
				this._codeEditor.revealRangeAtTop(range, editorCommon.ScrollType.Smooth);
276 277
				break;
			default:
278
				console.warn(`Unknown revealType: ${revealType}`);
279
				break;
280 281 282
		}
	}

283
	private _readConfiguration(model: IModel, codeEditor: ICodeEditor): IResolvedTextEditorConfiguration {
284 285 286 287
		if (model.isDisposed()) {
			// shutdown time
			return this._configuration;
		}
288
		let cursorStyle = this._configuration ? this._configuration.cursorStyle : TextEditorCursorStyle.Line;
289
		let lineNumbers: TextEditorLineNumbersStyle = this._configuration ? this._configuration.lineNumbers : TextEditorLineNumbersStyle.On;
290 291 292
		if (codeEditor) {
			let codeEditorOpts = codeEditor.getConfiguration();
			cursorStyle = codeEditorOpts.viewInfo.cursorStyle;
293

294 295 296 297 298 299 300 301 302 303
			switch (codeEditorOpts.viewInfo.renderLineNumbers) {
				case RenderLineNumbersType.Off:
					lineNumbers = TextEditorLineNumbersStyle.Off;
					break;
				case RenderLineNumbersType.Relative:
					lineNumbers = TextEditorLineNumbersStyle.Relative;
					break;
				default:
					lineNumbers = TextEditorLineNumbersStyle.On;
					break;
304
			}
305 306 307 308 309 310
		}

		let indent = model.getOptions();
		return {
			insertSpaces: indent.insertSpaces,
			tabSize: indent.tabSize,
311 312
			cursorStyle: cursorStyle,
			lineNumbers: lineNumbers
313 314 315
		};
	}

J
Johannes Rieken 已提交
316
	private _setConfiguration(newConfiguration: IResolvedTextEditorConfiguration): void {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		if (configurationsEqual(this._configuration, newConfiguration)) {
			return;
		}
		this._configuration = newConfiguration;
		this._onConfigurationChanged.fire(this._configuration);
	}

	public isFocused(): boolean {
		if (this._codeEditor) {
			return this._codeEditor.isFocused();
		}
		return false;
	}

	public matches(editor: IEditor): boolean {
		if (!editor) {
			return false;
		}
		return editor.getControl() === this._codeEditor;
	}

338
	public applyEdits(versionIdCheck: number, edits: ISingleEditOperation[], opts: IApplyEditsOptions): boolean {
339 340 341 342 343 344
		if (this._model.getVersionId() !== versionIdCheck) {
			// throw new Error('Model has changed in the meantime!');
			// model changed in the meantime
			return false;
		}

345 346 347 348
		if (!this._codeEditor) {
			// console.warn('applyEdits on invisible editor');
			return false;
		}
349

350
		if (opts.setEndOfLine === EndOfLine.CRLF) {
351
			this._model.setEOL(EndOfLineSequence.CRLF);
352
		} else if (opts.setEndOfLine === EndOfLine.LF) {
353
			this._model.setEOL(EndOfLineSequence.LF);
354 355
		}

356
		let transformedEdits = edits.map((edit): IIdentifiedSingleEditOperation => {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
			return {
				identifier: null,
				range: Range.lift(edit.range),
				text: edit.text,
				forceMoveMarkers: edit.forceMoveMarkers
			};
		});

		if (opts.undoStopBefore) {
			this._codeEditor.pushUndoStop();
		}
		this._codeEditor.executeEdits('MainThreadTextEditor', transformedEdits);
		if (opts.undoStopAfter) {
			this._codeEditor.pushUndoStop();
		}
		return true;
373
	}
374

A
Alex Dima 已提交
375
	insertSnippet(template: string, ranges: IRange[], opts: IUndoStopOptions) {
376

377 378 379 380
		if (!this._codeEditor) {
			return false;
		}

381
		const snippetController = SnippetController2.get(this._codeEditor);
382

383 384
		// // cancel previous snippet mode
		// snippetController.leaveSnippet();
385 386

		// set selection, focus editor
387
		const selections = ranges.map(r => new Selection(r.startLineNumber, r.startColumn, r.endLineNumber, r.endColumn));
388
		this._codeEditor.setSelections(selections);
389
		this._codeEditor.focus();
390

391
		// make modifications
392
		snippetController.insert(template, 0, 0, opts.undoStopBefore, opts.undoStopAfter);
393 394

		return true;
395
	}
396
}