Browse Source

Implemented auto-formatting of code parts when typing "}" or ";".

pull/478/head
Andreas Weizel 12 years ago
parent
commit
6fd2d1a8e7
  1. 48
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs
  2. 3
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml
  3. 2
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs

48
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -269,17 +269,39 @@ namespace CSharpBinding.FormattingStrategy
public override void FormatLines(ITextEditor textArea) public override void FormatLines(ITextEditor textArea)
{ {
using (textArea.Document.OpenUndoGroup()) { // Format current selection or whole document
// In any other case: Simply format selection or whole document
var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject);
int formattedTextOffset = 0; int formattedTextOffset = 0;
int formattedTextLength = textArea.Document.TextLength; int formattedTextLength = textArea.Document.TextLength;
if (textArea.SelectionLength != 0) { if (textArea.SelectionLength != 0) {
formattedTextOffset = textArea.SelectionStart; formattedTextOffset = textArea.SelectionStart;
formattedTextLength = textArea.SelectionLength; formattedTextLength = textArea.SelectionLength;
} }
CSharpFormatterHelper.Format(textArea, formattedTextOffset, formattedTextLength, formattingOptions.OptionsContainer); FormatCode(textArea, formattedTextOffset, formattedTextLength, false);
}
/// <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)
{
using (textArea.Document.OpenUndoGroup()) {
// In any other case: Simply format selection or whole document
var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject);
if (!respectAutoFormattingSetting || formattingOptions.OptionsContainer.EffectiveAutoFormatting) {
CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer);
return true;
}
} }
return false;
} }
public override void FormatLine(ITextEditor textArea, char ch) // used for comment tag formater/inserter 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 ']': case ']':
case '}':
case '{': case '{':
//if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) { //if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) {
IndentLine(textArea, curLine); IndentLine(textArea, curLine);
//} //}
break; 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': case '\n':
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
// curLine might have some text which should be added to indentation // curLine might have some text which should be added to indentation

3
src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml

@ -302,8 +302,7 @@
Name="autoFormattingCheckBox" Name="autoFormattingCheckBox"
Content="{core:Localize CSharpBinding.Formatting.AutoFormat}" Content="{core:Localize CSharpBinding.Formatting.AutoFormat}"
IsThreeState="True" IsChecked="{Binding AutoFormatting}" IsThreeState="True" IsChecked="{Binding AutoFormatting}"
Margin="0,0,0,10" Margin="0,0,0,10" />
Visibility="Collapsed"/>
<Grid <Grid
Visibility="{Binding Path=AllowPresets, Converter={StaticResource boolToVisibilityConverter}}"> Visibility="{Binding Path=AllowPresets, Converter={StaticResource boolToVisibilityConverter}}">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>

2
src/AddIns/BackendBindings/CSharpBinding/Project/Src/OptionPanels/CSharpFormattingEditor.xaml.cs

@ -135,7 +135,7 @@ namespace CSharpBinding.OptionPanels
public static readonly DependencyProperty AutoFormattingProperty = public static readonly DependencyProperty AutoFormattingProperty =
DependencyProperty.Register("AutoFormatting", typeof(bool?), typeof(CSharpFormattingEditor), DependencyProperty.Register("AutoFormatting", typeof(bool?), typeof(CSharpFormattingEditor),
new FrameworkPropertyMetadata()); new FrameworkPropertyMetadata(OnAutoFormattingPropertyChanged));
public bool? AutoFormatting { public bool? AutoFormatting {
get { return (bool?)GetValue(AutoFormattingProperty); } get { return (bool?)GetValue(AutoFormattingProperty); }

Loading…
Cancel
Save