Find usages of ITypeElement, or IDeclaredElement with Resharper SDK

79 views Asked by At

I'm trying to create a custom navigate to plugin with the Resharper SDK plugin. I have managed to get the IDeclaredElement or ITypeElement when I stand on my type doing

var referenceName = dataContext.GetSelectedTreeNode<IReferenceName>();
var declaration = referenceName?.Reference.Resolve()?.DeclaredElement as ITypeElement;
if (declaration != null)
{
    //TODO: Find all usages here and check if my type is used as single argument to a method (Visitor pattern)
}

The SDK docs are really sparse and I dont find anything on the subject. Thanks

1

There are 1 answers

0
Anders On BEST ANSWER

After some trial and error i Found a working solution. IFinder.FindAllReferences

var foundMethods = declaration
    .GetPsiServices()
    .Finder
    .FindAllReferences(declaration)
    .Select(r => ((r.GetTreeNode().Parent as IUserTypeUsage)?
        .Parent as IRegularParameterDeclaration)?
        .Parent as IFormalParameterList)
    .Where(list => list != null && list.ParameterDeclarations.Count == 1)
    .Select(m => m.Parent as IMethodDeclaration)
    .Where(m => m != null)
    .ToList();

Full code here