Skip to content

Commit

Permalink
Merge pull request #227 from redhat-developer/multiple-custom-schemas
Browse files Browse the repository at this point in the history
Made yamlSchemaService accept multiple uris from requestCustomSchema
  • Loading branch information
JPinkney authored Jan 27, 2020
2 parents 3d79d59 + 1def887 commit d1e652e
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as nls from 'vscode-nls';
import { convertSimple2RegExpPattern } from '../utils/strings';
const localize = nls.loadMessageBundle();

export declare type CustomSchemaProvider = (uri: string) => Thenable<string>;
export declare type CustomSchemaProvider = (uri: string) => Thenable<string | string[]>;

export enum MODIFICATION_ACTIONS {
'delete',
Expand Down Expand Up @@ -242,24 +242,43 @@ export class YAMLSchemaService extends JSONSchemaService {
if (this.customSchemaProvider) {
return this.customSchemaProvider(resource)
.then(schemaUri => {
if (!schemaUri) {
return resolveSchema();
}

return this.loadSchema(schemaUri)
.then(unsolvedSchema => this.resolveSchemaContent(unsolvedSchema, schemaUri, []).then(schema => {
if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[doc.currentDocIndex]) {
return new ResolvedSchema(schema.schema.schemaSequence[doc.currentDocIndex]);

if (Array.isArray(schemaUri)) {
if (schemaUri.length === 0) {
return resolveSchema();
}
return schema;
}));
return Promise.all(schemaUri.map(schemaUri => this.resolveCustomSchema(schemaUri, doc))).then(schemas =>
({
'errors': [],
'schema': {
'oneOf': schemas.map(schemaObj => schemaObj.schema)
}
})
, err => resolveSchema());

}

if (!schemaUri) {
return resolveSchema();
}

return this.resolveCustomSchema(schemaUri, doc);
})
.then(schema => schema, err => resolveSchema());
} else {
return resolveSchema();
}
}

private async resolveCustomSchema(schemaUri, doc) {
const unresolvedSchema = await this.loadSchema(schemaUri);
const schema = await this.resolveSchemaContent(unresolvedSchema, schemaUri, []);
if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[doc.currentDocIndex]) {
return new ResolvedSchema(schema.schema.schemaSequence[doc.currentDocIndex]);
}
return schema;
}

/**
* Save a schema with schema ID and schema content.
* Overrides previous schemas set for that schema ID.
Expand Down

0 comments on commit d1e652e

Please sign in to comment.