Search in sources :

Example 66 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameTypeParameterInMethod.

@Test
public void testRenameTypeParameterInMethod() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class B<T> {\n", "	private T t;\n", "	public <U|* extends Number> inspect(U u) { return u; }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    Position pos = mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("B.java", builder.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cu, pos, "UU");
    assertNotNull(edit);
    assertEquals(edit.getChanges().size(), 1);
    assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "public class B<T> {\n" + "	private T t;\n" + "	public <UU extends Number> inspect(UU u) { return u; }\n" + "}\n");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 67 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameTypeWithErrors.

@Test(expected = ResponseErrorException.class)
public void testRenameTypeWithErrors() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class Newname {\n", "   }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("Newname.java", builder.toString(), false, null);
    String[] codes1 = { "package test1;\n", "public class E|* {\n", "   public E() {\n", "   }\n", "   public int bar() {\n", "   }\n", "   public int foo() {\n", "		this.bar();\n", "   }\n", "}\n" };
    builder = new StringBuilder();
    Position pos = mergeCode(builder, codes1);
    cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cu, pos, "Newname");
    assertNotNull(edit);
    List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
    assertEquals(resourceChanges.size(), 3);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 68 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameParameter.

@Test
public void testRenameParameter() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class E {\n", "   public int foo(String str) {\n", "  		str|*.length();\n", "   }\n", "   public int bar(String str) {\n", "   	str.length();\n", "   }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    Position pos = mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cu, pos, "newname");
    assertNotNull(edit);
    assertEquals(edit.getChanges().size(), 1);
    assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "public class E {\n" + "   public int foo(String newname) {\n" + "  		newname.length();\n" + "   }\n" + "   public int bar(String str) {\n" + "   	str.length();\n" + "   }\n" + "}\n");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 69 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameTypeWithResourceChanges.

@Test
public void testRenameTypeWithResourceChanges() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class E|* {\n", "   public E() {\n", "   }\n", "   public int bar() {\n", "   }\n", "   public int foo() {\n", "		this.bar();\n", "   }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    Position pos = mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cu, pos, "Newname");
    assertNotNull(edit);
    List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
    assertEquals(resourceChanges.size(), 2);
    Either<TextDocumentEdit, ResourceOperation> change = resourceChanges.get(1);
    RenameFile resourceChange = (RenameFile) change.getRight();
    assertEquals(JDTUtils.toURI(cu), resourceChange.getOldUri());
    assertEquals(JDTUtils.toURI(cu).replaceFirst("(?s)E(?!.*?E)", "Newname"), resourceChange.getNewUri());
    List<TextEdit> testChanges = new LinkedList<>();
    testChanges.addAll(resourceChanges.get(0).getLeft().getEdits());
    String expected = "package test1;\n" + "public class Newname {\n" + "   public Newname() {\n" + "   }\n" + "   public int bar() {\n" + "   }\n" + "   public int foo() {\n" + "		this.bar();\n" + "   }\n" + "}\n";
    assertEquals(expected, TextEditUtil.apply(builder.toString(), testChanges));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) RenameFile(org.eclipse.lsp4j.RenameFile) LinkedList(java.util.LinkedList) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 70 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method mergeCode.

private Position mergeCode(StringBuilder builder, String[] codes) {
    Position pos = null;
    for (int i = 0; i < codes.length; i++) {
        int ind = codes[i].indexOf("|*");
        if (ind > 0) {
            pos = new Position(i, ind);
            codes[i] = codes[i].replace("|*", "");
        }
        builder.append(codes[i]);
    }
    return pos;
}
Also used : Position(org.eclipse.lsp4j.Position)

Aggregations

Position (org.eclipse.lsp4j.Position)240 Test (org.junit.Test)149 Range (org.eclipse.lsp4j.Range)103 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)95 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)73 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)70 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)50 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)36 Location (org.eclipse.lsp4j.Location)34 List (java.util.List)29 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)29 URI (java.net.URI)27 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)23 ArrayList (java.util.ArrayList)21 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)18 TextEdit (org.eclipse.lsp4j.TextEdit)15 CompletionList (org.eclipse.lsp4j.CompletionList)14 Document (org.eclipse.xtext.ide.server.Document)14 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)13 Command (org.eclipse.lsp4j.Command)12