forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschemaSelectionHandlers.ts
More file actions
86 lines (77 loc) · 3.11 KB
/
schemaSelectionHandlers.ts
File metadata and controls
86 lines (77 loc) · 3.11 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Connection } from 'vscode-languageserver/node';
import { JSONSchema } from '../../languageservice/jsonSchema';
import { yamlDocumentsCache } from '../../languageservice/parser/yaml-documents';
import { YAMLSchemaService } from '../../languageservice/services/yamlSchemaService';
import { getSchemaUrls } from '../../languageservice/utils/schemaUrls';
import { SettingsState } from '../../yamlSettings';
import { JSONSchemaDescription, JSONSchemaDescriptionExt, SchemaSelectionRequests } from '../../requestTypes';
export class JSONSchemaSelection {
constructor(
private readonly schemaService: YAMLSchemaService,
private readonly yamlSettings: SettingsState,
private readonly connection: Connection
) {
this.connection.onRequest(SchemaSelectionRequests.getSchema, (fileUri) => {
return this.getSchemas(fileUri);
});
this.connection.onRequest(SchemaSelectionRequests.getAllSchemas, (fileUri) => {
return this.getAllSchemas(fileUri);
});
}
async getSchemas(docUri: string): Promise<JSONSchemaDescription[]> {
const schemas = await this.getSchemasForFile(docUri);
const result = Array.from(schemas).map((val) => {
return {
name: val[1].title,
uri: val[0],
description: val[1].description,
versions: val[1].versions,
};
});
return result;
}
private async getSchemasForFile(docUri: string): Promise<Map<string, JSONSchema>> {
const document = this.yamlSettings.documents.get(docUri);
const schemas = new Map<string, JSONSchema>();
if (!document) {
return schemas;
}
const yamlDoc = yamlDocumentsCache.getYamlDocument(document);
for (const currentYAMLDoc of yamlDoc.documents) {
const schema = await this.schemaService.getSchemaForResource(document.uri, currentYAMLDoc);
if (schema?.schema) {
const schemaUrls = getSchemaUrls(schema?.schema);
if (schemaUrls.size === 0) {
continue;
}
for (const urlToSchema of schemaUrls) {
schemas.set(urlToSchema[0], urlToSchema[1]);
}
}
}
return schemas;
}
async getAllSchemas(docUri: string): Promise<JSONSchemaDescriptionExt[]> {
const fileSchemas = await this.getSchemasForFile(docUri);
const fileSchemasHandle: JSONSchemaDescriptionExt[] = Array.from(fileSchemas.entries()).map((val) => {
return {
uri: val[0],
fromStore: false,
usedForCurrentFile: true,
name: val[1].title,
description: val[1].description,
versions: val[1].versions,
};
});
const result = [];
let allSchemas = this.schemaService.getAllSchemas();
allSchemas = allSchemas.filter((val) => !fileSchemas.has(val.uri));
result.push(...fileSchemasHandle);
result.push(...allSchemas);
return result;
}
}