use of org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport in project eclipse.jdt.ls by eclipse.
the class FileEventHandler method getRenameEdit.
private static WorkspaceEdit getRenameEdit(IJavaElement targetElement, String newName, IProgressMonitor monitor) throws CoreException {
RenameSupport renameSupport = RenameSupport.create(targetElement, newName, RenameSupport.UPDATE_REFERENCES);
if (renameSupport == null) {
return null;
}
if (targetElement instanceof IPackageFragment) {
((RenamePackageProcessor) renameSupport.getJavaRenameProcessor()).setRenameSubpackages(true);
}
RenameRefactoring renameRefactoring = renameSupport.getRenameRefactoring();
RefactoringTickProvider rtp = renameRefactoring.getRefactoringTickProvider();
SubMonitor submonitor = SubMonitor.convert(monitor, "Creating rename changes...", rtp.getAllTicks());
CheckConditionsOperation checkConditionOperation = new CheckConditionsOperation(renameRefactoring, CheckConditionsOperation.ALL_CONDITIONS);
checkConditionOperation.run(submonitor.split(rtp.getCheckAllConditionsTicks()));
if (checkConditionOperation.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
JavaLanguageServerPlugin.logError(checkConditionOperation.getStatus().getMessageMatchingSeverity(RefactoringStatus.ERROR));
}
Change change = renameRefactoring.createChange(submonitor.split(rtp.getCreateChangeTicks()));
change.initializeValidationData(new NotCancelableProgressMonitor(submonitor.split(rtp.getInitializeChangeTicks())));
return ChangeUtil.convertToWorkspaceEdit(change);
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport in project eclipse.jdt.ls by eclipse.
the class RenameHandler method rename.
public WorkspaceEdit rename(RenameParams params, IProgressMonitor monitor) {
WorkspaceEdit edit = new WorkspaceEdit();
if (!preferenceManager.getPreferences().isRenameEnabled()) {
return edit;
}
try {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
IJavaElement[] elements = JDTUtils.findElementsAtSelection(unit, params.getPosition().getLine(), params.getPosition().getCharacter(), this.preferenceManager, monitor);
if (elements == null || elements.length == 0) {
return edit;
}
IJavaElement curr = null;
if (elements.length != 1) {
// they could be package fragments.
// We need to select the one that matches the package fragment of the current unit
IPackageFragment packageFragment = (IPackageFragment) unit.getParent();
IJavaElement found = Stream.of(elements).filter(e -> e.equals(packageFragment)).findFirst().orElse(null);
if (found == null) {
// this would be a binary package fragment
curr = elements[0];
} else {
curr = found;
}
} else {
curr = elements[0];
}
RenameSupport renameSupport = RenameSupport.create(curr, params.getNewName(), RenameSupport.UPDATE_REFERENCES);
if (renameSupport == null) {
return edit;
}
RenameRefactoring renameRefactoring = renameSupport.getRenameRefactoring();
CheckConditionsOperation check = new CheckConditionsOperation(renameRefactoring, CheckConditionsOperation.ALL_CONDITIONS);
CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(monitor);
if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidRequest, check.getStatus().getMessageMatchingSeverity(RefactoringStatus.ERROR), null));
}
Change change = create.getChange();
return ChangeUtil.convertToWorkspaceEdit(change);
} catch (CoreException ex) {
JavaLanguageServerPlugin.logException("Problem with rename for " + params.getTextDocument().getUri(), ex);
}
return edit;
}
Aggregations