use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.
the class WorkspaceDiagnosticsHandlerTest method testMavenMarkers.
@Test
public void testMavenMarkers() throws Exception {
String msg1 = "Some dependency is missing";
IMarker m1 = createMavenMarker(IMarker.SEVERITY_ERROR, msg1, 2, 95, 100);
IDocument d = mock(IDocument.class);
when(d.getLineOffset(1)).thenReturn(90);
List<Diagnostic> diags = handler.toDiagnosticsArray(d, new IMarker[] { m1, null });
assertEquals(1, diags.size());
Range r;
Diagnostic d1 = diags.get(0);
assertEquals(msg1, d1.getMessage());
assertEquals(DiagnosticSeverity.Error, d1.getSeverity());
r = d1.getRange();
assertEquals(1, r.getStart().getLine());
assertEquals(95, r.getStart().getCharacter());
assertEquals(1, r.getEnd().getLine());
assertEquals(100, r.getEnd().getCharacter());
}
use of org.eclipse.lsp4j.Diagnostic in project ballerina by ballerina-lang.
the class CommandUtil method getCommandsByDiagnostic.
/**
* Get the command instances for a given diagnostic.
* @param diagnostic Diagnostic to get the command against
* @param params Code Action parameters
* @param lsPackageCache Lang Server Package cache
* @return {@link List} List of commands related to the given diagnostic
*/
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
String diagnosticMessage = diagnostic.getMessage();
List<Command> commands = new ArrayList<>();
if (isUndefinedPackage(diagnosticMessage)) {
String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
Path openedPath = CommonUtil.getPath(sourceDocument);
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
sourceDocument.setSourceRoot(sourceRoot);
lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
}).forEach(pkgEntry -> {
PackageID packageID = pkgEntry.getValue().packageID;
String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
});
}
return commands;
}
use of org.eclipse.lsp4j.Diagnostic in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method publishDiagnostics.
private void publishDiagnostics(List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics, Path path) {
Map<String, List<Diagnostic>> diagnosticsMap = new HashMap<>();
balDiagnostics.forEach(diagnostic -> {
Diagnostic d = new Diagnostic();
d.setSeverity(DiagnosticSeverity.Error);
d.setMessage(diagnostic.getMessage());
Range r = new Range();
// LSP diagnostics range is 0 based
int startLine = diagnostic.getPosition().getStartLine() - 1;
int startChar = diagnostic.getPosition().getStartColumn() - 1;
int endLine = diagnostic.getPosition().getEndLine() - 1;
int endChar = diagnostic.getPosition().getEndColumn() - 1;
if (endLine <= 0) {
endLine = startLine;
}
if (endChar <= 0) {
endChar = startChar + 1;
}
r.setStart(new Position(startLine, startChar));
r.setEnd(new Position(endLine, endChar));
d.setRange(r);
String fileName = diagnostic.getPosition().getSource().getCompilationUnitName();
Path filePath = Paths.get(path.getParent().toString(), fileName);
String fileURI = filePath.toUri().toString();
if (!diagnosticsMap.containsKey(fileURI)) {
diagnosticsMap.put(fileURI, new ArrayList<Diagnostic>());
}
List<Diagnostic> clientDiagnostics = diagnosticsMap.get(fileURI);
clientDiagnostics.add(d);
});
// clear previous diagnostics
List<Diagnostic> empty = new ArrayList<Diagnostic>(0);
for (Map.Entry<String, List<Diagnostic>> entry : lastDiagnosticMap.entrySet()) {
if (diagnosticsMap.containsKey(entry.getKey())) {
continue;
}
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
diagnostics.setUri(entry.getKey());
diagnostics.setDiagnostics(empty);
this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
}
for (Map.Entry<String, List<Diagnostic>> entry : diagnosticsMap.entrySet()) {
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
diagnostics.setUri(entry.getKey());
diagnostics.setDiagnostics(entry.getValue());
this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
}
lastDiagnosticMap = diagnosticsMap;
}
use of org.eclipse.lsp4j.Diagnostic in project freemarker-languageserver by angelozerr.
the class FreemarkerTextDocumentService method validateFMDocument.
private List<Diagnostic> validateFMDocument(String xmlDocumentUri, String xmlDocumentContent) {
List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
if (fmConfiguration == null) {
fmConfiguration = new Configuration(Configuration.getVersion());
fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
fmConfiguration.setTabSize(1);
}
try {
@SuppressWarnings("unused") Template dummy = new Template(xmlDocumentUri, xmlDocumentContent, fmConfiguration);
} catch (ParseException e) {
Position start = new Position(e.getLineNumber() - 1, e.getColumnNumber());
Position end = new Position(e.getEndLineNumber() - 1, e.getEndColumnNumber());
Diagnostic diagnostic = new Diagnostic(new Range(start, end), e.getEditorMessage());
diagnostics.add(diagnostic);
} catch (IOException e) {
e.printStackTrace();
}
return diagnostics;
}
use of org.eclipse.lsp4j.Diagnostic in project freemarker-languageserver by angelozerr.
the class FreemarkerTextDocumentService method triggerValidation.
private void triggerValidation(TextDocumentItem document) {
String uri = document.getUri();
List<Diagnostic> diagnostics = validateFMDocument(uri, document.getText());
fmLanguageServer.getLanguageClient().publishDiagnostics(new PublishDiagnosticsParams(uri, diagnostics));
}
Aggregations