示例#1
0
        /// <summary>
        /// Run the visual studio built-in remove unused using statements command.
        /// </summary>
        /// <param name="textDocument">The text document to update.</param>
        /// <param name="isAutoSave">A flag indicating if occurring due to auto-save.</param>
        public void RemoveUnusedUsingStatements(TextDocument textDocument, bool isAutoSave)
        {
            if (!Settings.Default.Cleaning_RunVisualStudioRemoveUnusedUsingStatements)
            {
                return;
            }
            if (isAutoSave && Settings.Default.Cleaning_SkipRemoveUnusedUsingStatementsDuringAutoCleanupOnSave)
            {
                return;
            }

            // Capture all existing using statements that should be re-inserted if removed.
            string patternFormat = _package.UsePOSIXRegEx
                                       ? @"^:b*{0}:b*\n"
                                       : @"^[ \t]*{0}[ \t]*\r?\n";

            var points = (from usingStatement in _usingStatementsToReinsertWhenRemoved.Value
                          from editPoint in TextDocumentHelper.FindMatches(textDocument, string.Format(patternFormat, usingStatement))
                          select new { editPoint, text = editPoint.GetLine() }).Reverse().ToList();

            _package.IDE.ExecuteCommand("Edit.RemoveUnusedUsings", String.Empty);

            // Check each using statement point and re-insert it if removed.
            foreach (var point in points)
            {
                string text = point.editPoint.GetLine();
                if (text != point.text)
                {
                    point.editPoint.StartOfLine();
                    point.editPoint.Insert(point.text);
                    point.editPoint.Insert(Environment.NewLine);
                }
            }
        }
        /// <summary>
        /// Run the visual studio built-in remove and sort using statements command.
        /// </summary>
        /// <remarks>
        /// Before VS2017 these were two separate commands.  Starting in VS2017 they were merged into one.
        /// </remarks>
        /// <param name="textDocument">The text document to update.</param>
        public void RemoveAndSortUsingStatements(TextDocument textDocument)
        {
            if (!Settings.Default.Cleaning_RunVisualStudioRemoveAndSortUsingStatements)
            {
                return;
            }
            if (_package.IsAutoSaveContext && Settings.Default.Cleaning_SkipRemoveAndSortUsingStatementsDuringAutoCleanupOnSave)
            {
                return;
            }

            // Capture all existing using statements that should be re-inserted if removed.
            const string patternFormat = @"^[ \t]*{0}[ \t]*\r?\n";

            var points = (from usingStatement in _usingStatementsToReinsertWhenRemoved.Value
                          from editPoint in TextDocumentHelper.FindMatches(textDocument, string.Format(patternFormat, usingStatement))
                          select new { editPoint, text = editPoint.GetLine() }).Reverse().ToList();

            // Shift every captured point one character to the right so they will auto-advance
            // during new insertions at the start of the line.
            foreach (var point in points)
            {
                point.editPoint.CharRight();
            }

            if (_package.IDEVersion >= 15)
            {
                _commandHelper.ExecuteCommand(textDocument, "EditorContextMenus.CodeWindow.RemoveAndSort");
            }
            else
            {
                _commandHelper.ExecuteCommand(textDocument, "Edit.RemoveUnusedUsings");
                _commandHelper.ExecuteCommand(textDocument, "Edit.SortUsings");
            }

            // Check each using statement point and re-insert it if removed.
            foreach (var point in points)
            {
                string text = point.editPoint.GetLine();
                if (text != point.text)
                {
                    point.editPoint.StartOfLine();
                    point.editPoint.Insert(point.text);
                    point.editPoint.Insert(Environment.NewLine);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Retrieves code regions from the specified text selection.
        /// </summary>
        /// <param name="textSelection">The text selection to walk.</param>
        /// <returns>An enumerable collection of regions.</returns>
        internal IEnumerable <CodeItemRegion> RetrieveCodeRegions(TextSelection textSelection)
        {
            var editPoints = TextDocumentHelper.FindMatches(textSelection, RegionPattern);

            return(RetrieveCodeRegions(editPoints));
        }