How to implement the "rename resource" action in a plugin?


I have been trying for some days to implement the Resource file "rename resource" refactor, which is explained here:

This changes a resource file resource string name by another valid string value and replaces all occurrences it finds.

First I get the ISolution and navigate through all its projects through GetAllProjects() method, getting all its subItems through GetSubItems() and recursively on the folders, getting all resource files.
On the resource files, I navigate the Data Tags (resource file entries) through GetDataTags() method so I have all the resource files and its resource strings.

Let's say that I want to replace the resource string names by GUID's so they are unique...
The benefits of this are automating the resource string rename of the full solution's resource string names so saving a lot of hours of refactoring of the XAML views and source code files.. And also to save the time in doing that manually as well.

I have gotten a bit of support from ReSharper experts and the latest approach is the following piece of code (that is not working):

private void ChangeResourceStringNameRefactor(IResxDataTagDeclaration resxDataTagDeclaration, IDataContext context)
    var guidstring = Guid.NewGuid().ToString();
    var guidstringWithoutarrows = guidstring.Replace("-", "");
    Lifetimes.Using(lifetime => this.ExecuteResourceRename(context, guidstringWithoutarrows));
public void ExecuteResourceRename(IDataContext context, string name)
    var provider = RefactoringsManager.Instance.GetWorkflowProviders<RenameResourceWorkflowProvider>().FirstOrDefault();
    if (provider != null)
        var workflow = provider.CreateWorkflow(context).OfType<RenameResourceDrivenWorkflow>().FirstOrDefault();
        if (workflow != null && workflow.IsAvailable(context))
            workflow.Args.ResourceElementNewName = name;
            WorkflowExecuter.ExecuteBatch(context, workflow);

The problem is that workflow.IsAvailable(context) always returns null so it cannot be executed.
Any guidance, help or link on this matter would be great.

Thanks in advance,

1 comment
Comment actions Permalink

Hi Jose. The context needs to have rules defined that can provide a collection of IReferences, a text selection, or a declared element. If it can't find any of those, the action will be unavailable. You can create a new context in a manner similar to how you were doing it with your original approach:

        .AddRule("ManualChangeNameFix", DataConstants.DECLARED_ELEMENTS, element.ToDeclaredElementsDataConstant())
        .AddRule("ManualChangeNameFix", JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION, solution)
        .AddRule("ManualChangeNameFix", RenameWorkflow.RenameDataProvider, srdp)

The workflow should be available, given this context.


Please sign in to leave a comment.