|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.ComponentModel.Composition; |
| 7 | +using System.Diagnostics; |
7 | 8 | using System.IO; |
8 | 9 | using System.Linq; |
9 | 10 | using System.Text; |
@@ -39,20 +40,40 @@ public async Task<bool> RunAsync(Workspace workspace, CancellationToken cancella |
39 | 40 | foreach (var id in documentIds) |
40 | 41 | { |
41 | 42 | var document = solution.GetDocument(id); |
| 43 | + |
| 44 | + var fileInfo = new FileInfo(document.FilePath); |
| 45 | + if (!fileInfo.Exists || fileInfo.IsReadOnly) |
| 46 | + { |
| 47 | + Console.WriteLine("warning: skipping document '{0}' because it {1}.", |
| 48 | + document.FilePath, |
| 49 | + fileInfo.IsReadOnly ? "is read-only" : "does not exist"); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
42 | 53 | var shouldBeProcessed = await ShouldBeProcessedAsync(document); |
43 | 54 | if (!shouldBeProcessed) |
44 | 55 | continue; |
45 | 56 |
|
46 | 57 | Console.WriteLine("Processing document: " + document.Name); |
47 | 58 | var newDocument = await RewriteDocumentAsync(document, cancellationToken); |
48 | | - hasChanges |= newDocument != document; |
49 | 59 |
|
50 | | - solution = newDocument.Project.Solution; |
51 | | - } |
| 60 | + if (newDocument != document) |
| 61 | + { |
| 62 | + Debug.Assert(newDocument.FilePath == document.FilePath, "Formatting rules should not change a document's file path"); |
52 | 63 |
|
53 | | - if (workspace.TryApplyChanges(solution)) |
54 | | - { |
55 | | - Console.WriteLine("Solution changes committed"); |
| 64 | + hasChanges = true; |
| 65 | + var sourceText = await newDocument.GetTextAsync(cancellationToken); |
| 66 | + |
| 67 | + using (var file = File.Open(newDocument.FilePath, FileMode.Truncate, FileAccess.Write)) |
| 68 | + { |
| 69 | + using (var writer = new StreamWriter(file, sourceText.Encoding)) |
| 70 | + { |
| 71 | + sourceText.Write(writer, cancellationToken); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + solution = newDocument.Project.Solution; |
56 | 77 | } |
57 | 78 |
|
58 | 79 | return hasChanges; |
|
0 commit comments