Ejemplo n.º 1
0
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        internal void Reorganize(Document document)
        {
            if (!_codeReorganizationAvailabilityLogic.CanReorganize(document, true))
            {
                return;
            }

            new UndoTransactionHelper(_package, $"CodeMaid Reorganize for '{document.Name}'").Run(
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine($"CodeReorganizationManager.Reorganize started for '{document.FullName}'");
                _package.IDE.StatusBar.Text = $"CodeMaid is reorganizing '{document.Name}'...";

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file sort order.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, CodeSortOrder.File));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = $"CodeMaid reorganized '{document.Name}'.";
                OutputWindowHelper.DiagnosticWriteLine($"CodeReorganizationManager.Reorganize completed for '{document.FullName}'");
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        /// <param name="isAutoSave">A flag indicating if occurring due to auto-save.</param>
        internal void Reorganize(Document document, bool isAutoSave)
        {
            if (!CanReorganize(document))
            {
                return;
            }

            _undoTransactionHelper.Run(
                () => !(isAutoSave && Settings.Default.General_SkipUndoTransactionsDuringAutoCleanupOnSave),
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorderManager.Reorganize started for '{0}'", document.FullName));

                _package.IDE.StatusBar.Text = string.Format("CodeMaid is reorganizing '{0}'...", document.Name);

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file layout.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, TreeLayoutMode.FileLayout));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = string.Format("CodeMaid reorganized '{0}'.", document.Name);

                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorderManager.Reorganize completed for '{0}'", document.FullName));
            },
                delegate(Exception ex)
            {
                OutputWindowHelper.ExceptionWriteLine(
                    string.Format("Stopped reorganizing '{0}'", document.Name), ex);
                _package.IDE.StatusBar.Text = string.Format("CodeMaid stopped reorganizing '{0}'.  See output window for more details.", document.Name);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reorganizes the specified document.
        /// </summary>
        /// <param name="document">The document for reorganizing.</param>
        internal void Reorganize(Document document)
        {
            if (!_codeReorganizationAvailabilityLogic.CanReorganize(document))
            {
                return;
            }

            _undoTransactionHelper.Run(
                delegate
            {
                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorganizationManager.Reorganize started for '{0}'", document.FullName));

                _package.IDE.StatusBar.Text = string.Format("CodeMaid is reorganizing '{0}'...", document.Name);

                // Retrieve all relevant code items (excluding using statements).
                var rawCodeItems = _codeModelManager.RetrieveAllCodeItems(document).Where(x => !(x is CodeItemUsingStatement));

                // Build the code tree based on the current file sort order.
                var codeItems = new SetCodeItems(rawCodeItems);
                var codeTree  = CodeTreeBuilder.RetrieveCodeTree(new CodeTreeRequest(document, codeItems, CodeSortOrder.File));

                // Recursively reorganize the code tree.
                RecursivelyReorganize(codeTree);

                _package.IDE.StatusBar.Text = string.Format("CodeMaid reorganized '{0}'.", document.Name);

                OutputWindowHelper.DiagnosticWriteLine(
                    string.Format("CodeReorganizationManager.Reorganize completed for '{0}'", document.FullName));
            },
                delegate(Exception ex)
            {
                OutputWindowHelper.ExceptionWriteLine(
                    string.Format("Stopped reorganizing '{0}'", document.Name), ex);
                _package.IDE.StatusBar.Text = string.Format("CodeMaid stopped reorganizing '{0}'.  See output window for more details.", document.Name);
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to run code cleanup on the specified CSharp document.
        /// </summary>
        /// <param name="document">The document for cleanup.</param>
        private void RunCodeCleanupCSharp(Document document)
        {
            var textDocument = document.GetTextDocument();

            // Perform any actions that can modify the file code model first.
            RunExternalFormatting(textDocument);
            if (!document.IsExternal())
            {
                _usingStatementCleanupLogic.RemoveAndSortUsingStatements(textDocument);
            }

            // Interpret the document into a collection of elements.
            var codeItems = _codeModelManager.RetrieveAllCodeItems(document);

            var regions         = codeItems.OfType <CodeItemRegion>().ToList();
            var usingStatements = codeItems.OfType <CodeItemUsingStatement>().ToList();
            var namespaces      = codeItems.OfType <CodeItemNamespace>().ToList();
            var classes         = codeItems.OfType <CodeItemClass>().ToList();
            var delegates       = codeItems.OfType <CodeItemDelegate>().ToList();
            var enumerations    = codeItems.OfType <CodeItemEnum>().ToList();
            var events          = codeItems.OfType <CodeItemEvent>().ToList();
            var fields          = codeItems.OfType <CodeItemField>().ToList();
            var interfaces      = codeItems.OfType <CodeItemInterface>().ToList();
            var methods         = codeItems.OfType <CodeItemMethod>().ToList();
            var properties      = codeItems.OfType <CodeItemProperty>().ToList();
            var structs         = codeItems.OfType <CodeItemStruct>().ToList();

            // Build up more complicated collections.
            var usingStatementBlocks           = CodeModelHelper.GetCodeItemBlocks(usingStatements).ToList();
            var usingStatementsThatStartBlocks = (from IEnumerable <CodeItemUsingStatement> block in usingStatementBlocks select block.First()).ToList();
            var usingStatementsThatEndBlocks   = (from IEnumerable <CodeItemUsingStatement> block in usingStatementBlocks select block.Last()).ToList();

            // Perform file header cleanup.
            _fileHeaderLogic.UpdateFileHeader(textDocument);

            // Perform removal cleanup.
            _removeRegionLogic.RemoveRegionsPerSettings(regions);
            _removeWhitespaceLogic.RemoveEOLWhitespace(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesAtTop(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesAtBottom(textDocument);
            _removeWhitespaceLogic.RemoveEOFTrailingNewLine(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesAfterAttributes(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesAfterOpeningBrace(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesBeforeClosingBrace(textDocument);
            _removeWhitespaceLogic.RemoveBlankLinesBetweenChainedStatements(textDocument);
            _removeWhitespaceLogic.RemoveMultipleConsecutiveBlankLines(textDocument);

            // Perform insertion of blank line padding cleanup.
            _insertBlankLinePaddingLogic.InsertPaddingBeforeRegionTags(regions);
            _insertBlankLinePaddingLogic.InsertPaddingAfterRegionTags(regions);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeEndRegionTags(regions);
            _insertBlankLinePaddingLogic.InsertPaddingAfterEndRegionTags(regions);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(usingStatementsThatStartBlocks);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(usingStatementsThatEndBlocks);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(namespaces);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(namespaces);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(classes);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(classes);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(delegates);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(delegates);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(enumerations);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(enumerations);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(events);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(events);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(fields);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(fields);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(interfaces);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(interfaces);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(methods);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(methods);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(properties);
            _insertBlankLinePaddingLogic.InsertPaddingBetweenMultiLinePropertyAccessors(properties);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(properties);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCodeElements(structs);
            _insertBlankLinePaddingLogic.InsertPaddingAfterCodeElements(structs);

            _insertBlankLinePaddingLogic.InsertPaddingBeforeCaseStatements(textDocument);
            _insertBlankLinePaddingLogic.InsertPaddingBeforeSingleLineComments(textDocument);

            // Perform insertion of explicit access modifier cleanup.
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnClasses(classes);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnDelegates(delegates);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnEnumerations(enumerations);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnEvents(events);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnFields(fields);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnInterfaces(interfaces);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnMethods(methods);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnProperties(properties);
            _insertExplicitAccessModifierLogic.InsertExplicitAccessModifiersOnStructs(structs);

            // Perform insertion of whitespace cleanup.
            _insertWhitespaceLogic.InsertEOFTrailingNewLine(textDocument);

            // Perform update cleanup.
            _updateLogic.UpdateEndRegionDirectives(textDocument);
            _updateLogic.UpdateEventAccessorsToBothBeSingleLineOrMultiLine(events);
            _updateLogic.UpdatePropertyAccessorsToBothBeSingleLineOrMultiLine(properties);
            _updateLogic.UpdateSingleLineMethods(methods);

            // Perform comment cleaning.
            _commentFormatLogic.FormatComments(textDocument);
        }