11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using System . Text ;
54using System . Threading . Tasks ;
65using CodeFactory . WinVs . Stats ;
76
@@ -20,69 +19,30 @@ public class SourceClassManager : SourceContainerManager<CsClass>
2019 /// <param name="vsActions">The CodeFactory API for Visual Studio.</param>
2120 /// <param name="namespaceManager">Optional parameter that sets the default namespace manager to use, default is null.</param>
2221 /// <param name="mappedNamespaces">Optional parameter that sets the mapped namespaces used for namespace management.</param>
23- public SourceClassManager ( CsSource source , CsClass container , IVsActions vsActions , NamespaceManager namespaceManager = null , List < MapNamespace > mappedNamespaces = null ) : base ( source , container , vsActions , namespaceManager , mappedNamespaces )
22+ public SourceClassManager ( CsSource source , CsClass container , IVsActions vsActions , NamespaceManager namespaceManager = null , List < MapNamespace > mappedNamespaces = null ) : base ( source , container , vsActions , namespaceManager , mappedNamespaces )
2423 {
2524 //Intentionally blank
2625 }
2726
2827 /// <summary>
2928 /// Checks all types definitions for the loaded container if the container is not loaded will not add missing using statements.
3029 /// </summary>
31- public override async Task AddMissingUsingStatementsAsync ( )
30+ [ Obsolete ( "Use AddNamespacesFromContainerAsync instead. This method will be removed in a future version." ) ]
31+ public override Task AddMissingUsingStatementsAsync ( )
3232 {
33- if ( Container == null ) return ;
34-
35- if ( NamespaceManager == null ) LoadNamespaceManager ( ) ;
36-
37- if ( Container . HasAttributes )
38- {
39- foreach ( var containerAttribute in Container . Attributes )
40- {
41- await AddMissingUsingStatementsAsync ( containerAttribute ) ;
42- }
43- }
44-
45- if ( Container . Fields . Any ( ) )
46- {
47- foreach ( var field in Container . Fields )
48- {
49- await AddMissingUsingStatementsAsync ( field ) ;
50- }
51- }
52-
53- if ( Container . Properties . Any ( ) )
54- {
55- foreach ( var containerProperty in Container . Properties )
56- {
57- await AddMissingUsingStatementsAsync ( containerProperty ) ;
58- }
59- }
60-
61- if ( Container . Methods . Any ( ) )
62- {
63- foreach ( var containerMethod in Container . Methods )
64- {
65- await AddMissingUsingStatementsAsync ( containerMethod ) ;
66- }
67- }
68-
69- if ( Container . Constructors . Any ( ) )
70- {
71- foreach ( var containerConstructor in Container . Constructors )
72- {
73- await AddMissingUsingStatementsAsync ( containerConstructor ) ;
74- }
75- }
33+ //Calling the base implementation
34+ return base . AddNamespacesFromContainerAsync ( ) ;
7635 }
7736
7837 /// <summary>
7938 /// Adds the provided syntax before the field definitions.
8039 /// </summary>
8140 /// <param name="syntax">Target syntax to be added.</param>
8241 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
83- public override async Task FieldsAddBeforeAsync ( string syntax )
42+ // PERF: No async state machine — directly returns the Task from the transaction method.
43+ public override Task FieldsAddBeforeAsync ( string syntax )
8444 {
85- await FieldsAddBeforeTransactionAsync ( syntax ) ;
45+ return FieldsAddBeforeTransactionAsync ( syntax ) ;
8646 }
8747
8848 /// <summary>
@@ -99,12 +59,11 @@ public override async Task<TransactionDetail> FieldsAddBeforeTransactionAsync(st
9959
10060 var sourceDoc = source . SourceDocument ;
10161
102- TransactionDetail result = null ;
62+ // PERF: Single-pass — FirstOrDefault replaces Any() + First() (was 2 iterations).
63+ var fieldData = container . Fields . FirstOrDefault ( f => f . ModelSourceFile == sourceDoc && f . LoadedFromSource ) ;
10364
104- if ( container . Fields . Any ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) )
65+ if ( fieldData != null )
10566 {
106- var fieldData = container . Fields . First ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) ;
107-
10867 var updatedSource = await fieldData . AddBeforeTransactionAsync ( syntax ) ;
10968
11069 if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
@@ -113,45 +72,21 @@ public override async Task<TransactionDetail> FieldsAddBeforeTransactionAsync(st
11372
11473 UpdateSources ( updatedSource . Source , updatedContainer ) ;
11574
116- result = updatedSource . Transaction ;
117- }
118- else
119- {
120- result = await this . ContainerAddToBeginningTransactionAsync ( syntax ) ;
75+ return updatedSource . Transaction ;
12176 }
12277
123- return result ;
78+ return await ContainerAddToBeginningTransactionAsync ( syntax ) ;
12479 }
12580
12681 /// <summary>
12782 /// Adds the provided syntax after the field definitions.
12883 /// </summary>
12984 /// <param name="syntax">Target syntax to be added.</param>
13085 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
131- public override async Task FieldsAddAfterAsync ( string syntax )
86+ // PERF: No async state machine — directly returns the Task from the transaction method.
87+ public override Task FieldsAddAfterAsync ( string syntax )
13288 {
133- if ( string . IsNullOrEmpty ( syntax ) ) return ;
134- var source = Source ?? throw new ArgumentNullException ( nameof ( Source ) ) ;
135- var container = Container ?? throw new ArgumentNullException ( nameof ( Container ) ) ;
136-
137- var sourceDoc = source . SourceDocument ;
138-
139- if ( container . Fields . Any ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) )
140- {
141- var fieldData = container . Fields . Last ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) ;
142-
143- var updatedSource = await fieldData . AddAfterAsync ( syntax ) ;
144-
145- if ( updatedSource == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
146-
147- var updatedContainer = updatedSource . GetModel < CsClass > ( ContainerPath ) ;
148-
149- UpdateSources ( updatedSource , updatedContainer ) ;
150- }
151- else
152- {
153- await this . ContainerAddToBeginningAsync ( syntax ) ;
154- }
89+ return FieldsAddAfterTransactionAsync ( syntax ) ;
15590 }
15691
15792 /// <summary>
@@ -168,12 +103,11 @@ public override async Task<TransactionDetail> FieldsAddAfterTransactionAsync(str
168103
169104 var sourceDoc = source . SourceDocument ;
170105
171- TransactionDetail result = null ;
106+ // PERF: Single-pass — LastOrDefault replaces Any() + Last() (was 2 iterations).
107+ var fieldData = container . Fields . LastOrDefault ( f => f . ModelSourceFile == sourceDoc && f . LoadedFromSource ) ;
172108
173- if ( container . Fields . Any ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) )
109+ if ( fieldData != null )
174110 {
175- var fieldData = container . Fields . Last ( f => f . ModelSourceFile == sourceDoc & f . LoadedFromSource ) ;
176-
177111 var updatedSource = await fieldData . AddAfterTransactionAsync ( syntax ) ;
178112
179113 if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
@@ -182,24 +116,21 @@ public override async Task<TransactionDetail> FieldsAddAfterTransactionAsync(str
182116
183117 UpdateSources ( updatedSource . Source , updatedContainer ) ;
184118
185- result = updatedSource . Transaction ;
186- }
187- else
188- {
189- result = await this . ContainerAddToBeginningTransactionAsync ( syntax ) ;
119+ return updatedSource . Transaction ;
190120 }
191121
192- return result ;
122+ return await ContainerAddToBeginningTransactionAsync ( syntax ) ;
193123 }
194124
195125 /// <summary>
196126 /// Add the provided syntax before the constructor definitions.
197127 /// </summary>
198128 /// <param name="syntax">Target syntax to be added.</param>
199129 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
200- public override async Task ConstructorsAddBeforeAsync ( string syntax )
130+ // PERF: No async state machine — directly returns the Task from the transaction method.
131+ public override Task ConstructorsAddBeforeAsync ( string syntax )
201132 {
202- await ConstructorsAddBeforeTransactionAsync ( syntax ) ;
133+ return ConstructorsAddBeforeTransactionAsync ( syntax ) ;
203134 }
204135
205136 /// <summary>
@@ -216,11 +147,11 @@ public override async Task<TransactionDetail> ConstructorsAddBeforeTransactionAs
216147
217148 var sourceDoc = source . SourceDocument ;
218149
219- TransactionDetail result = null ;
220- if ( container . Constructors . Any ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) )
221- {
222- var constData = container . Constructors . First ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) ;
150+ // PERF: Single-pass — FirstOrDefault replaces Any() + First() (was 2 iterations).
151+ var constData = container . Constructors . FirstOrDefault ( c => c . ModelSourceFile == sourceDoc && c . LoadedFromSource ) ;
223152
153+ if ( constData != null )
154+ {
224155 var updatedSource = await constData . AddBeforeTransactionAsync ( syntax ) ;
225156
226157 if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
@@ -229,24 +160,23 @@ public override async Task<TransactionDetail> ConstructorsAddBeforeTransactionAs
229160
230161 UpdateSources ( updatedSource . Source , updatedContainer ) ;
231162
232- result = updatedSource . Transaction ;
233- }
234- else
235- {
236- result = await this . FieldsAddAfterTransactionAsync ( syntax ) ;
163+ return updatedSource . Transaction ;
237164 }
238165
239- return result ;
166+ // No constructors exist in this source file — fall back to placing syntax after
167+ // field definitions, which is the nearest logical position before constructors.
168+ return await FieldsAddAfterTransactionAsync ( syntax ) ;
240169 }
241170
242171 /// <summary>
243172 /// Add the provided syntax after the constructor definitions.
244173 /// </summary>
245174 /// <param name="syntax">Target syntax to be added.</param>
246175 /// <exception cref="ArgumentNullException">Thrown if either the source or the container is null after updating.</exception>
247- public override async Task ConstructorsAddAfterAsync ( string syntax )
176+ // PERF: No async state machine — directly returns the Task from the transaction method.
177+ public override Task ConstructorsAddAfterAsync ( string syntax )
248178 {
249- await ConstructorsAddAfterTransactionAsync ( syntax ) ;
179+ return ConstructorsAddAfterTransactionAsync ( syntax ) ;
250180 }
251181
252182 /// <summary>
@@ -263,12 +193,11 @@ public override async Task<TransactionDetail> ConstructorsAddAfterTransactionAsy
263193
264194 var sourceDoc = source . SourceDocument ;
265195
266- TransactionDetail result = null ;
196+ // PERF: Single-pass — LastOrDefault replaces Any() + Last() (was 2 iterations).
197+ var constData = container . Constructors . LastOrDefault ( c => c . ModelSourceFile == sourceDoc && c . LoadedFromSource ) ;
267198
268- if ( container . Constructors . Any ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) )
199+ if ( constData != null )
269200 {
270- var constData = container . Constructors . Last ( c => c . ModelSourceFile == sourceDoc & c . LoadedFromSource ) ;
271-
272201 var updatedSource = await constData . AddAfterTransactionAsync ( syntax ) ;
273202
274203 if ( updatedSource ? . Source == null ) throw new ArgumentNullException ( nameof ( Source ) ) ;
@@ -277,14 +206,12 @@ public override async Task<TransactionDetail> ConstructorsAddAfterTransactionAsy
277206
278207 UpdateSources ( updatedSource . Source , updatedContainer ) ;
279208
280- result = updatedSource . Transaction ;
281- }
282- else
283- {
284- result = await this . FieldsAddAfterTransactionAsync ( syntax ) ;
209+ return updatedSource . Transaction ;
285210 }
286211
287- return result ;
212+ // No constructors exist in this source file — fall back to placing syntax after
213+ // field definitions as the best available insertion point.
214+ return await FieldsAddAfterTransactionAsync ( syntax ) ;
288215 }
289216 }
290217}
0 commit comments