|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { Connection } from 'vscode-languageserver/node'; |
| 7 | +import { JSONSchema } from '../../languageservice/jsonSchema'; |
| 8 | +import { yamlDocumentsCache } from '../../languageservice/parser/yaml-documents'; |
| 9 | +import { YAMLSchemaService } from '../../languageservice/services/yamlSchemaService'; |
| 10 | +import { getSchemaUrls } from '../../languageservice/utils/schemaUrls'; |
| 11 | +import { SettingsState } from '../../yamlSettings'; |
| 12 | +import { JSONSchemaDescription, JSONSchemaDescriptionExt, SchemaSelectionRequests } from '../../requestTypes'; |
| 13 | + |
| 14 | +export class JSONSchemaSelection { |
| 15 | + constructor( |
| 16 | + private readonly schemaService: YAMLSchemaService, |
| 17 | + private readonly yamlSettings: SettingsState, |
| 18 | + private readonly connection: Connection |
| 19 | + ) { |
| 20 | + this.connection.onRequest(SchemaSelectionRequests.getSchema, (fileUri) => { |
| 21 | + return this.getSchemas(fileUri); |
| 22 | + }); |
| 23 | + this.connection.onRequest(SchemaSelectionRequests.getAllSchemas, (fileUri) => { |
| 24 | + return this.getAllSchemas(fileUri); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + async getSchemas(docUri: string): Promise<JSONSchemaDescription[]> { |
| 29 | + const schemas = await this.getSchemasForFile(docUri); |
| 30 | + const result = Array.from(schemas).map((val) => { |
| 31 | + return { |
| 32 | + name: val[1].title, |
| 33 | + uri: val[0], |
| 34 | + description: val[1].description, |
| 35 | + }; |
| 36 | + }); |
| 37 | + |
| 38 | + return result; |
| 39 | + } |
| 40 | + |
| 41 | + private async getSchemasForFile(docUri: string): Promise<Map<string, JSONSchema>> { |
| 42 | + const document = this.yamlSettings.documents.get(docUri); |
| 43 | + const schemas = new Map<string, JSONSchema>(); |
| 44 | + if (!document) { |
| 45 | + return schemas; |
| 46 | + } |
| 47 | + |
| 48 | + const yamlDoc = yamlDocumentsCache.getYamlDocument(document); |
| 49 | + |
| 50 | + for (const currentYAMLDoc of yamlDoc.documents) { |
| 51 | + const schema = await this.schemaService.getSchemaForResource(document.uri, currentYAMLDoc); |
| 52 | + if (schema?.schema) { |
| 53 | + const schemaUrls = getSchemaUrls(schema?.schema); |
| 54 | + if (schemaUrls.size === 0) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + for (const urlToSchema of schemaUrls) { |
| 58 | + schemas.set(urlToSchema[0], urlToSchema[1]); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return schemas; |
| 63 | + } |
| 64 | + |
| 65 | + async getAllSchemas(docUri: string): Promise<JSONSchemaDescriptionExt[]> { |
| 66 | + const fileSchemas = await this.getSchemasForFile(docUri); |
| 67 | + const fileSchemasHandle: JSONSchemaDescriptionExt[] = Array.from(fileSchemas.entries()).map((val) => { |
| 68 | + return { |
| 69 | + uri: val[0], |
| 70 | + fromStore: false, |
| 71 | + usedForCurrentFile: true, |
| 72 | + name: val[1].title, |
| 73 | + description: val[1].description, |
| 74 | + }; |
| 75 | + }); |
| 76 | + const result = []; |
| 77 | + let allSchemas = this.schemaService.getAllSchemas(); |
| 78 | + allSchemas = allSchemas.filter((val) => !fileSchemas.has(val.uri)); |
| 79 | + result.push(...fileSchemasHandle); |
| 80 | + result.push(...allSchemas); |
| 81 | + |
| 82 | + return result; |
| 83 | + } |
| 84 | +} |
0 commit comments