/// <summary>
        /// Executes the "sort lines" action.
        /// </summary>
        /// <param name="ea">
        /// A <see cref="DevExpress.CodeRush.Core.ExecuteEventArgs"/> that contains the event data.
        /// </param>
        /// <remarks>
        /// <para>
        /// This method provides the primary functionality for the Join Lines plugin.
        /// </para>
        /// <para>
        /// If there is no selection, all lines will be sorted and the caret will remain
        /// on the original line (or as close as possible) it was on.
        /// </para>
        /// <para>
        /// If there is a selection, the selected lines will be sorted and will remain selected.
        /// </para>
        /// </remarks>
        private void actionSortLines_Execute(DevExpress.CodeRush.Core.ExecuteEventArgs ea)
        {
            // Ensure context is satisfied
            if (!this.Available)
            {
                return;
            }

            // Ensure we have a document of at least two lines
            if (CodeRush.Documents.ActiveTextDocument == null)
            {
                Log.SendError("{0}Active text document is null.  No sort will occur.", LOG_PREFIX);
                return;
            }
            if (CodeRush.Documents.ActiveTextDocument.LineCount < 2)
            {
                return;
            }

            // Get the sort options
            if (this._sortDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            SourcePoint origLocation = CodeRush.Caret.SourcePoint;
            CodeRush.UndoStack.BeginUpdate("SortLines");
            try
            {
                // Get the correct comparison method
                LineComparer comparer = null;
                if (this._sortDialog.SortCaseSensitive)
                {
                    Log.SendMsg(ImageType.Options, "{0}Sorting case sensitive.", LOG_PREFIX);
                    comparer = new CaseSensitiveLineComparer();
                }
                else
                {
                    Log.SendMsg(ImageType.Options, "{0}Sorting case insensitive.", LOG_PREFIX);
                    comparer = new CaseInsensitiveLineComparer();
                }

                // Get the set of lines to sort
                bool selectFinalSet = false;
                int firstLine = 1;
                int lastLine = 0;
                if (CodeRush.Selection.Exists)
                {
                    // We're sorting the selection
                    selectFinalSet = true;
                    firstLine = CodeRush.TextViews.Active.Selection.Range.Top.Line;
                    lastLine = CodeRush.TextViews.Active.Selection.Range.Bottom.Line;
                    // If we've selected to the start of the next line, we don't actually
                    // want to sort that next line, so subtract 1.
                    if (CodeRush.TextViews.Active.Selection.Range.End.Offset==1)
                    {
                        lastLine--;
                    }
                }
                else
                {
                    // We're sorting the whole file
                    lastLine = CodeRush.Documents.ActiveTextDocument.LineCount;
                }

                // Verify we have something to sort
                if (firstLine >= lastLine)
                {
                    Log.SendMsg(ImageType.Info, "{0}No lines to sort.", LOG_PREFIX);
                    return;
                }

                // Add the lines to the list of all lines
                ArrayList lineList = new ArrayList();
                Regex matchExp = null;
                if (this._sortDialog.UseSortExpression)
                {
                    matchExp = new Regex(this._sortDialog.MatchExpression);
                }
                for (int i = firstLine; i <= lastLine; i++)
                {
                    LinePair lp = new LinePair();
                    lp.OriginalLine = CodeRush.Documents.ActiveTextDocument.GetLine(i);
                    if (matchExp != null)
                    {
                        lp.SortableLine = matchExp.Replace(lp.OriginalLine, this._sortDialog.SortExpression);
                    }
                    else
                    {
                        lp.SortableLine = lp.OriginalLine;
                    }
                    lineList.Add(lp);
                }

                // Sort the lines
                lineList.Sort(comparer);

                // Delete duplicates
                if (this._sortDialog.DeleteDuplicates)
                {
                    Log.SendMsg(ImageType.Options, "{0}Deleting duplicate lines.", LOG_PREFIX);
                    for (int i = lineList.Count - 1; i > 0; i--)
                    {
                        if (((LinePair)lineList[i]).SortableLine == ((LinePair)lineList[i - 1]).SortableLine)
                        {
                            lineList.RemoveAt(i);
                        }
                    }
                }

                // Delete the original set of lines
                CodeRush.Caret.MoveTo(firstLine, 0);
                CodeRush.Documents.ActiveTextDocument.SelectText(firstLine, 1, lastLine, CodeRush.Documents.ActiveTextDocument.GetLineLength(lastLine) + 1);

                // Insert the sorted lines
                string[] newLines = new string[lineList.Count];
                if (this._sortDialog.SortAscending)
                {
                    for (int i = 0; i < lineList.Count; i++)
                    {
                        newLines[i] = ((LinePair)(lineList[i])).OriginalLine;
                    }
                }
                else
                {
                    for (int i = lineList.Count - 1; i >= 0; i--)
                    {
                        int newLineIndex = lineList.Count - i - 1;
                        newLines[newLineIndex] = ((LinePair)(lineList[i])).OriginalLine;
                    }
                }
                CodeRush.Selection.Text = String.Join(System.Environment.NewLine, newLines);

                if (selectFinalSet)
                {
                    // Reselect if necessary
                    int newLastLine = firstLine + lineList.Count - 1;
                    CodeRush.Documents.ActiveTextDocument.SelectText(firstLine, 1, newLastLine, CodeRush.Documents.ActiveTextDocument.GetLineLength(newLastLine) + 1);
                }
                else
                {
                    // Put the caret back in the right spot
                    CodeRush.Caret.MoveTo(origLocation);
                }

                lineList = null;
            }
            catch (Exception err)
            {
                Log.SendException(String.Format("{0}Exception while sorting lines.", LOG_PREFIX), err);
            }
            finally
            {
                CodeRush.UndoStack.EndUpdate();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the "sort lines" action.
        /// </summary>
        /// <param name="ea">
        /// A <see cref="DevExpress.CodeRush.Core.ExecuteEventArgs"/> that contains the event data.
        /// </param>
        /// <remarks>
        /// <para>
        /// This method provides the primary functionality for the Join Lines plugin.
        /// </para>
        /// <para>
        /// If there is no selection, all lines will be sorted and the caret will remain
        /// on the original line (or as close as possible) it was on.
        /// </para>
        /// <para>
        /// If there is a selection, the selected lines will be sorted and will remain selected.
        /// </para>
        /// </remarks>
        private void actionSortLines_Execute(DevExpress.CodeRush.Core.ExecuteEventArgs ea)
        {
            // Ensure context is satisfied
            if (!this.Available)
            {
                return;
            }

            // Ensure we have a document of at least two lines
            if (CodeRush.Documents.ActiveTextDocument == null)
            {
                Log.SendError("{0}Active text document is null.  No sort will occur.", LOG_PREFIX);
                return;
            }
            if (CodeRush.Documents.ActiveTextDocument.LineCount < 2)
            {
                return;
            }

            // Get the sort options
            if (this._sortDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            SourcePoint origLocation = CodeRush.Caret.SourcePoint;

            CodeRush.UndoStack.BeginUpdate("SortLines");
            try
            {
                // Get the correct comparison method
                LineComparer comparer = null;
                if (this._sortDialog.SortCaseSensitive)
                {
                    Log.SendMsg(ImageType.Options, "{0}Sorting case sensitive.", LOG_PREFIX);
                    comparer = new CaseSensitiveLineComparer();
                }
                else
                {
                    Log.SendMsg(ImageType.Options, "{0}Sorting case insensitive.", LOG_PREFIX);
                    comparer = new CaseInsensitiveLineComparer();
                }

                // Get the set of lines to sort
                bool selectFinalSet = false;
                int  firstLine      = 1;
                int  lastLine       = 0;
                if (CodeRush.Selection.Exists)
                {
                    // We're sorting the selection
                    selectFinalSet = true;
                    firstLine      = CodeRush.TextViews.Active.Selection.Range.Top.Line;
                    lastLine       = CodeRush.TextViews.Active.Selection.Range.Bottom.Line;
                    // If we've selected to the start of the next line, we don't actually
                    // want to sort that next line, so subtract 1.
                    if (CodeRush.TextViews.Active.Selection.Range.End.Offset == 1)
                    {
                        lastLine--;
                    }
                }
                else
                {
                    // We're sorting the whole file
                    lastLine = CodeRush.Documents.ActiveTextDocument.LineCount;
                }

                // Verify we have something to sort
                if (firstLine >= lastLine)
                {
                    Log.SendMsg(ImageType.Info, "{0}No lines to sort.", LOG_PREFIX);
                    return;
                }

                // Add the lines to the list of all lines
                ArrayList lineList = new ArrayList();
                Regex     matchExp = null;
                if (this._sortDialog.UseSortExpression)
                {
                    matchExp = new Regex(this._sortDialog.MatchExpression);
                }
                for (int i = firstLine; i <= lastLine; i++)
                {
                    LinePair lp = new LinePair();
                    lp.OriginalLine = CodeRush.Documents.ActiveTextDocument.GetLine(i);
                    if (matchExp != null)
                    {
                        lp.SortableLine = matchExp.Replace(lp.OriginalLine, this._sortDialog.SortExpression);
                    }
                    else
                    {
                        lp.SortableLine = lp.OriginalLine;
                    }
                    lineList.Add(lp);
                }

                // Sort the lines
                lineList.Sort(comparer);

                // Delete duplicates
                if (this._sortDialog.DeleteDuplicates)
                {
                    Log.SendMsg(ImageType.Options, "{0}Deleting duplicate lines.", LOG_PREFIX);
                    for (int i = lineList.Count - 1; i > 0; i--)
                    {
                        if (((LinePair)lineList[i]).SortableLine == ((LinePair)lineList[i - 1]).SortableLine)
                        {
                            lineList.RemoveAt(i);
                        }
                    }
                }

                // Delete the original set of lines
                CodeRush.Caret.MoveTo(firstLine, 0);
                CodeRush.Documents.ActiveTextDocument.SelectText(firstLine, 1, lastLine, CodeRush.Documents.ActiveTextDocument.GetLineLength(lastLine) + 1);

                // Insert the sorted lines
                string[] newLines = new string[lineList.Count];
                if (this._sortDialog.SortAscending)
                {
                    for (int i = 0; i < lineList.Count; i++)
                    {
                        newLines[i] = ((LinePair)(lineList[i])).OriginalLine;
                    }
                }
                else
                {
                    for (int i = lineList.Count - 1; i >= 0; i--)
                    {
                        int newLineIndex = lineList.Count - i - 1;
                        newLines[newLineIndex] = ((LinePair)(lineList[i])).OriginalLine;
                    }
                }
                CodeRush.Selection.Text = String.Join(System.Environment.NewLine, newLines);

                if (selectFinalSet)
                {
                    // Reselect if necessary
                    int newLastLine = firstLine + lineList.Count - 1;
                    CodeRush.Documents.ActiveTextDocument.SelectText(firstLine, 1, newLastLine, CodeRush.Documents.ActiveTextDocument.GetLineLength(newLastLine) + 1);
                }
                else
                {
                    // Put the caret back in the right spot
                    CodeRush.Caret.MoveTo(origLocation);
                }

                lineList = null;
            }
            catch (Exception err)
            {
                Log.SendException(String.Format("{0}Exception while sorting lines.", LOG_PREFIX), err);
            }
            finally
            {
                CodeRush.UndoStack.EndUpdate();
            }
        }