Ejemplo n.º 1
0
        private void SortFormatTokens_Execute(Object sender, ApplyContentEventArgs ea)
        {
            MethodCallExpression TheStringFormat = _PrimitiveString.Parent as MethodCallExpression;
            string TheString = _PrimitiveString.Name;
            List<Expression> TheArguments = TheStringFormat.Arguments.ToList<Expression>().Skip(1).ToList<Expression>();
            SourceRange TheStringFormatRange = TheStringFormat.Range;

            //Get List of Tokens
            var Tokens = new TokenGatherer().GetTokens(TheString);

            // Determine "Sort" map.
            List<MapItem> Map = MapGenerator.GenerateMap(from item in Tokens select item.Index);

            // Affect string using map
            TheString = new StringTokenRenumberer().Renumber(TheString, Tokens, Map);

            // Affect params using map?
            TheArguments = new ListReorderer().Reorder(TheArguments, Tokens, Map);

            // Write TheString and TheList back to the code.
            TheStringFormat.Arguments.Clear();
            TheStringFormat.Arguments.Add(new PrimitiveExpression(TheString));
            TheStringFormat.Arguments.AddRange(TheArguments);
            string TheNewCode = CodeRush.CodeMod.GenerateCode(TheStringFormat, true);
            ea.TextDocument.SetText(TheStringFormatRange, TheNewCode);
        }
        public void RenumberShuffledListWithDuplicates_Test()
        {
            string Start = "{3}, {0}, {2}, {0}, {1}, {0}";
            string Expected = "{0}, {1}, {2}, {1}, {3}, {1}";

            var Tokens = new TokenGatherer().GetTokens(Start);
            var Map = MapGenerator.GenerateMap(from item in Tokens select item.Index);
            var Result = new StringTokenRenumberer().Renumber(Start, Tokens, Map);

            Assert.AreEqual(Expected, Result);
        }
        private void FixStringAndList(ref string startString, ref List<int> startList)
        {
            // gather Tokens
            var Tokens = new TokenGatherer().GetTokens(startString);

            // Generate map
            List<MapItem> Map = MapGenerator.GenerateMap(from item in Tokens select item.Index);

            // Use map to rebuild string.
            startString = new StringTokenRenumberer().Renumber(startString, Tokens, Map);
            // use Map to rebuild list
            startList = new ListReorderer().Reorder(startList, Tokens, Map);
        }