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
No known key found for this signature in database
GPG key ID: 7BE463C902ED152C
2 changed files with 27 additions and 16 deletions

View file

@ -22,8 +22,8 @@ namespace SchemaContentChangeNotification {
export const type: NotificationType<string, any> = new NotificationType('json/schemaContent');
}
namespace SchemaRetryNotification {
export const type: NotificationType<string, any> = new NotificationType('json/schemaRetry');
namespace ForceValidateRequest {
export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
}
export interface ISchemaAssociations {
@ -171,7 +171,7 @@ export function activate(context: ExtensionContext) {
let handleRetrySchemaCommand = () => {
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';
}
};

View file

@ -6,7 +6,7 @@
import {
createConnection, IConnection,
TextDocuments, TextDocument, InitializeParams, InitializeResult, NotificationType, RequestType,
DocumentRangeFormattingRequest, Disposable, ServerCapabilities
DocumentRangeFormattingRequest, Disposable, ServerCapabilities, Diagnostic
} from 'vscode-languageserver';
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');
}
namespace SchemaRetryNotification {
export const type: NotificationType<string, any> = new NotificationType('json/schemaRetry');
namespace ForceValidateRequest {
export const type: RequestType<string, Diagnostic[], any, any> = new RequestType('json/validate');
}
// Create a connection for the server
@ -212,11 +212,18 @@ connection.onNotification(SchemaContentChangeNotification.type, uri => {
});
// Retry schema validation on all open documents
connection.onNotification(SchemaRetryNotification.type, uri => {
const document = documents.get(uri);
if (document) {
triggerValidation(document);
}
connection.onRequest(ForceValidateRequest.type, uri => {
return new Promise<Diagnostic[]>(resolve => {
const document = documents.get(uri);
if (document) {
updateConfiguration();
validateTextDocument(document, diagnostics => {
resolve(diagnostics);
});
} else {
resolve([]);
}
});
});
function updateConfiguration() {
@ -283,10 +290,15 @@ function triggerValidation(textDocument: TextDocument): void {
}, 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) {
// ignore empty documents
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
respond([]); // ignore empty documents
return;
}
const jsonDocument = getJSONDocument(textDocument);
@ -297,8 +309,7 @@ function validateTextDocument(textDocument: TextDocument): void {
setTimeout(() => {
const currDocument = documents.get(textDocument.uri);
if (currDocument && currDocument.version === version) {
// Send the computed diagnostics to VSCode.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
respond(diagnostics); // Send the computed diagnostics to VSCode.
}
}, 100);
}, error => {