I am exploring writing Plugins for ReSharper / Rider. Following the documentation's example, I am building a context action for ReSharper.
Here is the context action, which is not doing much yet:
[ContextAction
(
Name = "New context action",
Description = "Some description...",
Group = "C#",
Disabled = false,
Priority = 1
)]
public sealed class NewContextAction : ContextActionBase
{
public override string Text => "New context action";
public NewContextAction(LanguageIndependentContextActionDataProvider dataProvider)
{
var selectedTreeNode = dataProvider.GetSelectedElement<ITreeNode>();
// --> How can I get a reference to the type?
}
...
}
Whenever I click on something in the source code (like a type as shown above), the constructor of my context action class is called. I have access to the name with the ITreeNode
but I do not know how to move from the tree representation to the actual type representation used by ReSharper.
Question
How can I to get a reference to the type (IType
, IDeclaredType
...) that has been clicked by the user?
You need something like this:
var objectCreationExpression = dataProvider.GetSelectedElement<IObjectCreationExpression>();
var typeUsage = objectCreationExpression?.TypeUsage;
if (typeUsage != null)
{
var type = CSharpTypeFactory.CreateType(typeUsage);
}