Skip to content

Commit

Permalink
Custom schema provider feature
Browse files Browse the repository at this point in the history
  • Loading branch information
andxu committed Jan 18, 2018
1 parent 02d73ff commit eeb8987
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
32 changes: 23 additions & 9 deletions src/languageService/services/jsonSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface ISchemaContributions {
schemaAssociations?: ISchemaAssociations;
}

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

export interface ISchemaHandle {
/**
* The schema id
Expand Down Expand Up @@ -219,13 +221,14 @@ export class JSONSchemaService implements IJSONSchemaService {
private callOnDispose: Function[];
private requestService: SchemaRequestService;
private promiseConstructor: PromiseConstructor;
private customSchemaProvider: CustomSchemaProvider;

constructor(requestService: SchemaRequestService, contextService?: WorkspaceContextService, promiseConstructor?: PromiseConstructor) {
constructor(requestService: SchemaRequestService, contextService?: WorkspaceContextService, customSchemaProvider?: CustomSchemaProvider, promiseConstructor?: PromiseConstructor) {
this.contextService = contextService;
this.requestService = requestService;
this.promiseConstructor = promiseConstructor || Promise;
this.callOnDispose = [];

this.customSchemaProvider = customSchemaProvider;
this.contributionSchemas = {};
this.contributionAssociations = {};
this.schemasById = {};
Expand Down Expand Up @@ -485,15 +488,26 @@ export class JSONSchemaService implements IJSONSchemaService {
}

public getSchemaForResource(resource: string ): Thenable<ResolvedSchema> {

// check for matching file names, last to first
for (let i = this.filePatternAssociations.length - 1; i >= 0; i--) {
let entry = this.filePatternAssociations[i];
if (entry.matchesPattern(resource)) {
return entry.getCombinedSchema(this).getResolvedSchema();
const resolveSchema = () => {
// check for matching file names, last to first
for (let i = this.filePatternAssociations.length - 1; i >= 0; i--) {
let entry = this.filePatternAssociations[i];
if (entry.matchesPattern(resource)) {
return entry.getCombinedSchema(this).getResolvedSchema();
}
}
return null;
};
if (this.customSchemaProvider) {
this.customSchemaProvider(resource).then(schemaUri => {
return this.loadSchema(schemaUri).then(unsolvedSchema => this.resolveSchemaContent(unsolvedSchema, schemaUri));
}).then(schema => schema, err => {
console.log('Fail to provide custom schemas.', err);
return resolveSchema();
});
} else {
return resolveSchema();
}
return this.promise.resolve(null);
}

public createCombinedSchema(combinedSchemaId: string, schemaIds: string[]): ISchemaHandle {
Expand Down
4 changes: 2 additions & 2 deletions src/languageService/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export interface LanguageService {
resetSchema(uri: string): boolean;
}

export function getLanguageService(schemaRequestService, workspaceContext, contributions, promiseConstructor?): LanguageService {
export function getLanguageService(schemaRequestService, workspaceContext, contributions, customSchemaProvider, promiseConstructor?): LanguageService {
let promise = promiseConstructor || Promise;

let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext);
let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext, customSchemaProvider);

let completer = new YAMLCompletion(schemaService, contributions, promise);
let hover = new YAMLHover(schemaService, contributions, promise);
Expand Down
17 changes: 16 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ namespace VSCodeContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('vscode/content');
}

namespace CustomSchemaContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('custom/schema/content');
}

namespace CustomSchemaRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('custom/schema/request');
}

namespace ColorSymbolRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('json/colorSymbols');
}
Expand Down Expand Up @@ -135,6 +143,12 @@ let schemaRequestService = (uri: string): Thenable<string> => {
}, error => {
return error.message;
});
} else {
let scheme = URI.parse(uri).scheme.toLowerCase();
if (scheme !== 'http' && scheme !== 'https') {
// custom scheme
return connection.sendRequest(CustomSchemaContentRequest.type, uri);
}
}
if (uri.indexOf('//schema.management.azure.com/') !== -1) {
connection.telemetry.logEvent({
Expand All @@ -160,7 +174,8 @@ export let languageService = getLanguageService({
});

export let KUBERNETES_SCHEMA_URL = "http://central.maven.org/maven2/io/fabric8/kubernetes-model/2.0.0/kubernetes-model-2.0.0-schema.json";
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, []);
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, [],
(resource) => connection.sendRequest(CustomSchemaRequest.type, resource));

// The settings interface describes the server relevant settings part
interface Settings {
Expand Down

1 comment on commit eeb8987

@hurricanehrndz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, I'm not well versed in javascript so please forgive me for some of my ignorance. Anyhow, schemas can no longer be resolved for LanguageClients that do not respond "custom/schema/request" is there anyway this can be addressed?

Please sign in to comment.