Skip to content

Commit

Permalink
Added registration for custom schema provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Sep 26, 2018
1 parent 74f640a commit 0e4192c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/languageservice/services/jsonSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ export class JSONSchemaService implements IJSONSchemaService {
private callOnDispose: Function[];
private requestService: SchemaRequestService;
private promiseConstructor: PromiseConstructor;
private customSchemaProvider: CustomSchemaProvider;
private customSchemaProvider: CustomSchemaProvider | undefined;

constructor(requestService: SchemaRequestService, contextService?: WorkspaceContextService, customSchemaProvider?: CustomSchemaProvider, promiseConstructor?: PromiseConstructor) {
constructor(requestService: SchemaRequestService, contextService?: WorkspaceContextService, promiseConstructor?: PromiseConstructor) {
this.contextService = contextService;
this.requestService = requestService;
this.promiseConstructor = promiseConstructor || Promise;
this.callOnDispose = [];
this.customSchemaProvider = customSchemaProvider;
this.customSchemaProvider = undefined;
this.contributionSchemas = {};
this.contributionAssociations = {};
this.schemasById = {};
Expand All @@ -259,6 +259,10 @@ export class JSONSchemaService implements IJSONSchemaService {
this.registeredSchemasIds = {};
}

registerCustomSchemaProvider(customSchemaProvider: CustomSchemaProvider) {
this.customSchemaProvider = customSchemaProvider;
}

public getRegisteredSchemaIds(filter?: (scheme) => boolean): string[] {
return Object.keys(this.registeredSchemasIds).filter(id => {
let scheme = URI.parse(id).scheme;
Expand Down
10 changes: 7 additions & 3 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { JSONSchemaService } from './services/jsonSchemaService'
import { JSONSchemaService, CustomSchemaProvider } from './services/jsonSchemaService'
import { TextDocument, Position, CompletionList, FormattingOptions, Diagnostic } from 'vscode-languageserver-types';
import { JSONSchema } from './jsonSchema';
import { YAMLDocumentSymbols } from './services/documentSymbols';
Expand Down Expand Up @@ -92,6 +92,7 @@ export interface SchemaConfiguration {

export interface LanguageService {
configure(settings): void;
registerCustomSchemaProvider(schemaProvider: CustomSchemaProvider): void; // Register a custom schema provider
doComplete(document: TextDocument, position: Position, doc): Thenable<CompletionList>;
doValidation(document: TextDocument, yamlDocument): Thenable<Diagnostic[]>;
doHover(document: TextDocument, position: Position, doc);
Expand All @@ -101,10 +102,10 @@ export interface LanguageService {
doFormat(document: TextDocument, options: FormattingOptions, customTags: Array<String>);
}

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

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

let completer = new YAMLCompletion(schemaService, contributions, promise);
let hover = new YAMLHover(schemaService, contributions, promise);
Expand All @@ -123,6 +124,9 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr
let customTagsSetting = settings && settings["customTags"] ? settings["customTags"] : [];
completer.configure(customTagsSetting);
},
registerCustomSchemaProvider: (schemaProvider: CustomSchemaProvider) => {
schemaService.registerCustomSchemaProvider(schemaProvider);
},
doComplete: completer.doComplete.bind(completer),
doResolve: completer.doResolve.bind(completer),
doValidation: yamlValidation.doValidation.bind(yamlValidation),
Expand Down
14 changes: 11 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Strings = require('./languageservice/utils/strings');
import { getLineOffsets, removeDuplicatesObj } from './languageservice/utils/arrUtils';
import { getLanguageService as getCustomLanguageService, LanguageSettings } from './languageservice/yamlLanguageService';
import * as nls from 'vscode-nls';
import { FilePatternAssociation } from './languageservice/services/jsonSchemaService';
import { FilePatternAssociation, CustomSchemaProvider } from './languageservice/services/jsonSchemaService';
import { parse as parseYAML } from './languageservice/parser/yamlParser';
import { JSONDocument } from './languageservice/parser/jsonParser';
import { JSONSchema } from './languageservice/jsonSchema';
Expand All @@ -36,6 +36,10 @@ namespace SchemaAssociationNotification {
export const type: NotificationType<{}, {}> = new NotificationType('json/schemaAssociations');
}

namespace DynamicCustomSchemaRequestRegistration {
export const type: NotificationType<{}, {}> = new NotificationType('yaml/registerCustomSchemaRequest');
}

namespace VSCodeContentRequest {
export const type: RequestType<{}, {}, {}, {}> = new RequestType('vscode/content');
}
Expand Down Expand Up @@ -167,8 +171,7 @@ let schemaRequestService = (uri: string): Thenable<string> => {

export let KUBERNETES_SCHEMA_URL = "https://gist.githubusercontent.com/JPinkney/ccaf3909ef811e5657ca2e2e1fa05d76/raw/f85e51bfb67fdb99ab7653c2953b60087cc871ea/openshift_schema_all.json";
export let KEDGE_SCHEMA_URL = "https://raw.githubusercontent.com/kedgeproject/json-schema/master/master/kedge-json-schema.json";
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, [],
(resource) => connection.sendRequest(CustomSchemaRequest.type, resource));
export let customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext, []);

// The settings interface describes the server relevant settings part
interface Settings {
Expand Down Expand Up @@ -287,6 +290,11 @@ connection.onNotification(SchemaAssociationNotification.type, associations => {
updateConfiguration();
});

connection.onNotification(DynamicCustomSchemaRequestRegistration.type, () => {
const schemaProvider = ((resource) => connection.sendRequest(CustomSchemaRequest.type, resource)) as CustomSchemaProvider;
customLanguageService.registerCustomSchemaProvider(schemaProvider);
});

function updateConfiguration() {
let languageSettings: LanguageSettings = {
validate: yamlShouldValidate,
Expand Down

0 comments on commit 0e4192c

Please sign in to comment.