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

Commit 3111578

Browse files
committed
Fix: Still not correctly updating child formatting options when editing the parent.
1 parent 96a0466 commit 3111578

4 files changed

Lines changed: 250 additions & 186 deletions

File tree

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ internal class CSharpFormattingOptionsContainer : INotifyPropertyChanged
3939

4040
readonly HashSet<string> activeOptions;
4141

42-
internal CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent = null)
42+
public CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent = null)
43+
: this(parent, new HashSet<string>())
44+
{
45+
}
46+
47+
private CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent, HashSet<string> activeOptions)
4348
{
4449
this.parent = parent;
45-
this.activeOptions = new HashSet<string>();
50+
if (parent != null) {
51+
parent.PropertyChanged += HandlePropertyChanged;
52+
}
53+
this.activeOptions = activeOptions;
4654
Reset();
47-
cachedOptions = CreateOptions();
55+
cachedOptions = CreateCachedOptions();
4856
}
4957

5058
public CSharpFormattingOptionsContainer Parent
5159
{
52-
get
53-
{
60+
get {
5461
return parent;
5562
}
5663
}
@@ -72,6 +79,36 @@ public void Reset(CSharpFormattingOptions options = null)
7279
OnPropertyChanged(null);
7380
}
7481

82+
/// <summary>
83+
/// Creates a clone of current options container.
84+
/// </summary>
85+
/// <returns>Clone of options container.</returns>
86+
public CSharpFormattingOptionsContainer Clone()
87+
{
88+
// var cloneActiveOptions = new HashSet<string>();
89+
// foreach (var activeOption in activeOptions)
90+
// cloneActiveOptions.Add(activeOption);
91+
// var clone = new CSharpFormattingOptionsContainer(parent, cloneActiveOptions);
92+
// clone.Reset(cachedOptions.Clone());
93+
// return clone;
94+
95+
var clone = new CSharpFormattingOptionsContainer(parent);
96+
clone.CloneFrom(this);
97+
return clone;
98+
}
99+
100+
/// <summary>
101+
/// Clones all properties from another options container.
102+
/// </summary>
103+
/// <returns>Clone of options container.</returns>
104+
public void CloneFrom(CSharpFormattingOptionsContainer options)
105+
{
106+
activeOptions.Clear();
107+
foreach (var activeOption in options.activeOptions)
108+
activeOptions.Add(activeOption);
109+
Reset(options.cachedOptions.Clone());
110+
}
111+
75112
#region INotifyPropertyChanged implementation
76113

77114
public event PropertyChangedEventHandler PropertyChanged;
@@ -87,9 +124,10 @@ private void OnPropertyChanged(string propertyName)
87124

88125
private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
89126
{
90-
if (e.PropertyName == "Parent") {
91-
// Parent of parent has been updated, recreate options object
92-
cachedOptions = CreateOptions();
127+
if ((e.PropertyName == "Parent") || (e.PropertyName == null)) {
128+
// All properties might have changed -> update everything
129+
cachedOptions = CreateCachedOptions();
130+
OnPropertyChanged(e.PropertyName);
93131
} else {
94132
// Some other property has changed, check if we have our own value for it
95133
if (!activeOptions.Contains(e.PropertyName)) {
@@ -98,6 +136,7 @@ private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
98136
if (propertyInfo != null) {
99137
var val = GetOption(e.PropertyName);
100138
propertyInfo.SetValue(cachedOptions, val);
139+
OnPropertyChanged(e.PropertyName);
101140
}
102141
}
103142
}
@@ -206,7 +245,7 @@ public CSharpFormattingOptions GetEffectiveOptions()
206245
/// container, resolving all options throughout container hierarchy.
207246
/// </summary>
208247
/// <returns>Created and filled <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormattingOptions"/> instance.</returns>
209-
private CSharpFormattingOptions CreateOptions()
248+
private CSharpFormattingOptions CreateCachedOptions()
210249
{
211250
var outputOptions = FormattingOptionsFactory.CreateEmpty();
212251

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static void SolutionClosed(object sender, SolutionEventArgs e)
9191
}
9292

9393
readonly Properties propertiesContainer;
94-
readonly CSharpFormattingOptionsContainer optionsContainer;
94+
CSharpFormattingOptionsContainer optionsContainer;
95+
CSharpFormattingOptionsContainer optionsContainerWorkingCopy;
9596

9697
/// <summary>
9798
/// Creates a new instance of formatting options persistence helper, using given options to predefine the options container.
@@ -109,13 +110,28 @@ public CSharpFormattingOptionsPersistence(Properties propertiesContainer, CSharp
109110
optionsContainer = initialContainer;
110111
}
111112

113+
/// <summary>
114+
/// Returns the option container managed by this helper.
115+
/// </summary>
112116
public CSharpFormattingOptionsContainer OptionsContainer
113117
{
114118
get {
115119
return optionsContainer;
116120
}
117121
}
118122

123+
/// <summary>
124+
/// Starts editing operation by creating a working copy of current formatter settings.
125+
/// </summary>
126+
/// <returns>
127+
/// New working copy of managed options container.
128+
/// </returns>
129+
public CSharpFormattingOptionsContainer StartEditing()
130+
{
131+
optionsContainerWorkingCopy = optionsContainer.Clone();
132+
return optionsContainerWorkingCopy;
133+
}
134+
119135
/// <summary>
120136
/// Loads formatting settings from properties container.
121137
/// </summary>
@@ -130,6 +146,13 @@ public void Load()
130146
/// <returns><c>True</c> if successful, <c>false</c> otherwise</returns>
131147
public bool Save()
132148
{
149+
// Apply all changes on working copy to main options container
150+
if (optionsContainerWorkingCopy != null) {
151+
optionsContainer.CloneFrom(optionsContainerWorkingCopy);
152+
optionsContainerWorkingCopy = null;
153+
}
154+
155+
// Convert to SD properties
133156
optionsContainer.Save(propertiesContainer);
134157
return true;
135158
}

0 commit comments

Comments
 (0)