org.eclipse.lsp4j.WorkspaceEdit#getDocumentChanges ( )源码实例Demo

下面列出了org.eclipse.lsp4j.WorkspaceEdit#getDocumentChanges ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: eclipse.jdt.ls   文件: ChangeUtil.java
/**
 * 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);
	}
}
 
源代码2 项目: eclipse.jdt.ls   文件: ChangeUtil.java
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());
		}
	}
}
 
源代码3 项目: eclipse.jdt.ls   文件: RenameHandlerTest.java
@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);
}
 
源代码4 项目: eclipse.jdt.ls   文件: ReorgQuickFixTest.java
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());
}
 
源代码5 项目: eclipse.jdt.ls   文件: AbstractQuickFixTest.java
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());
}
 
源代码6 项目: eclipse.jdt.ls   文件: FileEventHandlerTest.java
@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"
			);
}
 
源代码7 项目: eclipse.jdt.ls   文件: FileEventHandlerTest.java
@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"
			);
}
 
源代码8 项目: eclipse.jdt.ls   文件: RenameHandlerTest.java
@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));
}
 
源代码9 项目: eclipse.jdt.ls   文件: RenameHandlerTest.java
@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());
}