Advertisement
Guest User

Untitled

a guest
May 28th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1.   [ActionHandler("PasteCSV")]
  2.   public class PasteCSV : IActionHandler
  3.   {
  4.     private readonly ActionManager actionManager;
  5.  
  6.     public PasteCSV(ActionManager actionManager)
  7.     {
  8.       this.actionManager = actionManager;
  9.     }
  10.  
  11.     public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
  12.     {
  13.       return Clipboard.ContainsText(TextDataFormat.CommaSeparatedValue) &&
  14.               context.GetData(JetBrains.TextControl.DataContext.DataConstants.TEXT_CONTROL) != null;
  15.     }
  16.  
  17.     public void Execute(IDataContext context, DelegateExecute nextExecute)
  18.     {
  19.       var csv = Clipboard.GetText(TextDataFormat.CommaSeparatedValue);
  20.       var textControl = context.GetData(JetBrains.TextControl.DataContext.DataConstants.TEXT_CONTROL);
  21.       if (!string.IsNullOrWhiteSpace(csv))
  22.       {
  23.         var sb = new StringBuilder();
  24.         var lines = csv.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  25.         int rows = lines.Length;
  26.         int cols = lines[0].Split(',').Length;
  27.         if (IsAllDataNumeric(lines))
  28.         {
  29.           if (rows == 1 || cols == 1)
  30.           {
  31.             sb.AppendFormat("double[] foo = {{ {0} }};",
  32.                             rows == 1 ? lines[0] : lines.Join(","));
  33.           }
  34.           else
  35.           {
  36.             sb.Append("double[,] foo = {").AppendLine();
  37.             foreach (var line in lines)
  38.               sb.AppendFormat("{{ {0} }},", line).AppendLine();
  39.             sb.Append("};");
  40.           }
  41.         }
  42.         else
  43.         {
  44.           if (rows == 1 || cols == 1)
  45.           {
  46.             var data = rows == 1 ? lines[0].Split(',') : lines.ToArray();
  47.             sb.AppendFormat("var foo = Tuple.Create({0});", data.Select(FormatForType).Join(","));
  48.           } else
  49.           {
  50.             sb.Append("var foo = new[] {").AppendLine();
  51.             foreach (var line in lines)
  52.               sb.Append("Tuple.Create(").Append(line.Split(',').Select(FormatForType).Join(",")).Append("),");
  53.             sb.Append("};");
  54.           }
  55.         }
  56.  
  57.         var doc = textControl.Document;
  58.         var pos = textControl.Caret.Position;
  59.         doc.InsertText(pos.Value.ToDocOffset(), sb.ToString());
  60.       }
  61.     }
  62.  
  63.     public string FormatForType(string input)
  64.     {
  65.       DateTime dt;
  66.       double d;
  67.       if (DateTime.TryParse(input, out dt))
  68.         return dt.ToAssemblyCode();
  69.       else if (double.TryParse(input, out d))
  70.         return input;
  71.       else return input.Quoted();
  72.     }
  73.  
  74.     public bool IsAllDataNumeric(string[] lines)
  75.     {
  76.       double dummy;
  77.       return lines.SelectMany(line => line.Split(','))
  78.         .All(element => double.TryParse(element, out dummy));
  79.     }
  80.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement