Пример #1
0
 private SpellingFixResult(
     string sourceText,
     SpellingCapture capture,
     TextSpan span,
     SpellingFix fix,
     FileLinePositionSpan lineSpan)
 {
     SourceText = sourceText;
     Capture    = capture;
     Span       = span;
     Fix        = fix;
     LineSpan   = 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 FixList Add(string key, SpellingFix fix)
        {
            if (Items.TryGetValue(key, out ImmutableHashSet <SpellingFix> fixes))
            {
                if (fixes.Contains(fix))
                {
                    return(this);
                }

                fixes = fixes.Add(fix);

                ImmutableDictionary <string, ImmutableHashSet <SpellingFix> > values = Items.SetItem(key, fixes);

                return(new FixList(values));
            }
            else
            {
                fixes = ImmutableHashSet.Create(SpellingFixComparer.InvariantCultureIgnoreCase, fix);

                ImmutableDictionary <string, ImmutableHashSet <SpellingFix> > dic = Items.Add(key, fixes);

                return(new FixList(dic));
            }
        }
Пример #4
0
        private async Task <(List <SpellingFixResult>, bool allIgnored)> FixSymbolsAsync(
            Project project,
            List <SpellingDiagnostic> spellingDiagnostics,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            var results    = new List <SpellingFixResult>();
            var allIgnored = true;

            List <(SyntaxToken identifier, List <SpellingDiagnostic> diagnostics, DocumentId documentId)> symbolDiagnostics = spellingDiagnostics
                                                                                                                              .Where(f => f.IsSymbol)
                                                                                                                              .GroupBy(f => f.Identifier)
                                                                                                                              .Select(f => (
                                                                                                                                          identifier: f.Key,
                                                                                                                                          diagnostics: f.OrderBy(f => f.Span.Start).ToList(),
                                                                                                                                          documentId: project.GetDocument(f.Key.SyntaxTree).Id))
                                                                                                                              .OrderBy(f => f.documentId.Id)
                                                                                                                              .ThenByDescending(f => f.identifier.SpanStart)
                                                                                                                              .ToList();

            for (int i = 0; i < symbolDiagnostics.Count; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                (SyntaxToken identifier, List <SpellingDiagnostic> diagnostics, DocumentId documentId) = symbolDiagnostics[i];

                SyntaxNode node = null;

                foreach (SpellingDiagnostic diagnostic in diagnostics)
                {
                    if (diagnostic != null)
                    {
                        node = syntaxFacts.GetSymbolDeclaration(diagnostic.Identifier);
                        break;
                    }
                }

                if (node == null)
                {
                    continue;
                }

                Document document = project.GetDocument(documentId);

                if (document == null)
                {
                    Debug.Fail(identifier.GetLocation().ToString());

                    WriteLine($"    Cannot find document for '{identifier.ValueText}'", ConsoleColors.Yellow, Verbosity.Detailed);
                    continue;
                }

                SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                if (identifier.SyntaxTree != root.SyntaxTree)
                {
                    SyntaxToken identifier2 = root.FindToken(identifier.SpanStart, findInsideTrivia: false);

                    if (identifier.Span != identifier2.Span ||
                        identifier.RawKind != identifier2.RawKind ||
                        !string.Equals(identifier.ValueText, identifier2.ValueText, StringComparison.Ordinal))
                    {
                        continue;
                    }

                    SyntaxNode node2 = identifier2.Parent;

                    SyntaxNode n = identifier.Parent;
                    while (n != node)
                    {
                        node2 = node2.Parent;
                        n     = n.Parent;
                    }

                    identifier = identifier2;
                    node       = node2;
                }

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

                string sourceTextText = null;

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                ISymbol symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken)
                                 ?? semanticModel.GetSymbol(node, cancellationToken);

                if (symbol == null)
                {
                    // 8925 - C# tuple element
                    Debug.Assert(identifier.Parent.RawKind == 8925, identifier.ToString());

                    if (ShouldWrite(Verbosity.Detailed))
                    {
                        string message = $"    Cannot find symbol for '{identifier.ValueText}'";

                        string locationText = GetLocationText(identifier.GetLocation(), project);

                        if (locationText != null)
                        {
                            message += $" at {locationText}";
                        }

                        WriteLine(message, ConsoleColors.Yellow, Verbosity.Detailed);
                    }

                    continue;
                }

                if (!symbol.IsKind(SymbolKind.Namespace, SymbolKind.Alias) &&
                    !symbol.IsVisible(Options.SymbolVisibility))
                {
                    continue;
                }

                allIgnored = false;

                var    fixes       = new List <(SpellingDiagnostic diagnostic, SpellingFix fix)>();
                string newName     = identifier.ValueText;
                int    indexOffset = 0;

                for (int j = 0; j < diagnostics.Count; j++)
                {
                    SpellingDiagnostic diagnostic = diagnostics[j];

                    if (diagnostic == null)
                    {
                        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);

                            fixes.Add((diagnostic, fix));

                            newName = TextUtility.ReplaceRange(newName, fix.Value, diagnostic.Offset + indexOffset, diagnostic.Length);

                            indexOffset += fix.Value.Length - diagnostic.Length;
                        }
                        else
                        {
                            for (int k = 0; k < symbolDiagnostics.Count; k++)
                            {
                                List <SpellingDiagnostic> diagnostics2 = symbolDiagnostics[k].diagnostics;

                                for (int l = 0; l < diagnostics2.Count; l++)
                                {
                                    if (SpellingData.IgnoredValues.KeyComparer.Equals(diagnostics2[l]?.Value, diagnostic.Value))
                                    {
                                        diagnostics2[l] = null;
                                    }
                                }
                            }

                            AddIgnoredValue(diagnostic);
                            AddResult(results, diagnostic, default(SpellingFix), sourceText, ref sourceTextText);
                        }
                    }
                    else
                    {
                        AddResult(results, diagnostic, default(SpellingFix), sourceText, ref sourceTextText);
                    }
                }

                if (string.Equals(identifier.Text, newName, StringComparison.Ordinal))
                {
                    continue;
                }

                Solution newSolution = null;
                if (!Options.DryRun)
                {
                    WriteLine($"    Rename '{identifier.ValueText}' to '{newName}'", ConsoleColors.Green, Verbosity.Minimal);

                    try
                    {
                        //TODO: detect naming conflict
                        newSolution = await Microsoft.CodeAnalysis.Rename.Renamer.RenameSymbolAsync(
                            CurrentSolution,
                            symbol,
                            newName,
                            default(Microsoft.CodeAnalysis.Options.OptionSet),
                            cancellationToken)
                                      .ConfigureAwait(false);
                    }
                    catch (InvalidOperationException
#if DEBUG
                           ex
#endif
                           )
                    {
                        WriteLine($"    Cannot rename '{symbol.Name}'", ConsoleColors.Yellow, Verbosity.Normal);
#if DEBUG
                        WriteLine(document.FilePath);
                        WriteLine(identifier.ValueText);
                        WriteLine(ex.ToString());
#endif
                        continue;
                    }
                }

                if (newSolution != null)
                {
                    if (Workspace.TryApplyChanges(newSolution))
                    {
                        project = CurrentSolution.GetProject(project.Id);
                    }
                    else
                    {
                        Debug.Fail($"Cannot apply changes to solution '{newSolution.FilePath}'");
                        WriteLine($"    Cannot apply changes to solution '{newSolution.FilePath}'", ConsoleColors.Yellow, Verbosity.Normal);
                        continue;
                    }
                }

                foreach ((SpellingDiagnostic diagnostic, SpellingFix fix) in fixes)
                {
                    AddResult(results, diagnostic, fix, sourceText, ref sourceTextText);

                    ProcessFix(diagnostic, fix);
                }
            }

            return(results, allIgnored);
Пример #5
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);
        }
Пример #6
0
 public static SpellingFixResult Create(string sourceText, SpellingDiagnostic diagnostic, SpellingFix fix)
 {
     return(new SpellingFixResult(
                sourceText,
                new SpellingCapture(diagnostic.Value, diagnostic.Index, diagnostic.Parent, diagnostic.ParentIndex),
                diagnostic.Span,
                fix,
                diagnostic.Location.GetMappedLineSpan()));
 }
Пример #7
0
 public override int Compare(SpellingFix x, SpellingFix y)
 {
     return(StringComparer.CurrentCulture.Compare(x.Value, y.Value));
 }
Пример #8
0
 public override int GetHashCode(SpellingFix obj)
 {
     return(WordList.DefaultComparer.GetHashCode(obj.Value));
 }
Пример #9
0
 public override bool Equals(SpellingFix x, SpellingFix y)
 {
     return(WordList.DefaultComparer.Equals(x.Value, y.Value));
 }
Пример #10
0
 public override int Compare(SpellingFix x, SpellingFix y)
 {
     return(WordList.DefaultComparer.Compare(x.Value, y.Value));
 }
Пример #11
0
 public override int GetHashCode(SpellingFix obj)
 {
     return(StringComparer.CurrentCulture.GetHashCode(obj.Value));
 }
Пример #12
0
 public override bool Equals(SpellingFix x, SpellingFix y)
 {
     return(StringComparer.CurrentCulture.Equals(x.Value, y.Value));
 }