diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs index e3ec4d4304..1ab3e99fa1 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs @@ -268,18 +268,40 @@ namespace CSharpBinding.FormattingStrategy } public override void FormatLines(ITextEditor textArea) + { + // Format current selection or whole document + int formattedTextOffset = 0; + int formattedTextLength = textArea.Document.TextLength; + if (textArea.SelectionLength != 0) { + formattedTextOffset = textArea.SelectionStart; + formattedTextLength = textArea.SelectionLength; + } + FormatCode(textArea, formattedTextOffset, formattedTextLength, false); + } + + /// + /// Formats a code section according to currently effective formatting settings. + /// + /// Text editor instance to format code in. + /// Start offset of formatted code. + /// Length of formatted code. + /// + /// Set to true to perform formatting only if auto-formatting setting is active. + /// If false, formatting will be performed in any case. + /// + /// True, if code has been formatted, false if auto-formatting is currently forbidden. + private bool FormatCode(ITextEditor textArea, int offset, int length, bool respectAutoFormattingSetting) { 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; + if (!respectAutoFormattingSetting || formattingOptions.OptionsContainer.EffectiveAutoFormatting) { + CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer); + return true; } - CSharpFormatterHelper.Format(textArea, formattedTextOffset, formattedTextLength, formattingOptions.OptionsContainer); } + + return false; } public override void FormatLine(ITextEditor textArea, char ch) // used for comment tag formater/inserter @@ -375,12 +397,28 @@ namespace CSharpBinding.FormattingStrategy case ':': case ')': case ']': - case '}': case '{': //if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) { IndentLine(textArea, curLine); //} break; + case '}': + // Try to get corresponding block beginning brace + var bracketSearchResult = textArea.Language.BracketSearcher.SearchBracket(textArea.Document, cursorOffset); + if (bracketSearchResult != null) { + // Format the block + if (!FormatCode(textArea, bracketSearchResult.OpeningBracketOffset, + cursorOffset - bracketSearchResult.OpeningBracketOffset, true)) { + // No auto-formatting seems to be active, at least indent the line + IndentLine(textArea, curLine); + } + } + break; + case ';': + // Format this line + var lineBeginningOffset = textArea.Document.GetOffset(lineNr, 0); + FormatCode(textArea, lineBeginningOffset, cursorOffset - lineBeginningOffset, true); + break; case '\n': string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); // curLine might have some text which should be added to indentation diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml index 91da88f9b7..1519981726 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml @@ -302,8 +302,7 @@ Name="autoFormattingCheckBox" Content="{core:Localize CSharpBinding.Formatting.AutoFormat}" IsThreeState="True" IsChecked="{Binding AutoFormatting}" - Margin="0,0,0,10" - Visibility="Collapsed"/> + Margin="0,0,0,10" /> diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs index b9b8fd3cc1..df9a36becf 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs @@ -135,7 +135,7 @@ namespace CSharpBinding.OptionPanels public static readonly DependencyProperty AutoFormattingProperty = DependencyProperty.Register("AutoFormatting", typeof(bool?), typeof(CSharpFormattingEditor), - new FrameworkPropertyMetadata()); + new FrameworkPropertyMetadata(OnAutoFormattingPropertyChanged)); public bool? AutoFormatting { get { return (bool?)GetValue(AutoFormattingProperty); }