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

add a 'json.validate' command #244784

Merged
merged 1 commit into from
Mar 26, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extensions/json-language-features/client/src/jsonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from 'vscode';
import {
LanguageClientOptions, RequestType, NotificationType, FormattingOptions as LSPFormattingOptions, DocumentDiagnosticReportKind,
Diagnostic as LSPDiagnostic,
DidChangeConfigurationNotification, HandleDiagnosticsSignature, ResponseError, DocumentRangeFormattingParams,
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, ProvideHoverSignature, BaseLanguageClient, ProvideFoldingRangeSignature, ProvideDocumentSymbolsSignature, ProvideDocumentColorsSignature
} from 'vscode-languageclient';
Expand All @@ -38,6 +39,9 @@ namespace LanguageStatusRequest {
export const type: RequestType<string, JSONLanguageStatus, any> = new RequestType('json/languageStatus');
}

namespace ValidateContentRequest {
export const type: RequestType<{ schemaUri: string; content: string }, LSPDiagnostic[], any> = new RequestType('json/validateContent');
}
interface SortOptions extends LSPFormattingOptions {
}

Expand Down Expand Up @@ -211,6 +215,10 @@ async function startClientWithParticipants(_context: ExtensionContext, languageP
window.showInformationMessage(l10n.t('JSON schema cache cleared.'));
}));

toDispose.push(commands.registerCommand('json.validate', async (schemaUri: Uri, content: string) => {
const diagnostics: LSPDiagnostic[] = await client.sendRequest(ValidateContentRequest.type, { schemaUri: schemaUri.toString(), content });
return diagnostics.map(client.protocol2CodeConverter.asDiagnostic);
}));

toDispose.push(commands.registerCommand('json.sort', async () => {

Expand Down Expand Up @@ -767,3 +775,5 @@ function updateMarkdownString(h: MarkdownString): MarkdownString {
function isSchemaResolveError(d: Diagnostic) {
return d.code === /* SchemaResolveError */ 0x300;
}


3 changes: 2 additions & 1 deletion extensions/json-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"activationEvents": [
"onLanguage:json",
"onLanguage:jsonc",
"onLanguage:snippets"
"onLanguage:snippets",
"onCommand:json.validate"
],
"main": "./client/out/node/jsonClientMain",
"browser": "./client/dist/browser/jsonClientMain",
Expand Down
22 changes: 21 additions & 1 deletion extensions/json-language-features/server/src/jsonServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ namespace LanguageStatusRequest {
export const type: RequestType<string, JSONLanguageStatus, any> = new RequestType('json/languageStatus');
}

namespace ValidateContentRequest {
export const type: RequestType<{ schemaUri: string; content: string }, Diagnostic[], any> = new RequestType('json/validateContent');
}

export interface DocumentSortingParams {
/**
* The uri of the document to sort.
Expand Down Expand Up @@ -299,6 +303,14 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
return [];
});

connection.onRequest(ValidateContentRequest.type, async ({ schemaUri, content }) => {
const docURI = 'vscode://schemas/temp/' + new Date().getTime();
const document = TextDocument.create(docURI, 'json', 1, content);
updateConfiguration([{ uri: schemaUri, fileMatch: [docURI] }]);
return await validateTextDocument(document);
});


connection.onRequest(LanguageStatusRequest.type, async uri => {
const document = documents.get(uri);
if (document) {
Expand All @@ -319,7 +331,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
return [];
});

function updateConfiguration() {
function updateConfiguration(extraSchemas?: SchemaConfiguration[]) {
const languageSettings = {
validate: validateEnabled,
allowComments: true,
Expand Down Expand Up @@ -350,6 +362,10 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
}
});
}
if (extraSchemas) {
languageSettings.schemas.push(...extraSchemas);
}

languageService.configure(languageSettings);

diagnosticsSupport?.requestRefresh();
Expand Down Expand Up @@ -529,3 +545,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
function getFullRange(document: TextDocument): Range {
return Range.create(Position.create(0, 0), document.positionAt(document.getText().length));
}




Loading