Exemple #1
0
        public SettingsForm(AStyleInterface ai)
        {
            InitializeComponent();

            pgProps.SelectedObject = ai;
            this.ai = ai;
            tbCode.Text = ai.FormatSource(tbCode.Text);
        }
Exemple #2
0
{     /// Main function for this example.
    public static void Main(string[] args)
    { // files to pass to AStyle
        string[] fileName = { "AStyleDev/test-data/ASBeautifier.cpp",
                              "AStyleDev/test-data/ASFormatter.cpp",
                              "AStyleDev/test-data/astyle.h" };

        // options to pass to AStyle
        // mode=cs is required for C# files
        string options = "style=java, indent=tab, mode=cs";

        // create an object
        AStyleInterface AStyle = new AStyleInterface();

        // get Artistic Style version
        // does not need to terminate on an error
        string version = AStyle.GetVersion();

        if (version != string.Empty)
        {
            Console.WriteLine("Example C# - AStyle " + version);
        }

        // process the files
        for (int i = 0; i < fileName.Length; i++)
        {   // get the text to format
            string filePath = GetProjectDirectory(fileName[i]);
            string textIn   = GetText(filePath);

            // call the Artistic Style formatting function
            // does not need to terminate on an error
            string textOut = AStyle.FormatSource(textIn, options);
            // does not need to terminate on an error
            // an error message has been displayed by the error handler
            if (textOut == string.Empty)
            {
                Console.WriteLine("Cannot format " + filePath);
                continue;
            }

            // return the formatted text
            Console.WriteLine("Formatted " + fileName[i]);
            SetText(textOut, filePath);
        }

        return;
    }
Exemple #3
0
    /// Main function for Example
    public static void Main(string[] args)
    {
        // files to pass to AStyle
        String[] fileName =  { "AStyleDev/test-s/FileUtility.cs",
                               "AStyleDev/test-s/MainClass.cs" ,
                               "AStyleDev/test-s/StringParser.cs" ,
                             };

        // options to pass to AStyle
        // mode=cs is required for C# files
        String options = "brackets=linux mode=cs";

        // create an object
        AStyleInterface AStyle = new AStyleInterface();

        // get Artistic Style version
        // does not need to terminate on an error
        String version = AStyle.GetVersion();
        if (version != String.Empty)
            Console.WriteLine ("Example C# - AStyle " + version);

        // process the files
        for (int i = 0; i < fileName.Length; i++)
        {   // get the text to format
            String filePath = GetProjectDirectory(fileName[i]);
            String textIn = GetText(filePath);

            // call the Artistic Style formatting function
            // does not need to terminate on an error
            String textOut = AStyle.FormatSource(textIn, options);
            if (textOut == String.Empty)
            {   Console.WriteLine("Cannot format "  + filePath);
                continue;
            }

            // return the formatted text
            Console.WriteLine("Formatted " + fileName[i]);
            SetText(textOut, filePath);
        }

        return;
    }
        public static string AStyleDirectory(string dir, bool writeChanges, out bool changesMade)
        {
            var pattern = Settings.Default.Pattern;
            var options = Settings.Default.Options;
            var license = Settings.Default.License.Replace("\r", "").Replace("\n", "\r\n");
            var ignore  = Settings.Default.Ignore.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            changesMade = false;

            // Get a list of source files
            string[] sources = GitLsFiles(dir, pattern);

            if (sources.Length <= 0)
            {
                return("No source files found!");
            }

            // Format the files
            var output = new StringBuilder();

            var AStyle   = new AStyleInterface();
            var encoding = new UTF8Encoding(false);

            foreach (string file in sources)
            {
                try
                {
                    if (ignore.Any(p => Regex.IsMatch(file, p)))
                    {
                        continue;
                    }

                    var fileText   = encoding.GetString(File.ReadAllBytes(file));
                    var formatText = AStyle.FormatSource(fileText, options)
                                     .Replace("\r\n", "\n")
                                     .Replace("\n", "\r\n")
                                     .Trim('\uFEFF', '\u200B');

                    if (license.Length > 0 && !formatText.StartsWith(license))
                    {
                        formatText = license + fileText;
                    }

                    if (formatText == String.Empty)
                    {
                        output.AppendLine("Cannot format " + file);
                        continue;
                    }
                    if (!fileText.Equals(formatText))
                    {
                        changesMade = true;
                        if (writeChanges)
                        {
                            File.WriteAllText(file, formatText, encoding);
                        }
                        else
                        {
                            output.AppendLine(file);
                        }
                    }
                }
                catch
                {
                    // Ignored. Any files that can't be read or written will be skipped.
                }
            }

            return(output.ToString());
        }
Exemple #5
0
 public void ReformatSelectedText()
 {
     this.Selection.Text = m_astyle.FormatSource(this.Selection.Text);
 }