use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_superfluousSemicolon.
@Test
@Ignore
public void testCodeAction_superfluousSemicolon() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + ";" + " }\n" + "}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = getRange(unit, ";");
params.setRange(range);
params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), range))));
List<? extends Command> commands = server.codeAction(params).join();
Assert.assertNotNull(commands);
Assert.assertEquals(1, commands.size());
Command c = commands.get(0);
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
Assert.assertNotNull(c.getArguments());
Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0);
List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit));
Assert.assertEquals(1, edits.size());
Assert.assertEquals("", edits.get(0).getNewText());
Assert.assertEquals(range, edits.get(0).getRange());
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_exception.
@Test
public void testCodeAction_exception() throws JavaModelException {
URI uri = project.getFile("nopackage/Test.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
try {
cu.becomeWorkingCopy(new NullProgressMonitor());
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(uri.toString()));
final Range range = new Range();
range.setStart(new Position(0, 17));
range.setEnd(new Position(0, 17));
params.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Collections.emptyList());
params.setContext(context);
List<? extends Command> commands = server.codeAction(params).join();
Assert.assertNotNull(commands);
Assert.assertEquals(0, commands.size());
} finally {
cu.discardWorkingCopy();
}
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class CodeLensHandlerTest method testResolveCodeLense.
@SuppressWarnings("unchecked")
@Test
public void testResolveCodeLense() {
String source = "src/java/Foo.java";
String payload = createCodeLensRequest(source, 5, 13, 16);
CodeLens lens = getParams(payload);
Range range = lens.getRange();
assertRange(5, 13, 16, range);
CodeLens result = handler.resolve(lens, monitor);
assertNotNull(result);
// Check if command found
Command command = result.getCommand();
assertNotNull(command);
assertEquals("1 reference", command.getTitle());
assertEquals("java.show.references", command.getCommand());
// Check codelens args
List<Object> args = command.getArguments();
assertEquals(3, args.size());
// Check we point to the Bar class
String sourceUri = args.get(0).toString();
assertTrue(sourceUri.endsWith(source));
// CodeLens position
Position p = (Position) args.get(1);
assertEquals(5, p.getLine());
assertEquals(13, p.getCharacter());
// Reference location
List<Location> locations = (List<Location>) args.get(2);
assertEquals(1, locations.size());
Location loc = locations.get(0);
assertTrue(loc.getUri().endsWith("src/java/Bar.java"));
assertRange(5, 25, 28, loc.getRange());
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class CompletionHandlerTest method testCompletion_constructor.
@Test
public void testCompletion_constructor() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + " Object o = new O\n" + " }\n" + "}\n");
int[] loc = findCompletionLocation(unit, "new O");
CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
assertNotNull(list);
assertFalse("No proposals were found", list.getItems().isEmpty());
List<CompletionItem> items = new ArrayList<>(list.getItems());
Comparator<CompletionItem> comparator = (CompletionItem a, CompletionItem b) -> a.getSortText().compareTo(b.getSortText());
Collections.sort(items, comparator);
CompletionItem ctor = items.get(0);
assertEquals("Object()", ctor.getLabel());
assertEquals("Object", ctor.getInsertText());
CompletionItem resolvedItem = server.resolveCompletionItem(ctor).join();
assertNotNull(resolvedItem);
TextEdit te = resolvedItem.getTextEdit();
assertNotNull(te);
assertEquals("Object()", te.getNewText());
assertNotNull(te.getRange());
Range range = te.getRange();
assertEquals(2, range.getStart().getLine());
assertEquals(17, range.getStart().getCharacter());
assertEquals(2, range.getEnd().getLine());
assertEquals(18, range.getEnd().getCharacter());
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class WorkspaceDiagnosticsHandlerTest method testToDiagnosticsArray.
@Test
public void testToDiagnosticsArray() throws Exception {
String msg1 = "Something's wrong Jim";
IMarker m1 = createMarker(IMarker.SEVERITY_WARNING, msg1, 2, 95, 100);
String msg2 = "He's dead";
IMarker m2 = createMarker(IMarker.SEVERITY_ERROR, msg2, 10, 1015, 1025);
String msg3 = "It's probably time to panic";
IMarker m3 = createMarker(42, msg3, 100, 10000, 10005);
IDocument d = mock(IDocument.class);
when(d.getLineOffset(1)).thenReturn(90);
when(d.getLineOffset(9)).thenReturn(1000);
when(d.getLineOffset(99)).thenReturn(10000);
List<Diagnostic> diags = handler.toDiagnosticsArray(d, new IMarker[] { m1, m2, m3 });
assertEquals(3, diags.size());
Range r;
Diagnostic d1 = diags.get(0);
assertEquals(msg1, d1.getMessage());
assertEquals(DiagnosticSeverity.Warning, d1.getSeverity());
r = d1.getRange();
assertEquals(1, r.getStart().getLine());
assertEquals(5, r.getStart().getCharacter());
assertEquals(1, r.getEnd().getLine());
assertEquals(10, r.getEnd().getCharacter());
Diagnostic d2 = diags.get(1);
assertEquals(msg2, d2.getMessage());
assertEquals(DiagnosticSeverity.Error, d2.getSeverity());
r = d2.getRange();
assertEquals(9, r.getStart().getLine());
assertEquals(15, r.getStart().getCharacter());
assertEquals(9, r.getEnd().getLine());
assertEquals(25, r.getEnd().getCharacter());
Diagnostic d3 = diags.get(2);
assertEquals(msg3, d3.getMessage());
assertEquals(DiagnosticSeverity.Information, d3.getSeverity());
r = d3.getRange();
assertEquals(99, r.getStart().getLine());
assertEquals(0, r.getStart().getCharacter());
assertEquals(99, r.getEnd().getLine());
assertEquals(5, r.getEnd().getCharacter());
}
Aggregations