Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.refactoring.impl;
  
 import static org.eclipse.ltk.core.refactoring.RefactoringStatus.*;
 
 
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.SubMonitor;
 import  org.eclipse.ltk.core.refactoring.RefactoringStatus;
 import  org.eclipse.text.edits.ReplaceEdit;
 import  org.eclipse.text.edits.TextEdit;
 
Creates updates for all references from Xtext based resources to a renamed element using Xtext's serialization API.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 
 	private static final Logger log = Logger.getLogger(DefaultReferenceUpdater.class);
 	
 

Since:
2.4 Replaces the obsolete CrossReferenceSerializerFacade
 
 
 	protected void createReferenceUpdates(ElementRenameArguments elementRenameArguments,
 			Multimap<URIIReferenceDescriptionresource2referencesResourceSet resourceSet,
 			IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
 		SubMonitor progress = SubMonitor.convert(monitor"Creating reference updates"resource2references.keySet()
 				.size());
 		for (URI referringResourceURI : resource2references.keySet()) {
 			if (progress.isCanceled())
 				return;
 			Resource referringResource = resourceSet.getResource(referringResourceURIfalse);
 			if (!(referringResource instanceof XtextResource)) {
 				updateAcceptor.getRefactoringStatus().add(ERROR, "Resource {0} is not an XtextResource.",
 						referringResource.getURI(), resourceSet);
 			} else {
 				Iterable<IReferenceDescriptionreferenceDescriptions = resource2references.get(referringResourceURI);
 				processReferringResource(referringResourcereferenceDescriptionselementRenameArguments,
 						updateAcceptor);
 			}
 			progress.worked(1);
 		}
 	}

Override this method for pre- or post-processing hooks.
 
 	protected void processReferringResource(Resource referringResource,
 			Iterable<IReferenceDescriptionreferenceDescriptionsElementRenameArguments elementRenameArguments,
 			IRefactoringUpdateAcceptor updateAcceptor) {
 		((XtextResourcereferringResource).getCache().clear(referringResource);
 		for (IReferenceDescription referenceDescription : referenceDescriptions) {
 			createReferenceUpdate(referenceDescriptionreferringResource.getURI(), elementRenameArguments,
 					referringResource.getResourceSet(), updateAcceptor);
 		}
 	}
 
 	protected void createReferenceUpdate(IReferenceDescription referenceDescriptionURI referringResourceURI,
 			ElementRenameArguments elementRenameArgumentsResourceSet resourceSet,
			IRefactoringUpdateAcceptor updateAcceptor) {
		URI referringElementNewURI = elementRenameArguments
				.getNewElementURI(referenceDescription.getSourceEObjectUri());
		EObject referringElement = resourceSet.getEObject(referringElementNewURIfalse);
		URI targetElementNewURI = elementRenameArguments.getNewElementURI(referenceDescription.getTargetEObjectUri());
		EObject newTargetElement = resourceSet.getEObject(targetElementNewURIfalse);
		if (newTargetElement == null) {
			updateAcceptor.getRefactoringStatus().add(RefactoringStatus.ERROR, "Cannot find new target element."targetElementNewURI);
			.error("Cannot find new target element. ReferringElement:" + referringElement + " targetElementNewURI:" + targetElementNewURI);
			return;
		}
		createReferenceUpdate(referringElementreferringResourceURIreferenceDescription.getEReference(),
				referenceDescription.getIndexInList(), newTargetElementupdateAcceptor);
	}
	protected void createReferenceUpdate(EObject referringElementURI referringResourceURIEReference reference,
			int indexInListEObject newTargetElementIRefactoringUpdateAcceptor updateAcceptor) {
		if (!.isValueInListTransient(referringElementindexInListreference)) {
			ITextRegion referenceTextRegion = .getFullTextRegion(referringElementreference,
					indexInList);
			CrossReference crossReference = getCrossReference(referringElementreferenceTextRegion.getOffset());
			if (crossReference != null) {
				RefTextEvaluator refTextComparator = getRefTextEvaluator(referringElementreferringResourceURIreference,
						indexInListnewTargetElement);
				String newReferenceText = .getCrossRefText(referringElement,
						crossReferencenewTargetElementrefTextComparatorreferenceTextRegionupdateAcceptor.getRefactoringStatus());
				if (newReferenceText == null) {
					newReferenceText = resolveNameConflict(referringElementreferencenewTargetElementupdateAcceptor);
				}
				if (newReferenceText == null) {
					updateAcceptor.getRefactoringStatus().add(RefactoringStatus.ERROR, "Refactoring introduces a name conflict."referringElementreferenceTextRegion);
				}
				createTextChange(referenceTextRegionnewReferenceTextreferringElementnewTargetElementreference
						referringResourceURIupdateAcceptor);
			}
		}
	}

Return null if it is not possible to resolve a name conflict; otherwise a name which should be used.

Parameters:
updateAcceptor
Since:
2.6
	protected String resolveNameConflict(EObject referringElementEReference referenceEObject newTargetElementIRefactoringUpdateAcceptor updateAcceptor) {
		return null;
	}

The result is used to determine the best new link text in case of multiple possibilities. By default, the shortest text is chosen.

Since:
2.4
	protected RefTextEvaluator getRefTextEvaluator(EObject referringElementURI referringResourceURIEReference reference,
			int indexInListEObject newTargetElement) {
		// by default choose the shortest text
		return new RefTextEvaluator() {
			public boolean isValid(IEObjectDescription target) {
				return true;
			}
			public boolean isBetterThan(String newTextString currentText) {
				return newText.length() < currentText.length();
			}
		};
	}
	protected void createTextChange(ITextRegion referenceTextRegionString newReferenceText,
			EObject referringElementEObject newTargetElementEReference reference
			URI referringResourceURIIRefactoringUpdateAcceptor updateAcceptor) {
		if (newReferenceText != null) {
			TextEdit referenceEdit = new ReplaceEdit(referenceTextRegion.getOffset(),
					referenceTextRegion.getLength(), newReferenceText);
			updateAcceptor.accept(referringResourceURIreferenceEdit);
		}
	}
	protected CrossReference getCrossReference(EObject referringElementint offset) {
		ICompositeNode node = NodeModelUtils.getNode(referringElement);
		if (node != null) {
			while (iter.hasNext()) {
				INode childNode = iter.next();
				if (childNode.getOffset() >= offset && childNode.getGrammarElement() instanceof CrossReference)
					return (CrossReferencechildNode.getGrammarElement();
			}
		}
		return null;
	}
	}
	}
New to GrepCode? Check out our FAQ X