示例#1
0
            public override int GetHashCode(SpellingFixResult obj)
            {
                if (obj == null)
                {
                    throw new ArgumentNullException(nameof(obj));
                }

                return(Hash.Combine(
                           StringComparer.CurrentCulture.GetHashCode(obj.Value),
                           StringComparer.CurrentCulture.GetHashCode(obj.LineSpan)));
            }
示例#2
0
            static void AddResult(
                List<SpellingFixResult> results,
                SpellingDiagnostic diagnostic,
                SpellingFix fix,
                SourceText sourceText,
                ref string sourceTextText)
            {
                if (sourceTextText == null)
                    sourceTextText = (ShouldWrite(Verbosity.Detailed)) ? sourceText.ToString() : "";

                SpellingFixResult result = SpellingFixResult.Create(
                    (sourceTextText.Length == 0) ? null : sourceTextText,
                    diagnostic,
                    fix);

                results.Add(result);
            }
示例#3
0
            public override bool Equals(SpellingFixResult x, SpellingFixResult y)
            {
                if (object.ReferenceEquals(x, y))
                {
                    return(true);
                }

                if (x == null)
                {
                    return(false);
                }

                if (y == null)
                {
                    return(false);
                }

                return(StringComparer.CurrentCulture.Equals(x.Value, y.Value) &&
                       StringComparer.CurrentCulture.Equals(x.LineSpan, y.LineSpan));
            }
示例#4
0
        private async Task <List <SpellingFixResult> > FixCommentsAsync(
            Project project,
            List <SpellingDiagnostic> diagnostics,
            CancellationToken cancellationToken)
        {
            var results = new List <SpellingFixResult>();

            List <SpellingDiagnostic> commentDiagnostics = diagnostics.Where(f => !f.IsSymbol).ToList();

            var applyChanges = false;

            project = CurrentSolution.GetProject(project.Id);

            foreach (IGrouping <SyntaxTree, SpellingDiagnostic> grouping in commentDiagnostics
                     .GroupBy(f => f.SyntaxTree))
            {
                cancellationToken.ThrowIfCancellationRequested();

                Document document = project.GetDocument(grouping.Key);

                SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

                string sourceTextText = (ShouldWrite(Verbosity.Detailed)) ? sourceText.ToString() : null;

                List <TextChange> textChanges = null;

                foreach (SpellingDiagnostic diagnostic in grouping.OrderBy(f => f.Span.Start))
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (SpellingData.IgnoredValues.Contains(diagnostic.Value))
                    {
                        continue;
                    }

                    LogHelpers.WriteSpellingDiagnostic(diagnostic, Options, sourceText, Path.GetDirectoryName(project.FilePath), "    ", Verbosity.Normal);

                    SpellingFix fix = GetFix(diagnostic);

                    if (!fix.IsDefault)
                    {
                        if (!string.Equals(diagnostic.Value, fix.Value, StringComparison.Ordinal))
                        {
                            WriteFix(diagnostic, fix, ConsoleColors.Green);

                            if (!Options.DryRun)
                            {
                                (textChanges ??= new List <TextChange>()).Add(new TextChange(diagnostic.Span, fix.Value));
                            }

                            results.Add(SpellingFixResult.Create(sourceTextText, diagnostic, fix));

                            ProcessFix(diagnostic, fix);
                        }
                        else
                        {
                            AddIgnoredValue(diagnostic);
                            results.Add(SpellingFixResult.Create(sourceTextText, diagnostic));
                        }
                    }
                    else
                    {
                        results.Add(SpellingFixResult.Create(sourceTextText, diagnostic));
                    }
                }

                if (textChanges != null)
                {
                    document = await document.WithTextChangesAsync(textChanges, cancellationToken).ConfigureAwait(false);

                    project = document.Project;

                    applyChanges = true;
                }
            }

            if (applyChanges &&
                !Workspace.TryApplyChanges(project.Solution))
            {
                Debug.Fail($"Cannot apply changes to solution '{project.Solution.FilePath}'");
                WriteLine($"    Cannot apply changes to solution '{project.Solution.FilePath}'", ConsoleColors.Yellow, Verbosity.Diagnostic);
            }

            return(results);
        }