Java Code Examples for org.eclipse.lsp4j.WorkspaceEdit#getDocumentChanges()

The following examples show how to use org.eclipse.lsp4j.WorkspaceEdit#getDocumentChanges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Converts changes to resource operations if resource operations are supported
 * by the client otherwise converts to TextEdit changes.
 *
 * @param resourceChange
 *            resource changes after Refactoring operation
 * @param edit
 *            instance of workspace edit changes
 * @throws CoreException
 */
private static void convertResourceChange(ResourceChange resourceChange, WorkspaceEdit edit) throws CoreException {
	if (!JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) {
		return;
	}

	List<Either<TextDocumentEdit, ResourceOperation>> changes = edit.getDocumentChanges();
	if (changes == null) {
		changes = new ArrayList<>();
		edit.setDocumentChanges(changes);
	}

	// Resource change is needed and supported by client
	if (resourceChange instanceof RenameCompilationUnitChange) {
		convertCUResourceChange(edit, (RenameCompilationUnitChange) resourceChange);
	} else if (resourceChange instanceof RenamePackageChange) {
		convertRenamePackcageChange(edit, (RenamePackageChange) resourceChange);
	} else if (resourceChange instanceof MoveCompilationUnitChange) {
		convertMoveCompilationUnitChange(edit, (MoveCompilationUnitChange) resourceChange);
	} else if (resourceChange instanceof CreateFileChange) {
		convertCreateFileChange(edit, (CreateFileChange) resourceChange);
	} else if (resourceChange instanceof CreateCompilationUnitChange) {
		convertCreateCompilationUnitChange(edit, (CreateCompilationUnitChange) resourceChange);
	}
}
 
Example 2
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void appendChanges(WorkspaceEdit root, WorkspaceEdit child, boolean ignoreResourceChange) {
	if (root == null || child == null) {
		return;
	}

	if (child.getChanges() != null && !child.getChanges().isEmpty()) {
		if (root.getChanges() == null) {
			root.setChanges(new LinkedHashMap<>());
		}

		for (Entry<String, List<org.eclipse.lsp4j.TextEdit>> entry : child.getChanges().entrySet()) {
			root.getChanges().computeIfAbsent(entry.getKey(), (key -> new ArrayList<>()));
			root.getChanges().get(entry.getKey()).addAll(entry.getValue());
		}
	}

	if (child.getDocumentChanges() != null && !child.getDocumentChanges().isEmpty()) {
		if (root.getDocumentChanges() == null) {
			root.setDocumentChanges(new ArrayList<>());
		}

		if (ignoreResourceChange) {
			root.getDocumentChanges().addAll(
				child.getDocumentChanges().stream().filter((change) -> change.isLeft()).collect(Collectors.toList())
			);
		} else {
			root.getDocumentChanges().addAll(child.getDocumentChanges());
		}
	}
}
 
Example 3
Source File: RenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: ReorgQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void assertRenameFileOperation(Either<Command, CodeAction> codeAction, String newUri) {
	WorkspaceEdit edit = getWorkspaceEdit(codeAction);
	List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
	assertNotNull(documentChanges);
	assertEquals(1, documentChanges.size());
	ResourceOperation resourceOperation = documentChanges.get(0).getRight();
	assertNotNull(resourceOperation);
	assertTrue(resourceOperation instanceof RenameFile);
	assertEquals(newUri, ((RenameFile) resourceOperation).getNewUri());
}
 
Example 5
Source File: AbstractQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected String evaluateCodeActionCommand(Either<Command, CodeAction> codeAction)
		throws BadLocationException, JavaModelException {

	Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
	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);
	if (we.getDocumentChanges() != null) {
		return evaluateChanges(we.getDocumentChanges());
	}
	return evaluateChanges(we.getChanges());
}
 
Example 6
Source File: FileEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
	IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);

	StringBuilder codeA = new StringBuilder();
	codeA.append("package parent.pack1;\n");
	codeA.append("import parent.pack2.B;\n");
	codeA.append("public class A {\n");
	codeA.append("	public void foo() {\n");
	codeA.append("		B b = new B();\n");
	codeA.append("		b.foo();\n");
	codeA.append("	}\n");
	codeA.append("}\n");

	StringBuilder codeB = new StringBuilder();
	codeB.append("package parent.pack2;\n");
	codeB.append("public class B {\n");
	codeB.append("	public B() {}\n");
	codeB.append("	public void foo() {}\n");
	codeB.append("}\n");

	ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
	ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);

	String pack2Uri = JDTUtils.getFileURI(pack2.getResource());
	String newPack2Uri = pack2Uri.replace("pack2", "newpack2");
	WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new FileRenameParams(Arrays.asList(new FileRenameEvent(pack2Uri, newPack2Uri))), new NullProgressMonitor());
	assertNotNull(edit);
	List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
	assertEquals(2, documentChanges.size());

	assertTrue(documentChanges.get(0).isLeft());
	assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	assertEquals(TextEditUtil.apply(codeA.toString(), documentChanges.get(0).getLeft().getEdits()),
			"package parent.pack1;\n" +
			"import parent.newpack2.B;\n" +
			"public class A {\n" +
			"	public void foo() {\n" +
			"		B b = new B();\n" +
			"		b.foo();\n" +
			"	}\n" +
			"}\n"
			);

	assertTrue(documentChanges.get(1).isLeft());
	assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
	assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(1).getLeft().getEdits()),
			"package parent.newpack2;\n" +
			"public class B {\n" +
			"	public B() {}\n" +
			"	public void foo() {}\n" +
			"}\n"
			);
}
 
Example 7
Source File: FileEventHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRenameSubPackage() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment parentPack = sourceFolder.createPackageFragment("parent", false, null);
	IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
	IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);

	StringBuilder codeA = new StringBuilder();
	codeA.append("package parent.pack1;\n");
	codeA.append("import parent.pack2.B;\n");
	codeA.append("public class A {\n");
	codeA.append("	public void foo() {\n");
	codeA.append("		B b = new B();\n");
	codeA.append("		b.foo();\n");
	codeA.append("	}\n");
	codeA.append("}\n");

	StringBuilder codeB = new StringBuilder();
	codeB.append("package parent.pack2;\n");
	codeB.append("public class B {\n");
	codeB.append("	public B() {}\n");
	codeB.append("	public void foo() {}\n");
	codeB.append("}\n");

	ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
	ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);

	String parentPackUri = JDTUtils.getFileURI(parentPack.getResource());
	String newParentPackUri = parentPackUri.replace("parent", "newparent");
	WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new FileRenameParams(Arrays.asList(new FileRenameEvent(parentPackUri, newParentPackUri))), new NullProgressMonitor());
	assertNotNull(edit);
	List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
	assertEquals(3, documentChanges.size());

	assertTrue(documentChanges.get(0).isLeft());
	assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	assertTrue(documentChanges.get(1).isLeft());
	assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
	List<TextEdit> edits = new ArrayList<>();
	edits.addAll(documentChanges.get(0).getLeft().getEdits());
	edits.addAll(documentChanges.get(1).getLeft().getEdits());
	assertEquals(TextEditUtil.apply(codeA.toString(), edits),
			"package newparent.pack1;\n" +
			"import newparent.pack2.B;\n" +
			"public class A {\n" +
			"	public void foo() {\n" +
			"		B b = new B();\n" +
			"		b.foo();\n" +
			"	}\n" +
			"}\n"
			);

	assertTrue(documentChanges.get(2).isLeft());
	assertEquals(documentChanges.get(2).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
	assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(2).getLeft().getEdits()),
			"package newparent.pack2;\n" +
			"public class B {\n" +
			"	public B() {}\n" +
			"	public void foo() {}\n" +
			"}\n"
			);
}
 
Example 8
Source File: RenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@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));
}
 
Example 9
Source File: RenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@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());
}