Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/jsonschemas/common/jsonContributionRegistry.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
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
}
63
return id;
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];
87
this._onDidChangeSchema.fire(uri);
88
}));
89
}
90
}
91
92
public registerSchemaAssociation(uri: string, glob: string): IDisposable {
93
const normalizedUri = normalizeId(uri);
94
if (!this.schemaAssociations[normalizedUri]) {
95
this.schemaAssociations[normalizedUri] = [];
96
}
97
if (!this.schemaAssociations[normalizedUri].includes(glob)) {
98
this.schemaAssociations[normalizedUri].push(glob);
99
this._onDidChangeSchemaAssociations.fire();
100
}
101
102
return toDisposable(() => {
103
const associations = this.schemaAssociations[normalizedUri];
104
if (associations) {
105
const index = associations.indexOf(glob);
106
if (index !== -1) {
107
associations.splice(index, 1);
108
if (associations.length === 0) {
109
delete this.schemaAssociations[normalizedUri];
110
}
111
this._onDidChangeSchemaAssociations.fire();
112
}
113
}
114
});
115
}
116
117
public notifySchemaChanged(uri: string): void {
118
this._onDidChangeSchema.fire(uri);
119
}
120
121
public getSchemaContributions(): ISchemaContributions {
122
return {
123
schemas: this.schemasById,
124
};
125
}
126
127
public getSchemaContent(uri: string): string | undefined {
128
const schema = this.schemasById[uri];
129
return schema ? getCompressedContent(schema) : undefined;
130
}
131
132
public hasSchemaContent(uri: string): boolean {
133
return !!this.schemasById[uri];
134
}
135
136
public getSchemaAssociations(): { [uri: string]: string[] } {
137
return this.schemaAssociations;
138
}
139
140
}
141
142
const jsonContributionRegistry = new JSONContributionRegistry();
143
platform.Registry.add(Extensions.JSONContribution, jsonContributionRegistry);
144
145