Search in sources :

Example 1 with RenameContext

use of org.eclipse.xtext.ide.refactoring.RenameContext in project xtext-core by eclipse.

the class RenameService method rename.

@Override
public WorkspaceEdit rename(final WorkspaceManager workspaceManager, final RenameParams renameParams, final CancelIndicator cancelIndicator) {
    WorkspaceEdit _xblockexpression = null;
    {
        final URI uri = this._uriExtensions.toUri(renameParams.getTextDocument().getUri());
        final ServerRefactoringIssueAcceptor issueAcceptor = this.issueProvider.get();
        final Function2<Document, XtextResource, WorkspaceEdit> _function = (Document document, XtextResource resource) -> {
            final ProjectManager projectManager = workspaceManager.getProjectManager(uri);
            final XtextResourceSet resourceSet = projectManager.createNewResourceSet(projectManager.getIndexState().getResourceDescriptions());
            resourceSet.getLoadOptions().put(ResourceDescriptionsProvider.LIVE_SCOPE, Boolean.valueOf(true));
            final int offset = document.getOffSet(renameParams.getPosition());
            final WorkspaceEdit workspaceEdit = new WorkspaceEdit();
            final Resource xtextResource = resourceSet.getResource(resource.getURI(), true);
            if ((xtextResource instanceof XtextResource)) {
                final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(((XtextResource) xtextResource), offset);
                if (((element == null) || element.eIsProxy())) {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("No element found at position line:");
                    int _line = renameParams.getPosition().getLine();
                    _builder.append(_line);
                    _builder.append(" column:");
                    int _character = renameParams.getPosition().getCharacter();
                    _builder.append(_character);
                    issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, _builder.toString());
                } else {
                    String _newName = renameParams.getNewName();
                    URI _uRI = EcoreUtil.getURI(element);
                    final RenameChange change = new RenameChange(_newName, _uRI);
                    final IChangeSerializer changeSerializer = this.changeSerializerProvider.get();
                    final RenameContext context = new RenameContext(Collections.<RenameChange>unmodifiableList(CollectionLiterals.<RenameChange>newArrayList(change)), resourceSet, changeSerializer, issueAcceptor);
                    this.renameStrategy.applyRename(context);
                    final ChangeConverter changeConverter = this.converterFactory.create(workspaceManager, workspaceEdit);
                    changeSerializer.applyModifications(changeConverter);
                }
            } else {
                issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "Loaded resource is not an XtextResource", resource.getURI());
            }
            return workspaceEdit;
        };
        _xblockexpression = workspaceManager.<WorkspaceEdit>doRead(uri, _function);
    }
    return _xblockexpression;
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ChangeConverter(org.eclipse.xtext.ide.server.rename.ChangeConverter) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) XtextResource(org.eclipse.xtext.resource.XtextResource) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ServerRefactoringIssueAcceptor(org.eclipse.xtext.ide.server.rename.ServerRefactoringIssueAcceptor) ProjectManager(org.eclipse.xtext.ide.server.ProjectManager) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 2 with RenameContext

use of org.eclipse.xtext.ide.refactoring.RenameContext 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)

Example 3 with RenameContext

use of org.eclipse.xtext.ide.refactoring.RenameContext in project xtext-eclipse by eclipse.

the class RenameElementProcessor2 method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor pm, final CheckConditionsContext context) throws CoreException, OperationCanceledException {
    URI _targetElementURI = this.renameElementContext.getTargetElementURI();
    final RenameChange change = new RenameChange(this.newName, _targetElementURI);
    final RenameContext renameContext = new RenameContext(Collections.<RenameChange>unmodifiableList(CollectionLiterals.<RenameChange>newArrayList(change)), this.resourceSet, this.changeSerializer, this.status);
    this.renameStrategy.applyRename(renameContext);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("Rename ");
    _builder.append(this.originalName);
    _builder.append(" to ");
    _builder.append(this.newName);
    final ChangeConverter changeConverter = this.changeConverterFactory.create(_builder.toString(), null, this.status);
    this.changeSerializer.applyModifications(changeConverter);
    this.change = changeConverter.getChange();
    return this.status.getRefactoringStatus();
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ChangeConverter(org.eclipse.xtext.ui.refactoring2.ChangeConverter) URI(org.eclipse.emf.common.util.URI)

Example 4 with RenameContext

use of org.eclipse.xtext.ide.refactoring.RenameContext in project xtext-core by eclipse.

the class RenameService2 method applyModifications.

/**
 * <p>
 * Performs the actual renaming. Runs within a read transaction on the index and a live-scoped resource set.
 * </p>
 * <p>
 * Override this method to implement custom renaming behavior.
 * </p>
 *
 * @since 2.25
 */
protected void applyModifications(EObject element, WorkspaceEdit workspaceEdit, ServerRefactoringIssueAcceptor issueAcceptor, IRenameService2.Options options, ILanguageServerAccess.Context context) {
    IResourceServiceProvider services = serviceProviderRegistry.getResourceServiceProvider(element.eResource().getURI());
    IChangeSerializer changeSerializer = services.get(IChangeSerializer.class);
    RenameChange change = new RenameChange(options.getRenameParams().getNewName(), EcoreUtil.getURI(element));
    ResourceSet resourceSet = element.eResource().getResourceSet();
    RenameContext renameContext = new RenameContext(Lists.newArrayList(change), resourceSet, changeSerializer, issueAcceptor);
    IRenameStrategy2 renameStrategy = services.<IRenameStrategy2>get(IRenameStrategy2.class);
    renameStrategy.applyRename(renameContext);
    ChangeConverter2.Factory converterFactory = services.<ChangeConverter2.Factory>get(ChangeConverter2.Factory.class);
    ChangeConverter2 changeConverter = converterFactory.create(workspaceEdit, options.getLanguageServerAccess());
    changeSerializer.applyModifications(changeConverter);
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Aggregations

RenameChange (org.eclipse.xtext.ide.refactoring.RenameChange)4 RenameContext (org.eclipse.xtext.ide.refactoring.RenameContext)4 URI (org.eclipse.emf.common.util.URI)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)3 EObject (org.eclipse.emf.ecore.EObject)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 IRenameStrategy2 (org.eclipse.xtext.ide.refactoring.IRenameStrategy2)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IProject (org.eclipse.core.resources.IProject)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 IEditorPart (org.eclipse.ui.IEditorPart)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1