Skip to content

Commit 2b96210

Browse files
gorkemJPinkney
authored andcommitted
use yamlSchemaService
modifies to use yamlSchemaService for add and delete schema
1 parent 8f0da51 commit 2b96210

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/languageservice/apis/schemaModification.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { JSONSchemaService } from '../services/jsonSchemaService';
6-
import { JSONSchema } from '../jsonSchema04';
5+
import { JSONSchema } from '../jsonSchema07';
6+
import { YAMLSchemaService } from '../services/yamlSchemaService';
77

88
export enum MODIFICATION_ACTIONS {
99
'delete',
@@ -31,7 +31,7 @@ export class SchemaModification {
3131
/**
3232
* Add content to a specified schema at a specified path
3333
*/
34-
public async addContent(schemaService: JSONSchemaService, additions: SchemaAdditions) {
34+
public async addContent(schemaService: YAMLSchemaService, additions: SchemaAdditions) {
3535
const schema = await schemaService.getResolvedSchema(additions.schema);
3636
if (schema) {
3737
const resolvedSchemaLocation = this.resolveJSONSchemaToSection(schema.schema, additions.path);
@@ -46,7 +46,7 @@ export class SchemaModification {
4646
/**
4747
* Delete content in a specified schema at a specified path
4848
*/
49-
public async deleteContent(schemaService: JSONSchemaService, deletions: SchemaDeletions) {
49+
public async deleteContent(schemaService: YAMLSchemaService, deletions: SchemaDeletions) {
5050
const schema = await schemaService.getResolvedSchema(deletions.schema);
5151
if (schema) {
5252
const resolvedSchemaLocation = this.resolveJSONSchemaToSection(schema.schema, deletions.path);

src/languageservice/services/yamlSchemaService.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export class FilePatternAssociation {
4545
}
4646

4747
export class YAMLSchemaService extends JSONSchemaService {
48+
// To allow to use schemasById from super.
49+
// tslint:disable-next-line: no-any
50+
[x: string]: any;
4851

4952
private customSchemaProvider: CustomSchemaProvider | undefined;
5053
private filePatternAssociations: FilePatternAssociation[];
@@ -235,6 +238,26 @@ export class YAMLSchemaService extends JSONSchemaService {
235238
return resolveSchema();
236239
}
237240
}
241+
/**
242+
* Save a schema with schema ID and schema content.
243+
* Overrides previous schemas set for that schema ID.
244+
*/
245+
public async saveSchema(schemaId: string, schemaContent: JSONSchema): Promise<void> {
246+
const id = this.normalizeId(schemaId);
247+
this.getOrAddSchemaHandle(id, schemaContent);
248+
return Promise.resolve(undefined);
249+
}
250+
251+
/**
252+
* Delete a schema with schema ID.
253+
*/
254+
public async deleteSchema(schemaId: string): Promise<void> {
255+
const id = this.normalizeId(schemaId);
256+
if ( this.schemasById[id] ) {
257+
delete this.schemasById[id];
258+
}
259+
return Promise.resolve(undefined);
260+
}
238261

239262
/**
240263
* Everything below here is needed because we're importing from vscode-json-languageservice umd and we need

src/languageservice/yamlLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { YAMLSchemaService, CustomSchemaProvider } from './services/yamlSchemaService';
88
import { TextDocument, Position, CompletionList, Diagnostic, Hover, SymbolInformation, DocumentSymbol, CompletionItem, TextEdit } from 'vscode-languageserver-types';
9-
import { JSONSchema } from './jsonSchema04';
9+
import { JSONSchema } from './jsonSchema07';
1010
import { YAMLDocumentSymbols } from './services/documentSymbols';
1111
import { YAMLCompletion } from './services/yamlCompletion';
1212
import { YAMLHover } from './services/yamlHover';

test/schema.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ suite('JSON Schema', () => {
329329
assert.notEqual(schema, undefined);
330330
testDone();
331331
});
332+
});
332333
test('Modifying schema', async () => {
333-
const service = new SchemaService.JSONSchemaService(requestServiceMock, workspaceContext);
334+
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
334335
service.setSchemaContributions({
335336
schemas: {
336337
'https://myschemastore/main/schema1.json': {
@@ -377,7 +378,7 @@ suite('JSON Schema', () => {
377378
});
378379

379380
test('Deleting schema', async () => {
380-
const service = new SchemaService.JSONSchemaService(requestServiceMock, workspaceContext);
381+
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
381382
service.setSchemaContributions({
382383
schemas: {
383384
'https://myschemastore/main/schema1.json': {
@@ -421,7 +422,7 @@ suite('JSON Schema', () => {
421422
});
422423

423424
test('Modifying schema works with kubernetes resolution', async () => {
424-
const service = new SchemaService.JSONSchemaService(schemaRequestServiceForURL, workspaceContext);
425+
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
425426
service.registerExternalSchema(KUBERNETES_SCHEMA_URL);
426427

427428
const schemaModification = new SchemaModification();
@@ -441,7 +442,7 @@ suite('JSON Schema', () => {
441442
});
442443

443444
test('Deleting schema works with Kubernetes resolution', async () => {
444-
const service = new SchemaService.JSONSchemaService(schemaRequestServiceForURL, workspaceContext);
445+
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
445446
service.registerExternalSchema(KUBERNETES_SCHEMA_URL);
446447

447448
const schemaModification = new SchemaModification();
@@ -457,7 +458,7 @@ suite('JSON Schema', () => {
457458
});
458459

459460
test('Adding a brand new schema', async () => {
460-
const service = new SchemaService.JSONSchemaService(schemaRequestServiceForURL, workspaceContext);
461+
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
461462
service.saveSchema('hello_world', {
462463
enum: [
463464
'test1',
@@ -470,7 +471,7 @@ suite('JSON Schema', () => {
470471
});
471472

472473
test('Deleting an existing schema', async () => {
473-
const service = new SchemaService.JSONSchemaService(schemaRequestServiceForURL, workspaceContext);
474+
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
474475
service.saveSchema('hello_world', {
475476
enum: [
476477
'test1',

0 commit comments

Comments
 (0)