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

making the diagnostics computation async #116 #169

Merged
merged 3 commits into from
Nov 7, 2018
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<camel.version>2.22.1</camel.version>
<camel.tooling.common.version>0.0.3-SNAPSHOT</camel.tooling.common.version>
<roaster.version>2.20.1.Final</roaster.version>
<awaitility.version>3.1.2</awaitility.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -296,6 +297,12 @@
<artifactId>roaster-jdt</artifactId>
<version>${roaster.version}</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void didChange(DidChangeTextDocumentParams params) {
TextDocumentItem textDocumentItem = openedDocuments.get(params.getTextDocument().getUri());
if (!contentChanges.isEmpty()) {
textDocumentItem.setText(contentChanges.get(0).getText());
new DiagnosticService(camelCatalog, camelLanguageServer).compute(params);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.github.cameltooling.lsp.internal.diagnostic;

import java.util.List;
import java.util.Map;

import org.apache.camel.catalog.EndpointValidationResult;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.PublishDiagnosticsParams;

import com.github.cameltooling.lsp.internal.CamelEndpointDetailsWrapper;
import com.github.cameltooling.lsp.internal.CamelLanguageServer;

/**
* @author lheinema
*/
public class DiagnosticRunner {

private DiagnosticService diagnosticServer;
private CamelLanguageServer camelLanguageServer;
private String camelText;
private String uri;

public DiagnosticRunner(DiagnosticService diagnosticServer, CamelLanguageServer camelLanguageServer, String camelText, String uri) {
this.diagnosticServer = diagnosticServer;
this.camelLanguageServer = camelLanguageServer;
this.camelText = camelText;
this.uri = uri;
calculate();
}

private void calculate() {
Map<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointErrors = diagnosticServer.computeCamelErrors(camelText, uri);
List<Diagnostic> diagnostics = diagnosticServer.converToLSPDiagnostics(camelText, endpointErrors, camelLanguageServer.getTextDocumentService().getOpenedDocument(uri));
camelLanguageServer.getClient().publishDiagnostics(new PublishDiagnosticsParams(uri, diagnostics));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.apache.camel.parser.model.CamelEndpointDetails;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentItem;
import org.jboss.forge.roaster.Roaster;
Expand All @@ -65,6 +65,7 @@ public class DiagnosticService {

private CompletableFuture<CamelCatalog> camelCatalog;
private CamelLanguageServer camelLanguageServer;
private CompletableFuture<DiagnosticRunner> diagnostics;

public DiagnosticService(CompletableFuture<CamelCatalog> camelCatalog, CamelLanguageServer camelLanguageServer) {
this.camelCatalog = camelCatalog;
Expand All @@ -73,15 +74,24 @@ public DiagnosticService(CompletableFuture<CamelCatalog> camelCatalog, CamelLang

public void compute(DidSaveTextDocumentParams params) {
String camelText = retrieveFullText(params);
Map<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointErrors = computeCamelErrors(camelText, params);
List<Diagnostic> diagnostics = converToLSPDiagnostics(camelText, endpointErrors, camelLanguageServer.getTextDocumentService().getOpenedDocument(params.getTextDocument().getUri()));
PublishDiagnosticsParams diagnosticParam = new PublishDiagnosticsParams(params.getTextDocument().getUri(), diagnostics);
camelLanguageServer.getClient().publishDiagnostics(diagnosticParam);
computeDiagnostics(camelText, params.getTextDocument().getUri());
}

public void compute(DidChangeTextDocumentParams params) {
String camelText = params.getContentChanges().get(0).getText();
computeDiagnostics(camelText, params.getTextDocument().getUri());
}

private Map<CamelEndpointDetailsWrapper, EndpointValidationResult> computeCamelErrors(String camelText, DidSaveTextDocumentParams params) {
List<CamelEndpointDetails> endpoints = retrieveEndpoints(params, camelText);
return diagnoseEndpoints(params, endpoints);
private void computeDiagnostics(String camelText, String uri) {
if (diagnostics != null) {
diagnostics.cancel(true);
}
diagnostics = CompletableFuture.supplyAsync(() -> new DiagnosticRunner(this, camelLanguageServer, camelText, uri));
}

Map<CamelEndpointDetailsWrapper, EndpointValidationResult> computeCamelErrors(String camelText, String uri) {
List<CamelEndpointDetails> endpoints = retrieveEndpoints(uri, camelText);
return diagnoseEndpoints(uri, endpoints);
}

private String retrieveFullText(DidSaveTextDocumentParams params) {
Expand All @@ -92,7 +102,7 @@ private String retrieveFullText(DidSaveTextDocumentParams params) {
return camelText;
}

private Map<CamelEndpointDetailsWrapper, EndpointValidationResult> diagnoseEndpoints(DidSaveTextDocumentParams params, List<CamelEndpointDetails> endpoints) {
private Map<CamelEndpointDetailsWrapper, EndpointValidationResult> diagnoseEndpoints(String uri, List<CamelEndpointDetails> endpoints) {
Map<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointErrors = new HashMap<>();
try {
CamelCatalog camelCatalogResolved = camelCatalog.get();
Expand All @@ -104,21 +114,20 @@ private Map<CamelEndpointDetailsWrapper, EndpointValidationResult> diagnoseEndpo
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logExceptionValidatingDocument(params, e);
logExceptionValidatingDocument(uri, e);
} catch (ExecutionException e) {
logExceptionValidatingDocument(params, e);
logExceptionValidatingDocument(uri, e);
}
return endpointErrors;
}

private List<CamelEndpointDetails> retrieveEndpoints(DidSaveTextDocumentParams params, String camelText) {
private List<CamelEndpointDetails> retrieveEndpoints(String uri, String camelText) {
List<CamelEndpointDetails> endpoints = new ArrayList<>();
String uri = params.getTextDocument().getUri();
if (uri.endsWith(".xml")) {
try {
XmlRouteParser.parseXmlRouteEndpoints(new ByteArrayInputStream(camelText.getBytes(StandardCharsets.UTF_8)), "", "/"+uri, endpoints);
} catch (Exception e) {
logExceptionValidatingDocument(params, e);
logExceptionValidatingDocument(uri, e);
}
} else if(uri.endsWith(".java")) {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(camelText);
Expand All @@ -127,11 +136,11 @@ private List<CamelEndpointDetails> retrieveEndpoints(DidSaveTextDocumentParams p
return endpoints;
}

private void logExceptionValidatingDocument(DidSaveTextDocumentParams params, Exception e) {
LOGGER.warn("Error while trying to validate the document " + params.getTextDocument().getUri(), e);
private void logExceptionValidatingDocument(String docUri, Exception e) {
LOGGER.warn("Error while trying to validate the document {0}", docUri, e);
}

private List<Diagnostic> converToLSPDiagnostics(String fullCamelText, Map<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointErrors, TextDocumentItem textDocumentItem) {
List<Diagnostic> converToLSPDiagnostics(String fullCamelText, Map<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointErrors, TextDocumentItem textDocumentItem) {
List<Diagnostic> diagnostics = new ArrayList<>();
for (Map.Entry<CamelEndpointDetailsWrapper, EndpointValidationResult> endpointError : endpointErrors.entrySet()) {
EndpointValidationResult validationResult = endpointError.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.github.cameltooling.lsp.internal.diagnostic;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.awaitility.Duration;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
Expand Down Expand Up @@ -94,7 +96,8 @@ private void testDiagnostic(String fileUnderTest, int expectedNumberOfError, Str
DidSaveTextDocumentParams params = new DidSaveTextDocumentParams(new TextDocumentIdentifier(DUMMY_URI+extension));
camelLanguageServer.getTextDocumentService().didSave(params);

assertThat(lastPublishedDiagnostics.getDiagnostics()).hasSize(expectedNumberOfError);
await().timeout(Duration.ONE_SECOND).untilAsserted(() -> assertThat(lastPublishedDiagnostics).isNotNull());
await().timeout(Duration.ONE_SECOND).untilAsserted(() -> assertThat(lastPublishedDiagnostics.getDiagnostics()).hasSize(expectedNumberOfError));
}

}