org.eclipse.lsp4j.DeleteFile Java Examples

The following examples show how to use org.eclipse.lsp4j.DeleteFile. 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: ResourceOperationTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, ResourceOperation value) throws IOException {
	if (value.getClass().isAssignableFrom(CreateFile.class)) {
		createFileAdapter.write(out, (CreateFile) value);
	} else if (value.getClass().isAssignableFrom(DeleteFile.class)) {
		deleteFileAdapter.write(out, (DeleteFile) value);
	} else if (value.getClass().isAssignableFrom(RenameFile.class)) {
		renameFileAdapter.write(out, (RenameFile) value);
	}
}
 
Example #2
Source File: UtilsTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testApplyChanges() throws Exception {
    clearWorkDir();
    FileObject wd = FileUtil.toFileObject(getWorkDir());
    FileObject sourceFile1 = wd.createData("Test1.txt");
    try (OutputStream out = sourceFile1.getOutputStream()) {
        out.write(("0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n").getBytes("UTF-8"));
    }
    FileObject sourceFile2 = wd.createData("Test2.txt");
    try (OutputStream out = sourceFile2.getOutputStream()) {
        out.write(("0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n" +
                   "0123456789\n").getBytes("UTF-8"));
    }
    FileObject sourceFile3 = wd.createData("Test3.txt");
    WorkspaceEdit edit = new WorkspaceEdit(Arrays.asList(Either.forLeft(new TextDocumentEdit(new VersionedTextDocumentIdentifier(Utils.toURI(sourceFile1), -1), Arrays.asList(new TextEdit(new Range(new Position(2, 3), new Position(2, 6)), "a"),
                                                                                                                                                                              new TextEdit(new Range(new Position(1, 2), new Position(1, 6)), "b"),
                                                                                                                                                                              new TextEdit(new Range(new Position(3, 1), new Position(4, 4)), "c")))),
                                                         Either.forLeft(new TextDocumentEdit(new VersionedTextDocumentIdentifier(Utils.toURI(sourceFile2), -1), Arrays.asList(new TextEdit(new Range(new Position(2, 3), new Position(2, 6)), "a"),
                                                                                                                                                                              new TextEdit(new Range(new Position(1, 2), new Position(1, 6)), "b"),
                                                                                                                                                                              new TextEdit(new Range(new Position(3, 1), new Position(4, 4)), "c")))),
                                                         Either.forRight(new CreateFile(Utils.toURI(sourceFile2).replace("Test2", "Test4"))),
                                                         Either.forLeft(new TextDocumentEdit(new VersionedTextDocumentIdentifier(Utils.toURI(sourceFile2).replace("Test2", "Test4"), -1), Arrays.asList(new TextEdit(new Range(new Position(1, 1), new Position(1, 1)), "new content")))),
                                                         Either.forRight(new DeleteFile(Utils.toURI(sourceFile3))),
                                                         Either.forRight(new RenameFile(Utils.toURI(sourceFile1), Utils.toURI(sourceFile1).replace("Test1", "Test1a")))));
    Utils.applyWorkspaceEdit(edit);
    assertContent("0123456789\n" +
                  "01b6789\n" +
                  "012a6789\n" +
                  "0c456789\n",
                  wd.getFileObject("Test1a.txt"));
    assertContent("0123456789\n" +
                  "01b6789\n" +
                  "012a6789\n" +
                  "0c456789\n",
                  wd.getFileObject("Test2.txt"));
    assertContent("new content", wd.getFileObject("Test4.txt"));
    assertNull(wd.getFileObject("Test3.txt"));
    LifecycleManager.getDefault().saveAll();
}
 
Example #3
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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));

	}
}
 
Example #4
Source File: ResourceOperationTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
InnerResourceOperationTypeAdapter(TypeAdapterFactory factory, Gson gson) {
	createFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(CreateFile.class));
	deleteFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(DeleteFile.class));
	renameFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(RenameFile.class));
}