-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathCodeFormatter.cs
More file actions
291 lines (238 loc) · 12.8 KB
/
CodeFormatter.cs
File metadata and controls
291 lines (238 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Tools.Utilities;
using Microsoft.CodeAnalysis.Tools.Formatters;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.CodingConventions;
namespace Microsoft.CodeAnalysis.Tools
{
internal static class CodeFormatter
{
private static readonly ImmutableArray<ICodeFormatter> s_codeFormatters = new ICodeFormatter[]
{
new WhitespaceFormatter(),
new FinalNewlineFormatter(),
}.ToImmutableArray();
public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
FormatOptions options,
ILogger logger,
CancellationToken cancellationToken)
{
var (workspaceFilePath, isSolution, logLevel, saveFormattedFiles, _, filesToFormat) = options;
var logWorkspaceWarnings = logLevel == LogLevel.Trace;
logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, workspaceFilePath));
logger.LogTrace(Resources.Loading_workspace);
var workspaceStopwatch = Stopwatch.StartNew();
using (var workspace = await OpenWorkspaceAsync(
workspaceFilePath, isSolution, logWorkspaceWarnings, logger, cancellationToken).ConfigureAwait(false))
{
if (workspace is null)
{
return new WorkspaceFormatResult(filesFormatted: 0, fileCount: 0, exitCode: 1);
}
var loadWorkspaceMS = workspaceStopwatch.ElapsedMilliseconds;
logger.LogTrace(Resources.Complete_in_0_ms, workspaceStopwatch.ElapsedMilliseconds);
var projectPath = isSolution ? string.Empty : workspaceFilePath;
var solution = workspace.CurrentSolution;
logger.LogTrace(Resources.Determining_formattable_files);
var (fileCount, formatableFiles) = await DetermineFormattableFiles(
solution, projectPath, filesToFormat, logger, cancellationToken).ConfigureAwait(false);
var determineFilesMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS;
logger.LogTrace(Resources.Complete_in_0_ms, determineFilesMS);
logger.LogTrace(Resources.Running_formatters);
var formattedSolution = await RunCodeFormattersAsync(
solution, formatableFiles, options, logger, cancellationToken).ConfigureAwait(false);
var formatterRanMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS - determineFilesMS;
logger.LogTrace(Resources.Complete_in_0_ms, formatterRanMS);
var solutionChanges = formattedSolution.GetChanges(solution);
var filesFormatted = 0;
foreach (var projectChanges in solutionChanges.GetProjectChanges())
{
foreach (var changedDocumentId in projectChanges.GetChangedDocuments())
{
var changedDocument = solution.GetDocument(changedDocumentId);
logger.LogInformation(Resources.Formatted_code_file_0, Path.GetFileName(changedDocument.FilePath));
filesFormatted++;
}
}
var exitCode = 0;
if (saveFormattedFiles && !workspace.TryApplyChanges(formattedSolution))
{
logger.LogError(Resources.Failed_to_save_formatting_changes);
exitCode = 1;
}
logger.LogDebug(Resources.Formatted_0_of_1_files, filesFormatted, fileCount);
logger.LogInformation(Resources.Format_complete_in_0_ms, workspaceStopwatch.ElapsedMilliseconds);
return new WorkspaceFormatResult(filesFormatted, fileCount, exitCode);
}
}
private static async Task<Workspace> OpenWorkspaceAsync(
string solutionOrProjectPath,
bool isSolution,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
{
var properties = new Dictionary<string, string>(StringComparer.Ordinal)
{
// This property ensures that XAML files will be compiled in the current AppDomain
// rather than a separate one. Any tasks isolated in AppDomains or tasks that create
// AppDomains will likely not work due to https://github.com/Microsoft/MSBuildLocator/issues/16.
{ "AlwaysCompileMarkupFilesInSeparateDomain", bool.FalseString },
// This flag is used at restore time to avoid imports from packages changing the inputs to restore,
// without this it is possible to get different results between the first and second restore.
{ "ExcludeRestorePackageImports", bool.TrueString },
};
var workspace = MSBuildWorkspace.Create(properties);
workspace.WorkspaceFailed += LogWorkspaceWarnings;
var projectPath = string.Empty;
if (isSolution)
{
await workspace.OpenSolutionAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false);
}
else
{
try
{
await workspace.OpenProjectAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false);
projectPath = solutionOrProjectPath;
}
catch (InvalidOperationException)
{
logger.LogError(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, solutionOrProjectPath);
workspace.Dispose();
return null;
}
}
workspace.WorkspaceFailed -= LogWorkspaceWarnings;
return workspace;
void LogWorkspaceWarnings(object sender, WorkspaceDiagnosticEventArgs args)
{
if (args.Diagnostic.Kind == WorkspaceDiagnosticKind.Failure)
{
return;
}
if (!logWorkspaceWarnings)
{
logger.LogWarning(Resources.Warnings_were_encountered_while_loading_the_workspace_Set_the_verbosity_option_to_the_diagnostic_level_to_log_warnings);
((MSBuildWorkspace)sender).WorkspaceFailed -= LogWorkspaceWarnings;
return;
}
logger.LogWarning(args.Diagnostic.Message);
}
}
private static async Task<Solution> RunCodeFormattersAsync(
Solution solution,
ImmutableArray<(Document, OptionSet, ICodingConventionsSnapshot)> formattableDocuments,
FormatOptions options,
ILogger logger,
CancellationToken cancellationToken)
{
var formattedSolution = solution;
foreach (var codeFormatter in s_codeFormatters)
{
formattedSolution = await codeFormatter.FormatAsync(formattedSolution, formattableDocuments, options, logger, cancellationToken).ConfigureAwait(false);
}
return formattedSolution;
}
internal static async Task<(int, ImmutableArray<(Document, OptionSet, ICodingConventionsSnapshot)>)> DetermineFormattableFiles(
Solution solution,
string projectPath,
ImmutableHashSet<string> filesToFormat,
ILogger logger,
CancellationToken cancellationToken)
{
var codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager();
var optionsApplier = new EditorConfigOptionsApplier();
var fileCount = 0;
var getDocumentsAndOptions = new List<Task<(Document, OptionSet, ICodingConventionsSnapshot, bool)>>(solution.Projects.Sum(project => project.DocumentIds.Count));
foreach (var project in solution.Projects)
{
// If a project is used as a workspace, then ignore other referenced projects.
if (!string.IsNullOrEmpty(projectPath) && !project.FilePath.Equals(projectPath, StringComparison.OrdinalIgnoreCase))
{
logger.LogDebug(Resources.Skipping_referenced_project_0, project.Name);
continue;
}
// Ignore unsupported project types.
if (project.Language != LanguageNames.CSharp && project.Language != LanguageNames.VisualBasic)
{
logger.LogWarning(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, project.FilePath);
continue;
}
fileCount += project.DocumentIds.Count;
// Get project documents and options with .editorconfig settings applied.
var getProjectDocuments = project.DocumentIds.Select(documentId => Task.Run(async () => await GetDocumentAndOptions(
project, documentId, filesToFormat, codingConventionsManager, optionsApplier, cancellationToken).ConfigureAwait(false), cancellationToken));
getDocumentsAndOptions.AddRange(getProjectDocuments);
}
var documentsAndOptions = await Task.WhenAll(getDocumentsAndOptions).ConfigureAwait(false);
var foundEditorConfig = documentsAndOptions.Any(documentAndOptions => documentAndOptions.Item4);
var addedFilePaths = new HashSet<string>(documentsAndOptions.Length);
var formattableFiles = ImmutableArray.CreateBuilder<(Document, OptionSet, ICodingConventionsSnapshot)>(documentsAndOptions.Length);
foreach (var (document, options, codingConventions, hasEditorConfig) in documentsAndOptions)
{
if (document is null)
{
continue;
}
// If any code file has an .editorconfig, then we should ignore files without an .editorconfig entry.
if (foundEditorConfig && !hasEditorConfig)
{
continue;
}
// If we've already added this document, either via a link or multi-targeted framework, then ignore.
if (addedFilePaths.Contains(document.FilePath))
{
continue;
}
addedFilePaths.Add(document.FilePath);
formattableFiles.Add((document, options, codingConventions));
}
return (fileCount, formattableFiles.ToImmutableArray());
}
private static async Task<(Document, OptionSet, ICodingConventionsSnapshot, bool)> GetDocumentAndOptions(
Project project,
DocumentId documentId,
ImmutableHashSet<string> filesToFormat,
ICodingConventionsManager codingConventionsManager,
EditorConfigOptionsApplier optionsApplier,
CancellationToken cancellationToken)
{
var document = project.Solution.GetDocument(documentId);
// If a files list was passed in, then ignore files not present in the list.
if (!filesToFormat.IsEmpty && !filesToFormat.Contains(document.FilePath))
{
return (null, null, null, false);
}
if (!document.SupportsSyntaxTree)
{
return (null, null, null, false);
}
// Ignore generated code files.
if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken).ConfigureAwait(false))
{
return (null, null, null, false);
}
var context = await codingConventionsManager.GetConventionContextAsync(
document.FilePath, cancellationToken).ConfigureAwait(false);
OptionSet options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
// Check whether an .editorconfig was found for this document.
if (context?.CurrentConventions is null)
{
return (document, options, null, false);
}
options = optionsApplier.ApplyConventions(options, context.CurrentConventions, project.Language);
return (document, options, context.CurrentConventions, true);
}
}
}