use of org.eclipse.lsp4j.CreateFile 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());
}
use of org.eclipse.lsp4j.CreateFile in project eclipse.jdt.ls by eclipse.
the class ChangeUtil method convertCreateCompilationUnitChange.
private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) {
ICompilationUnit unit = change.getCu();
CreateFile createFile = new CreateFile();
createFile.setUri(JDTUtils.toURI(unit));
createFile.setOptions(new CreateFileOptions(false, true));
edit.getDocumentChanges().add(Either.forRight(createFile));
InsertEdit textEdit = new InsertEdit(0, change.getPreview());
convertTextEdit(edit, unit, textEdit);
}
use of org.eclipse.lsp4j.CreateFile in project eclipse.jdt.ls by eclipse.
the class ChangeUtil method convertCreateFileChange.
private static void convertCreateFileChange(WorkspaceEdit edit, CreateFileChange createFileChange) {
CreateFile createFile = new CreateFile();
createFile.setUri(ResourceUtils.fixURI(createFileChange.getPath().toFile().toURI()));
createFile.setOptions(new CreateFileOptions(false, true));
edit.getDocumentChanges().add(Either.forRight(createFile));
}
use of org.eclipse.lsp4j.CreateFile in project eclipse.jdt.ls by eclipse.
the class ChangeUtil method convertRenamePackcageChange.
private static void convertRenamePackcageChange(WorkspaceEdit edit, RenamePackageChange packageChange) throws CoreException {
IPackageFragment pack = (IPackageFragment) packageChange.getModifiedElement();
IPath newPackageFragment = new Path(packageChange.getNewName().replace('.', IPath.SEPARATOR));
IPath oldPackageFragment = new Path(packageChange.getOldName().replace('.', IPath.SEPARATOR));
IPath newPackagePath = pack.getResource().getLocation().removeLastSegments(oldPackageFragment.segmentCount()).append(newPackageFragment);
if (packageChange.getRenameSubpackages()) {
IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
String oldPrefix = packageChange.getOldName();
for (IPackageFragment currentPackage : allPackages) {
String newPkgName = packageChange.getNewName() + currentPackage.getElementName().substring(oldPrefix.length());
// update package's declaration
convertPackageUpdateEdit(currentPackage.getCompilationUnits(), newPkgName, edit);
}
RenameFile renameFile = new RenameFile();
renameFile.setNewUri(ResourceUtils.fixURI(newPackagePath.toFile().toURI()));
renameFile.setOldUri(ResourceUtils.fixURI(pack.getResource().getRawLocationURI()));
edit.getDocumentChanges().add(Either.forRight(renameFile));
} else {
// update package's declaration
convertPackageUpdateEdit(pack.getCompilationUnits(), packageChange.getNewName(), edit);
CreateFile createFile = new CreateFile();
createFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
createFile.setOptions(new CreateFileOptions(false, true));
edit.getDocumentChanges().add(Either.forRight(createFile));
for (ICompilationUnit unit : pack.getCompilationUnits()) {
RenameFile cuResourceChange = new RenameFile();
cuResourceChange.setOldUri(ResourceUtils.fixURI(unit.getResource().getLocationURI()));
IPath newCUPath = newPackagePath.append(unit.getPath().lastSegment());
cuResourceChange.setNewUri(ResourceUtils.fixURI(newCUPath.toFile().toURI()));
edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
}
// Workaround: https://github.com/Microsoft/language-server-protocol/issues/272
DeleteFile deleteFile = new DeleteFile();
deleteFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
deleteFile.setOptions(new DeleteFileOptions(false, true));
edit.getDocumentChanges().add(Either.forRight(deleteFile));
}
}
use of org.eclipse.lsp4j.CreateFile in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveInnerTypeToFile.
@Test
public void testMoveInnerTypeToFile() throws Exception {
System.setProperty("line.separator", "\n");
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("Top.java", "package test1;\n" + "\n" + "public class Top {\n" + " String name;\n\n" + " public class Inner {\n" + " public void print() {\n" + " System.out.println(Top.this.name);\n" + " }\n" + " }\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "class Inner");
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveTypeToNewFile", params, "Foo", true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(3, changes.size());
// @formatter:off
String expected = "package test1;\n" + "\n" + "public class Top {\n" + " String name;\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
ResourceOperation resourceOperation = changes.get(1).getRight();
assertNotNull(resourceOperation);
assertTrue(resourceOperation instanceof CreateFile);
assertEquals(ResourceUtils.fixURI(cu.getResource().getRawLocationURI()).replace("Top", "Inner"), ((CreateFile) resourceOperation).getUri());
// @formatter:off
expected = "package test1;\n" + "\n" + "public class Inner {\n" + " /**\n" + " *\n" + " */\n" + " private final Top top;\n\n" + " /**\n" + " * @param top\n" + " */\n" + " Inner(Top top) {\n" + " this.top = top;\n" + " }\n\n" + " public void print() {\n" + " System.out.println(this.top.name);\n" + " }\n" + "}";
// @formatter:on
textEdit = changes.get(2).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(pack1.getCompilationUnit("Inner.java").getWorkingCopy(null), textEdit.getEdits()));
}
Aggregations