untitledEditorInput.ts 4.9 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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
/*---------------------------------------------------------------------------------------------
 *  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 URI from 'vs/base/common/uri';
import {IEditorModesRegistry, Extensions} from 'vs/editor/common/modes/modesRegistry';
import {isUnspecific, guessMimeTypes, MIME_TEXT, suggestFilename} from 'vs/base/common/mime';
import labels = require('vs/base/common/labels');
import paths = require('vs/base/common/paths');
import {EditorModel, EncodingMode, IInputStatus, EditorInput, IResourceEditorInput, IEncodingSupport} from 'vs/workbench/common/editor';
import {Registry} from 'vs/platform/platform';
import {UntitledEditorModel} from 'vs/workbench/browser/parts/editor/untitledEditorModel';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';

/**
 * An editor input to be used for untitled text buffers.
 */
export class UntitledEditorInput extends EditorInput implements IResourceEditorInput, IEncodingSupport {

	public static ID: string = 'workbench.editors.untitledEditorInput';
	public static SCHEMA: string = 'untitled';

	private resource: URI;
	private hasAssociatedFilePath: boolean;
	private modeId: string;
	private cachedModel: UntitledEditorModel;

	constructor(
		resource: URI,
		hasAssociatedFilePath: boolean,
		modeId: string,
		@IInstantiationService private instantiationService: IInstantiationService,
		@ILifecycleService private lifecycleService: ILifecycleService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
	) {
		super();

		this.resource = resource;
		this.hasAssociatedFilePath = hasAssociatedFilePath;
		this.modeId = modeId;
	}

	public getId(): string {
		return UntitledEditorInput.ID;
	}

	public getResource(): URI {
		return this.resource;
	}

	public getName(): string {
		return this.hasAssociatedFilePath ? paths.basename(this.resource.fsPath) : this.resource.fsPath;
	}

	public getDescription(): string {
		return this.hasAssociatedFilePath ? labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService) : null;
	}

	public isDirty(): boolean {
		return this.cachedModel && this.cachedModel.isDirty();
	}

	public getStatus(): IInputStatus {
		let isDirty = this.isDirty();
		if (isDirty) {
			return { state: 'dirty', decoration: '\u25cf' };
		}

		return null;
	}

	public suggestFileName(): string {
		if (!this.hasAssociatedFilePath) {
			let mime = this.getMime();
80
			if (mime && mime !== MIME_TEXT /* do not suggest when the mime type is simple plain text */) {
E
Erich Gamma 已提交
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
				return suggestFilename(mime, this.getName());
			}
		}

		return this.getName();
	}

	public getMime(): string {
		if (this.cachedModel) {
			let modesRegistry = <IEditorModesRegistry>Registry.as(Extensions.EditorModes);

			return modesRegistry.getMimeForMode(this.cachedModel.getModeId());
		}

		return null;
	}

	public getEncoding(): string {
		if (this.cachedModel) {
			return this.cachedModel.getEncoding();
		}

		return null;
	}

	public setEncoding(encoding: string, mode: EncodingMode /* ignored, we only have Encode */): void {
		if (this.cachedModel) {
			this.cachedModel.setEncoding(encoding);
		}
	}

	public resolve(refresh?: boolean): TPromise<EditorModel> {

		// Use Cached Model
		if (this.cachedModel) {
			return TPromise.as(this.cachedModel);
		}

		// Otherwise Create Model and load
		let model = this.createModel();
		return model.load().then((resolvedModel: UntitledEditorModel) => {
			this.cachedModel = resolvedModel;

			return this.cachedModel;
		});
	}

	private createModel(): UntitledEditorModel {
		let content = '';
		let mime = this.modeId;
		if (!mime && this.hasAssociatedFilePath) {
			let mimeFromPath = guessMimeTypes(this.resource.fsPath)[0];
			if (!isUnspecific(mimeFromPath)) {
				mime = mimeFromPath; // take most specific mime type if file path is associated and mime is specific
			}
		}
		return this.instantiationService.createInstance(UntitledEditorModel, content, mime || MIME_TEXT,
			this.resource, this.hasAssociatedFilePath);
	}

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

		if (otherInput instanceof UntitledEditorInput) {
			let otherUntitledEditorInput = <UntitledEditorInput>otherInput;

			// Otherwise compare by properties
			return otherUntitledEditorInput.resource.toString() === this.resource.toString();
		}

		return false;
	}

	public dispose(): void {
		super.dispose();

		if (this.cachedModel) {
			this.cachedModel.dispose();
			this.cachedModel = null;
		}
	}
}