Merge from vscode 4d91d96e5e121b38d33508cdef17868bab255eae

This commit is contained in:
ADS Merger
2020-06-18 04:32:54 +00:00
committed by AzureDataStudio
parent a971aee5bd
commit 5e7071e466
1002 changed files with 24201 additions and 13193 deletions

View File

@@ -15,20 +15,19 @@ import { ITextModelService, ITextModelContentProvider, ITextEditorModel, IResolv
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { IFileService } from 'vs/platform/files/common/files';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
import { ModelUndoRedoParticipant } from 'vs/editor/common/services/modelUndoRedoParticipant';
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorModel>> {
private readonly providers: { [scheme: string]: ITextModelContentProvider[] } = Object.create(null);
private readonly providers = new Map<string, ITextModelContentProvider[]>();
private readonly modelsToDispose = new Set<string>();
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@ITextFileService private readonly textFileService: ITextFileService,
@IFileService private readonly fileService: IFileService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IModelService private readonly modelService: IModelService
) {
super();
@@ -61,7 +60,7 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
}
// Virtual documents
if (this.providers[resource.scheme]) {
if (this.providers.has(resource.scheme)) {
await this.resolveTextModelContent(key);
return this.instantiationService.createInstance(ResourceEditorModel, resource);
@@ -79,78 +78,83 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
destroyReferencedObject(key: string, modelPromise: Promise<ITextEditorModel>): void {
// Track as being disposed
// untitled and inMemory are bound to a different lifecycle
const resource = URI.parse(key);
if (resource.scheme === network.Schemas.untitled || resource.scheme === network.Schemas.inMemory) {
return;
}
// Track as being disposed before waiting for model to load
// to handle the case that the reference is aquired again
this.modelsToDispose.add(key);
modelPromise.then(model => {
if (!this.modelsToDispose.has(key)) {
return; // return if model has been aquired again meanwhile
}
(async () => {
try {
const model = await modelPromise;
const resource = URI.parse(key);
if (resource.scheme === network.Schemas.untitled || resource.scheme === network.Schemas.inMemory) {
// untitled and inMemory are bound to a different lifecycle
} else if (model instanceof TextFileEditorModel) {
this.textFileService.files.disposeModel(model);
} else {
if (!this.modelsToDispose.has(key)) {
// return if model has been aquired again meanwhile
return;
}
if (model instanceof TextFileEditorModel) {
// text file models have conditions that prevent them
// from dispose, so we have to wait until we can dispose
await this.textFileService.files.canDispose(model);
}
if (!this.modelsToDispose.has(key)) {
// return if model has been aquired again meanwhile
return;
}
// Finally we can dispose the model
model.dispose();
} catch (error) {
// ignore
} finally {
this.modelsToDispose.delete(key); // Untrack as being disposed
}
}, err => {
// ignore
});
})();
}
registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
const registry = this.providers;
const providers = registry[scheme] || (registry[scheme] = []);
let providers = this.providers.get(scheme);
if (!providers) {
providers = [];
this.providers.set(scheme, providers);
}
providers.unshift(provider);
return toDisposable(() => {
const array = registry[scheme];
if (!array) {
const providersForScheme = this.providers.get(scheme);
if (!providersForScheme) {
return;
}
const index = array.indexOf(provider);
const index = providersForScheme.indexOf(provider);
if (index === -1) {
return;
}
array.splice(index, 1);
providersForScheme.splice(index, 1);
if (array.length === 0) {
delete registry[scheme];
if (providersForScheme.length === 0) {
this.providers.delete(scheme);
}
});
}
hasTextModelContentProvider(scheme: string): boolean {
return this.providers[scheme] !== undefined;
return this.providers.get(scheme) !== undefined;
}
private async resolveTextModelContent(key: string): Promise<ITextModel> {
const resource = URI.parse(key);
const providers = this.providers[resource.scheme] || [];
const providersForScheme = this.providers.get(resource.scheme) || [];
if (resource.query || resource.fragment) {
type TextModelResolverUri = {
query: boolean;
fragment: boolean;
};
type TextModelResolverUriMeta = {
query: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
fragment: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
};
this.telemetryService.publicLog2<TextModelResolverUri, TextModelResolverUriMeta>('textmodelresolveruri', {
query: Boolean(resource.query),
fragment: Boolean(resource.fragment)
});
}
for (const provider of providers) {
for (const provider of providersForScheme) {
const value = await provider.provideTextContent(resource);
if (value) {
return value;
@@ -163,7 +167,7 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
export class TextModelResolverService extends Disposable implements ITextModelService {
_serviceBrand: undefined;
declare readonly _serviceBrand: undefined;
private readonly resourceModelCollection = this.instantiationService.createInstance(ResourceModelCollection);
@@ -171,13 +175,20 @@ export class TextModelResolverService extends Disposable implements ITextModelSe
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IFileService private readonly fileService: IFileService,
@IUndoRedoService private readonly undoRedoService: IUndoRedoService,
@IModelService private readonly modelService: IModelService
@IModelService private readonly modelService: IModelService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
) {
super();
this._register(new ModelUndoRedoParticipant(this.modelService, this, this.undoRedoService));
}
async createModelReference(resource: URI): Promise<IReference<IResolvedTextEditorModel>> {
// From this moment on, only operate on the canonical resource
// to ensure we reduce the chance of resolving the same resource
// with different resource forms (e.g. path casing on Windows)
resource = this.uriIdentityService.asCanonicalUri(resource);
const ref = this.resourceModelCollection.acquire(resource.toString());
try {

View File

@@ -9,7 +9,7 @@ import { URI } from 'vs/base/common/uri';
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorModel';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { workbenchInstantiationService, TestServiceAccessor } from 'vs/workbench/test/browser/workbenchTestServices';
import { workbenchInstantiationService, TestServiceAccessor, TestTextFileEditorModelManager } from 'vs/workbench/test/browser/workbenchTestServices';
import { toResource } from 'vs/base/test/common/utils';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { snapshotToString } from 'vs/workbench/services/textfile/common/textfiles';
@@ -17,6 +17,7 @@ import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/commo
import { Event } from 'vs/base/common/event';
import { timeout } from 'vs/base/common/async';
import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/untitledTextEditorInput';
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
suite('Workbench - TextModelResolverService', () => {
@@ -51,7 +52,7 @@ suite('Workbench - TextModelResolverService', () => {
});
let resource = URI.from({ scheme: 'test', authority: null!, path: 'thePath' });
let input: ResourceEditorInput = instantiationService.createInstance(ResourceEditorInput, 'The Name', 'The Description', resource, undefined);
let input: ResourceEditorInput = instantiationService.createInstance(ResourceEditorInput, resource, 'The Name', 'The Description', undefined);
const model = await input.resolve();
assert.ok(model);
@@ -72,7 +73,7 @@ suite('Workbench - TextModelResolverService', () => {
test('resolve file', async function () {
const textModel = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file_resolver.txt'), 'utf8', undefined);
(<TextFileEditorModelManager>accessor.textFileService.files).add(textModel.resource, textModel);
(<TestTextFileEditorModelManager>accessor.textFileService.files).add(textModel.resource, textModel);
await textModel.load();
@@ -94,6 +95,63 @@ suite('Workbench - TextModelResolverService', () => {
assert.equal(disposed, true);
});
test('resolved dirty file eventually disposes', async function () {
const textModel = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file_resolver.txt'), 'utf8', undefined);
(<TestTextFileEditorModelManager>accessor.textFileService.files).add(textModel.resource, textModel);
const loadedModel = await textModel.load();
loadedModel.updateTextEditorModel(createTextBufferFactory('make dirty'));
const ref = await accessor.textModelResolverService.createModelReference(textModel.resource);
let disposed = false;
Event.once(loadedModel.onDispose)(() => {
disposed = true;
});
ref.dispose();
await timeout(0);
assert.equal(disposed, false); // not disposed because model still dirty
loadedModel.revert();
await timeout(0);
assert.equal(disposed, true); // now disposed because model got reverted
});
test('resolved dirty file does not dispose when new reference created', async function () {
const textModel = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file_resolver.txt'), 'utf8', undefined);
(<TestTextFileEditorModelManager>accessor.textFileService.files).add(textModel.resource, textModel);
const loadedModel = await textModel.load();
loadedModel.updateTextEditorModel(createTextBufferFactory('make dirty'));
const ref1 = await accessor.textModelResolverService.createModelReference(textModel.resource);
let disposed = false;
Event.once(loadedModel.onDispose)(() => {
disposed = true;
});
ref1.dispose();
await timeout(0);
assert.equal(disposed, false); // not disposed because model still dirty
const ref2 = await accessor.textModelResolverService.createModelReference(textModel.resource);
loadedModel.revert();
await timeout(0);
assert.equal(disposed, false); // not disposed because we got another ref meanwhile
ref2.dispose();
await timeout(0);
assert.equal(disposed, true); // now disposed because last ref got disposed
});
test('resolve untitled', async () => {
const service = accessor.untitledTextEditorService;
const untitledModel = service.create();