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

BadLocationException and diagnostic with negative line number send to client #1379

Merged
merged 1 commit into from
Mar 11, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,17 +22,20 @@
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;
import java.util.List;
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;
Expand Down Expand Up @@ -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<PublishDiagnosticsParams> captor = ArgumentCaptor.forClass(PublishDiagnosticsParams.class);
verify(connection, atLeastOnce()).publishDiagnostics(captor.capture());
List<PublishDiagnosticsParams> allCalls = captor.getAllValues();
Collections.reverse(allCalls);
projectsManager.setConnection(client);
Optional<PublishDiagnosticsParams> param = allCalls.stream().filter(p -> p.getUri().equals(uri)).findFirst();
assertTrue(param.isPresent());
List<Diagnostic> diags = param.get().getDiagnostics();
assertEquals(diags.toString(), 2, diags.size());
Optional<Diagnostic> 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
Expand Down