-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathschema-extension-api.ts
167 lines (146 loc) · 4.9 KB
/
schema-extension-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { URI } from 'vscode-uri';
import { CommonLanguageClient as LanguageClient, RequestType } from 'vscode-languageclient/node';
import { workspace } from 'vscode';
import { logToExtensionOutputChannel } from './extension';
interface SchemaContributorProvider {
readonly requestSchema: (resource: string) => string;
readonly requestSchemaContent: (uri: string) => Promise<string> | string;
readonly label?: string;
}
export enum MODIFICATION_ACTIONS {
'delete',
'add',
}
export interface SchemaAdditions {
schema: string;
action: MODIFICATION_ACTIONS.add;
path: string;
key: string;
content: unknown;
}
export interface SchemaDeletions {
schema: string;
action: MODIFICATION_ACTIONS.delete;
path: string;
key: string;
}
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace SchemaModificationNotification {
// eslint-disable-next-line @typescript-eslint/ban-types
export const type: RequestType<SchemaAdditions | SchemaDeletions, void, {}> = new RequestType('json/schema/modify');
}
export interface ExtensionAPI {
registerContributor(
schema: string,
requestSchema: (resource: string) => string,
requestSchemaContent: (uri: string) => Promise<string> | string,
label?: string
): boolean;
modifySchemaContent(schemaModifications: SchemaAdditions | SchemaDeletions): Promise<void>;
}
class SchemaExtensionAPI implements ExtensionAPI {
private _customSchemaContributors: { [index: string]: SchemaContributorProvider } = {};
private _yamlClient: LanguageClient;
constructor(client: LanguageClient) {
this._yamlClient = client;
}
/**
* Register a custom schema provider
*
* @param {string} the provider's name
* @param requestSchema the requestSchema function
* @param requestSchemaContent the requestSchemaContent function
* @param label the content label, yaml key value pair, like 'apiVersion:some.api/v1'
* @returns {boolean}
*/
public registerContributor(
schema: string,
requestSchema: (resource: string) => string,
requestSchemaContent: (uri: string) => Promise<string> | string,
label?: string
): boolean {
if (this._customSchemaContributors[schema]) {
return false;
}
if (!requestSchema) {
throw new Error('Illegal parameter for requestSchema.');
}
if (label) {
const [first, second] = label.split(':');
if (first && second) {
label = second.trim();
label = label.replace('.', '\\.');
label = `${first}:[\t ]+${label}`;
}
}
this._customSchemaContributors[schema] = <SchemaContributorProvider>{
requestSchema,
requestSchemaContent,
label,
};
return true;
}
/**
* Call requestSchema for each provider and finds all matches.
*
* @param {string} resource
* @returns {string} the schema uri
*/
public requestCustomSchema(resource: string): string[] {
const matches = [];
for (const customKey of Object.keys(this._customSchemaContributors)) {
try {
const contributor = this._customSchemaContributors[customKey];
let uri: string;
if (contributor.label && workspace.textDocuments) {
const labelRegexp = new RegExp(contributor.label, 'g');
for (const doc of workspace.textDocuments) {
if (doc.uri.toString() === resource) {
if (labelRegexp.test(doc.getText())) {
uri = contributor.requestSchema(resource);
return [uri];
}
}
}
}
uri = contributor.requestSchema(resource);
if (uri) {
matches.push(uri);
}
} catch (error) {
logToExtensionOutputChannel(
`Error thrown while requesting schema "${error}" when calling the registered contributor "${customKey}"`
);
}
}
return matches;
}
/**
* Call requestCustomSchemaContent for named provider and get the schema content.
*
* @param {string} uri the schema uri returned from requestSchema.
* @returns {string} the schema content
*/
public requestCustomSchemaContent(uri: string): Promise<string> | string {
if (uri) {
const _uri = URI.parse(uri);
if (
_uri.scheme &&
this._customSchemaContributors[_uri.scheme] &&
this._customSchemaContributors[_uri.scheme].requestSchemaContent
) {
return this._customSchemaContributors[_uri.scheme].requestSchemaContent(uri);
}
}
}
public async modifySchemaContent(schemaModifications: SchemaAdditions | SchemaDeletions): Promise<void> {
return this._yamlClient.sendRequest(SchemaModificationNotification.type, schemaModifications);
}
public hasProvider(schema: string): boolean {
return this._customSchemaContributors[schema] !== undefined;
}
}
// constants
export const CUSTOM_SCHEMA_REQUEST = 'custom/schema/request';
export const CUSTOM_CONTENT_REQUEST = 'custom/schema/content';
export { SchemaExtensionAPI };