Skip to content

Commit

Permalink
Merge pull request #675 from evidolob/support-versions-from-store
Browse files Browse the repository at this point in the history
Support JSON Schema versions from schemastore
  • Loading branch information
msivasubramaniaan authored Mar 16, 2022
2 parents 1f5c37b + f68e240 commit 360c038
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/languageserver/handlers/schemaSelectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class JSONSchemaSelection {
name: val[1].title,
uri: val[0],
description: val[1].description,
versions: val[1].versions,
};
});

Expand Down Expand Up @@ -71,6 +72,7 @@ export class JSONSchemaSelection {
usedForCurrentFile: true,
name: val[1].title,
description: val[1].description,
versions: val[1].versions,
};
});
const result = [];
Expand Down
1 change: 1 addition & 0 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export class SettingsHandler {
priority: SchemaPriority.SchemaStore,
name: schema.name,
description: schema.description,
versions: schema.versions,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/languageservice/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { CompletionItemKind } from 'vscode-json-languageservice';
import { SchemaVersions } from './yamlTypes';

export type JSONSchemaRef = JSONSchema | boolean;

Expand All @@ -15,6 +16,7 @@ export interface JSONSchema {
type?: string | string[];
title?: string;
closestTitle?: string;
versions?: SchemaVersions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default?: any;
definitions?: { [name: string]: JSONSchema };
Expand Down
20 changes: 14 additions & 6 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { parse } from 'yaml';
import * as path from 'path';
import { getSchemaFromModeline } from './modelineUtil';
import { JSONSchemaDescriptionExt } from '../../requestTypes';
import { SchemaVersions } from '../yamlTypes';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -84,7 +85,11 @@ export class FilePatternAssociation {
return this.schemas;
}
}

interface SchemaStoreSchema {
name: string;
description: string;
versions?: SchemaVersions;
}
export class YAMLSchemaService extends JSONSchemaService {
// To allow to use schemasById from super.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -96,7 +101,7 @@ export class YAMLSchemaService extends JSONSchemaService {
private requestService: SchemaRequestService;
public schemaPriorityMapping: Map<string, Set<SchemaPriority>>;

private schemaUriToNameAndDescription = new Map<string, [string, string]>();
private schemaUriToNameAndDescription = new Map<string, SchemaStoreSchema>();

constructor(
requestService: SchemaRequestService,
Expand Down Expand Up @@ -129,10 +134,11 @@ export class YAMLSchemaService extends JSONSchemaService {
};

if (this.schemaUriToNameAndDescription.has(schemaUri)) {
const [name, description] = this.schemaUriToNameAndDescription.get(schemaUri);
const { name, description, versions } = this.schemaUriToNameAndDescription.get(schemaUri);
schemaHandle.name = name;
schemaHandle.description = description;
schemaHandle.fromStore = true;
schemaHandle.versions = versions;
}
result.push(schemaHandle);
}
Expand Down Expand Up @@ -653,9 +659,10 @@ export class YAMLSchemaService extends JSONSchemaService {
}
unresolvedJsonSchema.uri = schemaUri;
if (this.schemaUriToNameAndDescription.has(schemaUri)) {
const [name, description] = this.schemaUriToNameAndDescription.get(schemaUri);
const { name, description, versions } = this.schemaUriToNameAndDescription.get(schemaUri);
unresolvedJsonSchema.schema.title = name ?? unresolvedJsonSchema.schema.title;
unresolvedJsonSchema.schema.description = description ?? unresolvedJsonSchema.schema.description;
unresolvedJsonSchema.schema.versions = versions ?? unresolvedJsonSchema.schema.versions;
}
return unresolvedJsonSchema;
});
Expand All @@ -666,10 +673,11 @@ export class YAMLSchemaService extends JSONSchemaService {
filePatterns?: string[],
unresolvedSchema?: JSONSchema,
name?: string,
description?: string
description?: string,
versions?: SchemaVersions
): SchemaHandle {
if (name || description) {
this.schemaUriToNameAndDescription.set(uri, [name, description]);
this.schemaUriToNameAndDescription.set(uri, { name, description, versions });
}
return super.registerExternalSchema(uri, filePatterns, unresolvedSchema);
}
Expand Down
6 changes: 4 additions & 2 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
} from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { getFoldingRanges } from './services/yamlFolding';
import { FoldingRangesContext } from './yamlTypes';
import { FoldingRangesContext, SchemaVersions } from './yamlTypes';
import { YamlCodeActions } from './services/yamlCodeActions';
import { commandExecutor } from '../languageserver/commandExecutor';
import { doDocumentOnTypeFormatting } from './services/yamlOnTypeFormatting';
Expand Down Expand Up @@ -70,6 +70,7 @@ export interface SchemasSettings {
uri: string;
name?: string;
description?: string;
versions?: SchemaVersions;
}

export interface LanguageSettings {
Expand Down Expand Up @@ -198,7 +199,8 @@ export function getLanguageService(
settings.fileMatch,
settings.schema,
settings.name,
settings.description
settings.description,
settings.versions
);
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/languageservice/yamlTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export interface FoldingRangesContext {
*/
lineFoldingOnly?: boolean;
}

export type SchemaVersions = { [version: string]: string };
3 changes: 3 additions & 0 deletions src/requestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { NotificationType, RequestType } from 'vscode-languageserver';
import { SchemaAdditions, SchemaDeletions } from './languageservice/services/yamlSchemaService';
import { SchemaConfiguration } from './languageservice/yamlLanguageService';
import { SchemaVersions } from './languageservice/yamlTypes';

export type ISchemaAssociations = Record<string, string[]>;

Expand Down Expand Up @@ -30,6 +31,8 @@ export interface JSONSchemaDescriptionExt extends JSONSchemaDescription {
* Is schema from schema store
*/
fromStore: boolean;

versions?: SchemaVersions;
}

export namespace SchemaAssociationNotification {
Expand Down
7 changes: 5 additions & 2 deletions test/schemaSelectionHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('Schema Selection Handlers', () => {
usedForCurrentFile: false,
name: 'Schema name',
description: 'Schema description',
versions: undefined,
});
});

Expand All @@ -72,10 +73,11 @@ describe('Schema Selection Handlers', () => {
expect(result).length(1);
expect(result[0]).to.be.eqls({
uri: 'https://some.com/some.json',
fromStore: false,
usedForCurrentFile: true,
name: 'Schema name',
description: 'Schema description',
fromStore: false,
usedForCurrentFile: true,
versions: undefined,
});
});

Expand All @@ -94,6 +96,7 @@ describe('Schema Selection Handlers', () => {
uri: 'https://some.com/some.json',
name: 'Schema name',
description: 'Schema description',
versions: undefined,
});
});

Expand Down
1 change: 1 addition & 0 deletions test/settingsHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('Settings Handlers Tests', () => {
priority: SchemaPriority.SchemaStore,
name: '.adonisrc.json',
description: 'AdonisJS configuration file',
versions: undefined,
});
});

Expand Down

0 comments on commit 360c038

Please sign in to comment.