Path: blob/main/src/vs/platform/jsonschemas/common/jsonContributionRegistry.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Emitter, Event } from '../../../base/common/event.js';6import { getCompressedContent, IJSONSchema } from '../../../base/common/jsonSchema.js';7import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';8import * as platform from '../../registry/common/platform.js';910export const Extensions = {11JSONContribution: 'base.contributions.json'12};1314export interface ISchemaContributions {15schemas: { [id: string]: IJSONSchema };16}1718export interface IJSONContributionRegistry {1920readonly onDidChangeSchema: Event<string>;21readonly onDidChangeSchemaAssociations: Event<void>;2223/**24* Register a schema to the registry.25*/26registerSchema(uri: string, unresolvedSchemaContent: IJSONSchema, store?: DisposableStore): void;2728registerSchemaAssociation(uri: string, glob: string): IDisposable;2930/**31* Notifies all listeners that the content of the given schema has changed.32* @param uri The id of the schema33*/34notifySchemaChanged(uri: string): void;3536/**37* Get all schemas38*/39getSchemaContributions(): ISchemaContributions;4041getSchemaAssociations(): { [uri: string]: string[] };4243/**44* Gets the (compressed) content of the schema with the given schema ID (if any)45* @param uri The id of the schema46*/47getSchemaContent(uri: string): string | undefined;4849/**50* Returns true if there's a schema that matches the given schema ID51* @param uri The id of the schema52*/53hasSchemaContent(uri: string): boolean;54}55565758function normalizeId(id: string) {59if (id.length > 0 && id.charAt(id.length - 1) === '#') {60return id.substring(0, id.length - 1);61}62return id;63}64656667class JSONContributionRegistry extends Disposable implements IJSONContributionRegistry {6869private readonly schemasById: { [id: string]: IJSONSchema } = {};70private readonly schemaAssociations: { [uri: string]: string[] } = {};7172private readonly _onDidChangeSchema = this._register(new Emitter<string>());73readonly onDidChangeSchema: Event<string> = this._onDidChangeSchema.event;7475private readonly _onDidChangeSchemaAssociations = this._register(new Emitter<void>());76readonly onDidChangeSchemaAssociations: Event<void> = this._onDidChangeSchemaAssociations.event;7778public registerSchema(uri: string, unresolvedSchemaContent: IJSONSchema, store?: DisposableStore): void {79const normalizedUri = normalizeId(uri);80this.schemasById[normalizedUri] = unresolvedSchemaContent;81this._onDidChangeSchema.fire(uri);8283if (store) {84store.add(toDisposable(() => {85delete this.schemasById[normalizedUri];86this._onDidChangeSchema.fire(uri);87}));88}89}9091public registerSchemaAssociation(uri: string, glob: string): IDisposable {92const normalizedUri = normalizeId(uri);93if (!this.schemaAssociations[normalizedUri]) {94this.schemaAssociations[normalizedUri] = [];95}96if (!this.schemaAssociations[normalizedUri].includes(glob)) {97this.schemaAssociations[normalizedUri].push(glob);98this._onDidChangeSchemaAssociations.fire();99}100101return toDisposable(() => {102const associations = this.schemaAssociations[normalizedUri];103if (associations) {104const index = associations.indexOf(glob);105if (index !== -1) {106associations.splice(index, 1);107if (associations.length === 0) {108delete this.schemaAssociations[normalizedUri];109}110this._onDidChangeSchemaAssociations.fire();111}112}113});114}115116public notifySchemaChanged(uri: string): void {117this._onDidChangeSchema.fire(uri);118}119120public getSchemaContributions(): ISchemaContributions {121return {122schemas: this.schemasById,123};124}125126public getSchemaContent(uri: string): string | undefined {127const schema = this.schemasById[uri];128return schema ? getCompressedContent(schema) : undefined;129}130131public hasSchemaContent(uri: string): boolean {132return !!this.schemasById[uri];133}134135public getSchemaAssociations(): { [uri: string]: string[] } {136return this.schemaAssociations;137}138139}140141const jsonContributionRegistry = new JSONContributionRegistry();142platform.Registry.add(Extensions.JSONContribution, jsonContributionRegistry);143144145