Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit d8c7c08

Browse files
committed
Added AutoFormatting option in GUI and option container, but without function right now.
1 parent 84c262d commit d8c7c08

3 files changed

Lines changed: 94 additions & 14 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingOptionsContainer.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ namespace CSharpBinding.FormattingStrategy
3434
/// </summary>
3535
internal class CSharpFormattingOptionsContainer : INotifyPropertyChanged
3636
{
37+
private const string AutoFormattingOptionName = "AutoFormatting";
38+
3739
CSharpFormattingOptionsContainer parent;
3840
CSharpFormattingOptions cachedOptions;
3941

4042
readonly HashSet<string> activeOptions;
43+
bool? autoFormatting;
4144

4245
public CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent = null)
4346
: this(parent, new HashSet<string>())
@@ -68,6 +71,34 @@ public CSharpFormattingOptionsContainer Parent
6871
}
6972
}
7073

74+
public bool? AutoFormatting
75+
{
76+
get {
77+
return autoFormatting;
78+
}
79+
set {
80+
autoFormatting = value;
81+
OnPropertyChanged("AutoFormatting");
82+
}
83+
}
84+
85+
public bool EffectiveAutoFormatting
86+
{
87+
get {
88+
// Get "effective" option, i.e. walk up all parents to find a defined value
89+
CSharpFormattingOptionsContainer container = this;
90+
do
91+
{
92+
if (container.autoFormatting.HasValue) {
93+
return container.autoFormatting.Value;
94+
}
95+
container = container.parent;
96+
} while (container != null);
97+
98+
return true;
99+
}
100+
}
101+
71102
/// <summary>
72103
/// Resets all container's options to given <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormattingOptions"/> instance.
73104
/// </summary>
@@ -81,6 +112,7 @@ public void Reset(CSharpFormattingOptions options = null)
81112
foreach (var property in typeof(CSharpFormattingOptions).GetProperties()) {
82113
activeOptions.Add(property.Name);
83114
}
115+
autoFormatting = true;
84116
}
85117
OnPropertyChanged(null);
86118
}
@@ -106,6 +138,7 @@ public void CloneFrom(CSharpFormattingOptionsContainer options)
106138
foreach (var activeOption in options.activeOptions)
107139
activeOptions.Add(activeOption);
108140
cachedOptions = options.cachedOptions.Clone();
141+
autoFormatting = options.autoFormatting;
109142
OnPropertyChanged(null);
110143
}
111144

@@ -275,6 +308,11 @@ public void Load(Properties parentProperties)
275308
// Silently ignore loading error, then this property will be "as parent" automatically
276309
}
277310
}
311+
if (formatProperties.Contains(AutoFormattingOptionName)) {
312+
autoFormatting = formatProperties.Get(AutoFormattingOptionName, true);
313+
} else {
314+
autoFormatting = null;
315+
}
278316
}
279317
}
280318

@@ -291,6 +329,12 @@ public void Save(Properties parentProperties)
291329
formatProperties.Set(activeOption, val);
292330
}
293331
}
332+
if (formatProperties.Contains(AutoFormattingOptionName) && !autoFormatting.HasValue) {
333+
// AutoFormatting options was activated previously, remove it now
334+
formatProperties.Remove(AutoFormattingOptionName);
335+
} else {
336+
formatProperties.Set(AutoFormattingOptionName, autoFormatting);
337+
}
294338

295339
parentProperties.SetNestedProperties("CSharpFormatting", formatProperties);
296340
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,23 @@
298298
</UserControl.Resources>
299299

300300
<DockPanel>
301-
<Grid
302-
DockPanel.Dock="Top"
303-
Visibility="{Binding Path=AllowPresets, Converter={StaticResource boolToVisibilityConverter}}">
304-
305-
<Grid.ColumnDefinitions>
306-
<ColumnDefinition Width="Auto" />
307-
<ColumnDefinition Width="*" />
308-
</Grid.ColumnDefinitions>
309-
310-
<Button Grid.Column="0" Content="Reset to:" Click="ResetButton_Click" />
311-
<ComboBox Name="presetComboBox" Grid.Column="1" Margin="3,0,0,0" ItemsSource="{Binding Presets}" />
312-
</Grid>
301+
<StackPanel DockPanel.Dock="Top" Orientation="Vertical">
302+
<CheckBox
303+
Name="autoFormattingCheckBox"
304+
Content="Automatically format code"
305+
IsThreeState="True" IsChecked="{Binding AutoFormatting}"
306+
Margin="0,0,0,10"/>
307+
<Grid
308+
Visibility="{Binding Path=AllowPresets, Converter={StaticResource boolToVisibilityConverter}}">
309+
<Grid.ColumnDefinitions>
310+
<ColumnDefinition Width="Auto" />
311+
<ColumnDefinition Width="*" />
312+
</Grid.ColumnDefinitions>
313+
314+
<Button Grid.Column="0" Content="Reset to:" Click="ResetButton_Click" />
315+
<ComboBox Name="presetComboBox" Grid.Column="1" Margin="3,0,0,0" ItemsSource="{Binding Presets}" />
316+
</Grid>
317+
</StackPanel>
313318

314319
<sd:RestrictDesiredSize Margin="0,4,0,0">
315320
<ScrollViewer VerticalScrollBarVisibility="Auto">

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,44 @@ public bool AllowPresets {
133133
set { SetValue(AllowPresetsProperty, value); }
134134
}
135135

136+
public static readonly DependencyProperty AutoFormattingProperty =
137+
DependencyProperty.Register("AutoFormatting", typeof(bool?), typeof(CSharpFormattingEditor),
138+
new FrameworkPropertyMetadata());
139+
140+
public bool? AutoFormatting {
141+
get { return (bool?)GetValue(AutoFormattingProperty); }
142+
set { SetValue(AutoFormattingProperty, value); }
143+
}
144+
136145
static void OnOptionsContainerPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
137146
{
138147
var editor = o as CSharpFormattingEditor;
139148
if (editor != null) {
140-
var container = e.NewValue as CSharpFormattingOptionsContainer;
149+
var newContainer = e.NewValue as CSharpFormattingOptionsContainer;
150+
if (newContainer != null) {
151+
newContainer.PropertyChanged += (sender, eventArgs) =>
152+
{
153+
if (eventArgs.PropertyName == "AutoFormatting") {
154+
// Update AutoFormatting special option
155+
if (editor.AutoFormatting != newContainer.AutoFormatting)
156+
editor.AutoFormatting = newContainer.AutoFormatting;
157+
}
158+
};
159+
editor.autoFormattingCheckBox.IsThreeState = (newContainer.Parent != null);
160+
editor.AutoFormatting = newContainer.AutoFormatting;
161+
editor.FillPresetList(newContainer);
162+
}
163+
}
164+
}
165+
166+
static void OnAutoFormattingPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
167+
{
168+
var editor = o as CSharpFormattingEditor;
169+
if (editor != null) {
170+
var container = editor.OptionsContainer;
141171
if (container != null) {
142-
editor.FillPresetList(container);
172+
if (container.AutoFormatting != (bool?) e.NewValue)
173+
container.AutoFormatting = (bool?) e.NewValue;
143174
}
144175
}
145176
}

0 commit comments

Comments
 (0)