use of org.eclipse.xtext.ide.refactoring.IRenameStrategy2 in project xtext-eclipse by eclipse.
the class DefaultRenameElementHandler method isRefactoringEnabled.
protected boolean isRefactoringEnabled(IRenameElementContext renameElementContext, XtextResource resource) {
ResourceSet resourceSet = resource.getResourceSet();
if (renameElementContext != null && resourceSet != null) {
EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
if (targetElement != null && !targetElement.eIsProxy()) {
if (targetElement.eResource() == resource && renameElementContext.getTriggeringEditorSelection() instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) renameElementContext.getTriggeringEditorSelection();
ITextRegion selectedRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
INode crossReferenceNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, selectedRegion);
if (crossReferenceNode == null) {
// selection is on the declaration. make sure it's the name
ITextRegion significantRegion = locationInFileProvider.getSignificantTextRegion(targetElement);
if (!significantRegion.contains(selectedRegion))
return false;
}
}
IRenameStrategy.Provider renameStrategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
try {
if (renameStrategyProvider.get(targetElement, renameElementContext) != null) {
return true;
} else {
IRenameStrategy2 strategy2 = globalServiceProvider.findService(targetElement, IRenameStrategy2.class);
ISimpleNameProvider simpleNameProvider = globalServiceProvider.findService(targetElement, ISimpleNameProvider.class);
return strategy2 != null && simpleNameProvider.canRename(targetElement);
}
} catch (NoSuchStrategyException e) {
MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot rename element", e.getMessage());
}
}
}
return false;
}
use of org.eclipse.xtext.ide.refactoring.IRenameStrategy2 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;
}
Aggregations