Skip to content

Commit e187656

Browse files
author
Benjamin Pasero
committed
untitled - some cleanup and polish
1 parent 44b0291 commit e187656

8 files changed

Lines changed: 32 additions & 26 deletions

File tree

src/vs/workbench/browser/parts/editor/editor.contribution.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
105105
);
106106

107107
interface ISerializedUntitledTextEditorInput {
108-
resource: string;
109-
resourceJSON: object;
108+
resourceJSON: UriComponents;
110109
modeId: string | undefined;
111110
encoding: string | undefined;
112111
}
@@ -136,7 +135,6 @@ class UntitledTextEditorInputFactory implements IEditorInputFactory {
136135
}
137136

138137
const serialized: ISerializedUntitledTextEditorInput = {
139-
resource: resource.toString(), // Keep for backwards compatibility
140138
resourceJSON: resource.toJSON(),
141139
modeId: untitledTextEditorInput.getMode(),
142140
encoding: untitledTextEditorInput.getEncoding()
@@ -148,7 +146,7 @@ class UntitledTextEditorInputFactory implements IEditorInputFactory {
148146
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): UntitledTextEditorInput {
149147
return instantiationService.invokeFunction<UntitledTextEditorInput>(accessor => {
150148
const deserialized: ISerializedUntitledTextEditorInput = JSON.parse(serializedEditorInput);
151-
const resource = !!deserialized.resourceJSON ? URI.revive(<UriComponents>deserialized.resourceJSON) : URI.parse(deserialized.resource);
149+
const resource = URI.revive(deserialized.resourceJSON);
152150
const mode = deserialized.modeId;
153151
const encoding = deserialized.encoding;
154152

src/vs/workbench/contrib/codeEditor/browser/inspectKeybindings.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as nls from 'vs/nls';
77
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
88
import { EditorAction, ServicesAccessor, registerEditorAction } from 'vs/editor/browser/editorExtensions';
99
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
10-
import { IUntitledTextResourceInput } from 'vs/workbench/common/editor';
1110
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
1211
import { Registry } from 'vs/platform/registry/common/platform';
1312
import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/workbench/common/actions';
@@ -29,7 +28,7 @@ class InspectKeyMap extends EditorAction {
2928
const keybindingService = accessor.get(IKeybindingService);
3029
const editorService = accessor.get(IEditorService);
3130

32-
editorService.openEditor({ contents: keybindingService._dumpDebugInfo(), options: { pinned: true } } as IUntitledTextResourceInput);
31+
editorService.openEditor({ contents: keybindingService._dumpDebugInfo(), options: { pinned: true } });
3332
}
3433
}
3534

@@ -49,7 +48,7 @@ class InspectKeyMapJSON extends Action {
4948
}
5049

5150
public run(): Promise<any> {
52-
return this._editorService.openEditor({ contents: this._keybindingService._dumpDebugInfoJSON(), options: { pinned: true } } as IUntitledTextResourceInput);
51+
return this._editorService.openEditor({ contents: this._keybindingService._dumpDebugInfoJSON(), options: { pinned: true } });
5352
}
5453
}
5554

src/vs/workbench/contrib/files/browser/files.contribution.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ Registry.as<IEditorInputFactoryRegistry>(EditorInputExtensions.EditorInputFactor
113113
});
114114

115115
interface ISerializedFileInput {
116-
resource: string;
117-
resourceJSON: object;
116+
resourceJSON: UriComponents;
118117
encoding?: string;
119118
modeId?: string;
120119
}
@@ -130,7 +129,6 @@ class FileEditorInputFactory implements IEditorInputFactory {
130129
const fileEditorInput = <FileEditorInput>editorInput;
131130
const resource = fileEditorInput.getResource();
132131
const fileInput: ISerializedFileInput = {
133-
resource: resource.toString(), // Keep for backwards compatibility
134132
resourceJSON: resource.toJSON(),
135133
encoding: fileEditorInput.getEncoding(),
136134
modeId: fileEditorInput.getPreferredMode() // only using the preferred user associated mode here if available to not store redundant data
@@ -142,7 +140,7 @@ class FileEditorInputFactory implements IEditorInputFactory {
142140
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): FileEditorInput {
143141
return instantiationService.invokeFunction<FileEditorInput>(accessor => {
144142
const fileInput: ISerializedFileInput = JSON.parse(serializedEditorInput);
145-
const resource = !!fileInput.resourceJSON ? URI.revive(<UriComponents>fileInput.resourceJSON) : URI.parse(fileInput.resource);
143+
const resource = URI.revive(fileInput.resourceJSON);
146144
const encoding = fileInput.encoding;
147145
const mode = fileInput.modeId;
148146

src/vs/workbench/services/editor/browser/editorService.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ export class EditorService extends Disposable implements EditorServiceImpl {
593593
const input = this.instantiationService.createInstance(UntitledTextEditorInput, untitledModel);
594594

595595
// We dispose the untitled model once the editor
596-
// is being disposed. Since we created the model,
597-
// we are in charge of cleaning it up.
596+
// is being disposed. Even though we may have not
597+
// created the model initially, the lifecycle for
598+
// untitled is tightly coupled with the editor
599+
// lifecycle for now.
598600
Event.once(input.onDispose)(() => untitledModel.dispose());
599601

600602
return input;

src/vs/workbench/services/extensions/common/extensionHostProcessManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensio
1919
import * as nls from 'vs/nls';
2020
import { registerAction2, Action2 } from 'vs/platform/actions/common/actions';
2121
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
22-
import { IUntitledTextResourceInput } from 'vs/workbench/common/editor';
2322
import { StopWatch } from 'vs/base/common/stopwatch';
2423
import { VSBuffer } from 'vs/base/common/buffer';
2524
import { IExtensionHostStarter } from 'vs/workbench/services/extensions/common/extensions';
@@ -405,7 +404,7 @@ registerAction2(class MeasureExtHostLatencyAction extends Action2 {
405404
const editorService = accessor.get(IEditorService);
406405

407406
const measurements = await Promise.all(getLatencyTestProviders().map(provider => provider.measure()));
408-
editorService.openEditor({ contents: measurements.map(MeasureExtHostLatencyAction._print).join('\n\n'), options: { pinned: true } } as IUntitledTextResourceInput);
407+
editorService.openEditor({ contents: measurements.map(MeasureExtHostLatencyAction._print).join('\n\n'), options: { pinned: true } });
409408
}
410409

411410
private static _print(m: ExtHostLatencyResult | null): string {

src/vs/workbench/services/textfile/test/browser/textFileService.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ suite('Files - TextFileService', () => {
6868
assert.ok(accessor.textFileService.isDirty(untitled.resource));
6969

7070
untitled.dispose();
71+
model.dispose();
7172
});
7273

7374
test('save - file', async function () {

src/vs/workbench/services/textmodelResolver/test/browser/textModelResolverService.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ suite('Workbench - TextModelResolverService', () => {
111111

112112
test('resolve untitled', async () => {
113113
const service = accessor.untitledTextEditorService;
114-
const input = instantiationService.createInstance(UntitledTextEditorInput, service.create());
114+
const untitledModel = service.create();
115+
const input = instantiationService.createInstance(UntitledTextEditorInput, untitledModel);
115116

116117
await input.resolve();
117118
const ref = await accessor.textModelResolverService.createModelReference(input.getResource());
118119
const model = ref.object;
120+
assert.equal(untitledModel, model);
119121
const editorModel = model.textEditorModel;
120122
assert.ok(editorModel);
121123
ref.dispose();

src/vs/workbench/services/untitled/common/untitledTextEditorModel.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ export class UntitledTextEditorModel extends BaseTextEditorModel implements IUnt
256256
}
257257

258258
// Create text editor model if not yet done
259+
let createdUntitledModel = false;
259260
if (!this.textEditorModel) {
260261
this.createTextEditorModel(untitledContents, this.resource, this.preferredMode);
262+
createdUntitledModel = true;
261263
}
262264

263265
// Otherwise: the untitled model already exists and we must assume
@@ -275,18 +277,23 @@ export class UntitledTextEditorModel extends BaseTextEditorModel implements IUnt
275277
this._register(textEditorModel.onDidChangeContent(e => this.onModelContentChanged(textEditorModel, e)));
276278
this._register(textEditorModel.onDidChangeLanguage(() => this.onConfigurationChange())); // mode change can have impact on config
277279

278-
// Name
279-
if (backup || this.initialValue) {
280-
this.updateNameFromFirstLine();
281-
}
280+
// Only adjust name and dirty state etc. if we
281+
// actually created the untitled model
282+
if (createdUntitledModel) {
282283

283-
// Untitled associated to file path are dirty right away as well as untitled with content
284-
this.setDirty(this.hasAssociatedFilePath || !!backup || !!this.initialValue);
284+
// Name
285+
if (backup || this.initialValue) {
286+
this.updateNameFromFirstLine();
287+
}
285288

286-
// If we have initial contents, make sure to emit this
287-
// as the appropiate events to the outside.
288-
if (backup || this.initialValue) {
289-
this._onDidChangeContent.fire();
289+
// Untitled associated to file path are dirty right away as well as untitled with content
290+
this.setDirty(this.hasAssociatedFilePath || !!backup || !!this.initialValue);
291+
292+
// If we have initial contents, make sure to emit this
293+
// as the appropiate events to the outside.
294+
if (backup || this.initialValue) {
295+
this._onDidChangeContent.fire();
296+
}
290297
}
291298

292299
return this as UntitledTextEditorModel & IResolvedTextEditorModel;

0 commit comments

Comments
 (0)