using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Rename;

namespace RoslynFieldRenamer
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string solutionPath = args[0];
            var replacements = Zalipa.Replacements;

            using var workspace = MSBuildWorkspace.Create();
            var project = await workspace.OpenProjectAsync(solutionPath);
            Console.WriteLine(project);
            Console.WriteLine(project.Name);
            var compilation = await project.GetCompilationAsync();

            foreach (var namedTypeSymbol in compilation.GlobalNamespace.GetMembers())
            {
                Console.WriteLine(namedTypeSymbol.Name);
            }

            var targetFields = compilation.GlobalNamespace
                .GetNamespaceTypes()
                .SelectMany(t => t.GetMembers())
                .OfType<IMethodSymbol>();
            var solution = project.Solution;
            foreach (var methodSymbol in targetFields)
            {
                if (replacements.TryGetValue(methodSymbol.Name, out var replacement))
                {
                    solution =
                        await Renamer.RenameSymbolAsync(solution, methodSymbol, replacement, workspace.Options);
                }
            }

            workspace.TryApplyChanges(solution);
            Console.WriteLine("Field renamed successfully.");
        }
    }

    static class Extensions
    {
        public static IEnumerable<INamedTypeSymbol> GetNamespaceTypes(this INamespaceSymbol namespaceSymbol)
        {
            foreach (var member in namespaceSymbol.GetMembers())
            {
                if (member is INamespaceSymbol ns)
                {
                    foreach (var type in GetNamespaceTypes(ns))
                    {
                        yield return type;
                    }
                }
                else if (member is INamedTypeSymbol namedTypeSymbol)
                {
                    yield return namedTypeSymbol;
                }
            }
        }
    }
}

Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.GetRequiredDocument(Solution solution, SyntaxTree syntaxTree)
   at Microsoft.CodeAnalysis.Rename.SymbolicRenameLocations.ReferenceProcessing.<GetRenamableDefinitionLocationsAsync>g__AddRenameLocationIfNotGenerated|3_1(Location location, Boolean isRenamableAccessor, <>c__DisplayClass3_0&)
   at Microsoft.CodeAnalysis.Rename.SymbolicRenameLocations.ReferenceProcessing.GetRenamableDefinitionLocationsAsync(ISymbol referencedSymbol, ISymbol originalSymbol, Solution solution, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Rename.SymbolicRenameLocations.AddLocationsReferenceSymbolsAsync(ISymbol symbol, Solution solution, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Rename.SymbolicRenameLocations.FindLocationsInCurrentProcessAsync(ISymbol symbol, Solution solution, SymbolRenameOptions options, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Rename.Renamer.RenameSymbolInCurrentProcessAsync(Solution solution, ISymbol symbol, String newName, SymbolRenameOptions options, CodeCleanupOptionsProvider cleanupOptions, ImmutableArray`1 nonConflictSymbolKeys, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Rename.Renamer.RenameSymbolAsync(Solution solution, ISymbol symbol, String newName, SymbolRenameOptions options, CodeCleanupOptionsProvider fallbackOptions, ImmutableArray`1 nonConflictSymbolKeys, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Rename.Renamer.RenameSymbolAsync(Solution solution, ISymbol symbol, SymbolRenameOptions options, String newName, CancellationToken cancellationToken)
   at RoslynFieldRenamer.Program.Main(String[] args) in /home/deck/Code/CsharpProjects/TestProject/TestProject/Program.cs:line 39
   at RoslynFieldRenamer.Program.<Main>(String[] args)
Aborted (core dumped)