Skip to content

Instantly share code, notes, and snippets.

@MarkusAmshove
Created November 13, 2017 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MarkusAmshove/fde39b4ada477f5cf0536ecd85877c88 to your computer and use it in GitHub Desktop.
Save MarkusAmshove/fde39b4ada477f5cf0536ecd85877c88 to your computer and use it in GitHub Desktop.
Enhancing the Refactoring in your Xtext-DSL
class MyDslRenameStragegy extends DefaultRenameStrategy
{
private IRenameElementContext context
private static val DSL_FILE_EXTENSION = ".mydsl"
// This gets called when you start renaming (e.g. ALT+SHIFT+R on an element in Eclipse)
override initialize(EObject targetElement, IRenameElementContext context)
{
this.context = context
return super.initialize(targetElement, context)
}
// This is where we register a change to the document
override createDeclarationUpdates(String newName, ResourceSet resourceSet,
IRefactoringUpdateAcceptor updateAcceptor)
{
super.createDeclarationUpdates(newName, resourceSet, updateAcceptor)
// Get the path to the affected file
val path = getPathOfFile(targetElementOriginalURI, resourceSet)
// Get the filename
val lastSegment = path.lastSegment
// Register the rename
if (path != null && isMyDslFile(lastSegment, originalName))
{
updateAcceptor.accept(targetElementOriginalURI.trimFragment,
new RenameResourceChange(path, newName + "." + path.fileExtension))
}
}
private def getPathOfFile(URI elementURI, ResourceSet resourceSet)
{
var targetObject = resourceSet.getEObject(elementURI, false)
if (targetObject instanceof RootElement)
{
var resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(targetObject).trimFragment()
if (!resourceURI.isPlatformResource())
throw new RefactoringException("Renamed type does not reside in the workspace")
var path = new Path("/").append(new Path(resourceURI.path()).removeFirstSegments(1))
if (context instanceof IChangeRedirector.Aware)
{
if ((context as IChangeRedirector.Aware).getChangeRedirector().getRedirectedPath(path) != path)
return null
}
return path
}
return null
}
private def isMyDslFile(String lastSegment, String originalName)
{
return lastSegment == originalName + DSL_FILE_EXTENSION
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment