Running refactorings programmatically

You can call refactoring from an Execute() method of an action in the following
manner:

[ActionHandler("MyRefactoringAction")] internal class MyRefactoringAction : IActionHandler { public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate) { return new RenameRefactoringWorkflow().IsAvailable(context); } public void Execute(IDataContext context, DelegateExecute nextExecute) { IRefactoringWorkflow workflow = new RenameRefactoringWorkflow(); if (workflow.Initialize(context, null)) new WorkflowProcessor(workflow, (ISolution)context.GetData(DataConstants.SOLUTION)).Execute(); } } ]]>
You can substitute RenameRefactoringWorkflow with any other class that implements
IRefactoringWorkflow. Note that the code above will show refactoring wizard.

Thanks,
Andrey Simanovsky


2 comments
Comment actions Permalink

It is using the refactorings without the UI (so programatically driving) which I'm interested in.
I think I've got the rough gist of how things hang together but I'm getting an exception on ending the transaction (it does actually do the rename though).

I guess I'm violating the use sequence of the PsiManager.
My code is below; the CommitTransaction() trips some assertion:

";
        if(element != null)
        {
            description = element.ShortName;   
            AtomicRename rename = new AtomicRename(element, "ANewName");
            rename.SearchReferences(new Progress(), true);           
            description = rename.References.Length.ToString();
            PsiManagerImpl.GetInstance(solution).StartTransaction();
            rename.Rename(new Progress());
            PsiManagerImpl.GetInstance(solution).CommitTransaction();]]>

0
Comment actions Permalink

Hello James,


JM> I think I've got the rough gist of how things hang together but I'm
JM> getting an exception on ending the transaction (it does actually do
JM> the rename though).
JM>
JM> I guess I'm violating the use sequence of the PsiManager. My code is
JM> below; the CommitTransaction() trips some assertion:

It seems like you didn't get ModificationCookie for the file.

using (ModificationCookie mc = file.EnsureWritable())
{
// here modifying code
}

I'm writing from memory, so I may mess things up, but the idea may help you
find your way :)

Sincerely,
Ilya Ryzhenkov


0

Please sign in to leave a comment.