Skip to content

Commit

Permalink
Merge pull request #94 from redhat-developer/register-schema-provider
Browse files Browse the repository at this point in the history
Added registration for custom schema provider
  • Loading branch information
JPinkney committed Oct 1, 2018
2 parents d13a903 + 619438d commit c82830b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 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
15 changes: 10 additions & 5 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* 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 { TextDocument, Position, CompletionList, Diagnostic } from 'vscode-languageserver-types';

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';
import { YAMLCompletion } from "./services/yamlCompletion";
Expand Down Expand Up @@ -100,7 +101,8 @@ export interface CustomFormatterOptions {

export interface LanguageService {
configure(settings): void;
doComplete(document: TextDocument, position: Position, doc): Thenable<CompletionList>;
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);
findDocumentSymbols(document: TextDocument, doc);
Expand All @@ -109,10 +111,10 @@ export interface LanguageService {
doFormat(document: TextDocument, options: CustomFormatterOptions);
}

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 @@ -132,6 +134,9 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr
let customTagsSetting = settings && settings["customTags"] ? settings["customTags"] : [];
completer.configure(settings, 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, CustomFormatterOptions } 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 @@ -305,6 +308,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 c82830b

Please sign in to comment.