Example #1
0
        public static ILocalSymbol FirstValidSymbolOccuranceOfVariable(SemanticModel model, CSharpSyntaxNode context, string identifier)
        {
            var outerForLoop     = context.FirstAncestorOrSelf <ForStatementSyntax>();
            var outerCatchClause = context.FirstAncestorOrSelf <CatchClauseSyntax>();
            var foreachLoop      = context.FirstAncestorOrSelf <ForEachStatementSyntax>();
            var usings           = context.FirstAncestorOrSelf <UsingStatementSyntax>();

            if (outerForLoop != null && outerForLoop.Declaration.Variables.Any(x => x.Identifier.ToString() == identifier))
            {
                context = outerForLoop;
            }
            else if (outerCatchClause != null && outerCatchClause.Declaration.Identifier.ToString() == identifier)
            {
                context = outerCatchClause;
            }
            else if (foreachLoop != null && foreachLoop.Identifier.ToString() == identifier)
            {
                context = foreachLoop;
            }
            else if (usings.Declaration.Variables.Any(x => x.Identifier.ToString() == identifier))
            {
                context = usings;
            }
            else
            {
                context = context.FirstAncestorOrSelf <BlockSyntax>();
            }

            var walker = new VariableUsageFinder(model, context, identifier);

            context.Accept(walker);
            return((ILocalSymbol)walker.symbols.FirstOrDefault());
        }
Example #2
0
        public static ILocalSymbol FirstValidSymbolOccuranceOfVariable(SemanticModel model, CSharpSyntaxNode context, string identifier)
        {
            var outerForLoop  = context.FirstAncestorOrSelf<ForStatementSyntax>();
            var outerCatchClause = context.FirstAncestorOrSelf<CatchClauseSyntax>();
            var foreachLoop = context.FirstAncestorOrSelf<ForEachStatementSyntax>();
            var usings = context.FirstAncestorOrSelf<UsingStatementSyntax>();

            if (outerForLoop != null && outerForLoop.Declaration.Variables.Any(x => x.Identifier.ToString() == identifier))
                context = outerForLoop;
            else if (outerCatchClause != null && outerCatchClause.Declaration.Identifier.ToString() == identifier)
                context = outerCatchClause;
            else if (foreachLoop != null && foreachLoop.Identifier.ToString() == identifier)
                context = foreachLoop;
            else if (usings.Declaration.Variables.Any(x => x.Identifier.ToString() == identifier))
                context = usings;
            else
                context = context.FirstAncestorOrSelf<BlockSyntax>();

            var walker = new VariableUsageFinder(model, context, identifier);
            context.Accept(walker);
            return (ILocalSymbol)walker.symbols.FirstOrDefault();
        }