IActionHandler

Im am implementing a IActionhandler as suggjested by Andrey Simanovsky (see
below)

I mananged to comple and install this as an plugin (I can see the plugin
in the resharper -> plugins menu)
There is only one major question. How should a IActionHandler work? - I cannot
finde any documentation of this issue (ofcourse its CTP).

How does the "Execute(IDataContext context, DelegateExecute nextExecute)"
method get called ? - when I right click? or when the source is loaded? Please
help.

By the way ... super support you fellows are giving, and a super product
as well.

Thanx

Runi


**********
Hello Runi,

There is a jetbrains.resharper.openapi newsgroup dedicated to discussion
of ReSharper API.
You need not use live templates. You can easily implement the desired program

transformation by writing a small plug-in for ReSharper.
Refer to GettingStarted.html in the PluginSamples subfolder of the folder
where ReSharper is installed for more information on developing plugins.
What you need is to write say an action that will perform program transformation:

[ActionHandler("TransformAction")]
internal class TransformAction : IActionHandler
{
public bool Update(IDataContext context, ActionPresentation presentation,
DelegateUpdate nextUpdate)
{
// fetch focused text editor control
return (ISolution) context.GetData(DataConstants.SOLUTION) != null;
}

public void Execute(IDataContext context, DelegateExecute nextExecute)
{
ISolution solution = (ISolution) context.GetData(DataConstants.SOLUTION);

if (solution == null) return;

PsiManager manager = PsiManager.GetInstance(solution);

IDeclaredElement toLower = manager.Predefined.String.GetTypeElement().GetMembers("ToLower")[1];

IReference[] toLowerUsages = manager.Finder.FindReferences(toLower,
toLower.GetAccessibilityDomain(), NullProgressIndicator.INSTANCE);
foreach (IReference toLowerUsage in toLowerUsages)
{
IReferenceExpression invokedExpr1 = toLowerUsage.GetElement() as
IReferenceExpression;
if (invokedExpr1 == null) continue;
IExpression var1 = invokedExpr1.QualifierExpression;
if (var1 == null) continue;
IEqualityExpression equalsExpr = EqualityExpressionNavigator.GetByLeftOperand(InvocationExpressionNavigator.GetByInvokedExpression(invokedExpr1));
if (equalsExpr == null) continue;
IInvocationExpression invocation2 = equalsExpr.RightOperand as IInvocationExpression;
if (invocation2 == null) continue;
IReferenceExpression invokedExpr2 = invocation2.InvokedExpression
as IReferenceExpression;
if (invokedExpr2 == null || invokedExpr2.Reference.Resolve().DeclaredElement
!= toLower) continue;
IExpression var2 = invokedExpr2.QualifierExpression;
if (var2 == null) continue;
//now we have var1.ToLower() == var2.ToLower()
equalsExpr.ReplaceBy(manager.ElementFactory.CreateExpression("string.Compare($0,$1,

true) == 0", var1, var2));
}
}
}


1 comment
Comment actions Permalink

Runi,

Have you looked into PluginSamples\AddMenuItem sample plugin? I think it
may help you.

Valentin Kipiatkov
Chief Scientist, Vice President of Product Development
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Im am implementing a IActionhandler as suggjested by Andrey Simanovsky
(see below)

I mananged to comple and install this as an plugin (I can see the
plugin in the resharper -> plugins menu) There is only one major
question. How should a IActionHandler work? - I cannot finde any
documentation of this issue (ofcourse its CTP).

How does the "Execute(IDataContext context, DelegateExecute
nextExecute)" method get called ? - when I right click? or when the
source is loaded? Please help.

By the way ... super support you fellows are giving, and a super
product as well.

Thanx

Runi

**********
Hello Runi,
There is a jetbrains.resharper.openapi newsgroup dedicated to
discussion
of ReSharper API.
You need not use live templates. You can easily implement the desired
program
transformation by writing a small plug-in for ReSharper.
Refer to GettingStarted.html in the PluginSamples subfolder of the
folder
where ReSharper is installed for more information on developing
plugins.
What you need is to write say an action that will perform program
transformation:
[ActionHandler("TransformAction")]
internal class TransformAction : IActionHandler
{
public bool Update(IDataContext context, ActionPresentation
presentation,
DelegateUpdate nextUpdate)
{
// fetch focused text editor control
return (ISolution) context.GetData(DataConstants.SOLUTION) != null;
}
public void Execute(IDataContext context, DelegateExecute nextExecute)
{
ISolution solution = (ISolution)
context.GetData(DataConstants.SOLUTION);
if (solution == null) return;

PsiManager manager = PsiManager.GetInstance(solution);

IDeclaredElement toLower =
manager.Predefined.String.GetTypeElement().GetMembers("ToLower")[1];

IReference[] toLowerUsages = manager.Finder.FindReferences(toLower,

toLower.GetAccessibilityDomain(), NullProgressIndicator.INSTANCE);

foreach (IReference toLowerUsage in toLowerUsages)

{

IReferenceExpression invokedExpr1 = toLowerUsage.GetElement() as

IReferenceExpression;

if (invokedExpr1 == null) continue;

IExpression var1 = invokedExpr1.QualifierExpression;

if (var1 == null) continue;

IEqualityExpression equalsExpr =
EqualityExpressionNavigator.GetByLeftOperand(InvocationExpressionNavig
ator.GetByInvokedExpression(invokedExpr1));

if (equalsExpr == null) continue;

IInvocationExpression invocation2 = equalsExpr.RightOperand as
IInvocationExpression;

if (invocation2 == null) continue;

IReferenceExpression invokedExpr2 = invocation2.InvokedExpression

as IReferenceExpression;

if (invokedExpr2 == null ||
invokedExpr2.Reference.Resolve().DeclaredElement

!= toLower) continue;

IExpression var2 = invokedExpr2.QualifierExpression;

if (var2 == null) continue;

//now we have var1.ToLower() == var2.ToLower()

equalsExpr.ReplaceBy(manager.ElementFactory.CreateExpression("string.C
ompare($0,$1,

true) == 0", var1, var2));
}
}
}



0

Please sign in to leave a comment.