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

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

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