-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
[json] Add support for extensions associating JSON schemas with documents dynamically (via API) #230136
Comments
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
@CicDiplomacy why did you add that comment? |
I am interested in this feature too. I need to dynamically assign schemas, based on conditions that cannot be statically described using glob patterns. Moreover, I need to apply changes to the schemas before use. So I am interested in a solution, where I can not only control which document gets associated with which schema, but also proxy the schemas, so I can modify them minimally. My proposed APIExtensions should be able to supply providers for two different purposes. One provider for providing a schema interface JsonApi {
/**
* Register a provider to associate a json schema with a requested
* `TextDocument`.
*
* @param provider The schema association provider.
*/
registerSchemaAssociationProvider(provider: SchemaAssociationProvider): Disposable;
/**
* Notify the language server that the schema association has changed for a
* given uri.
*
* @param uri The uri of the document whose schema association has changed.
*/
schemaAssociationChanged(uri: Uri): void;
/**
* Register a provider to handle json schema requests for a specific uri
* schema.
*
* @param schema The uri schema to register for.
* @param provider The provider to handle the schema request.
*/
registerUriSchemaProvider(schema: string, provider: UriSchemaProvider): Disposable;
/**
* Notify the language server that the content of a specific schema, or
* multiple schemas has changed.
*
* @param uri The uri of the schema whose content has changed.
*/
schemaContentChanged(uri: Uri | Uri[]): void;
}
interface SchemaAssociationProvider {
provideSchemaAssociation(document: TextDocument): Uri | null | undefined;
}
interface UriSchemaProvider {
provideSchemaContent(uri: Uri): string;
} Naming can be improved. This api could be used like this: const api: JsonApi = vscode.extensions.getExtension('vscode.json-language-features').exports;
api.registerSchemaAssociationProvider({
provideSchemaAssociation(document) {
if (/.../.test(document.getText())) {
return vscode.Uri.parse('myExtenionSchema://mySchema');
}
},
});
api.registerUriSchemaProvider('myExtenionSchema', {
provideSchemaContent(uri) {
return mySchemas[uri.path];
},
}); |
BTW, this is very similar to the |
Add support for extensions associating JSON schemas with documents dynamically (via API).
Right now, it is only possible to associate a JSON schema with a text document by:
using schemas directly built-in into VSCode, e.g.,
package.json
,.babelrc
, ...explicitly mapping it in the user settings
explicitly mapping it in the workspace
explicitly mapping it in an extension via a file-match pattern
But all these associations are static and working only on a folder path and filename matching.
I would like to be able to dynamically/programmatically associate a JSON schema with a document within an extension.
E.g., the extension could first inspect the content of the file and check for specific well-known conditions in order to decide, which JSON schema (or if at all) should be associated with a given text document.
E.g., within our organization we have a lot of different file formats with existing JSON schemas, but we don't have canonical/standardized folders and filenames for the files that would need validation against these JSON schemas, with most of them just ending on
*.json
somewhere in the folder tree.I could imagine a mechanism like this to associate a JSON schema with a JSON document
doc
:with a unique
schemaId
provided by the extension andschemaSrc
either beURL
or a local file reference.If the same
schemaId
is provided for adoc
with noschemaSrc
the JSON schema association is removed again from that document.Theoretically
schemaSrc
could also be a JSON schema object (similar to a schema directly defined in the settings), which would allow you to dynamically build up a schema by the extension and provide this for validation.And since a lot of configuration files are not just
JSON
files but alsoYAML
files, some bonus points for allowing the association of JSON schemas also toYAML
files, not justJSON
files.Note, that there is also another related feature request:
Support for contentMatch in addition to fileMatch for extension contributes.jsonValidation
#229696The text was updated successfully, but these errors were encountered: