2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { Emitter, Event } from '../../../base/common/event.js';
7
>
import { getCompressedContent, IJSONSchema } from '../../../base/common/jsonSchema.js';
8
>
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
9
>
import * as platform from '../../registry/common/platform.js';
10
>
11
>
export const Extensions = {
12
>
JSONContribution: 'base.contributions.json'
13
>
};
14
>
15
>
export interface ISchemaContributions {
16
>
schemas: { [id: string]: IJSONSchema };
17
>
}
18
>
19
>
export interface IJSONContributionRegistry {
20
>
21
>
readonly onDidChangeSchema: Event<string>;
22
>
readonly onDidChangeSchemaAssociations: Event<void>;
23
>
24
>
/**
25
>
* Register a schema to the registry.
26
>
*/
27
>
registerSchema(uri: string, unresolvedSchemaContent: IJSONSchema, store?: DisposableStore): void;
28
>
29
>
registerSchemaAssociation(uri: string, glob: string): IDisposable;
30
>
31
>
/**
32
>
* Notifies all listeners that the content of the given schema has changed.
33
>
* @param uri The id of the schema
34
>
*/
35
>
notifySchemaChanged(uri: string): void;
36
>
37
>
/**
38
>
* Get all schemas
39
>
*/
40
>
getSchemaContributions(): ISchemaContributions;
41
>
42
>
getSchemaAssociations(): { [uri: string]: string[] };
43
>
44
>
/**
45
>
* Gets the (compressed) content of the schema with the given schema ID (if any)
46
>
* @param uri The id of the schema
47
>
*/
48
>
getSchemaContent(uri: string): string | undefined;
49
>
50
>
/**
51
>
* Returns true if there's a schema that matches the given schema ID
52
>
* @param uri The id of the schema
53
>
*/
54
>
hasSchemaContent(uri: string): boolean;
55
>
}
56
>
57
>
58
>
59
>
function normalizeId(id: string) {
60
>
if (id.length > 0 && id.charAt(id.length - 1) === '#') {
61
return id.substring(0, id.length - 1);
62
}
64
>
}
65
>
66
>
67
>
68
>
class JSONContributionRegistry extends Disposable implements IJSONContributionRegistry {
69
>
70
>
private readonly schemasById: { [id: string]: IJSONSchema } = {};
71
>
private readonly schemaAssociations: { [uri: string]: string[] } = {};
72
>
73
>
private readonly _onDidChangeSchema = this._register(new Emitter<string>());
74
>
readonly onDidChangeSchema: Event<string> = this._onDidChangeSchema.event;
75
>
76
>
private readonly _onDidChangeSchemaAssociations = this._register(new Emitter<void>());
77
>
readonly onDidChangeSchemaAssociations: Event<void> = this._onDidChangeSchemaAssociations.event;
78
>
79
>
public registerSchema(uri: string, unresolvedSchemaContent: IJSONSchema, store?: DisposableStore): void {
80
>
const normalizedUri = normalizeId(uri);
81
>
this.schemasById[normalizedUri] = unresolvedSchemaContent;
82
>
this._onDidChangeSchema.fire(uri);
83
>
84
>
if (store) {
85
store.add(toDisposable(() => {
86
delete this.schemasById[normalizedUri];