Modify project properties does not work

Hi,

I am trying to write a plugin für R# which is able to do stuff like modifying a project name and adding project references.
I was able to get to the point where I could select a project in the solution explorer and then call my plugin thru the "Resharper" menu. Then I retrieve a reference to the selected project like this:

     IProject myProject = context.GetData<IProjectModelElement>(DataConstants.PROJECT_MODEL_ELEMENT) as IProject;

Now I try to change the name of this project:

     myProject.Rename("SomeNewProjectName");

This does not lead to any effect. Also no error message seems to be displayed.

Now I found out that may need to use a WriteLock to do that:

     using (WriteLockCookie.Create())
     {
          myProject.Rename("SomeNewProjectName");
     }

This also has no effect.

Same thing with adding a reference:

     using (WriteLockCookie.Create())
     {
          myProject.AddProjectReference(someOtherProject);
     }

No visible effect in Solution explorer. However, in this case when I look in the debugger I can see that myProject.myProjectReferences actually increases it's size from 0 to 1. But I cannot see the reference in the solution explorer.

Does anyone know what I do wrong? Would you suggest to write a native Visual Studio Add-in for tasks like this instead?

Cheers,
Sven

0
6 comments

Sven,

Thanks for your feedback. It turns out that myProject.Rename("SomeNewProjectName"); does not work eventually. We'll try to fix it ASAP. The rename of files works though.

There is another problem in our public API: some of the the methods are integrated with VS and other just used to reflect changes, that happend in VS. To be more precise
instead of myProject.AddProjectReference(someOtherProject); call myProject.AddModuleReference(someOtherProject); It will do the trick.


0

I've just fixed myProject.Rename("SomeNewProjectName");
Try the upcomin nightly from http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.5+Nightly+Builds

0
Avatar
Permanently deleted user

Thank you very much for your reply! I'll try this in the next few days...

Just in case it would work:
Is the WriteLockCookie around this code the correct approach?
I am still slightly confused about the "Locking and Cookies"-concept. I don't
really know when to use WriteLockCookie and when to use CommandCookie...

Thanks!

0

Sven,

the overall idea is the following. Read and Write lock cookies are used to make sure that reading (or correspondingly writing) to project model or PSI is allowed. If you take ReadLock you may be sure that nothing in the model is altered unless you request it. To modify PSI or project model you need WriteLock.

CommandCookie is used to modify documents not using write PSI API. Normally, you should take ReadLock to make sure nobody changes anything, than CommandCookie and alter document. To modify document accurately you need to take ModificationCookie like the following

using (ModificationCookie modificationCookie = document.EnsureWritable())
{
  if (modificationCookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
       return;

  // your code

}


or vith DocumentUtil.ExecuteIfWritable
or ISolution.EnsureFilesWritable

0
Avatar
Permanently deleted user

It's really working right away. Cool stuff! Thanks again!

0

I was wrong a bit. CommandCookie is just brackets fro undo actions. The rest is true, though

0

Please sign in to leave a comment.