Search in sources :

Example 1 with ISimpleNameProvider

use of org.eclipse.xtext.ui.refactoring2.rename.ISimpleNameProvider 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;
}
Also used : ISimpleNameProvider(org.eclipse.xtext.ui.refactoring2.rename.ISimpleNameProvider) INode(org.eclipse.xtext.nodemodel.INode) IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ITextSelection(org.eclipse.jface.text.ITextSelection) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IRenameStrategy2 (org.eclipse.xtext.ide.refactoring.IRenameStrategy2)1 INode (org.eclipse.xtext.nodemodel.INode)1 IRenameStrategy (org.eclipse.xtext.ui.refactoring.IRenameStrategy)1 NoSuchStrategyException (org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException)1 ISimpleNameProvider (org.eclipse.xtext.ui.refactoring2.rename.ISimpleNameProvider)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1 TextRegion (org.eclipse.xtext.util.TextRegion)1