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;
}
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;
}
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();
}
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);
}
Aggregations