More powerfull templates wanted


I was looking at your live template API and have a suggestion (or question
how to achieve the same)

I was trying to create a live template (or another solution) for a performance
refactoring. Concrete a have 314 places in my code where there are string
comparison expressions like this:

var1.ToLower() == var2.ToLower()

This I would like to transfer to :

string.Compare(var1, var2, true) == 0

(Is this possible today?)

In all the cases I wish to transfer an expression where the == operator
is involved, the expressions on either side of the == operator are of type
string and the ToLower() method is involved in the string expressions. I’m
missing some kind of C# grammar based refactoring language so that I am able
to write something like this: (made up language :) )

Match
Expr1: Expression[string], Operator(equals)[string, string], Expr2 : Expression[string]

Where
Expr1 = string:ToLower(Expr3 : Expression[string])
And
Expr2 = string:ToLower(Expr4 : Expression[string])
Transfer
Operator(equals) string:Compare(Expr3, Expr4, true), 0

I hope Im making my suggestion clear enough


1 comment
Comment actions Permalink

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));
}
}
}

In most cases the code like the one above should work.


I was looking at your live template API and have a suggestion (or
question how to achieve the same)

I was trying to create a live template (or another solution) for a
performance refactoring. Concrete a have 314 places in my code where
there are string comparison expressions like this:

var1.ToLower() == var2.ToLower()

This I would like to transfer to :

string.Compare(var1, var2, true) == 0

(Is this possible today?)

In all the cases I wish to transfer an expression where the ==
operator is involved, the expressions on either side of the ==
operator are of type string and the ToLower() method is involved in
the string expressions. I’m missing some kind of C# grammar based
refactoring language so that I am able to write something like this:
(made up language :) )

Match
Expr1: Expression[string], Operator(equals)[string, string], Expr2 :
Expression[string]
Where
Expr1 = string:ToLower(Expr3 : Expression[string])
And
Expr2 = string:ToLower(Expr4 : Expression[string])
Transfer
Operator(equals) string:Compare(Expr3, Expr4, true), 0
I hope Im making my suggestion clear enough

Thanks,
Andrey Simanovsky


0

Please sign in to leave a comment.