Search in sources :

Example 81 with Range

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());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 82 with Range

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();
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) Test(org.junit.Test)

Example 83 with Range

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());
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Position(org.eclipse.lsp4j.Position) List(java.util.List) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) Location(org.eclipse.lsp4j.Location) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 84 with Range

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) Lsp4jAssertions.assertTextEdit(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertTextEdit) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 85 with Range

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());
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) IMarker(org.eclipse.core.resources.IMarker) Range(org.eclipse.lsp4j.Range) IDocument(org.eclipse.jface.text.IDocument) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

Range (org.eclipse.lsp4j.Range)126 Position (org.eclipse.lsp4j.Position)59 Test (org.junit.Test)38 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 TextEdit (org.eclipse.lsp4j.TextEdit)23 ArrayList (java.util.ArrayList)22 List (java.util.List)17 Location (org.eclipse.lsp4j.Location)16 Document (org.eclipse.xtext.ide.server.Document)16 Diagnostic (org.eclipse.lsp4j.Diagnostic)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)13 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)12 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)12 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)9 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)9 Command (org.eclipse.lsp4j.Command)9 CompletionList (org.eclipse.lsp4j.CompletionList)9 RenameParams (org.eclipse.lsp4j.RenameParams)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)8