예제 #1
0
        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);
        }
        /// <summary>
        /// Try to load guideline positions from .editorconfig
        /// </summary>
        /// <param name="codingConventionsManager">The coding conventions (.editorconfig) manager.</param>
        /// <param name="filePath">Path to the document being edited.</param>
        /// <returns>A task which completes when the convention has been loaded and applied.</returns>
        private async Task LoadGuidelinesFromEditorConfigAsync(ICodingConventionsManager codingConventionsManager, string filePath)
        {
            var cancellationToken       = _codingConventionsCancellationTokenSource.Token;
            var codingConventionContext = await codingConventionsManager.GetConventionContextAsync(filePath, cancellationToken).ConfigureAwait(false);

            codingConventionContext.CodingConventionsChangedAsync += OnCodingConventionsChangedAsync;
            cancellationToken.Register(() => codingConventionContext.CodingConventionsChangedAsync -= OnCodingConventionsChangedAsync);

            await UpdateGuidelinesFromCodingConventionAsync(codingConventionContext, cancellationToken).ConfigureAwait(false);
        }
예제 #3
0
        private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, ICodingConventionsManager codingConventionsManager)
        {
            var tree = context.Tree;
            var cancellationToken = context.CancellationToken;

            OptionSet options = CompilerAnalyzerConfigOptions.Empty;

            if (File.Exists(tree.FilePath))
            {
                var codingConventionContext = codingConventionsManager.GetConventionContextAsync(tree.FilePath, cancellationToken).GetAwaiter().GetResult();
                options = ApplyFormattingOptions(options, codingConventionContext);
            }

            FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormattingService, Descriptor, options);
        }
        private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, Workspace workspace, ICodingConventionsManager codingConventionsManager)
        {
            var tree = context.Tree;
            var cancellationToken = context.CancellationToken;

            var options = workspace.Options;

            if (File.Exists(tree.FilePath))
            {
                var codingConventionContext = codingConventionsManager.GetConventionContextAsync(tree.FilePath, cancellationToken).GetAwaiter().GetResult();
                options = ApplyFormattingOptions(options, codingConventionContext);
            }

            FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, _descriptor, workspace, options);
        }
예제 #5
0
        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 Task <ICodingConventionContext> GetConventionContextAsync(string path, CancellationToken cancellationToken)
 {
     return(IOUtilities.PerformIOAsync(
                () => _codingConventionsManager.GetConventionContextAsync(path, cancellationToken),
                defaultValue: EmptyCodingConventionContext.Instance));
 }
예제 #7
0
        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);
        }