org.eclipse.xtext.ui.refactoring.IRenameStrategy Java Examples

The following examples show how to use org.eclipse.xtext.ui.refactoring.IRenameStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RenamedElementTracker.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Map<URI, URI> renameAndTrack(Iterable<URI> renamedElementURIs, String newName, ResourceSet resourceSet, IRenameStrategy renameStrategy, IProgressMonitor monitor) {
	Map<EObject, URI> renamedElement2oldURI = resolveRenamedElements(renamedElementURIs, resourceSet);
	if (monitor.isCanceled()) {
		throw new OperationCanceledException();
	}
	renameStrategy.applyDeclarationChange(newName, resourceSet);
	if (monitor.isCanceled()) {
		throw new OperationCanceledException();
	}
	Map<URI, URI> old2newURI = relocateRenamedElements(renamedElement2oldURI);
	if (monitor.isCanceled()) {
		throw new OperationCanceledException();
	}
	renameStrategy.revertDeclarationChange(resourceSet);
	return old2newURI;
}
 
Example #2
Source File: DefaultRenameElementStrategyTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRenameElementStrategy() throws Exception {
	final XtextResource resource = getResourceFromString("A { B { C } }");
	EObject targetElement = resource.getContents().get(0).eContents().get(0);
	assertNotNull(targetElement);
	assertTrue(targetElement instanceof Element);
	assertEquals("A", ((Element) targetElement).getName());
	IRenameStrategy renameElementStrategy = strategyProvider.get(targetElement, null);
	assertNotNull(renameElementStrategy);
	assertEquals("A", renameElementStrategy.getOriginalName());
	RefactoringStatus validateNewNameStatus = renameElementStrategy.validateNewName("A");
	assertTrue(validateNewNameStatus.isOK());
	validateNewNameStatus = renameElementStrategy.validateNewName("}");
	assertTrue(validateNewNameStatus.hasFatalError());
	validateNewNameStatus = renameElementStrategy.validateNewName("ref");
	assertTrue(validateNewNameStatus.hasError());
	renameElementStrategy.applyDeclarationChange("D", resource.getResourceSet());
	assertEquals("D", ((Element) targetElement).getName());
	renameElementStrategy.createDeclarationUpdates("D", resource.getResourceSet(), this);
	assertEquals(0, changes.size());
	assertEquals(1, textEdits.size());
	assertTrue(textEdits.get(0) instanceof ReplaceEdit);
	ReplaceEdit renameEdit = (ReplaceEdit) textEdits.get(0);
	assertEquals(0, renameEdit.getOffset());
	assertEquals(1, renameEdit.getLength());
	assertEquals("D", renameEdit.getText());
}
 
Example #3
Source File: RenameStrategyTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testInferredClassRenamed() throws Exception {
	XtendClass fooClass = (XtendClass) testHelper.xtendFile("Foo", "class Foo { }").getXtendTypes().get(0);
	IRenameStrategy renameStrategy = createRenameStrategy(fooClass);
	renameStrategy.applyDeclarationChange("Bar", fooClass.eResource().getResourceSet());
	JvmGenericType inferredType = associations.getInferredType(fooClass);
	JvmConstructor inferredConstructor = associations.getInferredConstructor(fooClass);
	assertEquals("Bar", fooClass.getName());
	assertEquals("Bar", inferredType.getSimpleName());
	assertEquals("Bar", inferredConstructor.getSimpleName());
	renameStrategy.revertDeclarationChange(fooClass.eResource().getResourceSet());
	inferredType = associations.getInferredType(fooClass);
	inferredConstructor = associations.getInferredConstructor(fooClass);
	assertEquals("Foo", fooClass.getName());
	assertEquals("Foo", inferredType.getSimpleName());
	assertEquals("Foo", inferredConstructor.getSimpleName());
}
 
Example #4
Source File: RenamedElementTrackerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testResolveElements() throws Exception {
	URI resourceURI = URI.createFileURI("testresource.refactoringtestlanguage");
	String textualModel = "A { B { C { ref A.B } } ref B }";
	XtextResource resource = getResource(textualModel, resourceURI.toString());
	Element elementA = (Element) resource.getContents().get(0).eContents().get(0);
	Element elementB = elementA.getContained().get(0);
	Element elementC = elementB.getContained().get(0);

	URI uriB = EcoreUtil.getURI(elementB);
	URI uriC = EcoreUtil.getURI(elementC);
	String newName = "newB";

	List<URI> renamedElementURIs = newArrayList(uriB, uriC);
	IRenameStrategy renameStrategy = getInjector().getInstance(IRenameStrategy.Provider.class).get(elementB, null);

	IRenamedElementTracker renamedElementTracker = new RenamedElementTracker();
	Map<URI, URI> original2newElementURIs = renamedElementTracker.renameAndTrack(renamedElementURIs, newName,
			resource.getResourceSet(), renameStrategy, new NullProgressMonitor());

	assertEquals("B", elementB.getName());
	assertEquals(2, original2newElementURIs.size());
	assertEquals(resourceURI.appendFragment(newName), original2newElementURIs.get(uriB));
	assertEquals(uriC, original2newElementURIs.get(uriC));
}
 
Example #5
Source File: XtextRenameStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected IRenameStrategy createRenameStrategy(final EObject targetElement, final IRenameElementContext context) {
	return new XtextSwitch<IRenameStrategy>() {

		@Override
		public IRenameStrategy caseAbstractMetamodelDeclaration(AbstractMetamodelDeclaration metamodelDeclaration) {
			return metamodelDeclarationProvider.get();
		}

		@Override
		public IRenameStrategy caseAbstractRule(AbstractRule rule) {
			return ruleProvider.get();
		}

		@Override
		public IRenameStrategy caseGrammar(Grammar grammar) {
			return XtextRenameStrategyProvider.super.createRenameStrategy(targetElement, context);
		}
	}.doSwitch(targetElement);
}
 
Example #6
Source File: DefaultRenameStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IRenameStrategy get(EObject targetEObject, IRenameElementContext renameElementContext) throws NoSuchStrategyException {
	IRenameStrategy renameStrategy = createRenameStrategy(targetEObject, renameElementContext);
	if (renameStrategy instanceof DefaultRenameStrategyProvider.IInitializable
			&& ((DefaultRenameStrategyProvider.IInitializable) renameStrategy).initialize(targetEObject,
					renameElementContext)) {
		return renameStrategy;
	}
	return null;
}
 
Example #7
Source File: DispatchMethodRenameStrategy.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void revertDeclarationChange(ResourceSet resourceSet) {
	for(IRenameStrategy child: children)
		child.revertDeclarationChange(resourceSet);
	for(JvmOperation dispatcher: dispatchers) {
		dispatcher.setSimpleName(getOriginalName());
	}
}
 
Example #8
Source File: DispatchMethodRenameStrategy.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void applyDeclarationChange(String newName, ResourceSet resourceSet) {
	for(IRenameStrategy child: children)
		child.applyDeclarationChange(newName, resourceSet);
	for(JvmOperation dispatcher: dispatchers) {
		dispatcher.setSimpleName(newName);
	}
}
 
Example #9
Source File: XtendRenameStrategyProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected IRenameStrategy createRenameStrategy(EObject targetEObject, IRenameElementContext renameElementContext) {
	if (renameElementContext instanceof DispatchMethodRenameContext) 
		return dispatchStrategyProvider.get();
	else
		return super.createRenameStrategy(targetEObject, renameElementContext);
}
 
Example #10
Source File: DispatchMethodRenameStrategy.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public RefactoringStatus validateNewName(String newName) {
	RefactoringStatus status = new RefactoringStatus();
	for(IRenameStrategy child: children)
		status.merge(child.validateNewName(newName));
	return status;
}
 
Example #11
Source File: RenameStrategyTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testInferredMethodRenamed() throws Exception {
	XtendFunction fooMethod = (XtendFunction) ((XtendClass)testHelper.xtendFile("Foo", "class Foo { def Foo foo() {this} }")
			.getXtendTypes().get(0)).getMembers().get(0);
	IRenameStrategy renameStrategy = createRenameStrategy(fooMethod);
	renameStrategy.applyDeclarationChange("bar", fooMethod.eResource().getResourceSet());
	assertEquals("bar", fooMethod.getName());
	JvmOperation inferredOperation = associations.getDirectlyInferredOperation(fooMethod);
	assertEquals("bar", inferredOperation.getSimpleName());
	renameStrategy.revertDeclarationChange(fooMethod.eResource().getResourceSet());
	inferredOperation = associations.getDirectlyInferredOperation(fooMethod);
	assertEquals("foo", fooMethod.getName());
	assertEquals("foo", inferredOperation.getSimpleName());
}
 
Example #12
Source File: JvmMemberRenameStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IRenameStrategy get(EObject targetEObject, IRenameElementContext renameElementContext) throws NoSuchStrategyException {
	if(targetEObject instanceof JvmMember) {
		JvmMemberRenameStrategy jvmMemberRenameStrategy = guiceStartegyProvider.get();
		jvmMemberRenameStrategy.initialize((JvmMember) targetEObject, renameElementContext);
		return jvmMemberRenameStrategy;
	}
	return delegate == null ? null : delegate.get(targetEObject, renameElementContext);
}
 
Example #13
Source File: XtendRenameStrategyProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IRenameStrategy get(EObject targetEObject, IRenameElementContext renameElementContext) throws NoSuchStrategyException {
	if(targetEObject instanceof XtendConstructor)
		// Xtend constructors can be ignored in rename refactorings
		return null;
	return super.get(targetEObject, renameElementContext);
}
 
Example #14
Source File: SharedCommonTypesModule.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void configure(Binder binder) {
	binder.bind(IEObjectHoverProvider.class).to(JdtHoverProvider.class);
	binder.bind(IEObjectHoverDocumentationProvider.class).to(JdtHoverDocumentationProvider.class);
	binder.bind(IResourceServiceProvider.class).to(SharedCommonTypesResourceServiceProvider.class);
	binder.bind(IResourceSetProvider.class).to(XtextResourceSetProvider.class);
	binder.bindConstant().annotatedWith(Names.named(Constants.FILE_EXTENSIONS)).to("java");

	binder.bind(IQualifiedNameProvider.class).to(JvmIdentifiableQualifiedNameProvider.class);
	binder.bind(ICopyQualifiedNameService.class).to(DefaultCopyQualifiedNameService.class);
	binder.bind(IJvmTypeProvider.Factory.class).to(JdtTypeProviderFactory.class);
	binder.bind(IRenameRefactoringProvider.class).to(DefaultRenameRefactoringProvider.class);
	binder.bind(AbstractRenameProcessor.class).to(JvmMemberRenameProcessor.class);
	binder.bind(IRenameStrategy.Provider.class).to(JvmMemberRenameStrategy.Provider.class);
	binder.bind(RefactoringResourceSetProvider.class).to(JvmRefactoringResourceSetProvider.class);
	binder.bind(String.class).annotatedWith(Names.named(Constants.LANGUAGE_NAME)).toInstance("Java");

	binder.bind(IResourceDescriptions.class).annotatedWith(Names.named(ResourceDescriptionsProvider.LIVE_SCOPE))
			.to(LiveShadowedResourceDescriptions.class);
	binder.bind(IResourceDescriptions.class)
			.annotatedWith(Names.named(ResourceDescriptionsProvider.NAMED_BUILDER_SCOPE))
			.to(CurrentDescriptions.ResourceSetAware.class);
	binder.bind(IResourceDescriptions.class)
			.annotatedWith(Names.named(ResourceDescriptionsProvider.PERSISTED_DESCRIPTIONS))
			.to(IBuilderState.class);

	binder.bind(IWorkspaceRoot.class).toInstance(ResourcesPlugin.getWorkspace().getRoot());
	binder.bind(ITraceForStorageProvider.class).to(TraceForStorageProvider.class);

	binder.bind(IReferenceUpdater.class).to(NullReferenceUpdater.class);
}
 
Example #15
Source File: DefaultRenameElementHandler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example #16
Source File: RenameRefactoringController.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String getOriginalName(final XtextEditor xtextEditor) {
	return xtextEditor.getDocument().tryReadOnly(new IUnitOfWork<String, XtextResource>() {
		@Override
		public String exec(XtextResource state) throws Exception {
			EObject targetElement = state.getResourceSet().getEObject(renameElementContext.getTargetElementURI(),
					false);
			if (targetElement == null) {
				return null;
			}
			IRenameStrategy.Provider strategyProvider = globalServiceProvider.findService(targetElement,
					IRenameStrategy.Provider.class);
			try {
				if (strategyProvider != null) {
					IRenameStrategy strategy = strategyProvider.get(targetElement, renameElementContext);
					if (strategy != null)
						return strategy.getOriginalName();
				}
			} catch(NoSuchStrategyException e) {
				// null
			}
			ISimpleNameProvider simpleNameProvider = globalServiceProvider.findService(targetElement, ISimpleNameProvider.class);
			if (simpleNameProvider != null)
				return simpleNameProvider.getSimpleName(targetElement);
			else
				return null;
		}
	});
}
 
Example #17
Source File: SARLRenameStrategyProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected IRenameStrategy createRenameStrategy(EObject targetEObject, IRenameElementContext renameElementContext) {
	if (targetEObject instanceof SarlScript) {
		return this.guiceEcorePackageStrategyProvider.get();
	}
	return super.createRenameStrategy(targetEObject, renameElementContext);
}
 
Example #18
Source File: AbstractXtendUiModule.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return DefaultJvmModelRenameStrategy.class;
}
 
Example #19
Source File: AbstractXImportSectionTestLangUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return DefaultJvmModelRenameStrategy.class;
}
 
Example #20
Source File: AbstractBug462047LangUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return DefaultJvmModelRenameStrategy.class;
}
 
Example #21
Source File: AbstractPureXbaseUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return DefaultJvmModelRenameStrategy.class;
}
 
Example #22
Source File: AbstractPureXbaseUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public void configureJvmMemberRenameStrategy$Provider$Delegate(Binder binder) {
	binder.bind(IRenameStrategy.Provider.class).annotatedWith(JvmMemberRenameStrategy.Provider.Delegate.class).to(DefaultRenameStrategyProvider.class);
}
 
Example #23
Source File: AbstractPureXbaseUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy.Provider> bindIRenameStrategy$Provider() {
	return JvmMemberRenameStrategy.Provider.class;
}
 
Example #24
Source File: AbstractGamlUiModule.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return DefaultRenameStrategy.class;
}
 
Example #25
Source File: AbstractSARLUiModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public void configureJvmMemberRenameStrategy$Provider$Delegate(Binder binder) {
	binder.bind(IRenameStrategy.Provider.class).annotatedWith(JvmMemberRenameStrategy.Provider.Delegate.class).to(DefaultRenameStrategyProvider.class);
}
 
Example #26
Source File: AbstractSARLUiModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy.Provider> bindIRenameStrategy$Provider() {
	return SARLRenameStrategyProvider.class;
}
 
Example #27
Source File: AbstractXbaseWithAnnotationsUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return XbaseRenameStrategy.class;
}
 
Example #28
Source File: AbstractXbaseUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return XbaseRenameStrategy.class;
}
 
Example #29
Source File: AbstractSARLUiModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public Class<? extends IRenameStrategy> bindIRenameStrategy() {
	return XtendRenameStrategy.class;
}
 
Example #30
Source File: RenameElementProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected IRenameStrategy createRenameElementStrategy(EObject targetElement,
		IRenameElementContext renameElementContext) throws NoSuchStrategyException {
	IRenameStrategy result = strategyProvider.get(targetElement, renameElementContext);
	return result;
}