editor.ts 23.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 {TPromise} from 'vs/base/common/winjs.base';
import {EventEmitter} from 'vs/base/common/eventEmitter';
9
import Event, {Emitter} from 'vs/base/common/event';
E
Erich Gamma 已提交
10 11
import types = require('vs/base/common/types');
import URI from 'vs/base/common/uri';
12
import {IEditor, ICommonCodeEditor, IEditorViewState, IEditorOptions as ICodeEditorOptions} from 'vs/editor/common/editorCommon';
13
import {IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IResourceInput, Position} from 'vs/platform/editor/common/editor';
14
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
15
import {Event as BaseEvent} from 'vs/base/common/events';
16
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
B
Benjamin Pasero 已提交
17 18
import {SyncDescriptor, AsyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
import {IInstantiationService, IConstructorSignature0} from 'vs/platform/instantiation/common/instantiation';
19
import {IModel} from 'vs/editor/common/editorCommon';
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 84 85 86 87 88 89 90 91 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
export interface IEditorDescriptor {

	getId(): string;

	getName(): string;

	describes(obj: any): boolean;
}

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

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

	/**
	 * Registers the default input to be used for files in the workbench.
	 *
	 * @param editorInputDescriptor a descriptor that resolves to an instance of EditorInput that
	 * should be used to handle file inputs.
	 */
	registerDefaultFileInput(editorInputDescriptor: AsyncDescriptor<IFileEditorInput>): void;

	/**
	 * Returns a descriptor of the default input to be used for files in the workbench.
	 *
	 * @return a descriptor that resolves to an instance of EditorInput that should be used to handle
	 * file inputs.
	 */
	getDefaultFileInput(): AsyncDescriptor<IFileEditorInput>;

	/**
	 * 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 已提交
119 120 121 122 123
/**
 * 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.
 */
export abstract class EditorInput extends EventEmitter implements IEditorInput {
124

125
	protected _onDidChangeDirty: Emitter<void>;
126
	
E
Erich Gamma 已提交
127 128
	private disposed: boolean;

129 130
	constructor() {
		super();
B
Benjamin Pasero 已提交
131

132
		this._onDidChangeDirty = new Emitter<void>();
133 134 135
		this.disposed = false;
	}

136 137 138 139 140 141 142
	/**
	 * Fired when the dirty state of this input changes.
	 */
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

E
Erich Gamma 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	/**
	 * 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.
	 *
	 * @param verbose controls if the description should be short or can contain additional details.
	 */
	public getDescription(verbose?: boolean): string {
		return null;
	}

161 162 163 164 165
	/**
	 * Returns the unique type identifier of this input.
	 */
	public abstract getTypeId(): string;

E
Erich Gamma 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	/**
	 * 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;
	}

	/**
	 * 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.
	 */
	public abstract resolve(refresh?: boolean): TPromise<EditorModel>;

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
	/**
	 * 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);
	}

	/**
215
	 * Called when this input is no longer opened in any editor. Subclasses can free resources as needed.
216 217
	 */
	public close(): void {
218 219 220
		this.dispose();
	}

221 222 223 224 225 226 227
	/**
	 * Subclasses can set this to false if it does not make sense to split the editor input.
	 */
	public supportsSplitEditor(): boolean {
		return true;
	}

228 229 230 231 232
	/**
	 * Returns true if this input is identical to the otherInput.
	 */
	public matches(otherInput: any): boolean {
		return this === otherInput;
233 234
	}

E
Erich Gamma 已提交
235 236 237 238 239
	/**
	 * Called when an editor input is no longer needed. Allows to free up any resources taken by
	 * resolving the editor input.
	 */
	public dispose(): void {
240
		this._onDidChangeDirty.dispose();
E
Erich Gamma 已提交
241 242 243 244 245 246 247
		this.disposed = true;
		this.emit('dispose');

		super.dispose();
	}

	/**
P
Pascal Borreli 已提交
248
	 * Returns whether this input was disposed or not.
E
Erich Gamma 已提交
249 250 251 252 253 254
	 */
	public isDisposed(): boolean {
		return this.disposed;
	}
}

B
Benjamin Pasero 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
export class EditorInputEvent extends BaseEvent {
	private _editorInput: IEditorInput;
	private prevented: boolean;

	constructor(editorInput: IEditorInput) {
		super(null);

		this._editorInput = editorInput;
	}

	public get editorInput(): IEditorInput {
		return this._editorInput;
	}

	public prevent(): void {
		this.prevented = true;
	}

	public isPrevented(): boolean {
		return this.prevented;
	}
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
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.
 */
308
export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
309 310 311 312 313 314 315 316 317 318 319

	/**
	 * Gets the mime type of the file this input is about.
	 */
	getMime(): string;

	/**
	 * Sets the mime type of the file this input is about.
	 */
	setMime(mime: string): void;

320 321 322 323 324
	/**
	 * Gets the absolute file resource URI this input is about.
	 */
	getResource(): URI;

325 326 327 328
	/**
	 * Sets the absolute file resource URI this input is about.
	 */
	setResource(resource: URI): void;
329 330 331 332 333

	/**
	 * Sets the preferred encodingt to use for this input.
	 */
	setPreferredEncoding(encoding: string): void;
334 335 336 337 338
}

/**
 * The base class of untitled editor inputs in the workbench.
 */
339
export abstract class UntitledEditorInput extends EditorInput implements IEncodingSupport {
340 341 342 343 344 345 346 347 348 349 350 351 352 353

	abstract getResource(): URI;

	abstract isDirty(): boolean;

	abstract suggestFileName(): string;

	abstract getMime(): string;

	abstract getEncoding(): string;

	abstract setEncoding(encoding: string, mode: EncodingMode): void;
}

E
Erich Gamma 已提交
354 355 356
/**
 * The base class of editor inputs that have an original and modified side.
 */
357
export abstract class BaseDiffEditorInput extends EditorInput {
E
Erich Gamma 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	private _originalInput: EditorInput;
	private _modifiedInput: EditorInput;

	constructor(originalInput: EditorInput, modifiedInput: EditorInput) {
		super();

		this._originalInput = originalInput;
		this._modifiedInput = modifiedInput;
	}

	public get originalInput(): EditorInput {
		return this._originalInput;
	}

	public get modifiedInput(): EditorInput {
		return this._modifiedInput;
	}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	public isDirty(): boolean {
		return this._modifiedInput.isDirty();
	}

	public confirmSave(): ConfirmResult {
		return this._modifiedInput.confirmSave();
	}

	public save(): TPromise<boolean> {
		return this._modifiedInput.save();
	}

	public revert(): TPromise<boolean> {
		return this._modifiedInput.revert();
	}
E
Erich Gamma 已提交
391 392
}

393 394 395 396
export interface ITextEditorModel extends IEditorModel {
	textEditorModel: IModel;
}

E
Erich Gamma 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/**
 * 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.
 */
export class EditorModel extends EventEmitter implements IEditorModel {

	/**
	 * 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 {
		this.emit('dispose');

		super.dispose();
	}
}

/**
 * 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.
	 */
436
	public static create(settings: IEditorOptions): EditorOptions {
E
Erich Gamma 已提交
437 438 439
		let options = new EditorOptions();
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
440
		options.revealIfVisible = settings.revealIfVisible;
B
Benjamin Pasero 已提交
441 442
		options.pinned = settings.pinned;
		options.index = settings.index;
443
		options.inactive = settings.inactive;
E
Erich Gamma 已提交
444 445 446 447

		return options;
	}

B
Benjamin Pasero 已提交
448 449 450 451 452 453
	/**
	 * Inherit all options from other EditorOptions instance.
	 */
	public mixin(other: EditorOptions): void {
		this.preserveFocus = other.preserveFocus;
		this.forceOpen = other.forceOpen;
454
		this.revealIfVisible = other.revealIfVisible;
B
Benjamin Pasero 已提交
455 456 457 458
		this.pinned = other.pinned;
		this.index = other.index;
	}

E
Erich Gamma 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471
	/**
	 * 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;

472
	/**
473
	 * Will reveal the editor if it is already opened and visible in any of the opened editor groups.
474
	 */
475
	public revealIfVisible: boolean;
476

B
Benjamin Pasero 已提交
477
	/**
B
Benjamin Pasero 已提交
478 479
	 * 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 已提交
480 481
	 */
	public pinned: boolean;
B
Benjamin Pasero 已提交
482 483 484 485

	/**
	 * The index in the document stack where to insert the editor into when opening.
	 */
B
Benjamin Pasero 已提交
486
	public index: number;
487 488 489 490 491 492

	/**
	 * 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 已提交
493 494 495 496 497 498
}

/**
 * Base Text Editor Options.
 */
export class TextEditorOptions extends EditorOptions {
499 500 501 502
	protected startLineNumber: number;
	protected startColumn: number;
	protected endLineNumber: number;
	protected endColumn: number;
503

E
Erich Gamma 已提交
504
	private editorViewState: IEditorViewState;
505
	private editorOptions: ICodeEditorOptions;
E
Erich Gamma 已提交
506

507
	public static from(input: IResourceInput): TextEditorOptions {
E
Erich Gamma 已提交
508
		let options: TextEditorOptions = null;
509
		if (input && input.options) {
510
			if (input.options.selection || input.options.forceOpen || input.options.revealIfVisible || input.options.preserveFocus || input.options.pinned || input.options.inactive || typeof input.options.index === 'number') {
E
Erich Gamma 已提交
511 512 513
				options = new TextEditorOptions();
			}

514 515
			if (input.options.selection) {
				let selection = input.options.selection;
E
Erich Gamma 已提交
516 517 518
				options.selection(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
			}

519
			if (input.options.forceOpen) {
E
Erich Gamma 已提交
520 521 522
				options.forceOpen = true;
			}

523 524
			if (input.options.revealIfVisible) {
				options.revealIfVisible = true;
525 526
			}

527
			if (input.options.preserveFocus) {
E
Erich Gamma 已提交
528 529
				options.preserveFocus = true;
			}
530 531 532 533 534

			if (input.options.pinned) {
				options.pinned = true;
			}

535 536 537 538
			if (input.options.inactive) {
				options.inactive = true;
			}

539 540 541
			if (typeof input.options.index === 'number') {
				options.index = input.options.index;
			}
E
Erich Gamma 已提交
542 543 544 545 546 547 548 549
		}

		return options;
	}

	/**
	 * Helper to create TextEditorOptions inline.
	 */
550
	public static create(settings: ITextEditorOptions): TextEditorOptions {
E
Erich Gamma 已提交
551 552 553
		let options = new TextEditorOptions();
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
554
		options.revealIfVisible = settings.revealIfVisible;
555 556
		options.pinned = settings.pinned;
		options.index = settings.index;
E
Erich Gamma 已提交
557 558 559 560 561 562 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

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

	/**
	 * Sets the view state to be used when the editor is opening.
	 */
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	public fromEditor(editor: IEditor): void {

		// View state
		this.editorViewState = editor.saveViewState();

		// Selected editor options
		const codeEditor = <ICommonCodeEditor>editor;
		if (typeof codeEditor.getConfiguration === 'function') {
			const config = codeEditor.getConfiguration();
			if (config && config.viewInfo && config.wrappingInfo) {
				this.editorOptions = Object.create(null);
				this.editorOptions.renderWhitespace = config.viewInfo.renderWhitespace;
				this.editorOptions.renderControlCharacters = config.viewInfo.renderControlCharacters;
				this.editorOptions.wrappingColumn = config.wrappingInfo.isViewportWrapping ? 0 : -1;
			}
		}
E
Erich Gamma 已提交
606 607 608 609 610 611 612
	}

	/**
	 * Apply the view state or selection to the given editor.
	 *
	 * @return if something was applied
	 */
613 614 615 616 617 618 619 620 621 622 623 624
	public apply(editor: IEditor): boolean {

		// Editor options
		if (this.editorOptions) {
			editor.updateOptions(this.editorOptions);
		}

		// View state
		return this.applyViewState(editor);
	}

	private applyViewState(editor: IEditor): boolean {
E
Erich Gamma 已提交
625 626 627 628
		let gotApplied = false;

		// First try viewstate
		if (this.editorViewState) {
629
			editor.restoreViewState(this.editorViewState);
E
Erich Gamma 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643
			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)) {
				let range = {
					startLineNumber: this.startLineNumber,
					startColumn: this.startColumn,
					endLineNumber: this.endLineNumber,
					endColumn: this.endColumn
				};
644 645
				editor.setSelection(range);
				editor.revealRangeInCenter(range);
E
Erich Gamma 已提交
646 647 648 649 650 651 652 653
			}

			// Reveal
			else {
				let pos = {
					lineNumber: this.startLineNumber,
					column: this.startColumn
				};
654 655
				editor.setPosition(pos);
				editor.revealPositionInCenter(pos);
E
Erich Gamma 已提交
656 657 658 659 660 661 662 663 664
			}

			gotApplied = true;
		}

		return gotApplied;
	}
}

665 666 667 668 669 670 671 672 673
export interface ITextDiffEditorOptions extends ITextEditorOptions {

	/**
	 * Whether to auto reveal the first change when the text editor is opened or not. By default
	 * the first change will not be revealed.
	 */
	autoRevealFirstChange: boolean;
}

E
Erich Gamma 已提交
674 675 676 677 678 679 680 681
/**
 * Base Text Diff Editor Options.
 */
export class TextDiffEditorOptions extends TextEditorOptions {

	/**
	 * Helper to create TextDiffEditorOptions inline.
	 */
682
	public static create(settings: ITextDiffEditorOptions): TextDiffEditorOptions {
E
Erich Gamma 已提交
683
		let options = new TextDiffEditorOptions();
684

E
Erich Gamma 已提交
685
		options.autoRevealFirstChange = settings.autoRevealFirstChange;
686

E
Erich Gamma 已提交
687 688
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
689
		options.revealIfVisible = settings.revealIfVisible;
690 691 692 693 694 695 696 697 698
		options.pinned = settings.pinned;
		options.index = settings.index;

		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;
		}
E
Erich Gamma 已提交
699 700 701 702 703

		return options;
	}

	/**
P
Pascal Borreli 已提交
704
	 * Whether to auto reveal the first change when the text editor is opened or not. By default
E
Erich Gamma 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717
	 * the first change will not be revealed.
	 */
	public autoRevealFirstChange: boolean;
}

/**
 * Given an input, tries to get the associated URI for it (either file or untitled scheme).
 */
export function getUntitledOrFileResource(input: IEditorInput, supportDiff?: boolean): URI {
	if (!input) {
		return null;
	}

718 719 720
	// Untitled
	if (input instanceof UntitledEditorInput) {
		return input.getResource();
E
Erich Gamma 已提交
721 722
	}

723
	// File
E
Erich Gamma 已提交
724
	let fileInput = asFileEditorInput(input, supportDiff);
725 726 727 728
	return fileInput && fileInput && fileInput.getResource();
}

export function getResource(input: IEditorInput): URI {
B
Benjamin Pasero 已提交
729
	if (input && typeof (<any>input).getResource === 'function') {
730 731 732 733 734 735
		let candidate = (<any>input).getResource();
		if (candidate instanceof URI) {
			return candidate;
		}
	}
	return getUntitledOrFileResource(input, true);
E
Erich Gamma 已提交
736 737
}

738 739 740
/**
 * Helper to return all opened editors with resources not belonging to the currently opened workspace.
 */
741
export function getOutOfWorkspaceEditorResources(editorGroupService: IEditorGroupService, contextService: IWorkspaceContextService): URI[] {
742 743
	const resources: URI[] = [];

744
	editorGroupService.getStacksModel().groups.forEach(group => {
745 746 747 748 749 750 751 752 753 754 755 756
		const editors = group.getEditors();
		editors.forEach(editor => {
			const fileInput = asFileEditorInput(editor, true);
			if (fileInput && !contextService.isInsideWorkspace(fileInput.getResource())) {
				resources.push(fileInput.getResource());
			}
		});
	});

	return resources;
}

E
Erich Gamma 已提交
757 758 759 760 761 762 763 764 765
/**
 * Returns the object as IFileEditorInput only if it matches the signature.
 */
export function asFileEditorInput(obj: any, supportDiff?: boolean): IFileEditorInput {
	if (!obj) {
		return null;
	}

	// Check for diff if we are asked to
B
Benjamin Pasero 已提交
766 767
	if (supportDiff && obj instanceof BaseDiffEditorInput) {
		obj = (<BaseDiffEditorInput>obj).modifiedInput;
E
Erich Gamma 已提交
768 769 770 771
	}

	let i = <IFileEditorInput>obj;

772
	return i instanceof EditorInput && types.areFunctions(i.setResource, i.setMime, i.setEncoding, i.getEncoding, i.getResource, i.getMime) ? i : null;
773 774
}

775 776 777 778 779 780
export interface IStacksModelChangeEvent {
	group: IEditorGroup;
	editor?: IEditorInput;
	structural?: boolean;
}

781 782
export interface IEditorStacksModel {

783
	onModelChanged: Event<IStacksModelChangeEvent>;
784
	onEditorClosed: Event<IGroupEvent>;
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

	groups: IEditorGroup[];
	activeGroup: IEditorGroup;
	isActive(IEditorGroup): boolean;

	getGroup(id: GroupIdentifier): IEditorGroup;

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

	next(): IEditorIdentifier;
	previous(): IEditorIdentifier;

	isOpen(editor: IEditorInput): boolean;
	isOpen(resource: URI): boolean;

	toString(): string;
}

export interface IEditorGroup {

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

	getEditor(index: number): IEditorInput;
	indexOf(editor: IEditorInput): number;

	contains(editor: IEditorInput): boolean;
	contains(resource: URI): boolean;

	getEditors(mru?: boolean): IEditorInput[];
	isActive(editor: IEditorInput): boolean;
	isPreview(editor: IEditorInput): boolean;
	isPinned(editor: IEditorInput): boolean;
}

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

B
Benjamin Pasero 已提交
829 830 831 832
export interface IEditorContext extends IEditorIdentifier {
	event: any;
}

833 834 835
export interface IGroupEvent {
	editor: IEditorInput;
	pinned: boolean;
836
	index: number;
837 838
}

839 840
export type GroupIdentifier = number;

841 842 843
export const EditorOpenPositioning = {
	LEFT: 'left',
	RIGHT: 'right',
844 845
	FIRST: 'first',
	LAST: 'last'
846 847
};

848 849
export interface IWorkbenchEditorConfiguration {
	workbench: {
850 851 852 853 854 855
		editor: {
			showTabs: boolean;
			enablePreview: boolean;
			enablePreviewFromQuickOpen: boolean;
			openPositioning: string;
		}
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 var EditorCommands = {
880
	MoveActiveEditor: 'moveActiveEditor'
B
Benjamin Pasero 已提交
881
};