Example #1
0
 public override void FormatLines(ITextEditor textArea)
 {
     using (textArea.Document.OpenUndoGroup()) {
         // In any other case: Simply format selection or whole document
         var formattingOptions   = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject);
         int formattedTextOffset = 0;
         int formattedTextLength = textArea.Document.TextLength;
         if (textArea.SelectionLength != 0)
         {
             formattedTextOffset = textArea.SelectionStart;
             formattedTextLength = textArea.SelectionLength;
         }
         CSharpFormatterHelper.Format(textArea, formattedTextOffset, formattedTextLength, formattingOptions.OptionsContainer);
     }
 }
        /// <summary>
        /// Formats a code section according to currently effective formatting settings.
        /// </summary>
        /// <param name="textArea">Text editor instance to format code in.</param>
        /// <param name="offset">Start offset of formatted code.</param>
        /// <param name="length">Length of formatted code.</param>
        /// <param name="respectAutoFormattingSetting">
        /// Set to <c>true</c> to perform formatting only if auto-formatting setting is active.
        /// If <c>false</c>, formatting will be performed in any case.
        /// </param>
        /// <returns><c>True</c>, if code has been formatted, <c>false</c> if auto-formatting is currently forbidden.</returns>
        private bool FormatCode(ITextEditor textArea, int offset, int length, bool respectAutoFormattingSetting)
        {
            if ((offset > textArea.Document.TextLength) || ((offset + length) > textArea.Document.TextLength))
            {
                return(false);
            }
            if (respectAutoFormattingSetting && !CSharpFormattingPolicies.AutoFormatting)
            {
                return(false);
            }

            using (textArea.Document.OpenUndoGroup()) {
                var formattingOptions = CSharpFormattingPolicies.Instance.GetProjectOptions(SD.ProjectService.CurrentProject);
                try {
                    CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer);
                } catch (Exception) {
                    // Exceptions in formatting might happen if code contains syntax errors, we have to catch them
                    return(false);
                }
                return(true);
            }
        }