33using System . Linq ;
44using System . Text ;
55using System . Threading . Tasks ;
6+ using CodeFactory . WinVs . Stats ;
67
78namespace CodeFactory . WinVs . Models . CSharp . Builder
89{
@@ -81,28 +82,45 @@ public override async Task AddMissingUsingStatementsAsync()
8182 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
8283 public override async Task FieldsAddBeforeAsync ( string syntax )
8384 {
84- if ( string . IsNullOrEmpty ( syntax ) ) return ;
85+ await FieldsAddBeforeTransactionAsync ( syntax ) ;
86+ }
87+
88+ /// <summary>
89+ /// Adds the provided syntax before the field definitions.
90+ /// </summary>
91+ /// <param name="syntax">Target syntax to be added.</param>
92+ /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
93+ /// <returns>The details of the updated source or null if the transaction details could not be saved.</returns>
94+ public override async Task < TransactionDetail > FieldsAddBeforeTransactionAsync ( string syntax )
95+ {
96+ if ( string . IsNullOrEmpty ( syntax ) ) return null ;
8597 var source = Source ?? throw new ArgumentNullException ( nameof ( Source ) ) ;
8698 var container = Container ?? throw new ArgumentNullException ( nameof ( Container ) ) ;
8799
88100 var sourceDoc = source . SourceDocument ;
89101
102+ TransactionDetail result = null ;
103+
90104 if ( container . Fields . Any ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) )
91105 {
92106 var fieldData = container . Fields . First ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) ;
93107
94- var updatedSource = await fieldData . AddBeforeAsync ( syntax ) ;
108+ var updatedSource = await fieldData . AddBeforeTransactionAsync ( syntax ) ;
95109
96- if ( updatedSource == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
110+ if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
97111
98- var updatedContainer = updatedSource . GetModel < CsClass > ( ContainerPath ) ;
112+ var updatedContainer = updatedSource . Source . GetModel < CsClass > ( ContainerPath ) ;
99113
100- UpdateSources ( updatedSource , updatedContainer ) ;
114+ UpdateSources ( updatedSource . Source , updatedContainer ) ;
115+
116+ result = updatedSource . Transaction ;
101117 }
102118 else
103119 {
104- await this . ContainerAddToBeginningAsync ( syntax ) ;
120+ result = await this . ContainerAddToBeginningTransactionAsync ( syntax ) ;
105121 }
122+
123+ return result ;
106124 }
107125
108126 /// <summary>
@@ -136,35 +154,89 @@ public override async Task FieldsAddAfterAsync(string syntax)
136154 }
137155 }
138156
157+ /// <summary>
158+ /// Adds the provided syntax after the field definitions.
159+ /// </summary>
160+ /// <param name="syntax">Target syntax to be added.</param>
161+ /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
162+ /// <returns>The details of the updated source or null if the transaction details could not be saved.</returns>
163+ public override async Task < TransactionDetail > FieldsAddAfterTransactionAsync ( string syntax )
164+ {
165+ if ( string . IsNullOrEmpty ( syntax ) ) return null ;
166+ var source = Source ?? throw new ArgumentNullException ( nameof ( Source ) ) ;
167+ var container = Container ?? throw new ArgumentNullException ( nameof ( Container ) ) ;
168+
169+ var sourceDoc = source . SourceDocument ;
170+
171+ TransactionDetail result = null ;
172+
173+ if ( container . Fields . Any ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) )
174+ {
175+ var fieldData = container . Fields . Last ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) ;
176+
177+ var updatedSource = await fieldData . AddAfterTransactionAsync ( syntax ) ;
178+
179+ if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
180+
181+ var updatedContainer = updatedSource . Source . GetModel < CsClass > ( ContainerPath ) ;
182+
183+ UpdateSources ( updatedSource . Source , updatedContainer ) ;
184+
185+ result = updatedSource . Transaction ;
186+ }
187+ else
188+ {
189+ result = await this . ContainerAddToBeginningTransactionAsync ( syntax ) ;
190+ }
191+
192+ return result ;
193+ }
194+
139195 /// <summary>
140196 /// Add the provided syntax before the constructor definitions.
141197 /// </summary>
142198 /// <param name="syntax">Target syntax to be added.</param>
143199 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
144200 public override async Task ConstructorsAddBeforeAsync ( string syntax )
145201 {
146- if ( string . IsNullOrEmpty ( syntax ) ) return ;
202+ await ConstructorsAddBeforeTransactionAsync ( syntax ) ;
203+ }
204+
205+ /// <summary>
206+ /// Add the provided syntax before the constructor definitions.
207+ /// </summary>
208+ /// <param name="syntax">Target syntax to be added.</param>
209+ /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
210+ /// <returns>The details of the updated source or null if the transaction details could not be saved.</returns>
211+ public override async Task < TransactionDetail > ConstructorsAddBeforeTransactionAsync ( string syntax )
212+ {
213+ if ( string . IsNullOrEmpty ( syntax ) ) return null ;
147214 var source = Source ?? throw new ArgumentNullException ( nameof ( Source ) ) ;
148215 var container = Container ?? throw new ArgumentNullException ( nameof ( Container ) ) ;
149216
150217 var sourceDoc = source . SourceDocument ;
151218
219+ TransactionDetail result = null ;
152220 if ( container . Constructors . Any ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) )
153221 {
154222 var constData = container . Constructors . First ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) ;
155223
156- var updatedSource = await constData . AddBeforeAsync ( syntax ) ;
224+ var updatedSource = await constData . AddBeforeTransactionAsync ( syntax ) ;
157225
158- if ( updatedSource == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
226+ if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
159227
160- var updatedContainer = updatedSource . GetModel < CsClass > ( ContainerPath ) ;
228+ var updatedContainer = updatedSource . Source . GetModel < CsClass > ( ContainerPath ) ;
161229
162- UpdateSources ( updatedSource , updatedContainer ) ;
230+ UpdateSources ( updatedSource . Source , updatedContainer ) ;
231+
232+ result = updatedSource . Transaction ;
163233 }
164234 else
165235 {
166- await this . FieldsAddAfterAsync ( syntax ) ;
236+ result = await this . FieldsAddAfterTransactionAsync ( syntax ) ;
167237 }
238+
239+ return result ;
168240 }
169241
170242 /// <summary>
@@ -174,28 +246,45 @@ public override async Task ConstructorsAddBeforeAsync(string syntax)
174246 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
175247 public override async Task ConstructorsAddAfterAsync ( string syntax )
176248 {
177- if ( string . IsNullOrEmpty ( syntax ) ) return ;
249+ await ConstructorsAddAfterTransactionAsync ( syntax ) ;
250+ }
251+
252+ /// <summary>
253+ /// Add the provided syntax after the constructor definitions.
254+ /// </summary>
255+ /// <param name="syntax">Target syntax to be added.</param>
256+ /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
257+ /// <returns>The details of the updated source or null if the transaction details could not be saved.</returns>
258+ public override async Task < TransactionDetail > ConstructorsAddAfterTransactionAsync ( string syntax )
259+ {
260+ if ( string . IsNullOrEmpty ( syntax ) ) return null ;
178261 var source = Source ?? throw new ArgumentNullException ( nameof ( Source ) ) ;
179262 var container = Container ?? throw new ArgumentNullException ( nameof ( Container ) ) ;
180263
181264 var sourceDoc = source . SourceDocument ;
182265
266+ TransactionDetail result = null ;
267+
183268 if ( container . Constructors . Any ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) )
184269 {
185270 var constData = container . Constructors . Last ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) ;
186271
187- var updatedSource = await constData . AddAfterAsync ( syntax ) ;
272+ var updatedSource = await constData . AddAfterTransactionAsync ( syntax ) ;
188273
189- if ( updatedSource == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
274+ if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
190275
191- var updatedContainer = updatedSource . GetModel < CsClass > ( ContainerPath ) ;
276+ var updatedContainer = updatedSource . Source . GetModel < CsClass > ( ContainerPath ) ;
192277
193- UpdateSources ( updatedSource , updatedContainer ) ;
278+ UpdateSources ( updatedSource . Source , updatedContainer ) ;
279+
280+ result = updatedSource . Transaction ;
194281 }
195282 else
196283 {
197- await this . FieldsAddAfterAsync ( syntax ) ;
284+ result = await this . FieldsAddAfterTransactionAsync ( syntax ) ;
198285 }
286+
287+ return result ;
199288 }
200289 }
201290}
0 commit comments