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

            pgProps.SelectedObject = ai;
            this.ai = ai;
            tbCode.Text = ai.FormatSource(tbCode.Text);
        }
Exemple #2
0
        static ChameleonEditor()
        {
            m_titlePrefixes = new Dictionary <FileLocation, string>();
            m_titlePrefixes[FileLocation.Local]   = "(L) ";
            m_titlePrefixes[FileLocation.Remote]  = "(R) ";
            m_titlePrefixes[FileLocation.Unknown] = "(?) ";

            m_astyle = new AStyleInterface();
            m_astyle.SetDefaultChameleonStyleOptions();
        }
Exemple #3
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 #4
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;
    }
Exemple #5
0
        /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;

            AStyle = new AStyleInterface();
            AStyle.LoadDefaults();
            AStyleInterface.LoadSettings(ref AStyle);

            if (connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                object[] contextGUIDS = new object[] { };
                Commands2 commands = (Commands2)_applicationObject.Commands;

                #region Set up toolbar

                var commandBars = (CommandBars)_applicationObject.CommandBars;

                // Add a new toolbar
                CommandBar myTemporaryToolbar = null;
                try
                {
                    myTemporaryToolbar = commandBars[_toolBarName];
                }
                catch { }

                if (myTemporaryToolbar == null)
                    myTemporaryToolbar = commandBars.Add(_toolBarName, MsoBarPosition.msoBarTop, System.Type.Missing, false);

                Command optionsCommand = commands.AddNamedCommand2(_addInInstance, _optionsCommandStr, "Options", "Configure AstyleWrapper",
                     true, 2946, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                Command executeCommand = commands.AddNamedCommand2(_addInInstance, _execCommandStr, "Format selection", "Format selection",
                     true, 611, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                Command switchCommand = commands.AddNamedCommand2(_addInInstance, _switchCommandStr, "Switcher", AStyle.working ? _switchOffStr : _switchOnStr,
                    true, AStyle.working ? 1087 : 1088, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                // Add a new button on that toolbar

                //_switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _optionsCommandBtn = (CommandBarButton)optionsCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _execCommandBtn = (CommandBarButton)executeCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);

                // Make visible the toolbar
                myTemporaryToolbar.Visible = true;

                #endregion
            }
            else
            {
                // try get buttons
                var commandBars = (CommandBars)_applicationObject.CommandBars;
                try
                {
                    var myTemporaryToolbar = commandBars[_toolBarName];
                    _switchCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[1];
                    _execCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[2];
                    _optionsCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[3];
                }
                catch { }

                _hashes = new HashSet<int>();

                _textEditorEvents = _applicationObject.Events.get_TextEditorEvents(null);
                _textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(_textEditorEvents_LineChanged);
            }
        }
        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 #7
0
 public static void LoadSettings(ref AStyleInterface ai)
 {
     try
     {
         String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
         var serializer = new XmlSerializer(typeof(AStyleInterface));
         using (var reader = new StreamReader(path + "\\Settings.xml"))
         {
             ai = (AStyleInterface)serializer.Deserialize(reader);
         }
     }
     catch (System.Exception ex)
     {
     }
 }