mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-27 17:23:21 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { first } from 'vs/base/common/async';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { IModel } from 'vs/editor/common/editorCommon';
|
||||
import { IDisposable, toDisposable, IReference, ReferenceCollection, ImmortalReference } from 'vs/base/common/lifecycle';
|
||||
import { IModelService } from 'vs/editor/common/services/modelService';
|
||||
import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorModel';
|
||||
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
|
||||
import network = require('vs/base/common/network');
|
||||
import { ITextModelService, ITextModelContentProvider, ITextEditorModel } from 'vs/editor/common/services/resolverService';
|
||||
import { IUntitledEditorService, UNTITLED_SCHEMA } from 'vs/workbench/services/untitled/common/untitledEditorService';
|
||||
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
|
||||
|
||||
class ResourceModelCollection extends ReferenceCollection<TPromise<ITextEditorModel>> {
|
||||
|
||||
private providers: { [scheme: string]: ITextModelContentProvider[] } = Object.create(null);
|
||||
|
||||
constructor(
|
||||
@IInstantiationService private instantiationService: IInstantiationService,
|
||||
@ITextFileService private textFileService: ITextFileService
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
public createReferencedObject(key: string): TPromise<ITextEditorModel> {
|
||||
const resource = URI.parse(key);
|
||||
|
||||
if (resource.scheme === network.Schemas.file) {
|
||||
return this.textFileService.models.loadOrCreate(resource);
|
||||
}
|
||||
|
||||
return this.resolveTextModelContent(key).then(() => this.instantiationService.createInstance(ResourceEditorModel, resource));
|
||||
}
|
||||
|
||||
public destroyReferencedObject(modelPromise: TPromise<ITextEditorModel>): void {
|
||||
modelPromise.done(model => {
|
||||
if (model instanceof TextFileEditorModel) {
|
||||
this.textFileService.models.disposeModel(model);
|
||||
} else {
|
||||
model.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
|
||||
const registry = this.providers;
|
||||
const providers = registry[scheme] || (registry[scheme] = []);
|
||||
|
||||
providers.unshift(provider);
|
||||
|
||||
return toDisposable(() => {
|
||||
const array = registry[scheme];
|
||||
|
||||
if (!array) {
|
||||
return;
|
||||
}
|
||||
|
||||
const index = array.indexOf(provider);
|
||||
|
||||
if (index === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
array.splice(index, 1);
|
||||
|
||||
if (array.length === 0) {
|
||||
delete registry[scheme];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private resolveTextModelContent(key: string): TPromise<IModel> {
|
||||
const resource = URI.parse(key);
|
||||
const providers = this.providers[resource.scheme] || [];
|
||||
const factories = providers.map(p => () => p.provideTextContent(resource));
|
||||
|
||||
return first(factories).then(model => {
|
||||
if (!model) {
|
||||
console.error(`Unable to open '${resource}' resource is not available.`); // TODO PII
|
||||
return TPromise.wrapError<IModel>(new Error('resource is not available'));
|
||||
}
|
||||
|
||||
return model;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class TextModelResolverService implements ITextModelService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private resourceModelCollection: ResourceModelCollection;
|
||||
|
||||
constructor(
|
||||
@ITextFileService private textFileService: ITextFileService,
|
||||
@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
|
||||
@IInstantiationService private instantiationService: IInstantiationService,
|
||||
@IModelService private modelService: IModelService
|
||||
) {
|
||||
this.resourceModelCollection = instantiationService.createInstance(ResourceModelCollection);
|
||||
}
|
||||
|
||||
public createModelReference(resource: URI): TPromise<IReference<ITextEditorModel>> {
|
||||
return this._createModelReference(resource);
|
||||
}
|
||||
|
||||
private _createModelReference(resource: URI): TPromise<IReference<ITextEditorModel>> {
|
||||
|
||||
// Untitled Schema: go through cached input
|
||||
// TODO ImmortalReference is a hack
|
||||
if (resource.scheme === UNTITLED_SCHEMA) {
|
||||
return this.untitledEditorService.loadOrCreate({ resource }).then(model => new ImmortalReference(model));
|
||||
}
|
||||
|
||||
// InMemory Schema: go through model service cache
|
||||
// TODO ImmortalReference is a hack
|
||||
if (resource.scheme === 'inmemory') {
|
||||
const cachedModel = this.modelService.getModel(resource);
|
||||
|
||||
if (!cachedModel) {
|
||||
return TPromise.wrapError<IReference<ITextEditorModel>>(new Error('Cant resolve inmemory resource'));
|
||||
}
|
||||
|
||||
return TPromise.as(new ImmortalReference(this.instantiationService.createInstance(ResourceEditorModel, resource)));
|
||||
}
|
||||
|
||||
const ref = this.resourceModelCollection.acquire(resource.toString());
|
||||
|
||||
return ref.object.then(
|
||||
model => ({ object: model, dispose: () => ref.dispose() }),
|
||||
err => {
|
||||
ref.dispose();
|
||||
|
||||
return TPromise.wrapError<IReference<ITextEditorModel>>(err);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
|
||||
return this.resourceModelCollection.registerTextModelContentProvider(scheme, provider);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { IModel } from 'vs/editor/common/editorCommon';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
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, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
|
||||
import { toResource } from 'vs/base/test/common/utils';
|
||||
import { ITextModelService } from 'vs/editor/common/services/resolverService';
|
||||
import { IModelService } from 'vs/editor/common/services/modelService';
|
||||
import { IModeService } from 'vs/editor/common/services/modeService';
|
||||
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
|
||||
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
|
||||
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
|
||||
import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
|
||||
import { once } from 'vs/base/common/event';
|
||||
|
||||
class ServiceAccessor {
|
||||
constructor(
|
||||
@ITextModelService public textModelResolverService: ITextModelService,
|
||||
@IModelService public modelService: IModelService,
|
||||
@IModeService public modeService: IModeService,
|
||||
@ITextFileService public textFileService: TestTextFileService,
|
||||
@IUntitledEditorService public untitledEditorService: IUntitledEditorService
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
suite('Workbench - TextModelResolverService', () => {
|
||||
|
||||
let instantiationService: IInstantiationService;
|
||||
let accessor: ServiceAccessor;
|
||||
let model: TextFileEditorModel;
|
||||
|
||||
setup(() => {
|
||||
instantiationService = workbenchInstantiationService();
|
||||
accessor = instantiationService.createInstance(ServiceAccessor);
|
||||
});
|
||||
|
||||
teardown(() => {
|
||||
if (model) {
|
||||
model.dispose();
|
||||
model = void 0;
|
||||
}
|
||||
(<TextFileEditorModelManager>accessor.textFileService.models).clear();
|
||||
(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
|
||||
accessor.untitledEditorService.revertAll();
|
||||
});
|
||||
|
||||
test('resolve resource', function (done) {
|
||||
const dispose = accessor.textModelResolverService.registerTextModelContentProvider('test', {
|
||||
provideTextContent: function (resource: URI): TPromise<IModel> {
|
||||
if (resource.scheme === 'test') {
|
||||
let modelContent = 'Hello Test';
|
||||
let mode = accessor.modeService.getOrCreateMode('json');
|
||||
return TPromise.as(accessor.modelService.createModel(modelContent, mode, resource));
|
||||
}
|
||||
|
||||
return TPromise.as(null);
|
||||
}
|
||||
});
|
||||
|
||||
let resource = URI.from({ scheme: 'test', authority: null, path: 'thePath' });
|
||||
let input: ResourceEditorInput = instantiationService.createInstance(ResourceEditorInput, 'The Name', 'The Description', resource);
|
||||
|
||||
input.resolve().then((model: ResourceEditorModel) => {
|
||||
assert.ok(model);
|
||||
assert.equal(model.getValue(), 'Hello Test');
|
||||
|
||||
let disposed = false;
|
||||
once(model.onDispose)(() => {
|
||||
disposed = true;
|
||||
});
|
||||
|
||||
input.dispose();
|
||||
assert.equal(disposed, true);
|
||||
|
||||
dispose.dispose();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('resolve file', function () {
|
||||
model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file_resolver.txt'), 'utf8');
|
||||
(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);
|
||||
|
||||
return model.load().then(() => {
|
||||
return accessor.textModelResolverService.createModelReference(model.getResource()).then(ref => {
|
||||
const model = ref.object;
|
||||
const editorModel = model.textEditorModel;
|
||||
|
||||
assert.ok(editorModel);
|
||||
assert.equal(editorModel.getValue(), 'Hello Html');
|
||||
|
||||
let disposed = false;
|
||||
once(model.onDispose)(() => {
|
||||
disposed = true;
|
||||
});
|
||||
|
||||
ref.dispose();
|
||||
assert.equal(disposed, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('resolve untitled', function () {
|
||||
const service = accessor.untitledEditorService;
|
||||
const input = service.createOrGet();
|
||||
|
||||
return input.resolve().then(() => {
|
||||
return accessor.textModelResolverService.createModelReference(input.getResource()).then(ref => {
|
||||
const model = ref.object;
|
||||
const editorModel = model.textEditorModel;
|
||||
|
||||
assert.ok(editorModel);
|
||||
ref.dispose();
|
||||
|
||||
input.dispose();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('even loading documents should be refcounted', async () => {
|
||||
let resolveModel: Function;
|
||||
let waitForIt = new TPromise(c => resolveModel = c);
|
||||
|
||||
const disposable = accessor.textModelResolverService.registerTextModelContentProvider('test', {
|
||||
provideTextContent: async (resource: URI): TPromise<IModel> => {
|
||||
await waitForIt;
|
||||
|
||||
let modelContent = 'Hello Test';
|
||||
let mode = accessor.modeService.getOrCreateMode('json');
|
||||
return accessor.modelService.createModel(modelContent, mode, resource);
|
||||
}
|
||||
});
|
||||
|
||||
const uri = URI.from({ scheme: 'test', authority: null, path: 'thePath' });
|
||||
|
||||
const modelRefPromise1 = accessor.textModelResolverService.createModelReference(uri);
|
||||
const modelRefPromise2 = accessor.textModelResolverService.createModelReference(uri);
|
||||
|
||||
resolveModel();
|
||||
|
||||
const modelRef1 = await modelRefPromise1;
|
||||
const model1 = modelRef1.object;
|
||||
const modelRef2 = await modelRefPromise2;
|
||||
const model2 = modelRef2.object;
|
||||
const textModel = model1.textEditorModel;
|
||||
|
||||
assert.equal(model1, model2, 'they are the same model');
|
||||
assert(!textModel.isDisposed(), 'the text model should not be disposed');
|
||||
|
||||
modelRef1.dispose();
|
||||
assert(!textModel.isDisposed(), 'the text model should still not be disposed');
|
||||
|
||||
modelRef2.dispose();
|
||||
assert(textModel.isDisposed(), 'the text model should finally be disposed');
|
||||
|
||||
disposable.dispose();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user