Beispiel #1
0
        private Dictionary <ISymbol, string> GetSymbolsToRename()
        {
            ImmutableArray <ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
                MethodDeclaration.BodyOrExpressionBody(),
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            if (!declarationSymbols.Any())
            {
                return(null);
            }

            declarationSymbols = declarationSymbols.RemoveAll(f => f.IsKind(SymbolKind.NamedType, SymbolKind.Field));

            if (!declarationSymbols.Any())
            {
                return(null);
            }

            ImmutableArray <ISymbol> invocationSymbols = InvocationSemanticModel.GetSymbolsDeclaredInEnclosingSymbol(
                InvocationExpression.SpanStart,
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            invocationSymbols = invocationSymbols.AddRange(InvocationSemanticModel.LookupSymbols(InvocationExpression.SpanStart));

            var reservedNames = new HashSet <string>(invocationSymbols.Select(f => f.Name));

            List <ISymbol> symbols = null;

            foreach (ISymbol symbol in declarationSymbols)
            {
                if (reservedNames.Contains(symbol.Name))
                {
                    (symbols ?? (symbols = new List <ISymbol>())).Add(symbol);
                }
            }

            if (symbols == null)
            {
                return(null);
            }

            reservedNames.UnionWith(declarationSymbols.Select(f => f.Name));

            var symbolMap = new Dictionary <ISymbol, string>();

            foreach (ISymbol symbol in symbols)
            {
                string newName = NameGenerator.Default.EnsureUniqueName(symbol.Name, reservedNames);

                symbolMap.Add(symbol, newName);

                reservedNames.Add(newName);
            }

            return(symbolMap);
        }
        private Dictionary <ISymbol, string> GetSymbolsToRename()
        {
            ImmutableArray <ISymbol> invocationSymbols = InvocationSemanticModel.GetSymbolsDeclaredInEnclosingSymbol(
                InvocationExpression.SpanStart,
                excludeAnonymousTypeProperty: true,
                cancellationToken: CancellationToken);

            if (invocationSymbols.Any())
            {
                ImmutableArray <ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
                    MethodDeclaration.BodyOrExpressionBody(),
                    excludeAnonymousTypeProperty: true,
                    cancellationToken: CancellationToken);

                if (declarationSymbols.Any())
                {
                    var reservedNames = new HashSet <string>(invocationSymbols.Select(f => f.Name));

                    List <ISymbol> symbols = null;

                    foreach (ISymbol symbol in declarationSymbols)
                    {
                        if (reservedNames.Contains(symbol.Name))
                        {
                            (symbols ?? (symbols = new List <ISymbol>())).Add(symbol);
                        }
                    }

                    if (symbols != null)
                    {
                        reservedNames.UnionWith(declarationSymbols.Select(f => f.Name));

                        var symbolMap = new Dictionary <ISymbol, string>();

                        foreach (ISymbol symbol in symbols)
                        {
                            string newName = Identifier.EnsureUniqueName(symbol.Name, reservedNames);

                            symbolMap.Add(symbol, newName);

                            reservedNames.Add(newName);
                        }

                        return(symbolMap);
                    }
                }
            }

            return(null);
        }