Search in sources :

Example 71 with Position

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

the class RenameHandlerTest method testRenameSuperMethod.

@Test
public void testRenameSuperMethod() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "class A {\n", "   public void bar() {\n", "   }\n", "}\n", "class B extends A {\n", "   public void bar() {\n", "		super|*.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, "TypeA");
    assertNotNull(edit);
    assertEquals(1, edit.getChanges().size());
    assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "class TypeA {\n" + "   public void bar() {\n" + "   }\n" + "}\n" + "class B extends TypeA {\n" + "   public void bar() {\n" + "		super.bar();\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 72 with Position

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

the class RenameHandlerTest method testRenameMultipleFiles.

@Test
public void testRenameMultipleFiles() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes1 = { "package test1;\n", "public class A {\n", "   public void foo() {\n", "   }\n", "}\n" };
    String[] codes2 = { "package test1;\n", "public class B {\n", "   public void foo() {\n", "		A a = new A();\n", "		a.foo|*();\n", "   }\n", "}\n" };
    StringBuilder builderA = new StringBuilder();
    mergeCode(builderA, codes1);
    ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
    StringBuilder builderB = new StringBuilder();
    Position pos = mergeCode(builderB, codes2);
    ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cuB, pos, "newname");
    assertNotNull(edit);
    assertEquals(edit.getChanges().size(), 2);
    assertEquals(TextEditUtil.apply(builderA.toString(), edit.getChanges().get(JDTUtils.toURI(cuA))), "package test1;\n" + "public class A {\n" + "   public void newname() {\n" + "   }\n" + "}\n");
    assertEquals(TextEditUtil.apply(builderB.toString(), edit.getChanges().get(JDTUtils.toURI(cuB))), "package test1;\n" + "public class B {\n" + "   public void foo() {\n" + "		A a = new A();\n" + "		a.newname();\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 73 with Position

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

the class RenameHandlerTest method testRenamePackage.

@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.test2", false, null);
    String[] codes1 = { "package test1;\n", "import parent.test2.B;\n", "public class A {\n", "   public void foo(){\n", "		B b = new B();\n", "		b.foo();\n", "	}\n", "}\n" };
    String[] codes2 = { "package parent.test2|*;\n", "public class B {\n", "	public B() {}\n", "   public void foo() {}\n", "}\n" };
    StringBuilder builderA = new StringBuilder();
    mergeCode(builderA, codes1);
    ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
    StringBuilder builderB = new StringBuilder();
    Position pos = mergeCode(builderB, codes2);
    ICompilationUnit cuB = pack2.createCompilationUnit("B.java", builderB.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cuB, pos, "parent.newpackage");
    assertNotNull(edit);
    List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
    assertEquals(5, resourceChanges.size());
    List<TextEdit> testChangesA = new LinkedList<>();
    testChangesA.addAll(resourceChanges.get(0).getLeft().getEdits());
    List<TextEdit> testChangesB = new LinkedList<>();
    testChangesB.addAll(resourceChanges.get(1).getLeft().getEdits());
    String expectedA = "package test1;\n" + "import parent.newpackage.B;\n" + "public class A {\n" + "   public void foo(){\n" + "		B b = new B();\n" + "		b.foo();\n" + "	}\n" + "}\n";
    String expectedB = "package parent.newpackage;\n" + "public class B {\n" + "	public B() {}\n" + "   public void foo() {}\n" + "}\n";
    assertEquals(expectedA, TextEditUtil.apply(builderA.toString(), testChangesA));
    assertEquals(expectedB, TextEditUtil.apply(builderB.toString(), testChangesB));
    // moved package
    CreateFile resourceChange = (CreateFile) resourceChanges.get(2).getRight();
    assertEquals(ResourceUtils.fixURI(pack2.getResource().getRawLocationURI()).replaceFirst("test2[/]?", "newpackage/.temp"), resourceChange.getUri());
    // moved class B
    RenameFile resourceChange2 = (RenameFile) resourceChanges.get(3).getRight();
    assertEquals(ResourceUtils.fixURI(cuB.getResource().getRawLocationURI()), resourceChange2.getOldUri());
    assertEquals(ResourceUtils.fixURI(cuB.getResource().getRawLocationURI()).replace("test2", "newpackage"), resourceChange2.getNewUri());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) LinkedList(java.util.LinkedList) RenameFile(org.eclipse.lsp4j.RenameFile) CreateFile(org.eclipse.lsp4j.CreateFile) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 74 with Position

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

the class RenameHandlerTest method testRenameJavadoc.

@Test
public void testRenameJavadoc() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class E {\n", "	/**\n", "	 *@param i int\n", "	 */\n", "   public int foo(int i|*) {\n", "		E e = new E();\n", "		e.foo();\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, "i2");
    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" + "	/**\n" + "	 *@param i2 int\n" + "	 */\n" + "   public int foo(int i2) {\n" + "		E e = new E();\n" + "		e.foo();\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 75 with Position

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

the class RenameHandlerTest method testRenameInterfaceMethod.

@Test
public void testRenameInterfaceMethod() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes1 = { "package test1;\n", "public interface A {\n", "   public void foo();\n", "}\n" };
    String[] codes2 = { "package test1;\n", "public class B implements A {\n", "	@Override\n", "   public void foo|*() {\n", "   }\n", "}\n" };
    StringBuilder builderA = new StringBuilder();
    mergeCode(builderA, codes1);
    ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
    StringBuilder builderB = new StringBuilder();
    Position pos = mergeCode(builderB, codes2);
    ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cuB, pos, "newname");
    assertNotNull(edit);
    assertEquals(edit.getChanges().size(), 2);
    assertEquals(TextEditUtil.apply(builderA.toString(), edit.getChanges().get(JDTUtils.toURI(cuA))), "package test1;\n" + "public interface A {\n" + "   public void newname();\n" + "}\n");
    assertEquals(TextEditUtil.apply(builderB.toString(), edit.getChanges().get(JDTUtils.toURI(cuB))), "package test1;\n" + "public class B implements A {\n" + "	@Override\n" + "   public void newname() {\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)

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