Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using JetBrains.Application;
- using JetBrains.Application.Progress;
- using JetBrains.ProjectModel;
- using JetBrains.ReSharper.Daemon;
- using JetBrains.ReSharper.Daemon.Stages;
- using JetBrains.ReSharper.Daemon.Stages.Dispatcher;
- using JetBrains.ReSharper.Feature.Services.Bulbs;
- using JetBrains.ReSharper.Psi.CSharp;
- using JetBrains.ReSharper.Psi.CSharp.Tree;
- using JetBrains.ReSharper.Psi.ExtensionsAPI.Tree;
- using JetBrains.ReSharper.Psi.Tree;
- using JetBrains.TextControl;
- using JetBrains.Util;
- namespace Stuff
- {
- [ElementProblemAnalyzer(new[] { typeof(IInvocationExpression) },
- HighlightingTypes = new[] { typeof(IntPowerHighlighting) })]
- public class IntPowerProblemAnalyzer : ElementProblemAnalyzer<IInvocationExpression>
- {
- protected override void Run(IInvocationExpression element,
- ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
- {
- bool functionIsCalledPow = false;
- var e = element.InvokedExpression as IReferenceExpression;
- if (e != null)
- {
- if (e.Reference.GetName().Equals("Pow"))
- functionIsCalledPow = true;
- }
- bool firstArgIsIdentifier = false;
- bool secondArgIsInteger = false;
- int power = -1;
- if (element.Arguments.Count == 2)
- {
- firstArgIsIdentifier = element.Arguments[0].Value is IReferenceExpression;
- var secondArg = element.Arguments[1].Value as ICSharpLiteralExpression;
- if (secondArg != null && (secondArg.IsConstantValue()))
- {
- double value = -1.0;
- var cv = secondArg.ConstantValue;
- if (cv.IsDouble())
- value = (double)cv.Value;
- else if (cv.IsInteger())
- value = (int)cv.Value;
- power = (int)value;
- secondArgIsInteger = (value > 0.0) && value == Math.Floor(value);
- }
- }
- if (functionIsCalledPow && firstArgIsIdentifier && secondArgIsInteger)
- consumer.AddHighlighting(new IntPowerHighlighting(element, power),
- element.GetDocumentRange(), element.GetContainingFile());
- }
- }
- [StaticSeverityHighlighting(Severity.WARNING, CSharpLanguage.Name)]
- public class IntPowerHighlighting : IHighlighting
- {
- public IInvocationExpression Expression { get; private set; }
- public int Power { get; private set; }
- public IntPowerHighlighting(IInvocationExpression expression, int power)
- {
- Expression = expression;
- Power = power;
- }
- public bool IsValid()
- {
- return Expression != null && Expression.IsValid();
- }
- public string ToolTip { get { return "Inefficient use of integer-based power"; } }
- public string ErrorStripeToolTip { get { return ToolTip; } }
- public int NavigationOffsetPatch { get { return 0; } }
- }
- [QuickFix]
- public class IntPowerInliningFix : BulbItemImpl, IQuickFix
- {
- private readonly IntPowerHighlighting highlighting;
- public IntPowerInliningFix(IntPowerHighlighting highlighting)
- {
- this.highlighting = highlighting;
- }
- protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
- {
- var arg = highlighting.Expression.Arguments[0];
- var factory = CSharpElementFactory.GetInstance(highlighting.Expression.GetPsiModule());
- var replacement = factory.CreateExpression(
- Enumerable.Range(0, highlighting.Power).Select(i => "$0").Join("*"), arg.Value);
- ModificationUtil.ReplaceChild(highlighting.Expression, replacement);
- return null;
- }
- public override string Text
- {
- get { return "Inline integer power"; }
- }
- public bool IsAvailable(IUserDataHolder cache)
- {
- int power = highlighting.Power;
- return power == 2 || power == 3;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement