Search in sources :

Example 1 with LtkIssueAcceptor

use of org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor in project xtext-eclipse by eclipse.

the class RenameElementProcessor2 method validateNewName.

@Override
public RefactoringStatus validateNewName(String newName) {
    LtkIssueAcceptor nameStatus = statusProvider.get();
    nameValidator.validate(target, newName, nameStatus);
    return nameStatus.getRefactoringStatus();
}
Also used : LtkIssueAcceptor(org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor)

Example 2 with LtkIssueAcceptor

use of org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor in project xtext-eclipse by eclipse.

the class DefaultLinkedPositionGroupCalculator2 method getLinkedPositionGroup.

@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(IRenameElementContext renameElementContext, IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, 100);
    XtextEditor editor = (XtextEditor) renameElementContext.getTriggeringEditor();
    IProject project = projectUtil.getProject(renameElementContext.getContextResourceURI());
    if (project == null) {
        throw new IllegalStateException("Could not determine the project for context resource " + renameElementContext.getContextResourceURI());
    }
    CachingResourceSetProvider cachingResourceSetProvider = new CachingResourceSetProvider(resourceSetProvider);
    ResourceSet resourceSet = cachingResourceSetProvider.get(project);
    EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
    if (targetElement == null) {
        throw new IllegalStateException("Target element could not be loaded");
    }
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    IRenameStrategy2 renameStrategy = globalServiceProvider.findService(targetElement, IRenameStrategy2.class);
    if (renameStrategy == null) {
        throw new IllegalArgumentException("Cannot find a rename strategy for " + renameElementContext.getTargetElementURI());
    }
    String oldName = simpleNameProvider.getSimpleName(targetElement);
    String newName = getNewDummyName(oldName);
    IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
    LtkIssueAcceptor issues = resourceServiceProvider.get(LtkIssueAcceptor.class);
    IChangeSerializer changeSerializer = resourceServiceProvider.get(IChangeSerializer.class);
    changeSerializer.setUpdateRelatedFiles(false);
    RenameChange change = new RenameChange(newName, renameElementContext.getTargetElementURI());
    RenameContext renameContext = new RenameContext(Lists.newArrayList(change), resourceSet, changeSerializer, issues);
    renameStrategy.applyRename(renameContext);
    List<ReplaceEdit> replaceEdits = new ArrayList<>();
    changeSerializer.applyModifications(c -> {
        if (c instanceof ITextDocumentChange) {
            ITextDocumentChange textDocumentChange = (ITextDocumentChange) c;
            textDocumentChange.getReplacements().forEach(replacement -> {
                replaceEdits.add(new ReplaceEdit(replacement.getOffset(), replacement.getLength(), replacement.getReplacementText()));
            });
        }
    });
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    return () -> {
        return createLinkedGroupFromReplaceEdits(replaceEdits, editor, oldName, progress.newChild(10));
    };
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LtkIssueAcceptor(org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor) IProject(org.eclipse.core.resources.IProject) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) EObject(org.eclipse.emf.ecore.EObject) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider)

Aggregations

LtkIssueAcceptor (org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor)2 ArrayList (java.util.ArrayList)1 IProject (org.eclipse.core.resources.IProject)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 IRenameStrategy2 (org.eclipse.xtext.ide.refactoring.IRenameStrategy2)1 RenameChange (org.eclipse.xtext.ide.refactoring.RenameChange)1 RenameContext (org.eclipse.xtext.ide.refactoring.RenameContext)1 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)1 ITextDocumentChange (org.eclipse.xtext.ide.serializer.ITextDocumentChange)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)1 CachingResourceSetProvider (org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider)1