private static async Task <Solution> FormatFilesInProjectAsync(ILogger logger, Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, CancellationToken cancellationToken) { var isCommentTrivia = project.Language == LanguageNames.CSharp ? IsCSharpCommentTrivia : IsVisualBasicCommentTrivia; var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } var formatTask = Task.Run(async() => { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (GeneratedCodeUtilities.IsGeneratedCode(syntaxTree, isCommentTrivia, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); return(await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false)); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution); }
public static async Task <OptionSet> WithEditorConfigOptions(this OptionSet optionSet, string path, ILoggerFactory loggerFactory) { if (!Path.IsPathRooted(path)) { path = Directory.GetCurrentDirectory(); } var codingConventionsManager = CodingConventionsManagerFactory.CreateCodingConventionsManager(); var optionsApplier = new EditorConfigOptionsApplier(loggerFactory); var context = await codingConventionsManager.GetConventionContextAsync(path, CancellationToken.None); if (context != null && context.CurrentConventions != null) { return(optionsApplier.ApplyConventions(optionSet, context.CurrentConventions, LanguageNames.CSharp)); } return(optionSet); }
private static async Task <(Solution solution, int filesFormatted)> FormatFilesInProjectAsync(ILogger logger, Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, string[] filesToFormat, CancellationToken cancellationToken) { var isCommentTrivia = project.Language == LanguageNames.CSharp ? IsCSharpCommentTrivia : IsVisualBasicCommentTrivia; var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } if (filesToFormat != null) { var fileInArgumentList = filesToFormat.Any(relativePath => document.FilePath.EndsWith(relativePath, StringComparison.OrdinalIgnoreCase)); if (!fileInArgumentList) { continue; } } var formatTask = Task.Run(async() => { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (GeneratedCodeUtilities.IsGeneratedCode(syntaxTree, isCommentTrivia, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); var formattedSourceText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); if (formattedSourceText.ContentEquals(await document.GetTextAsync(cancellationToken).ConfigureAwait(false))) { // Avoid touching files that didn't actually change return(null); } return(formattedSourceText); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; var filesFormatted = 0; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } filesFormatted++; formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution, filesFormatted); }
private static async Task <(Solution solution, int filesFormatted)> FormatFilesInProjectAsync(Project project, ICodingConventionsManager codingConventionsManager, EditorConfigOptionsApplier optionsApplier, ImmutableHashSet <string> filesToFormat, ILogger logger, CancellationToken cancellationToken) { var formattedDocuments = new List <(DocumentId documentId, Task <SourceText> formatTask)>(); foreach (var documentId in project.DocumentIds) { var document = project.Solution.GetDocument(documentId); if (!document.SupportsSyntaxTree) { continue; } if (!filesToFormat.IsEmpty && !filesToFormat.Contains(document.FilePath)) { continue; } var formatTask = Task.Run(async() => { if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken)) { return(null); } logger.LogTrace(Resources.Formatting_code_file_0, Path.GetFileName(document.FilePath)); OptionSet documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); var codingConventionsContext = await codingConventionsManager.GetConventionContextAsync(document.FilePath, cancellationToken).ConfigureAwait(false); if (codingConventionsContext?.CurrentConventions != null) { documentOptions = optionsApplier.ApplyConventions(documentOptions, codingConventionsContext.CurrentConventions, project.Language); } var formattedDocument = await Formatter.FormatAsync(document, documentOptions, cancellationToken).ConfigureAwait(false); var formattedSourceText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false); if (formattedSourceText.ContentEquals(await document.GetTextAsync(cancellationToken).ConfigureAwait(false))) { // Avoid touching files that didn't actually change return(null); } logger.LogInformation(Resources.Formatted_code_file_0, Path.GetFileName(document.FilePath)); return(formattedSourceText); }, cancellationToken); formattedDocuments.Add((documentId, formatTask)); } var formattedSolution = project.Solution; var filesFormatted = 0; foreach (var(documentId, formatTask) in formattedDocuments) { var text = await formatTask.ConfigureAwait(false); if (text is null) { continue; } filesFormatted++; formattedSolution = formattedSolution.WithDocumentText(documentId, text); } return(formattedSolution, filesFormatted); }