Advertisement
Guest User

Dmitri

a guest
Nov 1st, 2009
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. namespace ResharperContextActions
  2. {
  3.   using System;
  4.   using System.Text;
  5.   using JetBrains.Application;
  6.   using JetBrains.ReSharper.Intentions;
  7.   using JetBrains.ReSharper.Intentions.CSharp.ContextActions;
  8.   using JetBrains.ReSharper.Psi.CSharp.Tree;
  9.   using JetBrains.ReSharper.Psi.ExtensionsAPI;
  10.  
  11.   /// <summary>
  12.   /// Inlines power declaration.
  13.   /// </summary>
  14.   [ContextAction(Group = "C#", Name = "Inline a power function",
  15.     Description = "Inlines a power statement; e.g., changes Math.Pow(x, 3) to x*x*x.",
  16.     Priority = 15)]
  17.   internal class InlinePowerAction : ContextActionBase
  18.   {
  19.     private string text;
  20.  
  21.     public InlinePowerAction(ICSharpContextActionDataProvider provider) : base(provider) {}
  22.  
  23.     protected override void Execute(JetBrains.ReSharper.Psi.Tree.IElement element)
  24.     {
  25.       IInvocationExpression expression = GetSelectedElement<IInvocationExpression>(false);
  26.       if (expression != null)
  27.       {
  28.         IInvocationExpressionNode node = expression.ToTreeNode();
  29.         if (node != null)
  30.         {
  31.           IArgumentListNode args = node.ArgumentList;
  32.           int count = (int)double.Parse(args.Arguments[1].Value.GetText().Replace("f", string.Empty));
  33.           bool isShort = node.Arguments[0].Value is ILiteralExpression ||
  34.                          node.Arguments[0].Value is IReferenceExpression;
  35.           var sb = new StringBuilder();
  36.           sb.Append("(");
  37.           for (int i = 0; i < count; ++i)
  38.           {
  39.             if (!isShort) sb.Append("(");
  40.             sb.Append(args.Arguments[0].GetText());
  41.             if (!isShort) sb.Append(")");
  42.             if (i + 1 != count)
  43.               sb.Append("*");
  44.           }
  45.           sb.Append(")");
  46.           // now replace everything
  47.           ICSharpExpression newExp = Provider.ElementFactory.CreateExpression(
  48.             sb.ToString(), new object[] { });
  49.           if (newExp != null)
  50.           {
  51.             LowLevelModificationUtil.ReplaceChildRange(
  52.               expression.ToTreeNode(),
  53.               expression.ToTreeNode(),
  54.               new[] { newExp.ToTreeNode() });
  55.           }
  56.         }
  57.       }
  58.     }
  59.  
  60.     protected override string GetText()
  61.     {
  62.       return text;
  63.     }
  64.  
  65.     protected override bool IsAvailable(JetBrains.ReSharper.Psi.Tree.IElement element)
  66.     {
  67.       using (ReadLockCookie.Create())
  68.       {
  69.         IInvocationExpression invEx = GetSelectedElement<IInvocationExpression>(false);
  70.  
  71.         if (invEx != null && invEx.InvokedExpression.GetText() == "Math.Pow")
  72.         {
  73.           IArgumentListNode node = invEx.ToTreeNode().ArgumentList;
  74.           if (node != null && node.Arguments.Count == 2)
  75.           {
  76.             ILiteralExpression value = node.Arguments[1].Value as ILiteralExpression;
  77.             if (value != null)
  78.             {
  79.               float n;
  80.               if (float.TryParse(value.GetText().Replace("f", string.Empty), out n) &&
  81.                   (n - Math.Floor(n) == 0 && n >= 1 && n <= 10))
  82.               {
  83.                 text = "Replace with " + (n-1) + " multiplications";
  84.                 return true;
  85.               }
  86.             }
  87.           }
  88.         }
  89.       }
  90.       return false;
  91.     }
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement