public
Created

Factoring context action

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.ReSharper.Psi.CSharp.Tree;
 
namespace ActiveMesa.R2P.Maths.Factor
{
using System;
using JetBrains.Application.Progress;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Feature.Services.CSharp.Bulbs;
using JetBrains.ReSharper.Intentions.Extensibility;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.TextControl;
using JetBrains.Util;
 
[ContextAction(Name = "Factor", Description = "Factors out common terms in an expression", Group = "C#")]
public class FactorCA : ContextActionBase
{
private readonly ICSharpContextActionDataProvider provider;
private Dictionary<string, int[]> histogram;
private KeyValuePair<string, int> bestTerm;
private int timesToExtract;
private IAdditiveExpression root;
private readonly HashSet<int> affectedTerms = new HashSet<int>();
private List<List<ICSharpExpression>> flatStructure;
 
public FactorCA(ICSharpContextActionDataProvider provider)
{
this.provider = provider;
}
 
public override bool IsAvailable(IUserDataHolder cache)
{
root = provider.GetSelectedElement<IAdditiveExpression>(true, true);
 
// make sure we're on the root
if (root == null) return false;
while (root.Parent is IAdditiveExpression)
root = (IAdditiveExpression)root.Parent;
 
// get all the parts of an addition as a list
var partsOfAddition = Flatten<IAdditiveExpression>(root).ToList();
flatStructure = FlattenMultiplications(partsOfAddition);
 
// build a frequencyTable of the most useful terms
int elementCount = flatStructure.Count;
histogram = new Dictionary<string, int[]>();
for (int i = 0; i < elementCount; ++i)
{
var group = flatStructure[i];
for (int index = 0; index < group.Count; index++)
{
string termText = group[index].GetText();
if (!histogram.ContainsKey(termText))
histogram.Add(termText, new int[elementCount]);
histogram[termText][i]++;
}
}
 
// find out the term with largest frequency
bestTerm = histogram.ToDictionary(k => k.Key, v => v.Value.Count(z => z > 0))
.ToList().OrderByDescending(x => x.Value).First();
if (bestTerm.Value < 2) return false;
 
// find out how many times we're going to take it out
timesToExtract = histogram[bestTerm.Key].Where(z => z > 0).Min();
var e = histogram[bestTerm.Key];
for (int i = 0; i < elementCount; ++i)
if (e[i] > 0)
{
e[i] -= timesToExtract;
affectedTerms.Add(i);
}
return true;
}
 
private IEnumerable<ICSharpExpression> Flatten<T>(IBinaryExpression expr)
where T : class, IBinaryExpression
{
Func<ICSharpExpression, List<ICSharpExpression>> yieldAll = t =>
{
var result = new List<ICSharpExpression>();
var op = t as T;
if (op != null)
foreach (var e in Flatten<T>(op))
result.Add(e);
else result.Add(t);
return result;
};
return yieldAll(expr.LeftOperand).Concat(yieldAll(expr.RightOperand));
}
 
private List<List<ICSharpExpression>> FlattenMultiplications(List<ICSharpExpression> parts)
{
var result = new List<List<ICSharpExpression>>();
foreach (var part in parts)
{
var ops = new List<ICSharpExpression>();
 
var expr = part as IMultiplicativeExpression;
if (expr != null)
ops.AddRange(Flatten<IMultiplicativeExpression>(expr));
else
ops.Add(part);
 
result.Add(ops);
}
return result;
}
 
protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
{
CSharpElementFactory factory = CSharpElementFactory.GetInstance(provider.PsiModule);
 
var sb = new StringBuilder();
 
// first, append the first term a suitable number of times
sb.Append(Enumerable.Repeat(bestTerm.Key, timesToExtract).Join("*"));
 
// brace the terms
sb.Append("*(");
 
// (very dubious predicate lambda here)
Action<Func<HashSet<int>, int, bool>> processTerms = predicate =>
{
for (int groupIndex = 0; groupIndex < flatStructure.Count; ++groupIndex)
{
// is this term even affected?
if (predicate(affectedTerms, groupIndex))
{
// go through each of the terms that's included in this group
foreach (string term in histogram.Keys)
if (histogram[term][groupIndex] > 0)
{
// suddenly this is relevant
sb.Append(Enumerable.Repeat(term, histogram[term][groupIndex]).Join("*"));
sb.Append("*");
}
sb.RemoveLast();
sb.Append(" + ");
}
}
sb.RemoveLast(3);
};
 
// if you know a better way, let me know
processTerms((set, i) => set.Contains(i));
sb.Append(")");
 
bool haveExtras = Enumerable.Range(0, flatStructure.Count).Any(x => !affectedTerms.Contains(x));
if (haveExtras)
{
sb.Append(" + ");
processTerms((set, i) => !set.Contains(i));
}
 
var expr = factory.CreateExpressionAsIs(sb.ToString());
root.ReplaceBy(expr);
 
return null;
}
 
public override string Text
{
get { return "Factor out " + Enumerable.Repeat(bestTerm.Key, timesToExtract).Join("*"); }
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.