提交 c5e3c8f1 编写于 作者: B Benjamin Pasero

Untitled: picked encoding is not restored after restart (fixes #28364)

上级 11c9c5bd
......@@ -90,6 +90,11 @@ export interface IUntitledResourceInput extends IBaseResourceInput {
* Optional contents of the untitled resource.
*/
contents?: string;
/**
* Optional encoding of the untitled resource.
*/
encoding?: string;
}
export interface IResourceDiffInput extends IBaseResourceInput {
......
......@@ -93,6 +93,7 @@ interface ISerializedUntitledEditorInput {
resource: string;
resourceJSON: any;
modeId: string;
encoding: string;
}
// Register Editor Input Factory
......@@ -118,7 +119,8 @@ class UntitledEditorInputFactory implements IEditorInputFactory {
const serialized: ISerializedUntitledEditorInput = {
resource: resource.toString(), // Keep for backwards compatibility
resourceJSON: resource.toJSON(),
modeId: untitledEditorInput.getModeId()
modeId: untitledEditorInput.getModeId(),
encoding: untitledEditorInput.getEncoding()
};
return JSON.stringify(serialized);
......@@ -130,8 +132,9 @@ class UntitledEditorInputFactory implements IEditorInputFactory {
const resource = !!deserialized.resourceJSON ? URI.revive(deserialized.resourceJSON) : URI.parse(deserialized.resource);
const filePath = resource.scheme === 'file' ? resource.fsPath : void 0;
const language = deserialized.modeId;
const encoding = deserialized.encoding;
return accessor.get(IWorkbenchEditorService).createInput({ resource, filePath, language }) as UntitledEditorInput;
return accessor.get(IWorkbenchEditorService).createInput({ resource, filePath, language, encoding }) as UntitledEditorInput;
});
}
}
......
......@@ -27,10 +27,7 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
public static ID: string = 'workbench.editors.untitledEditorInput';
private resource: URI;
private _hasAssociatedFilePath: boolean;
private initialValue: string;
private modeId: string;
private cachedModel: UntitledEditorModel;
private modelResolve: TPromise<UntitledEditorModel>;
......@@ -40,10 +37,11 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
private toUnbind: IDisposable[];
constructor(
resource: URI,
private resource: URI,
hasAssociatedFilePath: boolean,
modeId: string,
initialValue: string,
private modeId: string,
private initialValue: string,
private preferredEncoding: string,
@IInstantiationService private instantiationService: IInstantiationService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@ITextFileService private textFileService: ITextFileService,
......@@ -51,11 +49,9 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
) {
super();
this.resource = resource;
this.initialValue = initialValue;
this._hasAssociatedFilePath = hasAssociatedFilePath;
this.modeId = modeId;
this.toUnbind = [];
this._onDidModelChangeContent = new Emitter<void>();
this._onDidModelChangeEncoding = new Emitter<void>();
}
......@@ -146,10 +142,12 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
return this.cachedModel.getEncoding();
}
return null;
return this.preferredEncoding;
}
public setEncoding(encoding: string, mode: EncodingMode /* ignored, we only have Encode */): void {
this.preferredEncoding = encoding;
if (this.cachedModel) {
this.cachedModel.setEncoding(encoding);
}
......@@ -170,7 +168,7 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
}
private createModel(): UntitledEditorModel {
const model = this.instantiationService.createInstance(UntitledEditorModel, this.modeId, this.resource, this.hasAssociatedFilePath, this.initialValue);
const model = this.instantiationService.createInstance(UntitledEditorModel, this.modeId, this.resource, this.hasAssociatedFilePath, this.initialValue, this.preferredEncoding);
// re-emit some events from the model
this.toUnbind.push(model.onDidChangeContent(() => this._onDidModelChangeContent.fire()));
......
......@@ -38,16 +38,13 @@ export class UntitledEditorModel extends BaseTextEditorModel implements IEncodin
private contentChangeEventScheduler: RunOnceScheduler;
private configuredEncoding: string;
private preferredEncoding: string;
private hasAssociatedFilePath: boolean;
private initialValue: string;
constructor(
private modeId: string,
private resource: URI,
hasAssociatedFilePath: boolean,
initialValue: string,
private hasAssociatedFilePath: boolean,
private initialValue: string,
private preferredEncoding: string,
@IModeService modeService: IModeService,
@IModelService modelService: IModelService,
@IBackupFileService private backupFileService: IBackupFileService,
......@@ -56,8 +53,6 @@ export class UntitledEditorModel extends BaseTextEditorModel implements IEncodin
) {
super(modelService, modeService);
this.hasAssociatedFilePath = hasAssociatedFilePath;
this.initialValue = initialValue;
this.dirty = false;
this.versionId = 0;
......
......@@ -27,8 +27,6 @@ import { ITextModelResolverService } from 'vs/editor/common/services/resolverSer
* A file editor input is the input type for the file editor of file system resources.
*/
export class FileEditorInput extends EditorInput implements IFileEditorInput {
private resource: URI;
private preferredEncoding: string;
private forceOpenAsBinary: boolean;
private textModelReference: TPromise<IReference<TextFileEditorModel>>;
......@@ -46,8 +44,8 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
* An editor input who's contents are retrieved from file services.
*/
constructor(
resource: URI,
preferredEncoding: string,
private resource: URI,
private preferredEncoding: string,
@IInstantiationService private instantiationService: IInstantiationService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@ITextFileService private textFileService: ITextFileService,
......@@ -58,9 +56,6 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
this.toUnbind = [];
this.resource = resource;
this.preferredEncoding = preferredEncoding;
this.registerListeners();
}
......
......@@ -233,7 +233,7 @@ export class WorkbenchEditorService implements IWorkbenchEditorService {
// Untitled file support
const untitledInput = <IUntitledResourceInput>input;
if (!untitledInput.resource || typeof untitledInput.filePath === 'string' || (untitledInput.resource instanceof URI && untitledInput.resource.scheme === UNTITLED_SCHEMA)) {
return this.untitledEditorService.createOrGet(untitledInput.filePath ? URI.file(untitledInput.filePath) : untitledInput.resource, untitledInput.language, untitledInput.contents);
return this.untitledEditorService.createOrGet(untitledInput.filePath ? URI.file(untitledInput.filePath) : untitledInput.resource, untitledInput.language, untitledInput.contents, untitledInput.encoding);
}
const resourceInput = <IResourceInput>input;
......
......@@ -24,6 +24,7 @@ export interface IModelLoadOrCreateOptions {
resource?: URI;
modeId?: string;
initialValue?: string;
encoding?: string;
}
export interface IUntitledEditorService {
......@@ -77,7 +78,7 @@ export interface IUntitledEditorService {
* It is valid to pass in a file resource. In that case the path will be used as identifier.
* The use case is to be able to create a new file with a specific path with VSCode.
*/
createOrGet(resource?: URI, modeId?: string, initialValue?: string): UntitledEditorInput;
createOrGet(resource?: URI, modeId?: string, initialValue?: string, encoding?: string): UntitledEditorInput;
/**
* Creates a new untitled model with the optional resource URI or returns an existing one
......@@ -194,10 +195,10 @@ export class UntitledEditorService implements IUntitledEditorService {
}
public loadOrCreate(options: IModelLoadOrCreateOptions = Object.create(null)): TPromise<UntitledEditorModel> {
return this.createOrGet(options.resource, options.modeId, options.initialValue).resolve();
return this.createOrGet(options.resource, options.modeId, options.initialValue, options.encoding).resolve();
}
public createOrGet(resource?: URI, modeId?: string, initialValue?: string): UntitledEditorInput {
public createOrGet(resource?: URI, modeId?: string, initialValue?: string, encoding?: string): UntitledEditorInput {
// Massage resource if it comes with a file:// scheme
let hasAssociatedFilePath = false;
......@@ -216,10 +217,10 @@ export class UntitledEditorService implements IUntitledEditorService {
}
// Create new otherwise
return this.doCreate(resource, hasAssociatedFilePath, modeId, initialValue);
return this.doCreate(resource, hasAssociatedFilePath, modeId, initialValue, encoding);
}
private doCreate(resource?: URI, hasAssociatedFilePath?: boolean, modeId?: string, initialValue?: string): UntitledEditorInput {
private doCreate(resource?: URI, hasAssociatedFilePath?: boolean, modeId?: string, initialValue?: string, encoding?: string): UntitledEditorInput {
if (!resource) {
// Create new taking a resource URI that is not already taken
......@@ -238,7 +239,7 @@ export class UntitledEditorService implements IUntitledEditorService {
}
}
const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId, initialValue);
const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId, initialValue, encoding);
const contentListener = input.onDidModelChangeContent(() => {
this._onDidChangeContent.fire(resource);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册