예제 #1
0
        public static async Task <IEnumerable <Diagnostic> > GetDiagnosticsAsync(this AdhocWorkspace workspace, Language lang, string documentPath, bool isFramework, IEnumerable <string> diagnosticIds)
        {
            if (documentPath is null)
            {
                throw new ArgumentNullException(nameof(documentPath));
            }

            var project = await workspace.CreateProjectAsync(lang, isFramework);

            return(await GetDiagnosticsFromProjectAsync(project, documentPath, diagnosticIds).ConfigureAwait(false));
        }
예제 #2
0
        public static async Task <string> FixSourceAsync(this AdhocWorkspace workspace, Language lang, string documentPath, IEnumerable <string> diagnosticIds)
        {
            if (documentPath is null)
            {
                throw new ArgumentNullException(nameof(documentPath));
            }

            var project = await workspace.CreateProjectAsync(lang, isFramework : false);

            var projectId = project.Id;

            var diagnosticFixed = false;
            var solution        = project.Solution;

            const int MAX_TRIES   = 100;
            var       fixAttempts = 0;

            do
            {
                fixAttempts++;
                diagnosticFixed = false;
                project         = solution.GetProject(projectId) !;
                var diagnostics = (await GetDiagnosticsFromProjectAsync(project, documentPath, diagnosticIds).ConfigureAwait(false))
                                  .OrderBy(d => d.Location.SourceSpan.Start);

                foreach (var diagnostic in diagnostics)
                {
                    var doc           = project.GetDocument(diagnostic.Location.SourceTree) !;
                    var fixedSolution = await TryFixDiagnosticAsync(diagnostic, doc).ConfigureAwait(false);

                    if (fixedSolution != null)
                    {
                        solution        = fixedSolution;
                        diagnosticFixed = true;
                        break;
                    }
                }

                if (fixAttempts + 1 == MAX_TRIES)
                {
                    Assert.True(false, $"The code fixers were unable to resolve the following diagnostic(s):{Environment.NewLine}   {string.Join(',', diagnostics.Select(d => d.Id))}");
                }
            }while (diagnosticFixed);

            project = solution.GetProject(projectId) !;

            var result = await project.Documents
                         .First(d => documentPath.Equals(Path.GetFileNameWithoutExtension(d.Name), StringComparison.Ordinal))
                         .GetTextAsync();

            return(result.ToString());
        }