Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multiple schema deletions #397

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/languageserver/handlers/requestHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Connection } from 'vscode-languageserver';
import { MODIFICATION_ACTIONS, SchemaAdditions, SchemaDeletions } from '../../languageservice/services/yamlSchemaService';
import {
MODIFICATION_ACTIONS,
SchemaAdditions,
SchemaDeletions,
SchemaDeletionsAll,
} from '../../languageservice/services/yamlSchemaService';
import { LanguageService } from '../../languageservice/yamlLanguageService';
import { SchemaModificationNotification } from '../../requestTypes';

Expand All @@ -19,11 +24,15 @@ export class RequestHandlers {
);
}

private registerSchemaModificationNotificationHandler(modifications: SchemaAdditions | SchemaDeletions): void {
private registerSchemaModificationNotificationHandler(
modifications: SchemaAdditions | SchemaDeletions | SchemaDeletionsAll
): void {
if (modifications.action === MODIFICATION_ACTIONS.add) {
this.languageService.modifySchemaContent(modifications);
} else if (modifications.action === MODIFICATION_ACTIONS.delete) {
this.languageService.deleteSchemaContent(modifications);
} else if (modifications.action === MODIFICATION_ACTIONS.deleteAll) {
this.languageService.deleteSchemasWhole(modifications);
}
}
}
15 changes: 15 additions & 0 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export declare type CustomSchemaProvider = (uri: string) => Promise<string | str
export enum MODIFICATION_ACTIONS {
'delete',
'add',
'deleteAll',
}

export interface SchemaAdditions {
Expand All @@ -49,6 +50,11 @@ export interface SchemaDeletions {
key: string;
}

export interface SchemaDeletionsAll {
schemas: string[];
action: MODIFICATION_ACTIONS.deleteAll;
}

export class FilePatternAssociation {
private schemas: string[];
private patternRegExp: RegExp;
Expand Down Expand Up @@ -455,6 +461,15 @@ export class YAMLSchemaService extends JSONSchemaService {
return Promise.resolve(undefined);
}

/**
* Delete schemas on specific path
*/
public async deleteSchemas(deletions: SchemaDeletionsAll): Promise<void> {
deletions.schemas.forEach((s) => {
this.deleteSchema(s);
});
return Promise.resolve(undefined);
}
/**
* Delete a schema with schema ID.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { YAMLSchemaService, CustomSchemaProvider, SchemaAdditions, SchemaDeletions } from './services/yamlSchemaService';
import {
YAMLSchemaService,
CustomSchemaProvider,
SchemaAdditions,
SchemaDeletions,
SchemaDeletionsAll,
} from './services/yamlSchemaService';
import {
Position,
CompletionList,
Expand Down Expand Up @@ -118,6 +124,7 @@ export interface LanguageService {
deleteSchema(schemaID: string): void;
modifySchemaContent(schemaAdditions: SchemaAdditions): void;
deleteSchemaContent(schemaDeletions: SchemaDeletions): void;
deleteSchemasWhole(schemaDeletions: SchemaDeletionsAll): void;
getFoldingRanges(document: TextDocument, context: FoldingRangesContext): FoldingRange[] | null;
getCodeAction(document: TextDocument, params: CodeActionParams): CodeAction[] | undefined;
}
Expand Down Expand Up @@ -180,6 +187,9 @@ export function getLanguageService(
deleteSchemaContent: (schemaDeletions: SchemaDeletions) => {
return schemaService.deleteContent(schemaDeletions);
},
deleteSchemasWhole: (schemaDeletions: SchemaDeletionsAll) => {
return schemaService.deleteSchemas(schemaDeletions);
},
getFoldingRanges,
getCodeAction: (document, params) => {
return yamlCodeActions.getCodeAction(document, params);
Expand Down
17 changes: 17 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,23 @@ describe('JSON Schema', () => {
});
});

it('Deleting schemas', async () => {
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
service.setSchemaContributions({
schemas: {
'https://myschemastore/main/schema1.json': {
type: 'object',
},
},
});
await service.deleteSchemas({
action: MODIFICATION_ACTIONS.deleteAll,
schemas: ['https://myschemastore/main/schema1.json'],
} as SchemaService.SchemaDeletionsAll);
const fs = await service.getResolvedSchema('https://myschemastore/main/schema1.json');
assert.equal(fs, undefined);
});

it('Modifying schema works with kubernetes resolution', async () => {
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
service.registerExternalSchema(KUBERNETES_SCHEMA_URL);
Expand Down