@@ -22,13 +22,25 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
22
22
{
23
23
private readonly IEnumerable < IFormattingFilter > _filters ;
24
24
private readonly IEnumerable < IFormattingRule > _rules ;
25
+ private readonly IEnumerable < ISyntaxFormattingRule > _syntaxRules ;
26
+ private readonly IEnumerable < ILocalSemanticFormattingRule > _localSemanticRules ;
27
+ private readonly IEnumerable < IGlobalSemanticFormattingRule > _globalSemanticRules ;
28
+ private readonly bool _verbose ;
29
+ private readonly Stopwatch _watch = new Stopwatch ( ) ;
25
30
26
31
[ ImportingConstructor ]
27
- public FormattingEngineImplementation ( [ ImportMany ] IEnumerable < IFormattingFilter > filters ,
28
- [ ImportMany ] IEnumerable < Lazy < IFormattingRule , IOrderMetadata > > rules )
32
+ public FormattingEngineImplementation (
33
+ [ ImportMany ] IEnumerable < IFormattingFilter > filters ,
34
+ [ ImportMany ] IEnumerable < Lazy < IFormattingRule , IOrderMetadata > > rules ,
35
+ [ ImportMany ] IEnumerable < Lazy < ISyntaxFormattingRule , IOrderMetadata > > syntaxRules ,
36
+ [ ImportMany ] IEnumerable < Lazy < ILocalSemanticFormattingRule , IOrderMetadata > > localSemanticRules ,
37
+ [ ImportMany ] IEnumerable < Lazy < IGlobalSemanticFormattingRule , IOrderMetadata > > globalSemanticRules )
29
38
{
30
39
_filters = filters ;
31
40
_rules = rules . OrderBy ( r => r . Metadata . Order ) . Select ( r => r . Value ) ;
41
+ _syntaxRules = syntaxRules . OrderBy ( r => r . Metadata . Order ) . Select ( r => r . Value ) ;
42
+ _localSemanticRules = localSemanticRules . OrderBy ( r => r . Metadata . Order ) . Select ( r => r . Value ) ;
43
+ _globalSemanticRules = globalSemanticRules . OrderBy ( r => r . Metadata . Order ) . Select ( r => r . Value ) ;
32
44
}
33
45
34
46
public Task < bool > FormatSolutionAsync ( Solution solution , CancellationToken cancellationToken )
@@ -115,6 +127,97 @@ private async Task<Document> RewriteDocumentAsync(Document document, List<Tuple<
115
127
return await ChangeEncoding ( document , originalEncoding ) ;
116
128
}
117
129
130
+ private void StartDocument ( Document document )
131
+ {
132
+ Console . Write ( "\t Processing {0}" , document . Name ) ;
133
+ _watch . Restart ( ) ;
134
+ }
135
+
136
+ private void EndDocument ( )
137
+ {
138
+ _watch . Stop ( ) ;
139
+ if ( _verbose && _watch . Elapsed . TotalSeconds > 1 )
140
+ {
141
+
142
+ }
143
+ }
144
+
145
+ private Task < Solution > FormatDocumentsSyntaxPass ( Solution originalSolution , IReadOnlyList < DocumentId > documentIds , CancellationToken cancellationToken )
146
+ {
147
+ Console . WriteLine ( "Syntax Pass" ) ;
148
+
149
+ var currentSolution = originalSolution ;
150
+ foreach ( var documentId in documentIds )
151
+ {
152
+ var document = originalSolution . GetDocument ( documentId ) ;
153
+
154
+ Console . Write ( "\t Processing {0}" , document . Name ) ;
155
+
156
+ watch . Restart ( ) ;
157
+ var newRoot = await documentFunc ( document ) ;
158
+ watch . Stop ( ) ;
159
+
160
+ if ( _verbose && watch . Elapsed . TotalSeconds > 1 )
161
+ {
162
+ Console . Write ( " {0} seconds" , watch . Elapsed . TotalSeconds ) ;
163
+ }
164
+ Console . WriteLine ( ) ;
165
+
166
+ if ( newRoot != null )
167
+ {
168
+ currentSolution = currentSolution . WithDocumentSyntaxRoot ( documentId , newRoot ) ;
169
+ }
170
+ }
171
+
172
+ return currentSolution ;
173
+ }
174
+
175
+ private async Task < Solution > FormatDocumentsLocalSemanticPass ( Solution originalSolution , IReadOnlyList < DocumentId > documentIds , CancellationToken cancellationToken )
176
+ {
177
+ Console . WriteLine ( "Local Semantic Pass" ) ;
178
+ Func < Document , Task < SyntaxTree > documentFunc = async ( documentFunc ) =>
179
+ {
180
+ var syntaxRoot = await document . GetSyntaxRootAsync ( cancellationToken ) ;
181
+ if ( syntaxRoot == null )
182
+ {
183
+ return null ;
184
+ }
185
+
186
+ return FormatLocalSemantic ( document , syntaxRoot ) ;
187
+ } ;
188
+
189
+ return FormatDocumentsCore ( originalSolution , documentIds , documentFunc , cancellationToken ) ;
190
+ }
191
+
192
+ private async Task < Solution > FormatDocumentsCore (
193
+ Solution originalSolution ,
194
+ IReadOnlyList < DocumentId > documentIds ,
195
+ Func < Document , Task < SyntaxTree > documentFunc ,
196
+ CancellationToken cancellationToken )
197
+ {
198
+ }
199
+
200
+ private Task < SyntaxTree > FormatSyntaxTree ( Document document , SyntaxNode syntaxRoot )
201
+ {
202
+ foreach ( var syntaxRule in _syntaxRules )
203
+ {
204
+ syntaxRoot = syntaxRule . Process ( syntaxRoot ) ;
205
+ }
206
+
207
+ return Task . FromResult ( root ) ;
208
+ }
209
+
210
+ private async Task < SyntaxTree > FormatLocalSemantic ( Document originalDocument , SyntaxNode originalSyntaxRoot )
211
+ {
212
+ var currentSyntaxRoot = originalSyntaxRoot ;
213
+ foreach ( var localSemanticRule in _localSemanticRules )
214
+ {
215
+ currentSyntaxRoot = await localSemanticRule . ProcessAsync ( originalDocument , originalSyntaxRoot , currentSyntaxRoot )
216
+ }
217
+
218
+ return currentSyntaxRoot ;
219
+ }
220
+
118
221
private async Task < Document > ChangeEncoding ( Document document , Encoding encoding )
119
222
{
120
223
var text = await document . GetTextAsync ( ) ;
0 commit comments