/// <exception cref="ArgumentException">Unknown scope</exception>
            public override async Task <CodeAction> GetFixAsync([NotNull] FixAllContext fixAllContext)
            {
                var          diagnosticsToFix = new List <KeyValuePair <Project, ImmutableArray <Diagnostic> > >();
                const string TitleFormat      = "Convert all messages in {0} {1} to diagnostic constants";
                string       fixAllTitle;

                switch (fixAllContext.Scope)
                {
                case FixAllScope.Document:
                {
                    var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document).ConfigureAwait(false);

                    diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics));
                    fixAllTitle = string.Format(TitleFormat, "document", fixAllContext.Document.Name);
                    break;
                }

                case FixAllScope.Project:
                {
                    var project     = fixAllContext.Project;
                    var diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false);

                    diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics));
                    fixAllTitle = string.Format(TitleFormat, "project", fixAllContext.Project.Name);
                    break;
                }

                case FixAllScope.Solution:
                {
                    foreach (var project in fixAllContext.Solution.Projects)
                    {
                        var diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false);

                        diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(project, diagnostics));
                    }

                    fixAllTitle = "Add all items in the solution to the public API";
                    break;
                }

                case FixAllScope.Custom:
                    return(null);

                default:
                    throw new ArgumentException("Unknown scope", nameof(fixAllContext));
                }

                var severity = Severities
                               .FirstOrDefault(sev => string.Format(Title, sev) == fixAllContext.CodeActionEquivalenceKey)
                               ?? DefaultSeverity;

                foreach (var sev in Severities)
                {
                    if (string.Format(Title, sev) == fixAllContext.CodeActionEquivalenceKey)
                    {
                        severity = sev;
                        break;
                    }
                }

                return(CodeAction.Create(
                           title: fixAllTitle,
                           createChangedSolution: async c =>
                {
                    var diagsByDocument = from ds in diagnosticsToFix
                                          from d in ds.Value
                                          where d.Location.IsInSource
                                          let document = fixAllContext.Solution.GetDocument(d.Location.SourceTree)
                                                         group d by document;

                    async Task <KeyValuePair <DocumentId, Invocation[]> > GetInvocations(Document doc, IEnumerable <Diagnostic> diags)
                    {
                        var root = await doc.GetSyntaxRootAsync(c).ConfigureAwait(false);
                        var semanticModel = await doc.GetSemanticModelAsync(c).ConfigureAwait(false);
                        var creationExprs = from d in diags
                                            let span = d.Location.SourceSpan
                                                       let ancestors = root.FindToken(span.Start).Parent.AncestorsAndSelf()
                                                                       select ancestors.OfType <ObjectCreationExpressionSyntax>().First();
                        var literalCreations = MatchLiteralCreations(creationExprs, semanticModel);
                        var invocations = PlanInvocations(literalCreations.ToArray(), severity);
                        return new KeyValuePair <DocumentId, Invocation[]>(doc.Id, invocations);
                    }

                    var results = await Task.WhenAll(from grouping in diagsByDocument
                                                     select GetInvocations(grouping.Key, grouping));

                    return await ApplyInvocationsAsync(fixAllContext.Solution, results, c);
                },
                           equivalenceKey: fixAllTitle));
            }
Beispiel #2
0
        private decimal GetIssueSeverityId(string title)
        {
            var project = Severities.FirstOrDefault(x => x.SeverityTitle.Equals(title));

            return(project.Id);
        }