From c50fe180c1f5865484e5b5f5a5b617aa1485a91b Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Thu, 5 Mar 2020 22:30:20 +0100 Subject: [PATCH] BadLocationException and diagnostic with negative line number send to client Signed-off-by: Snjezana Peco --- .../handlers/WorkspaceDiagnosticsHandler.java | 10 +++++ .../WorkspaceDiagnosticsHandlerTest.java | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java index 099f6bbeea..ef14cce84e 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java @@ -358,6 +358,16 @@ private static Diagnostic toDiagnostic(IDocument document, IMarker marker, boole */ private static Range convertRange(IDocument document, IMarker marker) { int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1; + if (line < 0) { + int end = marker.getAttribute(IMarker.CHAR_END, -1); + int start = marker.getAttribute(IMarker.CHAR_START, -1); + if (start >= 0 && end >= start) { + int[] startPos = JsonRpcHelpers.toLine(document, start); + int[] endPos = JsonRpcHelpers.toLine(document, end); + return new Range(new Position(startPos[0], startPos[1]), new Position(endPos[0], endPos[1])); + } + return new Range(new Position(0, 0), new Position(0, 0)); + } int cStart = 0; int cEnd = 0; try { diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandlerTest.java index 7cf9761848..736170aae4 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandlerTest.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.jdt.ls.core.internal.handlers; +import static org.eclipse.jdt.ls.core.internal.WorkspaceHelper.getProject; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -21,6 +22,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -28,10 +30,12 @@ import java.util.Optional; import java.util.stream.Collectors; +import org.apache.commons.io.FileUtils; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.NullProgressMonitor; @@ -254,6 +258,39 @@ public void testProjectLevelMarkers() throws Exception { assertTrue(diags.get(0).getMessage().startsWith("Project build error: ")); } + @Test + public void testBadLocationException() throws Exception { + //import project + importProjects("eclipse/hello"); + IProject project = getProject("hello"); + IFile iFile = project.getFile("/src/test1/A.java"); + File file = iFile.getRawLocation().toFile(); + assertTrue(file.exists()); + iFile = project.getFile("/src/test1/A1.java"); + File destFile = iFile.getRawLocation().toFile(); + assertFalse(destFile.exists()); + FileUtils.copyFile(file, destFile, false); + String uri = destFile.toPath().toUri().toString(); + project.refreshLocal(IResource.DEPTH_INFINITE, null); + waitForBackgroundJobs(); + ArgumentCaptor captor = ArgumentCaptor.forClass(PublishDiagnosticsParams.class); + verify(connection, atLeastOnce()).publishDiagnostics(captor.capture()); + List allCalls = captor.getAllValues(); + Collections.reverse(allCalls); + projectsManager.setConnection(client); + Optional param = allCalls.stream().filter(p -> p.getUri().equals(uri)).findFirst(); + assertTrue(param.isPresent()); + List diags = param.get().getDiagnostics(); + assertEquals(diags.toString(), 2, diags.size()); + Optional d = diags.stream().filter(p -> p.getMessage().equals("The type A is already defined")).findFirst(); + assertTrue(d.isPresent()); + Diagnostic diag = d.get(); + assertTrue(diag.getRange().getStart().getLine() >= 0); + assertTrue(diag.getRange().getStart().getCharacter() >= 0); + assertTrue(diag.getRange().getEnd().getLine() >= 0); + assertTrue(diag.getRange().getEnd().getCharacter() >= 0); + } + @Test public void testMissingNatures() throws Exception { //import project