3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Collections . Immutable ;
6
7
using System . ComponentModel . Composition ;
8
+ using System . Diagnostics ;
7
9
using System . IO ;
8
10
using System . Linq ;
9
11
using System . Text ;
@@ -21,7 +23,6 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
21
23
private readonly IEnumerable < IFormattingFilter > _filters ;
22
24
private readonly IEnumerable < IFormattingRule > _rules ;
23
25
24
-
25
26
[ ImportingConstructor ]
26
27
public FormattingEngineImplementation ( [ ImportMany ] IEnumerable < IFormattingFilter > filters ,
27
28
[ ImportMany ] IEnumerable < Lazy < IFormattingRule , IOrderMetadata > > rules )
@@ -30,22 +31,44 @@ public FormattingEngineImplementation([ImportMany] IEnumerable<IFormattingFilter
30
31
_rules = rules . OrderBy ( r => r . Metadata . Order ) . Select ( r => r . Value ) ;
31
32
}
32
33
33
- public async Task < bool > RunAsync ( Workspace workspace , CancellationToken cancellationToken )
34
+ public Task < bool > FormatSolutionAsync ( Solution solution , CancellationToken cancellationToken )
35
+ {
36
+ var documentIds = solution . Projects . SelectMany ( x => x . DocumentIds ) . ToList ( ) ;
37
+ return FormatAsync ( solution . Workspace , documentIds , cancellationToken ) ;
38
+ }
39
+
40
+ public Task < bool > FormatProjectAsync ( Project project , CancellationToken cancellationToken )
41
+ {
42
+ return FormatAsync ( project . Solution . Workspace , project . DocumentIds , cancellationToken ) ;
43
+ }
44
+
45
+ private async Task < bool > FormatAsync ( Workspace workspace , IReadOnlyList < DocumentId > documentIds , CancellationToken cancellationToken )
34
46
{
35
47
var solution = workspace . CurrentSolution ;
36
- var documentIds = solution . Projects . SelectMany ( p => p . DocumentIds ) ;
37
48
var hasChanges = false ;
49
+ var longRuleList = new List < Tuple < string , TimeSpan > > ( ) ;
38
50
39
51
foreach ( var id in documentIds )
40
52
{
41
53
var document = solution . GetDocument ( id ) ;
42
54
var shouldBeProcessed = await ShouldBeProcessedAsync ( document ) ;
43
55
if ( ! shouldBeProcessed )
56
+ {
44
57
continue ;
58
+ }
45
59
46
- Console . WriteLine ( "Processing document: " + document . Name ) ;
47
- var newDocument = await RewriteDocumentAsync ( document , cancellationToken ) ;
60
+ longRuleList . Clear ( ) ;
61
+ var watch = new Stopwatch ( ) ;
62
+ watch . Start ( ) ;
63
+ Console . Write ( "Processing document: " + document . Name ) ;
64
+ var newDocument = await RewriteDocumentAsync ( document , longRuleList , cancellationToken ) ;
48
65
hasChanges |= newDocument != document ;
66
+ watch . Stop ( ) ;
67
+ Console . WriteLine ( " {0} seconds" , watch . Elapsed . TotalSeconds ) ;
68
+ foreach ( var tuple in longRuleList )
69
+ {
70
+ Console . WriteLine ( "\t {0} {1} seconds" , tuple . Item1 , tuple . Item2 . TotalSeconds ) ;
71
+ }
49
72
50
73
solution = newDocument . Project . Solution ;
51
74
}
@@ -70,12 +93,24 @@ private async Task<bool> ShouldBeProcessedAsync(Document document)
70
93
return true ;
71
94
}
72
95
73
- private async Task < Document > RewriteDocumentAsync ( Document document , CancellationToken cancellationToken )
96
+ private async Task < Document > RewriteDocumentAsync ( Document document , List < Tuple < string , TimeSpan > > longRuleList , CancellationToken cancellationToken )
74
97
{
75
98
var docText = await document . GetTextAsync ( ) ;
76
99
var originalEncoding = docText . Encoding ;
100
+ var watch = new Stopwatch ( ) ;
77
101
foreach ( var rule in _rules )
102
+ {
103
+ watch . Start ( ) ;
78
104
document = await rule . ProcessAsync ( document , cancellationToken ) ;
105
+ watch . Stop ( ) ;
106
+ var timeSpan = watch . Elapsed ;
107
+ if ( timeSpan . TotalSeconds > 1.0 )
108
+ {
109
+ longRuleList . Add ( Tuple . Create ( rule . GetType ( ) . Name , timeSpan ) ) ;
110
+ }
111
+
112
+ watch . Reset ( ) ;
113
+ }
79
114
80
115
return await ChangeEncoding ( document , originalEncoding ) ;
81
116
}
0 commit comments