Explore code (Psi?) structure for a project?

I'm attempting to use Resharper inside a custom Project Template Wizard in Visual Studio.  Inside this wizard, I want to perform some resharper refactorings after the template has been expanded.  I have the Resharper Shell.Instance, the solution, and the project that I just expanded.  What I'm missing is the link of how I find, for instance, an INamespaceDeclaration so I can run the rename refactoring on it.

Can anyone help?  I found TextControlToPsi, so I could open a file I know to contain the namespace and refactor there, but I'd rather something that allows me to find all ITreeNodes in a given project.  I bet I'm missing something obvious.

1 comment
Comment actions Permalink

If you've got access to a text control, there's nothing wrong with using that to get a psi file.

Alternatively, you can use the PsiModuleManger to get at the IPsiSourceFile. You can use PsiModuleManger.GetInstance(solution) to get the manager, then use GetPsiSourceFilesFor(IProjectFile).

You can get an IProjectFile via ISolution.GetProjectByName, or iterating ISolution.GetTopLevelProjects to get an IProject, and then use GetSubItems or ProjectFolderEx.FindProjectItemsByLocation to get the IProjectItem, which you can cast to IProjectFile.

Or you can use ITextControl.Document.GetPsiSourceFiles(solution)

Once you've got an IPsiSourceFile, you need to use the PsiManager (which you can most likely inject into your component's constructor) and use PsiManager.GetPsiFiles(psiSourceFile). This returns an IFile, which is the root ITreeNode of the AST.

So, you end up with three (or more) objects representing the file - IProjectFile, which represents the file in the project model, IPsiSourceFile, which represents a file in the PSI structure, and one or more IFile, which is the PSI tree structure for the file.


Please sign in to leave a comment.