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");
}
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);
}
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");
}
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));
}
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;
}
Aggregations