Пример #1
0
        public SpellingFix ChooseFix(
            SpellingDiagnostic diagnostic,
            List <SpellingFix> fixes,
            bool interactive)
        {
            fixes = fixes
                    .Distinct(SpellingFixComparer.InvariantCulture)
                    .Where(f =>
            {
                return(f.Kind == SpellingFixKind.Predefined ||
                       diagnostic.IsApplicableFix(f.Value));
            })
                    .Select(fix =>
            {
                if (TextUtility.GetTextCasing(fix.Value) != TextCasing.Undefined)
                {
                    return(fix.WithValue(TextUtility.SetTextCasing(fix.Value, diagnostic.Casing)));
                }

                return(fix);
            })
                    .OrderBy(f => f.Kind)
                    .Take(9)
                    .ToList();

            if (fixes.Count > 0)
            {
                for (int i = 0; i < fixes.Count; i++)
                {
                    WriteSuggestion(diagnostic, fixes[i], i, interactive);
                }

                if (TryReadSuggestion(out int index) &&
                    index < fixes.Count)
                {
                    return(fixes[index]);
                }
            }

            return(default);
Пример #2
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);