lawrencealan

FlashDevelop: Codeformatter fix script

Nov 11th, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. /* See: http://r0b0tz.com/2011/11/flashdevelop-script-for-codeformatter-fixes/ */
  2. using System;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5. using PluginCore;
  6.  
  7. public class FDScript
  8. {
  9.     /**
  10.     * Fixes CodeFormatter issues
  11.     * @author http://lawrencealan.com
  12.     */
  13.     public static void Execute()
  14.     {
  15.         try
  16.         {
  17.             ScintillaNet.ScintillaControl sci = PluginBase.MainForm.CurrentDocument.SciControl;
  18.             int pos = sci.CurrentPos;
  19.             int line = sci.LineFromPosition(pos);
  20.  
  21.  
  22.             if (sci == null) return; // document not editable
  23.             string src = sci.Text;
  24.            
  25.             // fix single line statements
  26.             string matchpattern = @"(?<!.*//.*)(\s+)?(for|if|while)\s+([^{\n]*)(?<!{)\n\s+";
  27.             string replacementpattern = @"$1$2 $3 ";
  28.             src = Regex.Replace(src, matchpattern, replacementpattern, RegexOptions.IgnoreCase);
  29.  
  30.             // fix lack of space between "){"
  31.             matchpattern = @"(for|if|while)\s+([^{]*)\{";
  32.             replacementpattern = @"$1 $2 {";
  33.             src = Regex.Replace(src, matchpattern, replacementpattern, RegexOptions.IgnoreCase & RegexOptions.Multiline );
  34.  
  35.            
  36.             sci.Text = src;
  37.             sci.GotoLine(line);
  38.             sci.GotoPos(pos);
  39.         }
  40.         catch (Exception ex)
  41.         {
  42.             MessageBox.Show(ex.ToString());
  43.         }
  44.     }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment