editor.ts 25.5 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, ScrollType } from 'vs/editor/common/editorCommon';
14
import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, Position, Verbosity, IEditor as IBaseEditor, IRevertOptions } from 'vs/platform/editor/common/editor';
J
Johannes Rieken 已提交
15
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
16
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
17
import { Registry } from 'vs/platform/registry/common/platform';
A
Alex Dima 已提交
18
import { ITextModel } from 'vs/editor/common/model';
19
import { Schemas } from 'vs/base/common/network';
20 21

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

23 24 25 26 27 28
export enum ConfirmResult {
	SAVE,
	DONT_SAVE,
	CANCEL
}

29 30 31 32 33
/**
 * Text diff editor id.
 */
export const TEXT_DIFF_EDITOR_ID = 'workbench.editors.textDiffEditor';

34 35
export const PREFERENCES_EDITOR_ID = 'workbench.editor.preferencesEditor';

36 37 38 39 40
/**
 * Binary diff editor id.
 */
export const BINARY_DIFF_EDITOR_ID = 'workbench.editors.binaryResourceDiffEditor';

41
export interface IFileInputFactory {
42

43
	createFileInput(resource: URI, encoding: string, instantiationService: IInstantiationService): IFileEditorInput;
44 45

	isFileInput(obj: any): obj is IFileEditorInput;
46 47
}

48
export interface IEditorInputFactoryRegistry {
B
Benjamin Pasero 已提交
49 50

	/**
51
	 * Registers the file input factory to use for file inputs.
B
Benjamin Pasero 已提交
52
	 */
53
	registerFileInputFactory(factory: IFileInputFactory): void;
B
Benjamin Pasero 已提交
54 55

	/**
56
	 * Returns the file input factory to use for file inputs.
B
Benjamin Pasero 已提交
57
	 */
58
	getFileInputFactory(): IFileInputFactory;
B
Benjamin Pasero 已提交
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

	/**
	 * 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 已提交
94 95 96 97
/**
 * 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.
 */
98 99
export abstract class EditorInput implements IEditorInput {
	private _onDispose: Emitter<void>;
100
	protected _onDidChangeDirty: Emitter<void>;
B
Benjamin Pasero 已提交
101
	protected _onDidChangeLabel: Emitter<void>;
102

E
Erich Gamma 已提交
103 104
	private disposed: boolean;

105
	constructor() {
106
		this._onDidChangeDirty = new Emitter<void>();
B
Benjamin Pasero 已提交
107
		this._onDidChangeLabel = new Emitter<void>();
108 109
		this._onDispose = new Emitter<void>();

110 111 112
		this.disposed = false;
	}

113 114 115 116 117 118 119
	/**
	 * Fired when the dirty state of this input changes.
	 */
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

B
Benjamin Pasero 已提交
120 121 122 123 124 125 126
	/**
	 * Fired when the label this input changes.
	 */
	public get onDidChangeLabel(): Event<void> {
		return this._onDidChangeLabel.event;
	}

127 128 129 130 131 132 133
	/**
	 * Fired when the model gets disposed.
	 */
	public get onDispose(): Event<void> {
		return this._onDispose.event;
	}

B
Benjamin Pasero 已提交
134 135 136 137 138 139 140
	/**
	 * Returns the associated resource of this input if any.
	 */
	public getResource(): URI {
		return null;
	}

E
Erich Gamma 已提交
141 142 143 144 145 146 147 148 149 150 151 152
	/**
	 * 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.
	 */
153
	public getDescription(verbosity?: Verbosity): string {
E
Erich Gamma 已提交
154 155 156
		return null;
	}

157 158 159 160
	public getTitle(verbosity?: Verbosity): string {
		return this.getName();
	}

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

178 179 180 181 182
	/**
	 * Returns a descriptor suitable for telemetry events or null if none is available.
	 *
	 * Subclasses should extend if they can contribute.
	 */
B
Benjamin Pasero 已提交
183
	public getTelemetryDescriptor(): object {
K
kieferrm 已提交
184
		/* __GDPR__FRAGMENT__
K
kieferrm 已提交
185 186 187 188
			"EditorTelemetryDescriptor" : {
				"typeId" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
			}
		*/
189 190 191
		return { typeId: this.getTypeId() };
	}

E
Erich Gamma 已提交
192 193 194 195 196 197
	/**
	 * 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.
	 */
198
	public abstract resolve(refresh?: boolean): TPromise<IEditorModel>;
E
Erich Gamma 已提交
199

200 201 202 203 204 205 206 207 208 209
	/**
	 * 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.
	 */
210 211
	public confirmSave(): TPromise<ConfirmResult> {
		return TPromise.wrap(ConfirmResult.DONT_SAVE);
212 213 214 215 216 217 218 219 220 221 222 223
	}

	/**
	 * 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.
	 */
224
	public revert(options?: IRevertOptions): TPromise<boolean> {
225 226 227 228
		return TPromise.as(true);
	}

	/**
229
	 * Called when this input is no longer opened in any editor. Subclasses can free resources as needed.
230 231
	 */
	public close(): void {
232 233 234
		this.dispose();
	}

235 236 237 238 239 240 241
	/**
	 * Subclasses can set this to false if it does not make sense to split the editor input.
	 */
	public supportsSplitEditor(): boolean {
		return true;
	}

242 243 244 245 246
	/**
	 * Returns true if this input is identical to the otherInput.
	 */
	public matches(otherInput: any): boolean {
		return this === otherInput;
247 248
	}

E
Erich Gamma 已提交
249 250 251 252 253 254
	/**
	 * 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;
255
		this._onDispose.fire();
E
Erich Gamma 已提交
256

257
		this._onDidChangeDirty.dispose();
B
Benjamin Pasero 已提交
258
		this._onDidChangeLabel.dispose();
259
		this._onDispose.dispose();
E
Erich Gamma 已提交
260 261 262
	}

	/**
P
Pascal Borreli 已提交
263
	 * Returns whether this input was disposed or not.
E
Erich Gamma 已提交
264 265 266 267 268 269
	 */
	public isDisposed(): boolean {
		return this.disposed;
	}
}

270 271
export interface IEditorOpeningEvent {
	input: IEditorInput;
272
	options?: IEditorOptions;
273 274 275 276 277 278 279 280 281 282 283 284
	position: Position;

	/**
	 * Allows to prevent the opening of an editor by providing a callback
	 * that will be executed instead. By returning another editor promise
	 * it is possible to override the opening with another editor. It is ok
	 * to return a promise that resolves to NULL to prevent the opening
	 * altogether.
	 */
	prevent(callback: () => TPromise<IBaseEditor>): void;
}

285
export class EditorOpeningEvent implements IEditorOpeningEvent {
286 287
	private override: () => TPromise<IBaseEditor>;

288
	constructor(private _input: IEditorInput, private _options: IEditorOptions, private _position: Position) {
289 290 291
	}

	public get input(): IEditorInput {
292 293 294 295 296
		return this._input;
	}

	public get options(): IEditorOptions {
		return this._options;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	}

	public get position(): Position {
		return this._position;
	}

	public prevent(callback: () => TPromise<IBaseEditor>): void {
		this.override = callback;
	}

	public isPrevented(): () => TPromise<IBaseEditor> {
		return this.override;
	}
}

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
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.
 */
342
export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
343

344 345 346 347
	/**
	 * Sets the preferred encodingt to use for this input.
	 */
	setPreferredEncoding(encoding: string): void;
348 349 350 351 352

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

E
Erich Gamma 已提交
355
/**
S
Sandeep Somavarapu 已提交
356
 * Side by side editor inputs that have a master and details side.
E
Erich Gamma 已提交
357
 */
S
Sandeep Somavarapu 已提交
358
export class SideBySideEditorInput extends EditorInput {
E
Erich Gamma 已提交
359

M
Matt Bierner 已提交
360
	public static readonly ID: string = 'workbench.editorinputs.sidebysideEditorInput';
361

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

S
Sandeep Somavarapu 已提交
364
	constructor(private name: string, private description: string, private _details: EditorInput, private _master: EditorInput) {
S
Sandeep Somavarapu 已提交
365 366 367
		super();
		this._toUnbind = [];
		this.registerListeners();
E
Erich Gamma 已提交
368 369
	}

S
Sandeep Somavarapu 已提交
370 371
	get master(): EditorInput {
		return this._master;
E
Erich Gamma 已提交
372 373
	}

S
Sandeep Somavarapu 已提交
374 375
	get details(): EditorInput {
		return this._details;
E
Erich Gamma 已提交
376 377
	}

378
	public isDirty(): boolean {
S
Sandeep Somavarapu 已提交
379
		return this.master.isDirty();
380 381
	}

382
	public confirmSave(): TPromise<ConfirmResult> {
S
Sandeep Somavarapu 已提交
383
		return this.master.confirmSave();
384 385 386
	}

	public save(): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
387
		return this.master.save();
388 389 390
	}

	public revert(): TPromise<boolean> {
S
Sandeep Somavarapu 已提交
391
		return this.master.revert();
392
	}
393

B
Benjamin Pasero 已提交
394
	public getTelemetryDescriptor(): object {
S
Sandeep Somavarapu 已提交
395
		const descriptor = this.master.getTelemetryDescriptor();
396 397
		return objects.assign(descriptor, super.getTelemetryDescriptor());
	}
S
Sandeep Somavarapu 已提交
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

	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 {
430
		return SideBySideEditorInput.ID;
S
Sandeep Somavarapu 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	}

	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 已提交
466 467
}

468
export interface ITextEditorModel extends IEditorModel {
A
Alex Dima 已提交
469
	textEditorModel: ITextModel;
470 471
}

E
Erich Gamma 已提交
472 473 474 475 476
/**
 * 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 已提交
477
export class EditorModel extends Disposable implements IEditorModel {
478 479 480
	private _onDispose: Emitter<void>;

	constructor() {
S
Sandeep Somavarapu 已提交
481
		super();
482 483 484 485 486 487 488 489 490
		this._onDispose = new Emitter<void>();
	}

	/**
	 * Fired when the model gets disposed.
	 */
	public get onDispose(): Event<void> {
		return this._onDispose.event;
	}
E
Erich Gamma 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

	/**
	 * 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 {
510 511
		this._onDispose.fire();
		this._onDispose.dispose();
S
Sandeep Somavarapu 已提交
512
		super.dispose();
E
Erich Gamma 已提交
513 514 515 516 517 518 519 520 521 522 523
	}
}

/**
 * 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.
	 */
524
	public static create(settings: IEditorOptions): EditorOptions {
525
		const options = new EditorOptions();
526

E
Erich Gamma 已提交
527 528
		options.preserveFocus = settings.preserveFocus;
		options.forceOpen = settings.forceOpen;
529
		options.revealIfVisible = settings.revealIfVisible;
530
		options.revealIfOpened = settings.revealIfOpened;
B
Benjamin Pasero 已提交
531 532
		options.pinned = settings.pinned;
		options.index = settings.index;
533
		options.inactive = settings.inactive;
E
Erich Gamma 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

		return options;
	}

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

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

556 557 558 559 560
	/**
	 * 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 已提交
561
	/**
B
Benjamin Pasero 已提交
562 563
	 * 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 已提交
564 565
	 */
	public pinned: boolean;
B
Benjamin Pasero 已提交
566 567 568 569

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

	/**
	 * 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 已提交
577 578 579 580 581 582
}

/**
 * Base Text Editor Options.
 */
export class TextEditorOptions extends EditorOptions {
583 584 585 586
	private startLineNumber: number;
	private startColumn: number;
	private endLineNumber: number;
	private endColumn: number;
587

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

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

596 597
		return TextEditorOptions.create(input.options);
	}
E
Erich Gamma 已提交
598

599 600 601 602 603
	/**
	 * Helper to convert options bag to real class
	 */
	public static create(options: ITextEditorOptions = Object.create(null)): TextEditorOptions {
		const textEditorOptions = new TextEditorOptions();
E
Erich Gamma 已提交
604

605 606 607
		if (options.selection) {
			const selection = options.selection;
			textEditorOptions.selection(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn);
608
		}
E
Erich Gamma 已提交
609

610 611
		if (options.viewState) {
			textEditorOptions.editorViewState = options.viewState as IEditorViewState;
612
		}
613

614 615
		if (options.forceOpen) {
			textEditorOptions.forceOpen = true;
616
		}
617

618 619
		if (options.revealIfVisible) {
			textEditorOptions.revealIfVisible = true;
620
		}
621

622 623
		if (options.revealIfOpened) {
			textEditorOptions.revealIfOpened = true;
624
		}
625

626 627
		if (options.preserveFocus) {
			textEditorOptions.preserveFocus = true;
628
		}
629

630 631
		if (options.revealInCenterIfOutsideViewport) {
			textEditorOptions.revealInCenterIfOutsideViewport = true;
632
		}
I
isidor 已提交
633

634 635
		if (options.pinned) {
			textEditorOptions.pinned = true;
636
		}
B
Benjamin Pasero 已提交
637

638 639
		if (options.inactive) {
			textEditorOptions.inactive = true;
E
Erich Gamma 已提交
640 641
		}

642 643
		if (typeof options.index === 'number') {
			textEditorOptions.index = options.index;
E
Erich Gamma 已提交
644 645
		}

646
		return textEditorOptions;
E
Erich Gamma 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	}

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

	/**
669
	 * Create a TextEditorOptions inline to be used when the editor is opening.
E
Erich Gamma 已提交
670
	 */
671 672
	public static fromEditor(editor: IEditor, settings?: IEditorOptions): TextEditorOptions {
		const options = TextEditorOptions.create(settings);
673 674

		// View state
675
		options.editorViewState = editor.saveViewState();
676

677
		return options;
E
Erich Gamma 已提交
678 679 680 681 682 683 684
	}

	/**
	 * Apply the view state or selection to the given editor.
	 *
	 * @return if something was applied
	 */
685
	public apply(editor: IEditor, scrollType: ScrollType): boolean {
686 687

		// View state
688
		return this.applyViewState(editor, scrollType);
689 690
	}

691
	private applyViewState(editor: IEditor, scrollType: ScrollType): boolean {
E
Erich Gamma 已提交
692 693 694 695
		let gotApplied = false;

		// First try viewstate
		if (this.editorViewState) {
696
			editor.restoreViewState(this.editorViewState);
E
Erich Gamma 已提交
697 698 699 700 701 702 703 704
			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)) {
705
				const range = {
E
Erich Gamma 已提交
706 707 708 709 710
					startLineNumber: this.startLineNumber,
					startColumn: this.startColumn,
					endLineNumber: this.endLineNumber,
					endColumn: this.endColumn
				};
711
				editor.setSelection(range);
I
isidor 已提交
712
				if (this.revealInCenterIfOutsideViewport) {
713
					editor.revealRangeInCenterIfOutsideViewport(range, scrollType);
I
isidor 已提交
714
				} else {
715
					editor.revealRangeInCenter(range, scrollType);
I
isidor 已提交
716
				}
E
Erich Gamma 已提交
717 718 719 720
			}

			// Reveal
			else {
721
				const pos = {
E
Erich Gamma 已提交
722 723 724
					lineNumber: this.startLineNumber,
					column: this.startColumn
				};
725
				editor.setPosition(pos);
I
isidor 已提交
726
				if (this.revealInCenterIfOutsideViewport) {
727
					editor.revealPositionInCenterIfOutsideViewport(pos, scrollType);
I
isidor 已提交
728
				} else {
729
					editor.revealPositionInCenter(pos, scrollType);
I
isidor 已提交
730
				}
E
Erich Gamma 已提交
731 732 733 734 735 736 737 738 739
			}

			gotApplied = true;
		}

		return gotApplied;
	}
}

740 741 742 743 744 745
export interface IStacksModelChangeEvent {
	group: IEditorGroup;
	editor?: IEditorInput;
	structural?: boolean;
}

746 747
export interface IEditorStacksModel {

748
	onModelChanged: Event<IStacksModelChangeEvent>;
749

B
Benjamin Pasero 已提交
750 751
	onWillCloseEditor: Event<IEditorCloseEvent>;
	onEditorClosed: Event<IEditorCloseEvent>;
752 753 754

	groups: IEditorGroup[];
	activeGroup: IEditorGroup;
B
Benjamin Pasero 已提交
755
	isActive(group: IEditorGroup): boolean;
756 757 758 759 760 761

	getGroup(id: GroupIdentifier): IEditorGroup;

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

762 763
	next(jumpGroups: boolean, cycleAtEnd?: boolean): IEditorIdentifier;
	previous(jumpGroups: boolean, cycleAtStart?: boolean): IEditorIdentifier;
764
	last(): IEditorIdentifier;
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

	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 已提交
780
	getEditor(resource: URI): IEditorInput;
781 782
	indexOf(editor: IEditorInput): number;

783
	contains(editorOrResource: IEditorInput | URI): boolean;
784 785 786 787

	getEditors(mru?: boolean): IEditorInput[];
	isActive(editor: IEditorInput): boolean;
	isPreview(editor: IEditorInput): boolean;
788
	isPinned(index: number): boolean;
789 790 791 792 793
	isPinned(editor: IEditorInput): boolean;
}

export interface IEditorIdentifier {
	group: IEditorGroup;
I
isidor 已提交
794
	editor: IEditorInput;
795 796
}

B
Benjamin Pasero 已提交
797 798 799 800 801 802 803 804 805 806
/**
 * The editor commands context is used for editor commands (e.g. in the editor title)
 * and we must ensure that the context is serializable because it potentially travels
 * to the extension host!
 */
export interface IEditorCommandsContext {
	groupId: GroupIdentifier;
	editorIndex?: number;
}

B
Benjamin Pasero 已提交
807
export interface IEditorCloseEvent extends IEditorIdentifier {
808
	replaced: boolean;
809
	index: number;
810 811
}

812 813
export type GroupIdentifier = number;

814 815 816
export const EditorOpenPositioning = {
	LEFT: 'left',
	RIGHT: 'right',
817 818
	FIRST: 'first',
	LAST: 'last'
819 820
};

821 822
export const OPEN_POSITIONING_CONFIG = 'workbench.editor.openPositioning';

823 824
export interface IWorkbenchEditorConfiguration {
	workbench: {
825 826
		editor: {
			showTabs: boolean;
827
			tabCloseButton: 'left' | 'right' | 'off';
828
			tabSizing: 'fit' | 'shrink';
829
			showIcons: boolean;
830 831
			enablePreview: boolean;
			enablePreviewFromQuickOpen: boolean;
832
			closeOnFileDelete: boolean;
833
			openPositioning: 'left' | 'right' | 'first' | 'last';
834
			revealIfOpen: boolean;
835
			swipeToNavigate: boolean,
836
			labelFormat: 'default' | 'short' | 'medium' | 'long';
B
Benjamin Pasero 已提交
837 838
		},
		iconTheme: string;
839
	};
S
Sandeep Somavarapu 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
}

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;
859
	value?: number;
S
Sandeep Somavarapu 已提交
860 861
}

862
export const EditorCommands = {
863
	MoveActiveEditor: 'moveActiveEditor'
864 865 866 867
};

export interface IResourceOptions {
	supportSideBySide?: boolean;
868
	filter?: string | string[];
869 870 871 872 873 874 875 876 877 878 879 880
}

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

B
Benjamin Pasero 已提交
881
	const resource = editor.getResource();
882 883 884 885 886 887 888 889 890 891 892
	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)) {
893 894
		includeFiles = (options.filter.indexOf(Schemas.file) >= 0);
		includeUntitled = (options.filter.indexOf(Schemas.untitled) >= 0);
895
	} else {
896 897
		includeFiles = (options.filter === Schemas.file);
		includeUntitled = (options.filter === Schemas.untitled);
898 899
	}

900
	if (includeFiles && resource.scheme === Schemas.file) {
901 902 903
		return resource;
	}

904
	if (includeUntitled && resource.scheme === Schemas.untitled) {
905 906 907 908 909 910
		return resource;
	}

	return null;
}

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 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
class EditorInputFactoryRegistry implements IEditorInputFactoryRegistry {
	private instantiationService: IInstantiationService;
	private fileInputFactory: IFileInputFactory;
	private editorInputFactoryConstructors: { [editorInputId: string]: IConstructorSignature0<IEditorInputFactory> } = Object.create(null);
	private editorInputFactoryInstances: { [editorInputId: string]: IEditorInputFactory } = Object.create(null);

	constructor() {
	}

	public setInstantiationService(service: IInstantiationService): void {
		this.instantiationService = service;

		for (let key in this.editorInputFactoryConstructors) {
			const element = this.editorInputFactoryConstructors[key];
			this.createEditorInputFactory(key, element);
		}

		this.editorInputFactoryConstructors = {};
	}

	private createEditorInputFactory(editorInputId: string, ctor: IConstructorSignature0<IEditorInputFactory>): void {
		const instance = this.instantiationService.createInstance(ctor);
		this.editorInputFactoryInstances[editorInputId] = instance;
	}

	public registerFileInputFactory(factory: IFileInputFactory): void {
		this.fileInputFactory = factory;
	}

	public getFileInputFactory(): IFileInputFactory {
		return this.fileInputFactory;
	}

	public registerEditorInputFactory(editorInputId: string, ctor: IConstructorSignature0<IEditorInputFactory>): void {
		if (!this.instantiationService) {
			this.editorInputFactoryConstructors[editorInputId] = ctor;
		} else {
			this.createEditorInputFactory(editorInputId, ctor);
		}
	}

	public getEditorInputFactory(editorInputId: string): IEditorInputFactory {
		return this.editorInputFactoryInstances[editorInputId];
	}
}

export const Extensions = {
	EditorInputFactories: 'workbench.contributions.editor.inputFactories'
};

I
isidor 已提交
961
Registry.add(Extensions.EditorInputFactories, new EditorInputFactoryRegistry());