editors - use preferred content only once

This commit is contained in:
Benjamin Pasero 2021-05-27 13:09:28 +02:00
parent 30074591ee
commit 14a4548e4a
4 changed files with 19 additions and 2 deletions

View file

@ -187,6 +187,14 @@ export class TextResourceEditorInput extends AbstractTextResourceEditorInput imp
model.updateTextEditorModel(typeof this.preferredContents === 'string' ? createTextBufferFactory(this.preferredContents) : undefined, this.preferredMode);
}
// Unset preferred contents and mode after having applied
// them once to prevent these properties to stick. We still
// want the user to change the language mode in the editor
// and want to show updated contents (if any) in future
// `resolve` calls.
this.preferredContents = undefined;
this.preferredMode = undefined;
return model;
}

View file

@ -319,6 +319,11 @@ export class FileEditorInput extends AbstractTextResourceEditorInput implements
reason: TextFileResolveReason.EDITOR
});
// Unset preferred contents after having applied it once
// to prevent this property to stick. We still want future
// `resolve` calls to fetch the contents from disk.
this.preferredContents = undefined;
// This is a bit ugly, because we first resolve the model and then resolve a model reference. the reason being that binary
// or very large files do not resolve to a text file model but should be opened as binary files without text. First calling into
// resolve() ensures we are not creating model references for these kind of resources.

View file

@ -170,6 +170,10 @@ suite('Files - FileEditorInput', () => {
await input.resolve();
assert.strictEqual(model.textEditorModel!.getValue(), 'Other contents');
model.textEditorModel?.setValue('Changed contents');
await input.resolve();
assert.strictEqual(model.textEditorModel!.getValue(), 'Changed contents'); // preferred contents only used once
const input2 = createFileInput(toResource.call(this, '/foo/bar/file.js'));
input2.setPreferredContents('My contents');

View file

@ -85,7 +85,7 @@ suite('Resource text editors', () => {
assert.strictEqual(model.textEditorModel?.getValue(), 'Some other contents');
await input.resolve();
assert.strictEqual(model.textEditorModel?.getValue(), 'My Resource Input Contents');
assert.strictEqual(model.textEditorModel?.getValue(), 'Some other contents'); // preferred contents only used once
});
test('preferred contents (via setPreferredContents)', async () => {
@ -103,6 +103,6 @@ suite('Resource text editors', () => {
assert.strictEqual(model.textEditorModel?.getValue(), 'Some other contents');
await input.resolve();
assert.strictEqual(model.textEditorModel?.getValue(), 'My Resource Input Contents');
assert.strictEqual(model.textEditorModel?.getValue(), 'Some other contents'); // preferred contents only used once
});
});