Refactor SchemaRetryNotification to ForceValidateRequest

Now returns the new diagnostics.

Also, actually refresh the schemas instead of just revalidating the
documents. It worked only sometimes before.
This commit is contained in:
Literallie
2018-10-11 22:06:48 +02:00
parent cb8d482429
commit 54f4967761
2 changed files with 27 additions and 16 deletions
@@ -22,8 +22,8 @@ namespace SchemaContentChangeNotification {
export const type: NotificationType<string, any> = new NotificationType('json/schemaContent'); export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
} }
namespace SchemaRetryNotification { namespace ForceValidateRequest {
export const type: NotificationType<string, any> = new NotificationType('json/schemaRetry'); export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
} }
export interface ISchemaAssociations { export interface ISchemaAssociations {
@@ -171,7 +171,7 @@ export function activate(context: ExtensionContext) {
let handleRetrySchemaCommand = () => { let handleRetrySchemaCommand = () => {
if (window.activeTextEditor) { if (window.activeTextEditor) {
client.sendNotification(SchemaRetryNotification.type, window.activeTextEditor.document.uri.toString()); client.sendRequest(ForceValidateRequest.type, window.activeTextEditor.document.uri.toString());
statusBarItem.text = '$(watch) JSON'; statusBarItem.text = '$(watch) JSON';
} }
}; };
@@ -6,7 +6,7 @@
import { import {
createConnection, IConnection, createConnection, IConnection,
TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType, TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType,
DocumentRangeFormattingRequest, Disposable, ServerCapabilities DocumentRangeFormattingRequest, Disposable, ServerCapabilities, Diagnostic
} from 'vscode-languageserver'; } from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light'; import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
@@ -34,8 +34,8 @@ namespace SchemaContentChangeNotification {
export const type: NotificationType<string, any> = new NotificationType('json/schemaContent'); export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
} }
namespace SchemaRetryNotification { namespace ForceValidateRequest {
export const type: NotificationType<string, any> = new NotificationType('json/schemaRetry'); export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
} }
// Create a connection for the server // Create a connection for the server
@@ -212,11 +212,18 @@ connection.onNotification(SchemaContentChangeNotification.type, uri => {
}); });
// Retry schema validation on all open documents // Retry schema validation on all open documents
connection.onNotification(SchemaRetryNotification.type, uri => { connection.onRequest(ForceValidateRequest.type, uri => {
const document = documents.get(uri); return new Promise<Diagnostic[]>(resolve => {
if (document) { const document = documents.get(uri);
triggerValidation(document); if (document) {
} updateConfiguration();
validateTextDocument(document, diagnostics => {
resolve(diagnostics);
});
} else {
resolve([]);
}
});
}); });
function updateConfiguration() { function updateConfiguration() {
@@ -283,10 +290,15 @@ function triggerValidation(textDocument: TextDocument): void {
}, validationDelayMs); }, validationDelayMs);
} }
function validateTextDocument(textDocument: TextDocument): void { function validateTextDocument(textDocument: TextDocument, callback?: (diagnostics: Diagnostic[]) => void): void {
const respond = (diagnostics: Diagnostic[]) => {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
if (callback) {
callback(diagnostics);
}
};
if (textDocument.getText().length === 0) { if (textDocument.getText().length === 0) {
// ignore empty documents respond([]); // ignore empty documents
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
return; return;
} }
const jsonDocument = getJSONDocument(textDocument); const jsonDocument = getJSONDocument(textDocument);
@@ -297,8 +309,7 @@ function validateTextDocument(textDocument: TextDocument): void {
setTimeout(() => { setTimeout(() => {
const currDocument = documents.get(textDocument.uri); const currDocument = documents.get(textDocument.uri);
if (currDocument && currDocument.version === version) { if (currDocument && currDocument.version === version) {
// Send the computed diagnostics to VSCode. respond(diagnostics); // Send the computed diagnostics to VSCode.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
} }
}, 100); }, 100);
}, error => { }, error => {