提交 be3e1c36 编写于 作者: S Sandeep Somavarapu

Update the schemas model on schema changes

上级 01ad59eb
......@@ -5,9 +5,8 @@
'use strict';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import platform = require('vs/platform/registry/common/platform');
import { EventEmitter } from 'vs/base/common/eventEmitter';
import { IDisposable } from 'vs/base/common/lifecycle';
import * as platform from 'vs/platform/registry/common/platform';
import Event, { Emitter } from 'vs/base/common/event';
export const Extensions = {
JSONContribution: 'base.contributions.json'
......@@ -19,6 +18,8 @@ export interface ISchemaContributions {
export interface IJSONContributionRegistry {
readonly onDidChangeSchema: Event<string>;
/**
* Register a schema to the registry.
*/
......@@ -28,12 +29,6 @@ export interface IJSONContributionRegistry {
* Get all schemas
*/
getSchemaContributions(): ISchemaContributions;
/**
* Adds a change listener
*/
addRegistryChangedListener(callback: (e: IJSONContributionRegistryEvent) => void): IDisposable;
}
export interface IJSONContributionRegistryEvent {
......@@ -50,21 +45,19 @@ function normalizeId(id: string) {
class JSONContributionRegistry implements IJSONContributionRegistry {
private schemasById: { [id: string]: IJSONSchema };
private eventEmitter: EventEmitter;
private _onDidChangeSchema: Emitter<string> = new Emitter<string>();
readonly onDidChangeSchema: Event<string> = this._onDidChangeSchema.event;
constructor() {
this.schemasById = {};
this.eventEmitter = new EventEmitter();
}
public addRegistryChangedListener(callback: (e: IJSONContributionRegistryEvent) => void): IDisposable {
return this.eventEmitter.addListener('registryChanged', callback);
}
public registerSchema(uri: string, unresolvedSchemaContent: IJSONSchema): void {
this.schemasById[normalizeId(uri)] = unresolvedSchemaContent;
this.eventEmitter.emit('registryChanged', {});
this._onDidChangeSchema.fire(uri);
}
public getSchemaContributions(): ISchemaContributions {
......
......@@ -14,6 +14,7 @@ import { Registry } from 'vs/platform/registry/common/platform';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IPreferencesService } from 'vs/workbench/parts/preferences/common/preferences';
import { dispose } from 'vs/base/common/lifecycle';
const schemaRegistry = Registry.as<JSONContributionRegistry.IJSONContributionRegistry>(JSONContributionRegistry.Extensions.JSONContribution);
......@@ -33,18 +34,16 @@ export class PreferencesContentProvider implements IWorkbenchContribution {
}
private start(): void {
this.textModelResolverService.registerTextModelContentProvider('vscode', {
provideTextContent: (uri: URI): TPromise<IModel> => {
if (uri.scheme !== 'vscode') {
return null;
}
if (uri.authority === 'schemas') {
let schemas = schemaRegistry.getSchemaContributions().schemas;
let schema = schemas[uri.toString()];
if (schema) {
let modelContent = JSON.stringify(schema);
let mode = this.modeService.getOrCreateMode('json');
return TPromise.as(this.modelService.createModel(modelContent, mode, uri));
const schemaModel = this.getSchemaModel(uri);
if (schemaModel) {
return TPromise.as(schemaModel);
}
}
return this.preferencesService.resolveContent(uri)
......@@ -59,4 +58,25 @@ export class PreferencesContentProvider implements IWorkbenchContribution {
}
});
}
private getSchemaModel(uri: URI): IModel {
let schema = schemaRegistry.getSchemaContributions().schemas[uri.toString()];
if (schema) {
const modelContent = JSON.stringify(schema);
const mode = this.modeService.getOrCreateMode('json');
const model = this.modelService.createModel(modelContent, mode, uri);
let disposables = [];
disposables.push(schemaRegistry.onDidChangeSchema(schemaUri => {
if (schemaUri === uri.toString()) {
schema = schemaRegistry.getSchemaContributions().schemas[uri.toString()];
model.setValue(JSON.stringify(schema));
}
}));
disposables.push(model.onWillDispose(() => dispose(disposables)));
return model;
}
return null;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册