editor.ts 24.9 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';

J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
S
Sandeep Somavarapu 已提交
8
import Event, { Emitter, once } from 'vs/base/common/event';
9
import * as objects from 'vs/base/common/objects';
E
Erich Gamma 已提交
10 11
import types = require('vs/base/common/types');
import URI from 'vs/base/common/uri';
S
Sandeep Somavarapu 已提交
12
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
13
import { IEditor, IEditorViewState, IModel } from 'vs/editor/common/editorCommon';
14
import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, Position, Verbosity } from 'vs/platform/editor/common/editor';
15
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
J
Johannes Rieken 已提交
16
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
17 18 19
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';

export const TextCompareEditorVisible = new RawContextKey<boolean>('textCompareEditorVisible', false);
E
Erich Gamma 已提交
20

21 22 23 24 25 26
export enum ConfirmResult {
	SAVE,
	DONT_SAVE,
	CANCEL
}

B
Benjamin Pasero 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
export interface IEditorDescriptor {

	getId(): string;

	getName(): string;

	describes(obj: any): boolean;
}

export const Extensions = {
	Editors: 'workbench.contributions.editors'
};

40 41 42 43 44 45 46 47 48 49
/**
 * Text diff editor id.
 */
export const TEXT_DIFF_EDITOR_ID = 'workbench.editors.textDiffEditor';

/**
 * Binary diff editor id.
 */
export const BINARY_DIFF_EDITOR_ID = 'workbench.editors.binaryResourceDiffEditor';

50 51 52 53
export interface IFileInputFactory {
	createFileInput(resource: URI, encoding: string, instantiationService: IInstantiationService): IFileEditorInput;
}

B
Benjamin Pasero 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
export interface IEditorRegistry {

	/**
	 * Registers an editor to the platform for the given input type. The second parameter also supports an
	 * array of input classes to be passed in. If the more than one editor is registered for the same editor
	 * input, the input itself will be asked which editor it prefers if this method is provided. Otherwise
	 * the first editor in the list will be returned.
	 *
	 * @param editorInputDescriptor a constructor function that returns an instance of EditorInput for which the
	 * registered editor should be used for.
	 */
	registerEditor(descriptor: IEditorDescriptor, editorInputDescriptor: SyncDescriptor<EditorInput>): void;
	registerEditor(descriptor: IEditorDescriptor, editorInputDescriptor: SyncDescriptor<EditorInput>[]): void;

	/**
	 * Returns the editor descriptor for the given input or null if none.
	 */
	getEditor(input: EditorInput): IEditorDescriptor;

	/**
	 * Returns the editor descriptor for the given identifier or null if none.
	 */
	getEditorById(editorId: string): IEditorDescriptor;

	/**
	 * Returns an array of registered editors known to the platform.
	 */
	getEditors(): IEditorDescriptor[];

	/**
84
	 * Registers the file input factory to use for file inputs.
B
Benjamin Pasero 已提交
85
	 */
86
	registerFileInputFactory(factory: IFileInputFactory): void;
B
Benjamin Pasero 已提交
87 88

	/**
89
	 * Returns the file input factory to use for file inputs.
B
Benjamin Pasero 已提交
90
	 */
91
	getFileInputFactory(): IFileInputFactory;
B
Benjamin Pasero 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

	/**
	 * Registers a editor input factory for the given editor input to the registry. An editor input factory
	 * is capable of serializing and deserializing editor inputs from string data.
	 *
	 * @param editorInputId the identifier of the editor input
	 * @param factory the editor input factory for serialization/deserialization
	 */
	registerEditorInputFactory(editorInputId: string, ctor: IConstructorSignature0<IEditorInputFactory>): void;

	/**
	 * Returns the editor input factory for the given editor input.
	 *
	 * @param editorInputId the identifier of the editor input
	 */
	getEditorInputFactory(editorInputId: string): IEditorInputFactory;

	setInstantiationService(service: IInstantiationService): void;
}

export interface IEditorInputFactory {

	/**
	 * Returns a string representation of the provided editor input that contains enough information
	 * to deserialize back to the original editor input from the deserialize() method.
	 */
	serialize(editorInput: EditorInput): string;

	/**
	 * Returns an editor input from the provided serialized form of the editor input. This form matches
	 * the value returned from the serialize() method.
	 */
	deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput;
}

E
Erich Gamma 已提交
127 128 129 130
/**
 * Editor inputs are lightweight objects that can be passed to the workbench API to open inside the editor part.
 * Each editor input is mapped to an editor that is capable of opening it through the Platform facade.
 */
131 132
export abstract class EditorInput implements IEditorInput {
	private _onDispose: Emitter<void>;
133
	protected _onDidChangeDirty: Emitter<void>;
B
Benjamin Pasero 已提交
134
	protected _onDidChangeLabel: Emitter<void>;
135

E
Erich Gamma 已提交
136 137
	private disposed: boolean;

138
	constructor() {
139
		this._onDidChangeDirty = new Emitter<void>();
B
Benjamin Pasero 已提交
140
		this._onDidChangeLabel = new Emitter<void>();
141 142
		this._onDispose = new Emitter<void>();

143 144 145
		this.disposed = false;
	}

146 147 148 149 150 151 152
	/**
	 * Fired when the dirty state of this input changes.
	 */
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

B
Benjamin Pasero 已提交
153 154 155 156 157 158 159
	/**
	 * Fired when the label this input changes.
	 */
	public get onDidChangeLabel(): Event<void> {
		return this._onDidChangeLabel.event;
	}

160 161 162 163 164 165 166
	/**
	 * Fired when the model gets disposed.
	 */
	public get onDispose(): Event<void> {
		return this._onDispose.event;
	}

E
Erich Gamma 已提交
167 168 169 170 171 172 173 174 175 176 177 178
	/**
	 * Returns the name of this input that can be shown to the user. Examples include showing the name of the input
	 * above the editor area when the input is shown.
	 */
	public getName(): string {
		return null;
	}

	/**
	 * Returns the description of this input that can be shown to the user. Examples include showing the description of
	 * the input above the editor area to the side of the name of the input.
	 */
179
	public getDescription(): string {
E
Erich Gamma 已提交
180 181 182
		return null;
	}

183 184 185 186
	public getTitle(verbosity?: Verbosity): string {
		return this.getName();
	}

187 188 189 190 191
	/**
	 * Returns the unique type identifier of this input.
	 */
	public abstract getTypeId(): string;

E
Erich Gamma 已提交
192 193 194 195 196 197 198 199 200 201 202 203
	/**
	 * Returns the preferred editor for this input. A list of candidate editors is passed in that whee registered
	 * for the input. This allows subclasses to decide late which editor to use for the input on a case by case basis.
	 */
	public getPreferredEditorId(candidates: string[]): string {
		if (candidates && candidates.length > 0) {
			return candidates[0];
		}

		return null;
	}

204 205 206 207 208
	/**
	 * Returns a descriptor suitable for telemetry events or null if none is available.
	 *
	 * Subclasses should extend if they can contribute.
	 */
B
Benjamin Pasero 已提交
209
	public getTelemetryDescriptor(): object {
210 211 212
		return { typeId: this.getTypeId() };
	}

E
Erich Gamma 已提交
213 214 215 216 217 218
	/**
	 * Returns a type of EditorModel that represents the resolved input. Subclasses should
	 * override to provide a meaningful model. The optional second argument allows to specify
	 * if the EditorModel should be refreshed before returning it. Depending on the implementation
	 * this could mean to refresh the editor model contents with the version from disk.
	 */
219
	public abstract resolve(refresh?: boolean): TPromise<IEditorModel>;
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
	/**
	 * An editor that is dirty will be asked to be saved once it closes.
	 */
	public isDirty(): boolean {
		return false;
	}

	/**
	 * Subclasses should bring up a proper dialog for the user if the editor is dirty and return the result.
	 */
	public confirmSave(): ConfirmResult {
		return ConfirmResult.DONT_SAVE;
	}

	/**
	 * Saves the editor if it is dirty. Subclasses return a promise with a boolean indicating the success of the operation.
	 */
	public save(): TPromise<boolean> {
		return TPromise.as(true);
	}

	/**
	 * Reverts the editor if it is dirty. Subclasses return a promise with a boolean indicating the success of the operation.
	 */
	public revert(): TPromise<boolean> {
		return TPromise.as(true);
	}

	/**
250
	 * Called when this input is no longer opened in any editor. Subclasses can free resources as needed.
251 252
	 */
	public close(): void {
253 254 255
		this.dispose();
	}

256 257 258 259 260 261 262
	/**
	 * Subclasses can set this to false if it does not make sense to split the editor input.
	 */
	public supportsSplitEditor(): boolean {
		return true;
	}

263 264 265 266 267
	/**
	 * Returns true if this input is identical to the otherInput.
	 */
	public matches(otherInput: any): boolean {
		return this === otherInput;
268 269
	}

E
Erich Gamma 已提交
270 271 272 273 274 275
	/**
	 * Called when an editor input is no longer needed. Allows to free up any resources taken by
	 * resolving the editor input.
	 */
	public dispose(): void {
		this.disposed = true;
276
		this._onDispose.fire();
E
Erich Gamma 已提交
277

278
		this._onDidChangeDirty.dispose();
B
Benjamin Pasero 已提交
279
		this._onDidChangeLabel.dispose();
280
		this._onDispose.dispose();
E
Erich Gamma 已提交
281 282 283
	}

	/**
P
Pascal Borreli 已提交
284
	 * Returns whether this input was disposed or not.
E
Erich Gamma 已提交
285 286 287 288 289 290
	 */
	public isDisposed(): boolean {
		return this.disposed;
	}
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
export enum EncodingMode {

	/**
	 * Instructs the encoding support to encode the current input with the provided encoding
	 */
	Encode,

	/**
	 * Instructs the encoding support to decode the current input with the provided encoding
	 */
	Decode
}

export interface IEncodingSupport {

	/**
	 * Gets the encoding of the input if known.
	 */
	getEncoding(): string;

	/**
	 * Sets the encoding for the input for saving.
	 */
	setEncoding(encoding: string, mode: EncodingMode): void;
}

/**
 * This is a tagging interface to declare an editor input being capable of dealing with files. It is only used in the editor registry
 * to register this kind of input to the platform.
 */
321
export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
322

323 324 325 326 327
	/**
	 * Gets the absolute file resource URI this input is about.
	 */
	getResource(): URI;

328 329 330 331
	/**
	 * Sets the preferred encodingt to use for this input.
	 */
	setPreferredEncoding(encoding: string): void;
332 333 334 335 336

	/**
	 * Forces this file input to open as binary instead of text.
	 */
	setForceOpenAsBinary(): void;
337 338
}

E
Erich Gamma 已提交
339
/**
S
Sandeep Somavarapu 已提交
340
 * Side by side editor inputs that have a master and details side.
E
Erich Gamma 已提交
341
 */
S
Sandeep Somavarapu 已提交
342
export class SideBySideEditorInput extends EditorInput {
E
Erich Gamma 已提交
343

344 345
	public static ID: string = 'workbench.editorinputs.sidebysideEditorInput';

S
Sandeep Somavarapu 已提交
346
	private _toUnbind: IDisposable[];
E
Erich Gamma 已提交
347

S
Sandeep Somavarapu 已提交
348
	constructor(private name: string, private description: string, private _details: EditorInput, private _master: EditorInput) {
S
Sandeep Somavarapu 已提交
349 350 351
		super();
		this._toUnbind = [];
		this.registerListeners();
E
Erich Gamma 已提交
352 353
	}

S
Sandeep Somavarapu 已提交
354 355
	get master(): EditorInput {
		return this._master;
E
Erich Gamma 已提交
356 357
	}

S
Sandeep Somavarapu 已提交
358 359
	get details(): EditorInput {
		return this._details;
E
Erich Gamma 已提交
360 361
	}

362
	public isDirty(): boolean {
S
Sandeep Somavarapu 已提交
363
		return this.master.isDirty();
364 365 366
	}

	public confirmSave(): ConfirmResult {
S
Sandeep Somavarapu 已提交
367
		return this.master.confirmSave();
368 369 370
	}

	public save(): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
371
		return this.master.save();
372 373 374
	}

	public revert(): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
375
		return this.master.revert();
376
	}
377

B
Benjamin Pasero 已提交
378
	public getTelemetryDescriptor(): object {
S
Sandeep Somavarapu 已提交
379
		const descriptor = this.master.getTelemetryDescriptor();
380 381
		return objects.assign(descriptor, super.getTelemetryDescriptor());
	}
S
Sandeep Somavarapu 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

	private registerListeners(): void {

		// When the details or master input gets disposed, dispose this diff editor input
		const onceDetailsDisposed = once(this.details.onDispose);
		this._toUnbind.push(onceDetailsDisposed(() => {
			if (!this.isDisposed()) {
				this.dispose();
			}
		}));

		const onceMasterDisposed = once(this.master.onDispose);
		this._toUnbind.push(onceMasterDisposed(() => {
			if (!this.isDisposed()) {
				this.dispose();
			}
		}));

		// Reemit some events from the master side to the outside
		this._toUnbind.push(this.master.onDidChangeDirty(() => this._onDidChangeDirty.fire()));
		this._toUnbind.push(this.master.onDidChangeLabel(() => this._onDidChangeLabel.fire()));
	}

	public get toUnbind() {
		return this._toUnbind;
	}

	public resolve(refresh?: boolean): TPromise<EditorModel> {
		return TPromise.as(null);
	}

	getTypeId(): string {
414
		return SideBySideEditorInput.ID;
S
Sandeep Somavarapu 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	}

	public getName(): string {
		return this.name;
	}

	public getDescription(): string {
		return this.description;
	}

	public supportsSplitEditor(): boolean {
		return false;
	}

	public matches(otherInput: any): boolean {
		if (super.matches(otherInput) === true) {
			return true;
		}

		if (otherInput) {
			if (!(otherInput instanceof SideBySideEditorInput)) {
				return false;
			}

			const otherDiffInput = <SideBySideEditorInput>otherInput;
			return this.details.matches(otherDiffInput.details) && this.master.matches(otherDiffInput.master);
		}

		return false;
	}

	public dispose(): void {
		this._toUnbind = dispose(this._toUnbind);
		super.dispose();
	}
E
Erich Gamma 已提交
450 451
}

452 453 454 455
export interface ITextEditorModel extends IEditorModel {
	textEditorModel: IModel;
}

E
Erich Gamma 已提交
456 457 458 459 460
/**
 * The editor model is the heavyweight counterpart of editor input. Depending on the editor input, it
 * connects to the disk to retrieve content and may allow for saving it back or reverting it. Editor models
 * are typically cached for some while because they are expensive to construct.
 */
S
Sandeep Somavarapu 已提交
461
export class EditorModel extends Disposable implements IEditorModel {
462 463 464
	private _onDispose: Emitter<void>;

	constructor() {
S
Sandeep Somavarapu 已提交
465
		super();
466 467 468 469 470 471 472 473 474
		this._onDispose = new Emitter<void>();
	}

	/**
	 * Fired when the model gets disposed.
	 */
	public get onDispose(): Event<void> {
		return this._onDispose.event;
	}
E
Erich Gamma 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

	/**
	 * Causes this model to load returning a promise when loading is completed.
	 */
	public load(): TPromise<EditorModel> {
		return TPromise.as(this);
	}

	/**
	 * Returns whether this model was loaded or not.
	 */
	public isResolved(): boolean {
		return true;
	}

	/**
	 * Subclasses should implement to free resources that have been claimed through loading.
	 */
	public dispose(): void {
494 495
		this._onDispose.fire();
		this._onDispose.dispose();
S
Sandeep Somavarapu 已提交
496
		super.dispose();
E
Erich Gamma 已提交
497 498 499 500 501 502 503 504 505 506 507
	}
}

/**
 * The editor options is the base class of options that can be passed in when opening an editor.
 */
export class EditorOptions implements IEditorOptions {

	/**
	 * Helper to create EditorOptions inline.
	 */
508
	public static create(settings: IEditorOptions): EditorOptions {
509
		const options = new EditorOptions();
E
Erich Gamma 已提交
510 511
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
512
		options.revealIfVisible = settings.revealIfVisible;
513
		options.revealIfOpened = settings.revealIfOpened;
B
Benjamin Pasero 已提交
514 515
		options.pinned = settings.pinned;
		options.index = settings.index;
516
		options.inactive = settings.inactive;
E
Erich Gamma 已提交
517 518 519 520

		return options;
	}

B
Benjamin Pasero 已提交
521 522 523
	/**
	 * Inherit all options from other EditorOptions instance.
	 */
524 525 526 527 528
	public mixin(other: IEditorOptions): void {
		if (other) {
			this.preserveFocus = other.preserveFocus;
			this.forceOpen = other.forceOpen;
			this.revealIfVisible = other.revealIfVisible;
529
			this.revealIfOpened = other.revealIfOpened;
530 531 532 533
			this.pinned = other.pinned;
			this.index = other.index;
			this.inactive = other.inactive;
		}
B
Benjamin Pasero 已提交
534 535
	}

E
Erich Gamma 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548
	/**
	 * Tells the editor to not receive keyboard focus when the editor is being opened. By default,
	 * the editor will receive keyboard focus on open.
	 */
	public preserveFocus: boolean;

	/**
	 * Tells the editor to replace the editor input in the editor even if it is identical to the one
	 * already showing. By default, the editor will not replace the input if it is identical to the
	 * one showing.
	 */
	public forceOpen: boolean;

549
	/**
550
	 * Will reveal the editor if it is already opened and visible in any of the opened editor groups.
551
	 */
552
	public revealIfVisible: boolean;
553

554 555 556 557 558
	/**
	 * Will reveal the editor if it is already opened (even when not visible) in any of the opened editor groups.
	 */
	public revealIfOpened: boolean;

B
Benjamin Pasero 已提交
559
	/**
B
Benjamin Pasero 已提交
560 561
	 * An editor that is pinned remains in the editor stack even when another editor is being opened.
	 * An editor that is not pinned will always get replaced by another editor that is not pinned.
B
Benjamin Pasero 已提交
562 563
	 */
	public pinned: boolean;
B
Benjamin Pasero 已提交
564 565 566 567

	/**
	 * The index in the document stack where to insert the editor into when opening.
	 */
B
Benjamin Pasero 已提交
568
	public index: number;
569 570 571 572 573 574

	/**
	 * An active editor that is opened will show its contents directly. Set to true to open an editor
	 * in the background.
	 */
	public inactive: boolean;
E
Erich Gamma 已提交
575 576 577 578 579 580
}

/**
 * Base Text Editor Options.
 */
export class TextEditorOptions extends EditorOptions {
581 582 583 584
	protected startLineNumber: number;
	protected startColumn: number;
	protected endLineNumber: number;
	protected endColumn: number;
585

I
isidor 已提交
586
	private revealInCenterIfOutsideViewport: boolean;
E
Erich Gamma 已提交
587 588
	private editorViewState: IEditorViewState;

589
	public static from(input: IBaseResourceInput): TextEditorOptions {
590 591 592 593
		if (!input || !input.options) {
			return null;
		}

E
Erich Gamma 已提交
594
		let options: TextEditorOptions = null;
595 596 597 598 599 600 601 602 603 604
		if (
			input.options.selection ||
			input.options.viewState ||
			typeof input.options.forceOpen === 'boolean' ||
			typeof input.options.revealIfVisible === 'boolean' ||
			typeof input.options.revealIfOpened === 'boolean' ||
			typeof input.options.preserveFocus === 'boolean' ||
			typeof input.options.revealInCenterIfOutsideViewport === 'boolean' ||
			typeof input.options.pinned === 'boolean' ||
			typeof input.options.inactive === 'boolean' ||
605
			typeof input.options.index === 'number'
606
		) {
607
			options = new TextEditorOptions();
608
		}
E
Erich Gamma 已提交
609

610 611 612 613
		if (input.options.selection) {
			const selection = input.options.selection;
			options.selection(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
		}
E
Erich Gamma 已提交
614

615 616 617
		if (input.options.viewState) {
			options.editorViewState = input.options.viewState as IEditorViewState;
		}
E
Erich Gamma 已提交
618

619 620 621
		if (input.options.forceOpen) {
			options.forceOpen = true;
		}
622

623 624 625
		if (input.options.revealIfVisible) {
			options.revealIfVisible = true;
		}
626

627 628 629
		if (input.options.revealIfOpened) {
			options.revealIfOpened = true;
		}
630

631 632 633
		if (input.options.preserveFocus) {
			options.preserveFocus = true;
		}
634

635 636 637
		if (input.options.revealInCenterIfOutsideViewport) {
			options.revealInCenterIfOutsideViewport = true;
		}
638

639 640 641
		if (input.options.pinned) {
			options.pinned = true;
		}
I
isidor 已提交
642

643 644 645
		if (input.options.inactive) {
			options.inactive = true;
		}
B
Benjamin Pasero 已提交
646

647 648
		if (typeof input.options.index === 'number') {
			options.index = input.options.index;
E
Erich Gamma 已提交
649 650 651 652 653 654 655 656
		}

		return options;
	}

	/**
	 * Helper to create TextEditorOptions inline.
	 */
657
	public static create(settings: ITextEditorOptions = Object.create(null)): TextEditorOptions {
658
		const options = new TextEditorOptions();
659

E
Erich Gamma 已提交
660 661
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
662
		options.revealIfVisible = settings.revealIfVisible;
663
		options.revealIfOpened = settings.revealIfOpened;
664 665
		options.pinned = settings.pinned;
		options.index = settings.index;
666
		options.inactive = settings.inactive;
E
Erich Gamma 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

		if (settings.selection) {
			options.startLineNumber = settings.selection.startLineNumber;
			options.startColumn = settings.selection.startColumn;
			options.endLineNumber = settings.selection.endLineNumber || settings.selection.startLineNumber;
			options.endColumn = settings.selection.endColumn || settings.selection.startColumn;
		}

		return options;
	}

	/**
	 * Returns if this options object has objects defined for the editor.
	 */
	public hasOptionsDefined(): boolean {
		return !!this.editorViewState || (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn));
	}

	/**
	 * Tells the editor to set show the given selection when the editor is being opened.
	 */
	public selection(startLineNumber: number, startColumn: number, endLineNumber: number = startLineNumber, endColumn: number = startColumn): EditorOptions {
		this.startLineNumber = startLineNumber;
		this.startColumn = startColumn;
		this.endLineNumber = endLineNumber;
		this.endColumn = endColumn;

		return this;
	}

	/**
698
	 * Create a TextEditorOptions inline to be used when the editor is opening.
E
Erich Gamma 已提交
699
	 */
700 701
	public static fromEditor(editor: IEditor, settings?: IEditorOptions): TextEditorOptions {
		const options = TextEditorOptions.create(settings);
702 703

		// View state
704
		options.editorViewState = editor.saveViewState();
705

706
		return options;
E
Erich Gamma 已提交
707 708 709 710 711 712 713
	}

	/**
	 * Apply the view state or selection to the given editor.
	 *
	 * @return if something was applied
	 */
B
Benjamin Pasero 已提交
714
	public apply(editor: IEditor): boolean {
715 716

		// View state
B
Benjamin Pasero 已提交
717
		return this.applyViewState(editor);
718 719
	}

B
Benjamin Pasero 已提交
720
	private applyViewState(editor: IEditor): boolean {
E
Erich Gamma 已提交
721 722 723 724
		let gotApplied = false;

		// First try viewstate
		if (this.editorViewState) {
725
			editor.restoreViewState(this.editorViewState);
E
Erich Gamma 已提交
726 727 728 729 730 731 732 733
			gotApplied = true;
		}

		// Otherwise check for selection
		else if (!types.isUndefinedOrNull(this.startLineNumber) && !types.isUndefinedOrNull(this.startColumn)) {

			// Select
			if (!types.isUndefinedOrNull(this.endLineNumber) && !types.isUndefinedOrNull(this.endColumn)) {
734
				const range = {
E
Erich Gamma 已提交
735 736 737 738 739
					startLineNumber: this.startLineNumber,
					startColumn: this.startColumn,
					endLineNumber: this.endLineNumber,
					endColumn: this.endColumn
				};
740
				editor.setSelection(range);
I
isidor 已提交
741 742 743 744 745
				if (this.revealInCenterIfOutsideViewport) {
					editor.revealRangeInCenterIfOutsideViewport(range);
				} else {
					editor.revealRangeInCenter(range);
				}
E
Erich Gamma 已提交
746 747 748 749
			}

			// Reveal
			else {
750
				const pos = {
E
Erich Gamma 已提交
751 752 753
					lineNumber: this.startLineNumber,
					column: this.startColumn
				};
754
				editor.setPosition(pos);
I
isidor 已提交
755 756 757 758 759
				if (this.revealInCenterIfOutsideViewport) {
					editor.revealPositionInCenterIfOutsideViewport(pos);
				} else {
					editor.revealPositionInCenter(pos);
				}
E
Erich Gamma 已提交
760 761 762 763 764 765 766 767 768
			}

			gotApplied = true;
		}

		return gotApplied;
	}
}

769 770 771 772 773 774
export interface IStacksModelChangeEvent {
	group: IEditorGroup;
	editor?: IEditorInput;
	structural?: boolean;
}

775 776
export interface IEditorStacksModel {

777
	onModelChanged: Event<IStacksModelChangeEvent>;
778

B
Benjamin Pasero 已提交
779 780
	onWillCloseEditor: Event<IEditorCloseEvent>;
	onEditorClosed: Event<IEditorCloseEvent>;
781 782 783

	groups: IEditorGroup[];
	activeGroup: IEditorGroup;
B
Benjamin Pasero 已提交
784
	isActive(group: IEditorGroup): boolean;
785 786 787 788 789 790

	getGroup(id: GroupIdentifier): IEditorGroup;

	positionOfGroup(group: IEditorGroup): Position;
	groupAt(position: Position): IEditorGroup;

791 792
	next(jumpGroups: boolean, cycleAtEnd?: boolean): IEditorIdentifier;
	previous(jumpGroups: boolean, cycleAtStart?: boolean): IEditorIdentifier;
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

	isOpen(resource: URI): boolean;

	toString(): string;
}

export interface IEditorGroup {

	id: GroupIdentifier;
	label: string;
	count: number;
	activeEditor: IEditorInput;
	previewEditor: IEditorInput;

	getEditor(index: number): IEditorInput;
B
Benjamin Pasero 已提交
808
	getEditor(resource: URI): IEditorInput;
809 810
	indexOf(editor: IEditorInput): number;

811
	contains(editorOrResource: IEditorInput | URI): boolean;
812 813 814 815

	getEditors(mru?: boolean): IEditorInput[];
	isActive(editor: IEditorInput): boolean;
	isPreview(editor: IEditorInput): boolean;
816
	isPinned(index: number): boolean;
817 818 819 820 821 822 823 824
	isPinned(editor: IEditorInput): boolean;
}

export interface IEditorIdentifier {
	group: IEditorGroup;
	editor: IEditorInput;
}

B
Benjamin Pasero 已提交
825
export interface IEditorContext extends IEditorIdentifier {
826
	event?: any;
B
Benjamin Pasero 已提交
827 828
}

B
Benjamin Pasero 已提交
829
export interface IEditorCloseEvent extends IEditorIdentifier {
830
	pinned: boolean;
831
	index: number;
832 833
}

834 835
export type GroupIdentifier = number;

836 837 838
export const EditorOpenPositioning = {
	LEFT: 'left',
	RIGHT: 'right',
839 840
	FIRST: 'first',
	LAST: 'last'
841 842
};

843 844
export interface IWorkbenchEditorConfiguration {
	workbench: {
845 846
		editor: {
			showTabs: boolean;
847
			tabCloseButton: 'left' | 'right' | 'off';
848
			showIcons: boolean;
849 850
			enablePreview: boolean;
			enablePreviewFromQuickOpen: boolean;
851
			closeOnFileDelete: boolean;
852
			openPositioning: 'left' | 'right' | 'first' | 'last';
853
			revealIfOpen: boolean;
854
			swipeToNavigate: boolean
855
		}
856
	};
S
Sandeep Somavarapu 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
}

export const ActiveEditorMovePositioning = {
	FIRST: 'first',
	LAST: 'last',
	LEFT: 'left',
	RIGHT: 'right',
	CENTER: 'center',
	POSITION: 'position',
};

export const ActiveEditorMovePositioningBy = {
	TAB: 'tab',
	GROUP: 'group'
};

export interface ActiveEditorMoveArguments {
	to?: string;
	by?: string;
876
	value?: number;
S
Sandeep Somavarapu 已提交
877 878
}

879
export const EditorCommands = {
880
	MoveActiveEditor: 'moveActiveEditor'
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
};

export interface IResourceOptions {
	supportSideBySide?: boolean;
	filter?: 'file' | 'untitled' | ['file', 'untitled'] | ['untitled', 'file'];
}

export function hasResource(editor: IEditorInput, options?: IResourceOptions): boolean {
	return !!toResource(editor, options);
}

export function toResource(editor: IEditorInput, options?: IResourceOptions): URI {
	if (!editor) {
		return null;
	}

	// Check for side by side if we are asked to
	if (options && options.supportSideBySide && editor instanceof SideBySideEditorInput) {
		editor = editor.master;
	}

	const resource = doGetEditorResource(editor);
	if (!options || !options.filter) {
		return resource; // return early if no filter is specified
	}

	if (!resource) {
		return null;
	}

	let includeFiles: boolean;
	let includeUntitled: boolean;
	if (Array.isArray(options.filter)) {
		includeFiles = (options.filter.indexOf('file') >= 0);
		includeUntitled = (options.filter.indexOf('untitled') >= 0);
	} else {
		includeFiles = (options.filter === 'file');
		includeUntitled = (options.filter === 'untitled');
	}

	if (includeFiles && resource.scheme === 'file') {
		return resource;
	}

	if (includeUntitled && resource.scheme === 'untitled') {
		return resource;
	}

	return null;
}

// TODO@Ben every editor should have an associated resource
function doGetEditorResource(editor: IEditorInput): URI {
	if (editor instanceof EditorInput && typeof (<any>editor).getResource === 'function') {
		const candidate = (<any>editor).getResource();
		if (candidate instanceof URI) {
			return candidate;
		}
	}

	return null;
}