extHostEditors.ts 23.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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 URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
8
import {IdGenerator} from 'vs/base/common/idGenerator';
E
Erich Gamma 已提交
9
import Event, {Emitter} from 'vs/base/common/event';
J
Joao Moreno 已提交
10
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
11 12
import {TPromise} from 'vs/base/common/winjs.base';
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
J
Johannes Rieken 已提交
13
import {ExtHostModelService, ExtHostDocumentData} from 'vs/workbench/api/node/extHostDocuments';
14
import {Selection, Range, Position, EditorOptions, EndOfLine} from './extHostTypes';
15
import {ISingleEditOperation, ISelection, IRange, IEditor, EditorType, ICommonCodeEditor, ICommonDiffEditor, IDecorationRenderOptions, IRangeWithMessage} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
16 17
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
18
import {Position as EditorPosition} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
19
import {IModelService} from 'vs/editor/common/services/modelService';
20
import {MainThreadEditorsTracker, TextEditorRevealType, MainThreadTextEditor, ITextEditorConfigurationUpdate, IResolvedTextEditorConfiguration} from 'vs/workbench/api/node/mainThreadEditors';
21
import * as TypeConverters from './extHostTypeConverters';
22
import {TextDocument, TextEditorSelectionChangeEvent, TextEditorOptionsChangeEvent, TextEditorOptions, TextEditorViewColumnChangeEvent, ViewColumn} from 'vscode';
23
import {EventType} from 'vs/workbench/common/events';
24
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
E
Erich Gamma 已提交
25 26
import {IEventService} from 'vs/platform/event/common/event';
import {equals as arrayEquals} from 'vs/base/common/arrays';
27
import {equals as objectEquals} from 'vs/base/common/objects';
E
Erich Gamma 已提交
28 29 30 31

export interface ITextEditorAddData {
	id: string;
	document: URI;
32
	options: IResolvedTextEditorConfiguration;
E
Erich Gamma 已提交
33
	selections: ISelection[];
34 35 36 37 38
	editorPosition: EditorPosition;
}

export interface ITextEditorPositionData {
	[id: string]: EditorPosition;
E
Erich Gamma 已提交
39 40
}

A
Alex Dima 已提交
41
@Remotable.ExtHostContext('ExtHostEditors')
42
export class ExtHostEditors {
E
Erich Gamma 已提交
43 44 45 46 47 48 49

	public onDidChangeTextEditorSelection: Event<TextEditorSelectionChangeEvent>;
	private _onDidChangeTextEditorSelection: Emitter<TextEditorSelectionChangeEvent>;

	public onDidChangeTextEditorOptions: Event<TextEditorOptionsChangeEvent>;
	private _onDidChangeTextEditorOptions: Emitter<TextEditorOptionsChangeEvent>;

50 51 52
	public onDidChangeTextEditorViewColumn: Event<TextEditorViewColumnChangeEvent>;
	private _onDidChangeTextEditorViewColumn: Emitter<TextEditorViewColumnChangeEvent>;

53
	private _editors: { [id: string]: ExtHostTextEditor };
E
Erich Gamma 已提交
54 55
	private _proxy: MainThreadEditors;
	private _onDidChangeActiveTextEditor: Emitter<vscode.TextEditor>;
56
	private _modelService: ExtHostModelService;
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64 65 66 67 68
	private _activeEditorId: string;
	private _visibleEditorIds: string[];

	constructor(
		@IThreadService threadService: IThreadService
	) {
		this._onDidChangeTextEditorSelection = new Emitter<TextEditorSelectionChangeEvent>();
		this.onDidChangeTextEditorSelection = this._onDidChangeTextEditorSelection.event;

		this._onDidChangeTextEditorOptions = new Emitter<TextEditorOptionsChangeEvent>();
		this.onDidChangeTextEditorOptions = this._onDidChangeTextEditorOptions.event;

69 70 71
		this._onDidChangeTextEditorViewColumn = new Emitter<TextEditorViewColumnChangeEvent>();
		this.onDidChangeTextEditorViewColumn = this._onDidChangeTextEditorViewColumn.event;

72
		this._modelService = threadService.getRemotable(ExtHostModelService);
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80
		this._proxy = threadService.getRemotable(MainThreadEditors);
		this._onDidChangeActiveTextEditor = new Emitter<vscode.TextEditor>();
		this._editors = Object.create(null);

		this._visibleEditorIds = [];
	}

	getActiveTextEditor(): vscode.TextEditor {
81
		return this._editors[this._activeEditorId];
E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91
	}

	getVisibleTextEditors(): vscode.TextEditor[] {
		return this._visibleEditorIds.map(id => this._editors[id]);
	}

	get onDidChangeActiveTextEditor(): Event<vscode.TextEditor> {
		return this._onDidChangeActiveTextEditor && this._onDidChangeActiveTextEditor.event;
	}

92 93
	showTextDocument(document: TextDocument, column: ViewColumn, preserveFocus: boolean): TPromise<vscode.TextEditor> {
		return this._proxy._tryShowTextDocument(<URI> document.uri, TypeConverters.fromViewColumn(column), preserveFocus).then(id => {
E
Erich Gamma 已提交
94 95 96 97
			let editor = this._editors[id];
			if (editor) {
				return editor;
			} else {
98
				throw new Error(`Failed to show text document ${document.uri.toString()}, should show in editor #${id}`);
E
Erich Gamma 已提交
99 100 101 102 103 104 105 106 107 108
			}
		});
	}

	createTextEditorDecorationType(options: vscode.DecorationRenderOptions): vscode.TextEditorDecorationType {
		return new TextEditorDecorationType(this._proxy, options);
	}

	// --- called from main thread

J
Johannes Rieken 已提交
109
	_acceptTextEditorAdd(data: ITextEditorAddData): void {
110
		let document = this._modelService.getDocumentData(data.document);
111
		let newEditor = new ExtHostTextEditor(this._proxy, data.id, document, data.selections.map(TypeConverters.toSelection), data.options, TypeConverters.toViewColumn(data.editorPosition));
E
Erich Gamma 已提交
112 113 114
		this._editors[data.id] = newEditor;
	}

115
	_acceptOptionsChanged(id: string, opts: IResolvedTextEditorConfiguration): void {
E
Erich Gamma 已提交
116 117 118 119 120 121 122 123
		let editor = this._editors[id];
		editor._acceptOptions(opts);
		this._onDidChangeTextEditorOptions.fire({
			textEditor: editor,
			options: opts
		});
	}

J
Johannes Rieken 已提交
124
	_acceptSelectionsChanged(id: string, _selections: ISelection[]): void {
E
Erich Gamma 已提交
125 126 127 128 129 130 131 132 133
		let selections = _selections.map(TypeConverters.toSelection);
		let editor = this._editors[id];
		editor._acceptSelections(selections);
		this._onDidChangeTextEditorSelection.fire({
			textEditor: editor,
			selections: selections
		});
	}

J
Johannes Rieken 已提交
134
	_acceptActiveEditorAndVisibleEditors(id: string, visibleIds: string[]): void {
E
Erich Gamma 已提交
135 136 137 138 139 140 141 142 143 144
		this._visibleEditorIds = visibleIds;

		if (this._activeEditorId === id) {
			// nothing to do
			return;
		}
		this._activeEditorId = id;
		this._onDidChangeActiveTextEditor.fire(this.getActiveTextEditor());
	}

145 146
	_acceptEditorPositionData(data: ITextEditorPositionData): void {
		for (let id in data) {
147 148 149 150 151
			let textEditor = this._editors[id];
			let viewColumn = TypeConverters.toViewColumn(data[id]);
			if (textEditor.viewColumn !== viewColumn) {
				textEditor._acceptViewColumn(viewColumn);
				this._onDidChangeTextEditorViewColumn.fire({ textEditor, viewColumn });
152 153 154 155
			}
		}
	}

J
Johannes Rieken 已提交
156
	_acceptTextEditorRemove(id: string): void {
E
Erich Gamma 已提交
157
		// make sure the removed editor is not visible
B
Benjamin Pasero 已提交
158
		let newVisibleEditors = this._visibleEditorIds.filter(visibleEditorId => visibleEditorId !== id);
E
Erich Gamma 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

		if (this._activeEditorId === id) {
			// removing the current active editor
			this._acceptActiveEditorAndVisibleEditors(undefined, newVisibleEditors);
		} else {
			this._acceptActiveEditorAndVisibleEditors(this._activeEditorId, newVisibleEditors);
		}

		let editor = this._editors[id];
		editor.dispose();
		delete this._editors[id];
	}
}

class TextEditorDecorationType implements vscode.TextEditorDecorationType {

J
Johannes Rieken 已提交
175
	private static _Keys = new IdGenerator('TextEditorDecorationType');
E
Erich Gamma 已提交
176 177 178 179

	private _proxy: MainThreadEditors;
	public key: string;

J
Johannes Rieken 已提交
180
	constructor(proxy: MainThreadEditors, options: vscode.DecorationRenderOptions) {
J
Johannes Rieken 已提交
181
		this.key = TextEditorDecorationType._Keys.nextId();
E
Erich Gamma 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		this._proxy = proxy;
		this._proxy._registerTextEditorDecorationType(this.key, <any>options);
	}

	public dispose(): void {
		this._proxy._removeTextEditorDecorationType(this.key);
	}
}

export interface ITextEditOperation {
	range: Range;
	text: string;
	forceMoveMarkers: boolean;
}

export interface IEditData {
	documentVersionId: number;
	edits: ITextEditOperation[];
200
	setEndOfLine: EndOfLine;
E
Erich Gamma 已提交
201 202 203 204 205 206
}

export class TextEditorEdit {

	private _documentVersionId: number;
	private _collectedEdits: ITextEditOperation[];
207
	private _setEndOfLine: EndOfLine;
E
Erich Gamma 已提交
208 209 210 211

	constructor(document: vscode.TextDocument) {
		this._documentVersionId = document.version;
		this._collectedEdits = [];
212
		this._setEndOfLine = 0;
E
Erich Gamma 已提交
213 214 215 216 217
	}

	finalize(): IEditData {
		return {
			documentVersionId: this._documentVersionId,
218 219
			edits: this._collectedEdits,
			setEndOfLine: this._setEndOfLine
E
Erich Gamma 已提交
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 259 260 261 262 263 264 265 266 267
		};
	}

	replace(location: Position | Range | Selection, value: string): void {
		let range: Range = null;

		if (location instanceof Position) {
			range = new Range(location, location);
		} else if (location instanceof Range) {
			range = location;
		} else if (location instanceof Selection) {
			range = new Range(location.start, location.end);
		} else {
			throw new Error('Unrecognized location');
		}

		this._collectedEdits.push({
			range: range,
			text: value,
			forceMoveMarkers: false
		});
	}

	insert(location: Position, value: string): void {
		this._collectedEdits.push({
			range: new Range(location, location),
			text: value,
			forceMoveMarkers: true
		});
	}

	delete(location: Range | Selection): void {
		let range: Range = null;

		if (location instanceof Range) {
			range = location;
		} else if (location instanceof Selection) {
			range = new Range(location.start, location.end);
		} else {
			throw new Error('Unrecognized location');
		}

		this._collectedEdits.push({
			range: range,
			text: null,
			forceMoveMarkers: true
		});
	}
268 269 270 271 272 273 274 275

	setEndOfLine(endOfLine:EndOfLine): void {
		if (endOfLine !== EndOfLine.LF && endOfLine !== EndOfLine.CRLF) {
			throw illegalArg('endOfLine');
		}

		this._setEndOfLine = endOfLine;
	}
E
Erich Gamma 已提交
276 277 278 279 280
}

function readonly(name: string, alt?: string) {
	let message = `The property '${name}' is readonly.`;
	if (alt) {
B
Benjamin Pasero 已提交
281
		message += ` Use '${alt}' instead.`;
E
Erich Gamma 已提交
282 283 284 285 286 287 288 289
	}
	return new Error(message);
}

function illegalArg(name: string) {
	return new Error(`illgeal argument '${name}'`);
}

J
Johannes Rieken 已提交
290
function deprecated(name: string, message: string = 'Refer to the documentation for further details.') {
E
Erich Gamma 已提交
291 292 293 294 295
	return (target: Object, key: string, descriptor: TypedPropertyDescriptor<any>) => {
		const originalMethod = descriptor.value;
		descriptor.value = function(...args: any[]) {
			console.warn(`[Deprecation Warning] method '${name}' is deprecated and should no longer be used. ${message}`);
			return originalMethod.apply(this, args);
B
Benjamin Pasero 已提交
296
		};
E
Erich Gamma 已提交
297 298

		return descriptor;
B
Benjamin Pasero 已提交
299
	};
E
Erich Gamma 已提交
300 301
}

302
class ExtHostTextEditor implements vscode.TextEditor {
E
Erich Gamma 已提交
303 304 305 306

	private _proxy: MainThreadEditors;
	private _id: string;

307
	private _documentData: ExtHostDocumentData;
E
Erich Gamma 已提交
308 309
	private _selections: Selection[];
	private _options: TextEditorOptions;
310
	private _viewColumn: vscode.ViewColumn;
E
Erich Gamma 已提交
311

312
	constructor(proxy: MainThreadEditors, id: string, document: ExtHostDocumentData, selections: Selection[], options: EditorOptions, viewColumn: vscode.ViewColumn) {
E
Erich Gamma 已提交
313 314
		this._proxy = proxy;
		this._id = id;
315
		this._documentData = document;
E
Erich Gamma 已提交
316 317
		this._selections = selections;
		this._options = options;
318
		this._viewColumn = viewColumn;
E
Erich Gamma 已提交
319 320 321
	}

	dispose() {
322
		this._documentData = null;
E
Erich Gamma 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335
	}

	@deprecated('TextEditor.show') show(column: vscode.ViewColumn) {
		this._proxy._tryShowEditor(this._id, TypeConverters.fromViewColumn(column));
	}

	@deprecated('TextEditor.hide') hide() {
		this._proxy._tryHideEditor(this._id);
	}

	// ---- the document

	get document(): vscode.TextDocument {
336
		return this._documentData.document;
E
Erich Gamma 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	}

	set document(value) {
		throw readonly('document');
	}

	// ---- options

	get options(): TextEditorOptions {
		return this._options;
	}

	set options(value: TextEditorOptions) {
		this._options = value;
		this._runOnProxy(() => {
			return this._proxy._trySetOptions(this._id, this._options);
		}, true);
	}

	_acceptOptions(options: EditorOptions): void {
B
Benjamin Pasero 已提交
357
		this._options = options;
E
Erich Gamma 已提交
358 359
	}

360 361 362 363 364 365 366 367 368 369 370 371 372 373
	// ---- view column

	get viewColumn(): vscode.ViewColumn {
		return this._viewColumn;
	}

	set viewColumn(value) {
		throw readonly('viewColumn');
	}

	_acceptViewColumn(value: vscode.ViewColumn) {
		this._viewColumn = value;
	}

E
Erich Gamma 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	// ---- selections

	get selection(): Selection {
		return this._selections && this._selections[0];
	}

	set selection(value: Selection) {
		if (!(value instanceof Selection)) {
			throw illegalArg('selection');
		}
		this._selections = [value];
		this._trySetSelection(true);
	}

	get selections(): Selection[] {
		return this._selections;
	}

	set selections(value: Selection[]) {
		if (!Array.isArray(value) || value.some(a => !(a instanceof Selection))) {
			throw illegalArg('selections');
		}
		this._selections = value;
		this._trySetSelection(true);
	}

J
Johannes Rieken 已提交
400
	setDecorations(decorationType: vscode.TextEditorDecorationType, ranges: Range[] | vscode.DecorationOptions[]): void {
E
Erich Gamma 已提交
401 402 403 404 405 406 407 408 409 410
		this._runOnProxy(
			() => this._proxy._trySetDecorations(
				this._id,
				decorationType.key,
				TypeConverters.fromRangeOrRangeWithMessage(ranges)
			),
			true
		);
	}

J
Johannes Rieken 已提交
411
	revealRange(range: Range, revealType: vscode.TextEditorRevealType): void {
E
Erich Gamma 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		this._runOnProxy(
			() => this._proxy._tryRevealRange(
				this._id,
				TypeConverters.fromRange(range),
				(<TextEditorRevealType><any>revealType) || TextEditorRevealType.Default
			),
			true
		);
	}

	private _trySetSelection(silent: boolean): TPromise<vscode.TextEditor> {
		let selection = this._selections.map(TypeConverters.fromSelection);
		return this._runOnProxy(() => this._proxy._trySetSelections(this._id, selection), silent);
	}

J
Johannes Rieken 已提交
427
	_acceptSelections(selections: Selection[]): void {
E
Erich Gamma 已提交
428 429 430 431 432
		this._selections = selections;
	}

	// ---- editing

J
Johannes Rieken 已提交
433
	edit(callback: (edit: TextEditorEdit) => void): Thenable<boolean> {
434
		let edit = new TextEditorEdit(this._documentData.document);
E
Erich Gamma 已提交
435 436 437 438
		callback(edit);
		return this._applyEdit(edit);
	}

J
Johannes Rieken 已提交
439 440
	_applyEdit(editBuilder: TextEditorEdit): TPromise<boolean> {
		let editData = editBuilder.finalize();
E
Erich Gamma 已提交
441 442

		// prepare data for serialization
B
Benjamin Pasero 已提交
443
		let edits: ISingleEditOperation[] = editData.edits.map((edit) => {
E
Erich Gamma 已提交
444 445 446 447 448 449 450
			return {
				range: TypeConverters.fromRange(edit.range),
				text: edit.text,
				forceMoveMarkers: edit.forceMoveMarkers
			};
		});

451
		return this._proxy._tryApplyEdits(this._id, editData.documentVersionId, edits, editData.setEndOfLine);
E
Erich Gamma 已提交
452 453 454 455
	}

	// ---- util

J
Johannes Rieken 已提交
456
	private _runOnProxy(callback: () => TPromise<any>, silent: boolean): TPromise<ExtHostTextEditor> {
E
Erich Gamma 已提交
457 458 459 460 461 462 463 464 465 466 467 468
		return callback().then(() => this, err => {
			if (!silent) {
				return TPromise.wrapError(silent);
			}
			console.warn(err);
		});
	}
}

@Remotable.MainContext('MainThreadEditors')
export class MainThreadEditors {

469
	private _proxy: ExtHostEditors;
E
Erich Gamma 已提交
470
	private _workbenchEditorService: IWorkbenchEditorService;
471
	private _telemetryService: ITelemetryService;
E
Erich Gamma 已提交
472 473
	private _editorTracker: MainThreadEditorsTracker;
	private _toDispose: IDisposable[];
J
Johannes Rieken 已提交
474 475
	private _textEditorsListenersMap: { [editorId: string]: IDisposable[]; };
	private _textEditorsMap: { [editorId: string]: MainThreadTextEditor; };
E
Erich Gamma 已提交
476 477
	private _activeTextEditor: string;
	private _visibleEditors: string[];
478
	private _editorPositionData: ITextEditorPositionData;
E
Erich Gamma 已提交
479 480 481 482

	constructor(
		@IThreadService threadService: IThreadService,
		@IWorkbenchEditorService workbenchEditorService: IWorkbenchEditorService,
483
		@ITelemetryService telemetryService: ITelemetryService,
J
Johannes Rieken 已提交
484 485 486
		@ICodeEditorService editorService: ICodeEditorService,
		@IEventService eventService: IEventService,
		@IModelService modelService: IModelService
E
Erich Gamma 已提交
487
	) {
488
		this._proxy = threadService.getRemotable(ExtHostEditors);
E
Erich Gamma 已提交
489
		this._workbenchEditorService = workbenchEditorService;
490
		this._telemetryService = telemetryService;
E
Erich Gamma 已提交
491 492 493 494 495
		this._toDispose = [];
		this._textEditorsListenersMap = Object.create(null);
		this._textEditorsMap = Object.create(null);
		this._activeTextEditor = null;
		this._visibleEditors = [];
496
		this._editorPositionData = null;
E
Erich Gamma 已提交
497 498 499 500 501 502 503 504 505 506

		this._editorTracker = new MainThreadEditorsTracker(editorService, modelService);
		this._toDispose.push(this._editorTracker);

		this._toDispose.push(this._editorTracker.onTextEditorAdd((textEditor) => this._onTextEditorAdd(textEditor)));
		this._toDispose.push(this._editorTracker.onTextEditorRemove((textEditor) => this._onTextEditorRemove(textEditor)));

		this._toDispose.push(this._editorTracker.onDidUpdateTextEditors(() => this._updateActiveAndVisibleTextEditors()));
		this._toDispose.push(this._editorTracker.onChangedFocusedTextEditor((focusedTextEditorId) => this._updateActiveAndVisibleTextEditors()));
		this._toDispose.push(eventService.addListener2(EventType.EDITOR_INPUT_CHANGED, () => this._updateActiveAndVisibleTextEditors()));
507
		this._toDispose.push(eventService.addListener2(EventType.EDITOR_POSITION_CHANGED, () => this._updateActiveAndVisibleTextEditors()));
E
Erich Gamma 已提交
508 509 510 511
	}

	public dispose(): void {
		Object.keys(this._textEditorsListenersMap).forEach((editorId) => {
J
Joao Moreno 已提交
512
			dispose(this._textEditorsListenersMap[editorId]);
E
Erich Gamma 已提交
513 514
		});
		this._textEditorsListenersMap = Object.create(null);
J
Joao Moreno 已提交
515
		this._toDispose = dispose(this._toDispose);
E
Erich Gamma 已提交
516 517
	}

J
Johannes Rieken 已提交
518
	private _onTextEditorAdd(textEditor: MainThreadTextEditor): void {
E
Erich Gamma 已提交
519 520 521 522 523 524 525 526 527 528
		let id = textEditor.getId();
		let toDispose: IDisposable[] = [];
		toDispose.push(textEditor.onConfigurationChanged((opts) => {
			this._proxy._acceptOptionsChanged(id, opts);
		}));
		toDispose.push(textEditor.onSelectionChanged((selection) => {
			this._proxy._acceptSelectionsChanged(id, selection);
		}));
		this._proxy._acceptTextEditorAdd({
			id: id,
529
			document: textEditor.getModel().uri,
E
Erich Gamma 已提交
530
			options: textEditor.getConfiguration(),
531 532
			selections: textEditor.getSelections(),
			editorPosition: this._findEditorPosition(textEditor)
E
Erich Gamma 已提交
533 534 535 536 537 538
		});

		this._textEditorsListenersMap[id] = toDispose;
		this._textEditorsMap[id] = textEditor;
	}

J
Johannes Rieken 已提交
539
	private _onTextEditorRemove(textEditor: MainThreadTextEditor): void {
E
Erich Gamma 已提交
540
		let id = textEditor.getId();
J
Joao Moreno 已提交
541
		dispose(this._textEditorsListenersMap[id]);
E
Erich Gamma 已提交
542 543 544 545 546 547
		delete this._textEditorsListenersMap[id];
		delete this._textEditorsMap[id];
		this._proxy._acceptTextEditorRemove(id);
	}

	private _updateActiveAndVisibleTextEditors(): void {
548 549

		// active and visible editors
E
Erich Gamma 已提交
550 551
		let visibleEditors = this._editorTracker.getVisibleTextEditorIds();
		let activeEditor = this._findActiveTextEditorId();
552 553 554 555 556
		if (activeEditor !== this._activeTextEditor || !arrayEquals(this._visibleEditors, visibleEditors, (a, b) => a === b)) {
			this._activeTextEditor = activeEditor;
			this._visibleEditors = visibleEditors;
			this._proxy._acceptActiveEditorAndVisibleEditors(this._activeTextEditor, this._visibleEditors);
		}
E
Erich Gamma 已提交
557

558 559 560 561 562
		// editor columns
		let editorPositionData = this._getTextEditorPositionData();
		if (!objectEquals(this._editorPositionData, editorPositionData)) {
			this._editorPositionData = editorPositionData;
			this._proxy._acceptEditorPositionData(this._editorPositionData);
E
Erich Gamma 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
		}
	}

	private _findActiveTextEditorId(): string {
		let focusedTextEditorId = this._editorTracker.getFocusedTextEditorId();
		if (focusedTextEditorId) {
			return focusedTextEditorId;
		}

		let activeEditor = this._workbenchEditorService.getActiveEditor();
		if (!activeEditor) {
			return null;
		}

		let editor = <IEditor>activeEditor.getControl();
		// Substitute for (editor instanceof ICodeEditor)
		if (!editor || typeof editor.getEditorType !== 'function') {
			// Not a text editor...
			return null;
		}

		if (editor.getEditorType() === EditorType.ICodeEditor) {
			return this._editorTracker.findTextEditorIdFor(<ICommonCodeEditor>editor);
		}

		// Must be a diff editor => use the modified side
		return this._editorTracker.findTextEditorIdFor((<ICommonDiffEditor>editor).getModifiedEditor());
	}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	private _findEditorPosition(editor: MainThreadTextEditor): EditorPosition {
		for (let workbenchEditor of this._workbenchEditorService.getVisibleEditors()) {
			if (editor.matches(workbenchEditor)) {
				return workbenchEditor.position;
			}
		}
	}

	private _getTextEditorPositionData(): ITextEditorPositionData {
		let result: ITextEditorPositionData = Object.create(null);
		for (let workbenchEditor of this._workbenchEditorService.getVisibleEditors()) {
			let editor = <IEditor>workbenchEditor.getControl();
			// Substitute for (editor instanceof ICodeEditor)
			if (!editor || typeof editor.getEditorType !== 'function') {
				// Not a text editor...
				continue;
			}
			if (editor.getEditorType() === EditorType.ICodeEditor) {
				let id = this._editorTracker.findTextEditorIdFor(<ICommonCodeEditor>editor);
				if (id) {
					result[id] = workbenchEditor.position;
				}
			}
		}
		return result;
	}

A
Alex Dima 已提交
619
	// --- from extension host process
E
Erich Gamma 已提交
620

621
	_tryShowTextDocument(resource: URI, position: EditorPosition, preserveFocus: boolean): TPromise<string> {
E
Erich Gamma 已提交
622

J
Johannes Rieken 已提交
623 624 625 626
		const input = {
			resource,
			options: { preserveFocus }
		};
E
Erich Gamma 已提交
627

J
Johannes Rieken 已提交
628
		return this._workbenchEditorService.openEditor(input, position).then(editor => {
E
Erich Gamma 已提交
629

630 631 632 633
			if (!editor) {
				return;
			}

E
Erich Gamma 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
			return new TPromise<void>(c => {
				// not very nice but the way it is: changes to the editor state aren't
				// send to the ext host as they happen but stuff is delayed a little. in
				// order to provide the real editor on #openTextEditor we need to sync on
				// that update
				let subscription: IDisposable;
				let handle: number;
				function contd() {
					subscription.dispose();
					clearTimeout(handle);
					c(undefined);
				}
				subscription = this._editorTracker.onDidUpdateTextEditors(() => {
					contd();
				});
				handle = setTimeout(() => {
					contd();
651
				}, 1000);
E
Erich Gamma 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665

			}).then(() => {
				// find the editor we have just opened and return the
				// id we have assigned to it.
				for (let id in this._textEditorsMap) {
					if (this._textEditorsMap[id].matches(editor)) {
						return id;
					}
				}
			});
		});
	}

	_tryShowEditor(id: string, position: EditorPosition): TPromise<void> {
666 667 668
		// check how often this is used
		this._telemetryService.publicLog('api.deprecated', { function: 'TextEditor.show' });

E
Erich Gamma 已提交
669 670 671 672
		let mainThreadEditor = this._textEditorsMap[id];
		if (mainThreadEditor) {
			let model = mainThreadEditor.getModel();
			return this._workbenchEditorService.openEditor({
673
				resource: model.uri,
E
Erich Gamma 已提交
674
				options: { preserveFocus: false }
J
Johannes Rieken 已提交
675
			}, position).then(() => { return; });
E
Erich Gamma 已提交
676 677 678 679
		}
	}

	_tryHideEditor(id: string): TPromise<void> {
680 681 682
		// check how often this is used
		this._telemetryService.publicLog('api.deprecated', { function: 'TextEditor.hide' });

E
Erich Gamma 已提交
683 684 685 686 687
		let mainThreadEditor = this._textEditorsMap[id];
		if (mainThreadEditor) {
			let editors = this._workbenchEditorService.getVisibleEditors();
			for (let editor of editors) {
				if (mainThreadEditor.matches(editor)) {
J
Johannes Rieken 已提交
688
					return this._workbenchEditorService.closeEditor(editor).then(() => { return; });
E
Erich Gamma 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701
				}
			}
		}
	}

	_trySetSelections(id: string, selections: ISelection[]): TPromise<any> {
		if (!this._textEditorsMap[id]) {
			return TPromise.wrapError('TextEditor disposed');
		}
		this._textEditorsMap[id].setSelections(selections);
		return TPromise.as(null);
	}

J
Johannes Rieken 已提交
702
	_trySetDecorations(id: string, key: string, ranges: IRangeWithMessage[]): TPromise<any> {
E
Erich Gamma 已提交
703 704 705 706 707 708 709
		if (!this._textEditorsMap[id]) {
			return TPromise.wrapError('TextEditor disposed');
		}
		this._textEditorsMap[id].setDecorations(key, ranges);
		return TPromise.as(null);
	}

J
Johannes Rieken 已提交
710
	_tryRevealRange(id: string, range: IRange, revealType: TextEditorRevealType): TPromise<any> {
E
Erich Gamma 已提交
711 712 713 714 715 716
		if (!this._textEditorsMap[id]) {
			return TPromise.wrapError('TextEditor disposed');
		}
		this._textEditorsMap[id].revealRange(range, revealType);
	}

717
	_trySetOptions(id: string, options: ITextEditorConfigurationUpdate): TPromise<any> {
E
Erich Gamma 已提交
718 719 720 721 722 723 724
		if (!this._textEditorsMap[id]) {
			return TPromise.wrapError('TextEditor disposed');
		}
		this._textEditorsMap[id].setConfiguration(options);
		return TPromise.as(null);
	}

725
	_tryApplyEdits(id: string, modelVersionId: number, edits: ISingleEditOperation[], setEndOfLine:EndOfLine): TPromise<boolean> {
E
Erich Gamma 已提交
726 727 728
		if (!this._textEditorsMap[id]) {
			return TPromise.wrapError('TextEditor disposed');
		}
729
		return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits, setEndOfLine));
E
Erich Gamma 已提交
730 731
	}

J
Johannes Rieken 已提交
732
	_registerTextEditorDecorationType(key: string, options: IDecorationRenderOptions): void {
E
Erich Gamma 已提交
733 734 735
		this._editorTracker.registerTextEditorDecorationType(key, options);
	}

J
Johannes Rieken 已提交
736
	_removeTextEditorDecorationType(key: string): void {
E
Erich Gamma 已提交
737 738 739
		this._editorTracker.removeTextEditorDecorationType(key);
	}
}