Search in sources :

Example 1 with RenameSupport

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);
}
Also used : RefactoringTickProvider(org.eclipse.ltk.core.refactoring.RefactoringTickProvider) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenamePackageProcessor(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenamePackageProcessor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) SubMonitor(org.eclipse.core.runtime.SubMonitor) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) Change(org.eclipse.ltk.core.refactoring.Change) NotCancelableProgressMonitor(org.eclipse.ltk.internal.core.refactoring.NotCancelableProgressMonitor) RenameSupport(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport)

Example 2 with RenameSupport

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ResponseError(org.eclipse.lsp4j.jsonrpc.messages.ResponseError) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Change(org.eclipse.ltk.core.refactoring.Change) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) CoreException(org.eclipse.core.runtime.CoreException) ResponseErrorException(org.eclipse.lsp4j.jsonrpc.ResponseErrorException) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) RenameSupport(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 RenameSupport (org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport)2 Change (org.eclipse.ltk.core.refactoring.Change)2 CheckConditionsOperation (org.eclipse.ltk.core.refactoring.CheckConditionsOperation)2 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)2 CoreException (org.eclipse.core.runtime.CoreException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 RenamePackageProcessor (org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenamePackageProcessor)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1 ResponseErrorException (org.eclipse.lsp4j.jsonrpc.ResponseErrorException)1 ResponseError (org.eclipse.lsp4j.jsonrpc.messages.ResponseError)1 CreateChangeOperation (org.eclipse.ltk.core.refactoring.CreateChangeOperation)1 RefactoringTickProvider (org.eclipse.ltk.core.refactoring.RefactoringTickProvider)1 NotCancelableProgressMonitor (org.eclipse.ltk.internal.core.refactoring.NotCancelableProgressMonitor)1