Search in sources :

Example 1 with CachingResourceSetProvider

use of org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider in project xtext-eclipse by eclipse.

the class DefaultLinkedPositionGroupCalculator method getLinkedPositionGroup.

@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(IRenameElementContext renameElementContext, IProgressMonitor monitor) {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    final XtextEditor editor = (XtextEditor) renameElementContext.getTriggeringEditor();
    IProject project = projectUtil.getProject(renameElementContext.getContextResourceURI());
    if (project == null)
        throw new IllegalStateException("Could not determine project for context resource " + renameElementContext.getContextResourceURI());
    RefactoringResourceSetProvider resourceSetProvider = new CachingResourceSetProvider(DefaultLinkedPositionGroupCalculator.this.resourceSetProvider);
    ResourceSet resourceSet = resourceSetProvider.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();
    }
    IRenameStrategy.Provider strategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
    IRenameStrategy renameStrategy = null;
    try {
        renameStrategy = strategyProvider.get(targetElement, renameElementContext);
    } catch (NoSuchStrategyException exc) {
    // handle in next line
    }
    if (renameStrategy == null)
        throw new IllegalArgumentException("Cannot find a rename strategy for " + notNull(renameElementContext.getTargetElementURI()));
    String newName = renameStrategy.getOriginalName();
    IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
    IDependentElementsCalculator dependentElementsCalculator = resourceServiceProvider.get(IDependentElementsCalculator.class);
    Iterable<URI> dependentElementURIs = dependentElementsCalculator.getDependentElementURIs(targetElement, progress.newChild(10));
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    LocalResourceRefactoringUpdateAcceptor updateAcceptor = updateAcceptorProvider.get();
    updateAcceptor.setLocalResourceURI(renameElementContext.getContextResourceURI());
    renameStrategy.createDeclarationUpdates(newName, resourceSet, updateAcceptor);
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    Map<URI, URI> original2newEObjectURI = renamedElementTracker.renameAndTrack(concat(Collections.singleton(renameElementContext.getTargetElementURI()), dependentElementURIs), newName, resourceSet, renameStrategy, progress.newChild(10));
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    ElementRenameArguments elementRenameArguments = new ElementRenameArguments(renameElementContext.getTargetElementURI(), newName, renameStrategy, original2newEObjectURI, resourceSetProvider);
    final List<IReferenceDescription> referenceDescriptions = newArrayList();
    IReferenceFinder.Acceptor referenceAcceptor = new IReferenceFinder.Acceptor() {

        @Override
        public void accept(IReferenceDescription referenceDescription) {
            referenceDescriptions.add(referenceDescription);
        }

        @Override
        public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
            referenceDescriptions.add(new DefaultReferenceDescription(EcoreUtil2.getFragmentPathURI(source), targetURI, eReference, index, null));
        }
    };
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    referenceFinder.findReferences(targetURIConverter.fromIterable(elementRenameArguments.getRenamedElementURIs()), resourceSet.getResource(renameElementContext.getContextResourceURI(), true), referenceAcceptor, progress.newChild(10));
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    referenceUpdater.createReferenceUpdates(elementRenameArguments, referenceDescriptions, updateAcceptor, progress.newChild(60));
    final List<ReplaceEdit> textEdits = updateAcceptor.getTextEdits();
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    final IRenameStrategy renameStrategy2 = renameStrategy;
    return new Provider<LinkedPositionGroup>() {

        @Override
        public LinkedPositionGroup get() {
            LinkedPositionGroup linkedGroup = createLinkedGroupFromReplaceEdits(textEdits, editor, renameStrategy2.getOriginalName(), progress.newChild(10));
            return linkedGroup;
        }
    };
}
Also used : IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) ElementRenameArguments(org.eclipse.xtext.ui.refactoring.ElementRenameArguments) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) URI(org.eclipse.emf.common.util.URI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) EObject(org.eclipse.emf.ecore.EObject) IDependentElementsCalculator(org.eclipse.xtext.ui.refactoring.IDependentElementsCalculator) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException) EReference(org.eclipse.emf.ecore.EReference) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) IRefactoringUpdateAcceptor(org.eclipse.xtext.ui.refactoring.IRefactoringUpdateAcceptor) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) SubMonitor(org.eclipse.core.runtime.SubMonitor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IProject(org.eclipse.core.resources.IProject) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) IGlobalServiceProvider(org.eclipse.xtext.resource.IGlobalServiceProvider) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Provider(com.google.inject.Provider) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) IReferenceFinder(org.eclipse.xtext.findReferences.IReferenceFinder) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 2 with CachingResourceSetProvider

use of org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider in project xtext-eclipse by eclipse.

the class DefaultLinkedPositionGroupCalculator2 method getLinkedPositionGroup.

@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(final IRenameElementContext renameElementContext, final IProgressMonitor monitor) {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    IEditorPart _triggeringEditor = renameElementContext.getTriggeringEditor();
    final XtextEditor editor = ((XtextEditor) _triggeringEditor);
    final IProject project = this.projectUtil.getProject(renameElementContext.getContextResourceURI());
    if ((project == null)) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("Could not determine project for context resource ");
        URI _contextResourceURI = renameElementContext.getContextResourceURI();
        _builder.append(_contextResourceURI);
        String _string = _builder.toString();
        throw new IllegalStateException(_string);
    }
    final CachingResourceSetProvider resourceSetProvider = new CachingResourceSetProvider(this.resourceSetProvider);
    final ResourceSet resourceSet = resourceSetProvider.get(project);
    final EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
    if ((targetElement == null)) {
        throw new IllegalStateException("Target element could not be loaded");
    }
    boolean _isCanceled = monitor.isCanceled();
    if (_isCanceled) {
        throw new OperationCanceledException();
    }
    IRenameStrategy2 renameStrategy = this.globalServiceProvider.<IRenameStrategy2>findService(targetElement, IRenameStrategy2.class);
    if ((renameStrategy == null)) {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("Cannot find a rename strategy for ");
        URI _targetElementURI = renameElementContext.getTargetElementURI();
        _builder_1.append(_targetElementURI);
        throw new IllegalArgumentException(_builder_1.toString());
    }
    final String oldName = this.simpleNameProvider.getSimpleName(targetElement);
    final String newName = this.getNewDummyName(oldName);
    final IResourceServiceProvider resourceServiceProvider = this.resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
    final LtkIssueAcceptor issues = resourceServiceProvider.<LtkIssueAcceptor>get(LtkIssueAcceptor.class);
    final IChangeSerializer changeSerializer = resourceServiceProvider.<IChangeSerializer>get(IChangeSerializer.class);
    changeSerializer.setUpdateRelatedFiles(false);
    URI _targetElementURI_1 = renameElementContext.getTargetElementURI();
    final RenameChange change = new RenameChange(newName, _targetElementURI_1);
    final RenameContext renameContext = new RenameContext(Collections.<RenameChange>unmodifiableList(CollectionLiterals.<RenameChange>newArrayList(change)), resourceSet, changeSerializer, issues);
    renameStrategy.applyRename(renameContext);
    final ArrayList<ReplaceEdit> replaceEdits = CollectionLiterals.<ReplaceEdit>newArrayList();
    final IAcceptor<IEmfResourceChange> _function = (IEmfResourceChange c) -> {
        if ((c instanceof ITextDocumentChange)) {
            final Function1<ITextReplacement, ReplaceEdit> _function_1 = (ITextReplacement replacement) -> {
                int _offset = replacement.getOffset();
                int _length = replacement.getLength();
                String _replacementText = replacement.getReplacementText();
                return new ReplaceEdit(_offset, _length, _replacementText);
            };
            List<ReplaceEdit> _map = ListExtensions.<ITextReplacement, ReplaceEdit>map(((ITextDocumentChange) c).getReplacements(), _function_1);
            Iterables.<ReplaceEdit>addAll(replaceEdits, _map);
        }
    };
    changeSerializer.applyModifications(_function);
    boolean _isCanceled_1 = monitor.isCanceled();
    if (_isCanceled_1) {
        throw new OperationCanceledException();
    }
    final Provider<LinkedPositionGroup> _function_1 = () -> {
        return this.createLinkedGroupFromReplaceEdits(replaceEdits, editor, oldName, progress.newChild(10));
    };
    return _function_1;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) URI(org.eclipse.emf.common.util.URI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) List(java.util.List) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) SubMonitor(org.eclipse.core.runtime.SubMonitor) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange) IEditorPart(org.eclipse.ui.IEditorPart) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LtkIssueAcceptor(org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor) IProject(org.eclipse.core.resources.IProject) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange)

Aggregations

IProject (org.eclipse.core.resources.IProject)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 URI (org.eclipse.emf.common.util.URI)2 EObject (org.eclipse.emf.ecore.EObject)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 CachingResourceSetProvider (org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider)2 Provider (com.google.inject.Provider)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EReference (org.eclipse.emf.ecore.EReference)1 IEditorPart (org.eclipse.ui.IEditorPart)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 IReferenceFinder (org.eclipse.xtext.findReferences.IReferenceFinder)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1 IRenameStrategy2 (org.eclipse.xtext.ide.refactoring.IRenameStrategy2)1