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

Commit 0cbabd9

Browse files
committed
Showing appropriate selections for option values now functional.
1 parent 03feaeb commit 0cbabd9

7 files changed

Lines changed: 316 additions & 61 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="Src\Completion\XmlDocCompletionData.cs" />
8383
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsContainer.cs" />
8484
<Compile Include="Src\FormattingStrategy\CSharpFormatter.cs" />
85+
<Compile Include="Src\FormattingStrategy\FormattingOptionBinding.cs" />
8586
<Compile Include="Src\FormsDesigner\CSharpDesignerGenerator.cs" />
8687
<Compile Include="Src\FormsDesigner\CSharpDesignerLoader.cs" />
8788
<Compile Include="Src\FormsDesigner\CSharpDesignerLoaderProvider.cs" />

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

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,31 @@ private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
9797
// We rely on property value from some of the parents and have to update it from there
9898
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(e.PropertyName);
9999
if (propertyInfo != null) {
100-
propertyInfo.SetValue(cachedOptions, GetOption<object>(e.PropertyName));
100+
var val = GetOption(e.PropertyName);
101+
propertyInfo.SetValue(cachedOptions, val);
101102
}
102103
}
103104
}
104105
}
105106

107+
/// <summary>
108+
/// Retrieves the value of a formatting option or null, if none is set.
109+
/// </summary>
110+
/// <param name="option">Name of option</param>
111+
/// <returns>True, if option with given type could be found in hierarchy. False otherwise.</returns>
112+
public object GetOption(string option)
113+
{
114+
// Run up the hierarchy until we find a defined value for property
115+
if (activeOptions.Contains(option)) {
116+
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(option);
117+
if (propertyInfo != null) {
118+
return propertyInfo.GetValue(cachedOptions);
119+
}
120+
}
121+
122+
return null;
123+
}
124+
106125
/// <summary>
107126
/// Retrieves the value of a formatting option where desired type and option name are defined by a
108127
/// property getter on <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormattingOptions"/>.
@@ -113,15 +132,18 @@ private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
113132
/// (example: o =&gt; o.IndentStructBody)
114133
/// </param>
115134
/// <returns>True, if option with given type could be found in hierarchy. False otherwise.</returns>
116-
public T GetOption<T>(Expression<Func<CSharpFormattingOptions, T>> propertyGetter)
135+
public T GetEffectiveOption<T>(Expression<Func<CSharpFormattingOptions, T>> propertyGetter)
117136
where T : struct
118137
{
119138
// Get name of property (to look for in dictionary)
120139
string optionName = null;
121140
MemberExpression memberExpression = propertyGetter.Body as MemberExpression;
122141
if (memberExpression != null) {
123142
optionName = memberExpression.Member.Name;
124-
return GetOption<T>(optionName);
143+
var val = GetEffectiveOption(optionName);
144+
if (val is T) {
145+
return (T) val;
146+
}
125147
}
126148

127149
return default(T);
@@ -133,48 +155,66 @@ public T GetOption<T>(Expression<Func<CSharpFormattingOptions, T>> propertyGette
133155
/// </summary>
134156
/// <param name="option">Name of option</param>
135157
/// <returns>True, if option with given type could be found in hierarchy. False otherwise.</returns>
136-
public T GetOption<T>(string option)
158+
public object GetEffectiveOption(string option)
137159
{
138160
// Run up the hierarchy until we find a defined value for property
139161
CSharpFormattingOptionsContainer container = this;
140162
do
141163
{
164+
object val = null;
142165
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(option);
143-
if ((propertyInfo != null) && (propertyInfo.PropertyType == typeof(T))) {
144-
return (T) propertyInfo.GetValue(container.cachedOptions);
166+
if (propertyInfo != null) {
167+
val = propertyInfo.GetValue(container.cachedOptions);
168+
}
169+
if (val != null) {
170+
return val;
145171
}
146172
container = container.parent;
147173
} while (container != null);
148174

149-
return default(T);
175+
return null;
150176
}
151177

152178
/// <summary>
153179
/// Sets an option.
154180
/// </summary>
155181
/// <param name="option">Option name.</param>
156182
/// <param name="value">Option value, <c>null</c> to reset.</param>
157-
public void SetOption<T>(string option, T? value)
158-
where T : struct
183+
public void SetOption(string option, object value)
159184
{
160-
if (value.HasValue) {
185+
if (value != null) {
161186
// Save value in option values and cached options
162187
activeOptions.Add(option);
163188
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(option);
164-
if ((propertyInfo != null) && (propertyInfo.PropertyType == typeof(T))) {
165-
propertyInfo.SetValue(cachedOptions, value.Value);
189+
if ((propertyInfo != null) && (propertyInfo.PropertyType == value.GetType())) {
190+
propertyInfo.SetValue(cachedOptions, value);
166191
}
167192
} else {
168193
// Reset this option
169194
activeOptions.Remove(option);
170195
// Update formatting options object from parents
171196
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(option);
172-
if ((propertyInfo != null) && (propertyInfo.PropertyType == typeof(T))) {
173-
propertyInfo.SetValue(cachedOptions, GetOption<T>(option));
197+
if ((propertyInfo != null) && (propertyInfo.PropertyType == value.GetType())) {
198+
propertyInfo.SetValue(cachedOptions, GetOption(option));
174199
}
175200
}
176201
}
177202

203+
/// <summary>
204+
/// Retrieves the type of a given option.
205+
/// </summary>
206+
/// <param name="option">Option name</param>
207+
/// <returns>Option's type.</returns>
208+
public Type GetOptionType(string option)
209+
{
210+
PropertyInfo propertyInfo = typeof(CSharpFormattingOptions).GetProperty(option);
211+
if (propertyInfo != null) {
212+
return propertyInfo.PropertyType;
213+
}
214+
215+
return null;
216+
}
217+
178218
/// <summary>
179219
/// Retrieves a <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormattingOptions"/> instance from current
180220
/// container, resolving all options throughout container hierarchy.
@@ -197,7 +237,7 @@ private CSharpFormattingOptions CreateOptions()
197237

198238
// Look at all container options and try to set identically named properties of CSharpFormattingOptions
199239
foreach (PropertyInfo propertyInfo in typeof(CSharpFormattingOptions).GetProperties()) {
200-
object val = GetOption<object>(propertyInfo.Name);
240+
object val = GetEffectiveOption(propertyInfo.Name);
201241
if ((val != null) && (val.GetType() == propertyInfo.PropertyType)) {
202242
propertyInfo.SetValue(outputOptions, val);
203243
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.Linq;
21+
using System.Windows;
22+
using System.Windows.Controls;
23+
using System.Windows.Controls.Primitives;
24+
using ICSharpCode.NRefactory.CSharp;
25+
26+
namespace CSharpBinding.FormattingStrategy
27+
{
28+
/// <summary>
29+
/// Offers an attached property to bind a formatting option to a ComboBox.
30+
/// </summary>
31+
public static class FormattingOptionBinding
32+
{
33+
public static readonly DependencyProperty ContainerProperty =
34+
DependencyProperty.RegisterAttached("Container", typeof(CSharpFormattingOptionsContainer),
35+
typeof(FormattingOptionBinding),
36+
new FrameworkPropertyMetadata(OnContainerPropertyChanged));
37+
public static readonly DependencyProperty OptionProperty =
38+
DependencyProperty.RegisterAttached("Option", typeof(string), typeof(FormattingOptionBinding),
39+
new FrameworkPropertyMetadata(OnOptionPropertyChanged));
40+
41+
public static CSharpFormattingOptionsContainer GetContainer(Selector element)
42+
{
43+
return (CSharpFormattingOptionsContainer) element.GetValue(ContainerProperty);
44+
}
45+
46+
public static void SetContainer(Selector element, CSharpFormattingOptionsContainer enumType)
47+
{
48+
element.SetValue(ContainerProperty, enumType);
49+
}
50+
51+
public static string GetOption(Selector element)
52+
{
53+
return (string) element.GetValue(OptionProperty);
54+
}
55+
56+
public static void SetOption(Selector element, string enumType)
57+
{
58+
element.SetValue(OptionProperty, enumType);
59+
}
60+
61+
static void OnContainerPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
62+
{
63+
// No op?
64+
}
65+
66+
static void OnOptionPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
67+
{
68+
string option = e.NewValue as string;
69+
ComboBox comboBox = o as ComboBox;
70+
if ((option != null) && (comboBox != null)) {
71+
// Add "default" entry in ComboBox
72+
// TODO Add located resource!
73+
comboBox.Items.Add(new ComboBoxItem { Content = "(default)", Tag = null });
74+
comboBox.SelectedIndex = 0;
75+
76+
CSharpFormattingOptionsContainer container = GetContainer(comboBox);
77+
if (container != null) {
78+
Type optionType = container.GetOptionType(option);
79+
FillComboValues(comboBox, optionType);
80+
object currentValue = container.GetOption(option);
81+
comboBox.SelectedItem = comboBox.Items.OfType<ComboBoxItem>().FirstOrDefault(item => currentValue.Equals(item.Tag));
82+
}
83+
}
84+
}
85+
86+
static void FillComboValues(ComboBox comboBox, Type type)
87+
{
88+
if (type == typeof(bool)) {
89+
FillBoolComboValues(comboBox);
90+
} else if (type == typeof(BraceStyle)) {
91+
FillBraceStyleComboValues(comboBox);
92+
} else if (type == typeof(PropertyFormatting)) {
93+
FillPropertyFormattingComboValues(comboBox);
94+
} else if (type == typeof(Wrapping)) {
95+
FillWrappingComboValues(comboBox);
96+
} else if (type == typeof(NewLinePlacement)) {
97+
FillNewLinePlacementComboValues(comboBox);
98+
} else if (type == typeof(UsingPlacement)) {
99+
FillUsingPlacementComboValues(comboBox);
100+
} else if (type == typeof(EmptyLineFormatting)) {
101+
FillEmptyLineFormattingComboValues(comboBox);
102+
}
103+
}
104+
105+
static void FillBoolComboValues(ComboBox comboBox)
106+
{
107+
// TODO Add located resources!
108+
comboBox.Items.Add(new ComboBoxItem { Content = "Yes", Tag = true });
109+
comboBox.Items.Add(new ComboBoxItem { Content = "No", Tag = false });
110+
}
111+
112+
static void FillBraceStyleComboValues(ComboBox comboBox)
113+
{
114+
// TODO Add located resources!
115+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = BraceStyle.DoNotChange });
116+
comboBox.Items.Add(new ComboBoxItem { Content = "End of line", Tag = BraceStyle.EndOfLine });
117+
comboBox.Items.Add(new ComboBoxItem { Content = "End of line without space", Tag = BraceStyle.EndOfLineWithoutSpace });
118+
comboBox.Items.Add(new ComboBoxItem { Content = "Next line", Tag = BraceStyle.NextLine });
119+
comboBox.Items.Add(new ComboBoxItem { Content = "Next line shifted", Tag = BraceStyle.NextLineShifted });
120+
comboBox.Items.Add(new ComboBoxItem { Content = "Next line shifted 2", Tag = BraceStyle.NextLineShifted2 });
121+
comboBox.Items.Add(new ComboBoxItem { Content = "Banner style", Tag = BraceStyle.BannerStyle });
122+
}
123+
124+
static void FillPropertyFormattingComboValues(ComboBox comboBox)
125+
{
126+
// TODO Add located resources!
127+
comboBox.Items.Add(new ComboBoxItem { Content = "Allow one line", Tag = PropertyFormatting.AllowOneLine });
128+
comboBox.Items.Add(new ComboBoxItem { Content = "Force one line", Tag = PropertyFormatting.ForceOneLine });
129+
comboBox.Items.Add(new ComboBoxItem { Content = "Force new line", Tag = PropertyFormatting.ForceNewLine });
130+
}
131+
132+
static void FillWrappingComboValues(ComboBox comboBox)
133+
{
134+
// TODO Add located resources!
135+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = Wrapping.DoNotChange });
136+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not wrap", Tag = Wrapping.DoNotWrap });
137+
comboBox.Items.Add(new ComboBoxItem { Content = "Wrap always", Tag = Wrapping.WrapAlways });
138+
comboBox.Items.Add(new ComboBoxItem { Content = "Wrap if too long", Tag = Wrapping.WrapIfTooLong });
139+
}
140+
141+
static void FillNewLinePlacementComboValues(ComboBox comboBox)
142+
{
143+
// TODO Add located resources!
144+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not care", Tag = NewLinePlacement.DoNotCare });
145+
comboBox.Items.Add(new ComboBoxItem { Content = "New line", Tag = NewLinePlacement.NewLine });
146+
comboBox.Items.Add(new ComboBoxItem { Content = "Same line", Tag = NewLinePlacement.SameLine });
147+
}
148+
149+
static void FillUsingPlacementComboValues(ComboBox comboBox)
150+
{
151+
// TODO Add located resources!
152+
comboBox.Items.Add(new ComboBoxItem { Content = "Top of file", Tag = UsingPlacement.TopOfFile });
153+
comboBox.Items.Add(new ComboBoxItem { Content = "Inside namespace", Tag = UsingPlacement.InsideNamespace });
154+
}
155+
156+
static void FillEmptyLineFormattingComboValues(ComboBox comboBox)
157+
{
158+
// TODO Add located resources!
159+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not change", Tag = EmptyLineFormatting.DoNotChange });
160+
comboBox.Items.Add(new ComboBoxItem { Content = "Indent", Tag = EmptyLineFormatting.Indent });
161+
comboBox.Items.Add(new ComboBoxItem { Content = "Do not indent", Tag = EmptyLineFormatting.DoNotIndent });
162+
}
163+
}
164+
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<UserControl
22
x:Class="CSharpBinding.OptionPanels.CSharpFormattingEditor"
3+
x:Name="CSharpFormattingEditor_Self"
34
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:local="clr-namespace:CSharpBinding.OptionPanels">
6+
xmlns:sd="http://icsharpcode.net/sharpdevelop/core"
7+
xmlns:local="clr-namespace:CSharpBinding.OptionPanels"
8+
xmlns:format="clr-namespace:CSharpBinding.FormattingStrategy">
69

710
<UserControl.Resources>
811

@@ -25,12 +28,28 @@
2528
<ListBox
2629
ItemsSource="{Binding Children}"
2730
BorderThickness="0"
31+
HorizontalContentAlignment="Stretch"
2832
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
33+
<ListBox.ItemTemplate>
34+
<DataTemplate>
35+
<DockPanel Margin="0,0,0,0">
36+
<ComboBox DockPanel.Dock="Right" Margin="0,0,0,0" MinWidth="100"
37+
VerticalAlignment="Center"
38+
format:FormattingOptionBinding.Container="{Binding ElementName=CSharpFormattingEditor_Self, Path=OptionsContainer}"
39+
format:FormattingOptionBinding.Option="{Binding Option}" />
40+
<sd:RestrictDesiredSize RestrictHeight="False" MinWidth="150">
41+
<TextBlock Text="{Binding Text}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Text}" VerticalAlignment="Center" />
42+
</sd:RestrictDesiredSize>
43+
</DockPanel>
44+
</DataTemplate>
45+
</ListBox.ItemTemplate>
2946
</ListBox>
3047
</DataTemplate>
3148

3249
</UserControl.Resources>
3350

34-
<ItemsControl ItemsSource="{Binding}" Margin="0,0,0,0" Background="{x:Static SystemColors.WindowBrush}">
35-
</ItemsControl>
51+
<sd:RestrictDesiredSize Margin="0,4,0,0">
52+
<ItemsControl ItemsSource="{Binding}" Background="{x:Static SystemColors.WindowBrush}">
53+
</ItemsControl>
54+
</sd:RestrictDesiredSize>
3655
</UserControl>

0 commit comments

Comments
 (0)