@@ -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
0 commit comments