-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods which allow client get schemas info (#556)
* Add methods which allow client get schemas info Signed-off-by: Yevhen Vydolob <[email protected]> * fix review comment Signed-off-by: Yevhen Vydolob <[email protected]> * Delete commented line Signed-off-by: Yevhen Vydolob <[email protected]>
- Loading branch information
Showing
12 changed files
with
375 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,6 +254,90 @@ docker run -it quay.io/redhat-developer/yaml-language-server:latest | |
|
||
`yaml-language-server` use `[email protected]` which implements [LSP 3.16](https://github.com/Microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md) | ||
|
||
## Language Server Protocol extensions | ||
|
||
### SchemaSelectionRequests | ||
|
||
#### SupportSchemaSelection Notification | ||
|
||
The support schema selection notification is sent from a client to the server to inform server that client supports JSON Schema selection. | ||
|
||
_Notification:_ | ||
- method: `'yaml/supportSchemaSelection'` | ||
- params: `void` | ||
|
||
#### SchemaStoreInitialized Notification | ||
|
||
The schema store initialized notification is sent from the server to a client to inform client that server has finished initializing/loading schemas from schema store, and client now can ask for schemas. | ||
|
||
_Notification:_ | ||
- method: `'yaml/schema/store/initialized'` | ||
- params: `void` | ||
|
||
#### GetAllSchemas Request | ||
|
||
The get all schemas request sent from a client to server to get all known schemas. | ||
|
||
_Request:_ | ||
- method: `'yaml/get/all/jsonSchemas'`; | ||
- params: the document uri, server will mark used schema for document | ||
|
||
_Response:_ | ||
|
||
- result: `JSONSchemaDescriptionExt[]` | ||
```typescript | ||
interface JSONSchemaDescriptionExt { | ||
/** | ||
* Schema URI | ||
*/ | ||
uri: string; | ||
/** | ||
* Schema name, from schema store | ||
*/ | ||
name?: string; | ||
/** | ||
* Schema description, from schema store | ||
*/ | ||
description?: string; | ||
/** | ||
* Is schema used for current document | ||
*/ | ||
usedForCurrentFile: boolean; | ||
/** | ||
* Is schema from schema store | ||
*/ | ||
fromStore: boolean; | ||
} | ||
``` | ||
|
||
#### GetSchemas Request | ||
|
||
The request sent from a client to server to get schemas used for current document. Client can use this method to indicate in UI which schemas used for current YAML document. | ||
|
||
_Request:_ | ||
- method: `'yaml/get/jsonSchema'`; | ||
- params: the document uri to get used schemas | ||
|
||
_Response:_ | ||
- result: `JSONSchemaDescription[]` | ||
|
||
```typescript | ||
interface JSONSchemaDescriptionExt { | ||
/** | ||
* Schema URI | ||
*/ | ||
uri: string; | ||
/** | ||
* Schema name, from schema store | ||
*/ | ||
name?: string; | ||
/** | ||
* Schema description, from schema store | ||
*/ | ||
description?: string; | ||
} | ||
``` | ||
|
||
## Clients | ||
|
||
This repository only contains the server implementation. Here are some known clients consuming this server: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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, | ||
}; | ||
}); | ||
|
||
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, | ||
}; | ||
}); | ||
const result = []; | ||
let allSchemas = this.schemaService.getAllSchemas(); | ||
allSchemas = allSchemas.filter((val) => !fileSchemas.has(val.uri)); | ||
result.push(...fileSchemasHandle); | ||
result.push(...allSchemas); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.