コード例 #1
0
        private Dictionary <ISymbol, string> GetSymbolsToRename()
        {
            ImmutableArray <ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
                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);
            }

            int position = Node.SpanStart;

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

            invocationSymbols = invocationSymbols.AddRange(InvocationSemanticModel.LookupSymbols(position));

            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);
        }
コード例 #2
0
        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 = NameGenerator.Default.EnsureUniqueName(symbol.Name, reservedNames);

                            symbolMap.Add(symbol, newName);

                            reservedNames.Add(newName);
                        }

                        return(symbolMap);
                    }
                }
            }

            return(null);
        }