Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatically associate YAML files with schemas by other extensions #61

Closed
akaroml opened this issue Feb 4, 2018 · 6 comments
Closed

Comments

@akaroml
Copy link

akaroml commented Feb 4, 2018

Here to request a way of specifying a schema for a YAML file programmatically by other extensions.

Scenario

When a user modifies the content of a YAML, a third party can recognize the features of the new content and tell the YAML extension what schema to use for the document.
In this way, the YAML extension can focus on generic language features but let other extensions contribute the schema knowledge.
As user types, other extensions start to recognize the contents and specifies a schema on the fly. This is super useful when a user tries to write the YAML from scratch.

Current Implementation

Currently, a user needs to specify a schema for a YAML file inside the settings.json. The users have to fill the URL of the schema and persist that association inside settings.json. When there are multiple YAML files with different schemas, this work could be time consuming and error-prone.

Proposed Implementation

Besides hard-coding the schema association, we can let other extensions contribute the logic of associating schemas with YAML files. For example, the Kubernetes extension has the manifest file schema and can tell whether a YAML file conforms to the schema. So whenever the Kubernetes extension recognizes a YAML file, it tells the YAML extension to process that YAML file with the Kubernetes schema at runtime.

@JPinkney
Copy link
Contributor

JPinkney commented Feb 6, 2018

You can use the "json/schemaAssociations" notification in the language server to inject the schemas https://github.com/redhat-developer/yaml-language-server/blob/master/src/server.ts#L34-L36

@andxu
Copy link
Contributor

andxu commented Feb 28, 2018

see PR for integrating custom schema providers

@andxu
Copy link
Contributor

andxu commented Mar 2, 2018

The PR is merged.

@JPinkney JPinkney closed this as completed Mar 2, 2018
apupier pushed a commit to apupier/vscode-yaml that referenced this issue Jul 2, 2020
@Nusserdt
Copy link

Can you provide an example how it works?

@Nusserdt
Copy link

Nusserdt commented Mar 18, 2022

I try something like:

const extension: vscode.Extension<any> = vscode.extensions.getExtension("redhat.vscode-yaml");
const client: any = await extension.activate();
client.registerContributor(SCHEMA, onRequestSchema, onRequestSchemaContent, "test: test");

But I don't get it to work. I think the problem here is that the functions onRequestSchema and onRequestSchemaContent never get called from the YAML extension api.

@Nusserdt
Copy link

I found a working minimal example here:
#216 (comment)

But I was not able to apply that on custom file types/languages (only directly on *.yaml)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants