Search in sources :

Example 1 with IRenameElementContext

use of org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext in project xtext-xtend by eclipse.

the class XtendFileRenameParticipant method createRenameElementContexts.

@Override
protected List<? extends IRenameElementContext> createRenameElementContexts(Object element) {
    if (super.getNewName().endsWith(".xtend")) {
        IFile file = (IFile) element;
        final IPath filePath = file.getFullPath();
        final IPath newPath = file.getFullPath().removeLastSegments(1).append(getNewName() + ".xtend");
        String className = trimFileExtension(file.getName());
        if (className != null) {
            ResourceSet resourceSet = resourceSetProvider.get(file.getProject());
            URI resourceURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
            Resource resource = resourceSet.getResource(resourceURI, true);
            if (resource != null && !resource.getContents().isEmpty()) {
                for (XtendTypeDeclaration type : EcoreUtil2.eAllOfType(resource.getContents().get(0), XtendTypeDeclaration.class)) {
                    if (equal(className, type.getName())) {
                        IRenameElementContext renameElementContext = renameContextFactory.createRenameElementContext(type, null, null, (XtextResource) resource);
                        if (renameElementContext instanceof IChangeRedirector.Aware)
                            ((IChangeRedirector.Aware) renameElementContext).setChangeRedirector(new IChangeRedirector() {

                                @Override
                                public IPath getRedirectedPath(IPath source) {
                                    return source.equals(filePath) ? newPath : source;
                                }
                            });
                        return singletonList(renameElementContext);
                    }
                }
            }
        }
    }
    return super.createRenameElementContexts(element);
}
Also used : IFile(org.eclipse.core.resources.IFile) IChangeRedirector(org.eclipse.xtext.ui.refactoring.IChangeRedirector) IPath(org.eclipse.core.runtime.IPath) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 2 with IRenameElementContext

use of org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext in project xtext-xtend by eclipse.

the class JavaRefactoringIntegrationTest method createXtendRenameRefactoring.

protected ProcessorBasedRefactoring createXtendRenameRefactoring(final XtextEditor editor, final int offset, String newName) {
    IRenameElementContext renameElementContext = createRenameElementContext(editor, offset);
    ProcessorBasedRefactoring renameRefactoring = renameRefactoringProvider.getRenameRefactoring(renameElementContext);
    RefactoringProcessor processor = renameRefactoring.getProcessor();
    if (processor instanceof AbstractRenameProcessor)
        ((AbstractRenameProcessor) processor).setNewName(newName);
    else if (processor instanceof JavaRenameProcessor)
        ((JavaRenameProcessor) processor).setNewElementName(newName);
    return renameRefactoring;
}
Also used : CompositeRefactoringProcessor(org.eclipse.xtext.common.types.ui.refactoring.participant.CompositeRefactoringProcessor) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) AbstractRenameProcessor(org.eclipse.xtext.ui.refactoring.impl.AbstractRenameProcessor) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)

Aggregations

IRenameElementContext (org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext)2 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 JavaRenameProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)1 ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)1 RefactoringProcessor (org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 CompositeRefactoringProcessor (org.eclipse.xtext.common.types.ui.refactoring.participant.CompositeRefactoringProcessor)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IChangeRedirector (org.eclipse.xtext.ui.refactoring.IChangeRedirector)1 AbstractRenameProcessor (org.eclipse.xtext.ui.refactoring.impl.AbstractRenameProcessor)1