Java Code Examples for org.eclipse.lsp4j.RenameFile#setOldUri()

The following examples show how to use org.eclipse.lsp4j.RenameFile#setOldUri() . 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
private static void convertMoveCompilationUnitChange(WorkspaceEdit edit, MoveCompilationUnitChange change) throws JavaModelException {
	IPackageFragment newPackage = change.getDestinationPackage();
	ICompilationUnit unit = change.getCu();
	CompilationUnit astCU = RefactoringASTParser.parseWithASTProvider(unit, true, new NullProgressMonitor());
	ASTRewrite rewrite = ASTRewrite.create(astCU.getAST());

	IPackageDeclaration[] packDecls = unit.getPackageDeclarations();
	String oldPackageName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
	if (!Objects.equals(oldPackageName, newPackage.getElementName())) {
		// update the package declaration
		if (updatePackageStatement(astCU, newPackage.getElementName(), rewrite, unit)) {
			convertTextEdit(edit, unit, rewrite.rewriteAST());
		}
	}

	RenameFile cuResourceChange = new RenameFile();
	cuResourceChange.setOldUri(JDTUtils.toURI(unit));
	IPath newCUPath = newPackage.getResource().getLocation().append(unit.getPath().lastSegment());
	String newUri = ResourceUtils.fixURI(newCUPath.toFile().toURI());
	cuResourceChange.setNewUri(newUri);
	edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
}
 
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 convertCUResourceChange(WorkspaceEdit edit, RenameCompilationUnitChange cuChange) {
	ICompilationUnit modifiedCU = (ICompilationUnit) cuChange.getModifiedElement();
	RenameFile rf = new RenameFile();
	String newCUName = cuChange.getNewName();
	IPath currentPath = modifiedCU.getResource().getLocation();
	rf.setOldUri(ResourceUtils.fixURI(modifiedCU.getResource().getRawLocationURI()));
	IPath newPath = currentPath.removeLastSegments(1).append(newCUName);
	rf.setNewUri(ResourceUtils.fixURI(newPath.toFile().toURI()));
	edit.getDocumentChanges().add(Either.forRight(rf));
}
 
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));

	}
}