/// <summary>
    /// This function sets the indentation level in a range of lines.
    /// </summary>
    /// <param name="textArea">The text area.</param>
    /// <param name="begin">The begin.</param>
    /// <param name="end">The end.</param>
    public override void IndentLines(TextArea textArea, int begin, int end)
    {
      if (textArea.Document.TextEditorProperties.IndentStyle != IndentStyle.Smart)
      {
        base.IndentLines(textArea, begin, end);
        return;
      }
      int cursorPos = textArea.Caret.Position.Y;
      int oldIndentLength = 0;

      if (cursorPos >= begin && cursorPos <= end)
        oldIndentLength = GetIndentation(textArea, cursorPos).Length;

      IndentationSettings set = new IndentationSettings();
      set.IndentString = Tab.GetIndentationString(textArea.Document);
      CSharpIndentationReformatter r = new CSharpIndentationReformatter();
      DocumentAccessor acc = new DocumentAccessor(textArea.Document, begin, end);
      r.Reformat(acc, set);

      if (cursorPos >= begin && cursorPos <= end)
      {
        int newIndentLength = GetIndentation(textArea, cursorPos).Length;
        if (oldIndentLength != newIndentLength)
        {
          // fix cursor position if indentation was changed
          int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
          textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), cursorPos);
        }
      }
    }
        /// <summary>
        /// This function sets the indentation level in a range of lines.
        /// </summary>
        /// <param name="textArea">The text area.</param>
        /// <param name="begin">The begin.</param>
        /// <param name="end">The end.</param>
        public override void IndentLines(TextArea textArea, int begin, int end)
        {
            if (textArea.Document.TextEditorProperties.IndentStyle != IndentStyle.Smart)
            {
                base.IndentLines(textArea, begin, end);
                return;
            }
            int cursorPos       = textArea.Caret.Position.Y;
            int oldIndentLength = 0;

            if (cursorPos >= begin && cursorPos <= end)
            {
                oldIndentLength = GetIndentation(textArea, cursorPos).Length;
            }

            IndentationSettings set = new IndentationSettings();

            set.IndentString = Tab.GetIndentationString(textArea.Document);
            CSharpIndentationReformatter r   = new CSharpIndentationReformatter();
            DocumentAccessor             acc = new DocumentAccessor(textArea.Document, begin, end);

            r.Reformat(acc, set);

            if (cursorPos >= begin && cursorPos <= end)
            {
                int newIndentLength = GetIndentation(textArea, cursorPos).Length;
                if (oldIndentLength != newIndentLength)
                {
                    // fix cursor position if indentation was changed
                    int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
                    textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), cursorPos);
                }
            }
        }
    /// <summary>
    /// Define CSharp specific smart indenting for a line :)
    /// </summary>
    protected override int SmartIndentLine(TextArea textArea, int lineNr)
    {
      if (lineNr <= 0)
        return AutoIndentLine(textArea, lineNr);

      string oldText = textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr));

      DocumentAccessor acc = new DocumentAccessor(textArea.Document, lineNr, lineNr);

      IndentationSettings set = new IndentationSettings();
      set.IndentString = Tab.GetIndentationString(textArea.Document);
      set.LeaveEmptyLines = false;
      CSharpIndentationReformatter r = new CSharpIndentationReformatter();

      r.Reformat(acc, set);

      string t = acc.Text;
      if (t.Length == 0)
      {
        // use AutoIndentation for new lines in comments / verbatim strings.
        return AutoIndentLine(textArea, lineNr);
      }
      else
      {
        int newIndentLength = t.Length - t.TrimStart().Length;
        int oldIndentLength = oldText.Length - oldText.TrimStart().Length;
        if (oldIndentLength != newIndentLength && lineNr == textArea.Caret.Position.Y)
        {
          // fix cursor position if indentation was changed
          int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
          textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), lineNr);
        }
        return newIndentLength;
      }
    }
        /// <summary>
        /// Indents the specified line.
        /// </summary>
        protected override int SmartIndentLine(TextArea textArea, int lineNr)
        {
            if (lineNr <= 0)
            {
                return(AutoIndentLine(textArea, lineNr));
            }

            string oldText = textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr));

            DocumentAccessor acc = new DocumentAccessor(textArea.Document, lineNr, lineNr);

            IndentationSettings set = new IndentationSettings();

            set.IndentString    = Tab.GetIndentationString(textArea.Document);
            set.LeaveEmptyLines = false;
            CSharpIndentationReformatter r = new CSharpIndentationReformatter();

            r.Reformat(acc, set);

            string t = acc.Text;

            if (t.Length == 0)
            {
                // use AutoIndentation for new lines in comments / verbatim strings.
                return(AutoIndentLine(textArea, lineNr));
            }
            else
            {
                int newIndentLength = t.Length - t.TrimStart().Length;
                int oldIndentLength = oldText.Length - oldText.TrimStart().Length;
                if (oldIndentLength != newIndentLength && lineNr == textArea.Caret.Position.Y)
                {
                    // fix cursor position if indentation was changed
                    int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
                    textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), lineNr);
                }
                return(newIndentLength);
            }
        }