Exemple #1
0
            public override void SetTypeCode()
            {
                var name = Name;

                var watches = new List <WatchVariable> ();

                var deps = new List <string> ();

                var commentlessCode  = rawCode;
                var instrumentedCode = commentlessCode;

                TypeCode.Set(name, usings, rawCode, instrumentedCode, deps, nsName, watches);
            }
            public override void SetTypeCode()
            {
                var name = Name;

                var commentlessCode = String.Join(Environment.NewLine, Declaration.GetText().Lines.Select(l => l.ToString()));

                var usings =
                    Root.DescendantNodes()
                    .OfType <UsingDirectiveSyntax> ()
                    .Select(u => u.GetText().ToString())
                    .ToList();

                var deps =
                    Root.DescendantNodes()
                    .OfType <IdentifierNameSyntax>()
                    .Select(n => Model.GetSymbolInfo(n))
                    .Where(s => s.Symbol != null && s.Symbol.Kind == SymbolKind.NamedType)
                    .Select(n => n.Symbol.Name)
                    .Distinct()
                    .ToList();

                var ns =
                    Declaration.Ancestors()
                    .OfType <NamespaceDeclarationSyntax>()
                    .Select(n => n.Name.GetText().ToString().Trim())
                    .FirstOrDefault() ?? "";

                // create an 'instrumented' instance of the document with watch calls
                var rewriter         = new WatchExpressionRewriter(Document.FullPath);
                var instrumented     = rewriter.Visit(Declaration);
                var instrumentedCode = instrumented.ToString();

                // the rewriter collects the WatchVariable definitions as it walks the tree
                var watches = rewriter.WatchVariables;

                TypeCode.Set(name, usings, commentlessCode, instrumentedCode, deps, ns, watches);
            }
Exemple #3
0
            static TypeCode Set(DocumentRef doc, TypeDeclaration rtypedecl, CSharpAstResolver resolver)
            {
                var rawCode = GetCommentlessCode(rtypedecl);

                var typedecl = (TypeDeclaration)rtypedecl.Clone();

                var ns     = rtypedecl.Parent as NamespaceDeclaration;
                var nsName = ns == null ? "" : ns.FullName;

                var name = rtypedecl.Name;

                var usings =
                    resolver.RootNode.Descendants.
                    OfType <UsingDeclaration> ().
                    Select(x => x.ToString().Trim()).
                    ToList();

                //
                // Find dependencies
                //
                var deps = new List <String> ();

                foreach (var d in rtypedecl.Descendants.OfType <SimpleType> ())
                {
                    deps.Add(d.Identifier);
                }

                //
                // Find watches and instrument
                //
                var watches = new List <WatchVariable> ();

                foreach (var d in typedecl.Descendants.OfType <VariableInitializer> ())
                {
                    var endLoc = d.EndLocation;
                    var p      = d.Parent;
                    if (p == null || p.Parent == null)
                    {
                        continue;
                    }
                    var nc = p.GetNextSibling(x => x is Comment && x.StartLocation.Line == endLoc.Line);
                    if (nc == null || !nc.ToString().StartsWith("//="))
                    {
                        continue;
                    }
                    var id         = Guid.NewGuid().ToString();
                    var instrument = GetWatchInstrument(id, new IdentifierExpression(d.Name));
                    p.Parent.InsertChildBefore(nc, instrument, BlockStatement.StatementRole);
                    watches.Add(new WatchVariable {
                        Id                 = id,
                        Expression         = d.Name,
                        ExplicitExpression = "",
                        FilePath           = doc.FullPath,
                        FileLine           = nc.StartLocation.Line,
                        FileColumn         = nc.StartLocation.Column,
                    });
                }
                foreach (var d in typedecl.Descendants.OfType <AssignmentExpression> ())
                {
                    var endLoc = d.EndLocation;
                    var p      = d.Parent;
                    if (p == null || p.Parent == null)
                    {
                        continue;
                    }
                    var nc = p.GetNextSibling(x => x is Comment && x.StartLocation.Line == endLoc.Line);
                    if (nc == null || !nc.ToString().StartsWith("//="))
                    {
                        continue;
                    }
                    var id         = Guid.NewGuid().ToString();
                    var instrument = GetWatchInstrument(id, (Expression)d.Left.Clone());
                    p.Parent.InsertChildBefore(nc, instrument, BlockStatement.StatementRole);
                    watches.Add(new WatchVariable {
                        Id                 = id,
                        Expression         = d.Left.ToString(),
                        ExplicitExpression = "",
                        FilePath           = doc.FullPath,
                        FileLine           = nc.StartLocation.Line,
                        FileColumn         = nc.StartLocation.Column,
                    });
                }
                foreach (var d in typedecl.Descendants.OfType <Comment> ().Where(x => x.CommentType == CommentType.SingleLine))
                {
                    var m = WatchVariable.CommentContentRe.Match(d.Content);
                    if (!m.Success || string.IsNullOrWhiteSpace(m.Groups [1].Value))
                    {
                        continue;
                    }

                    var p = d.Parent as BlockStatement;
                    if (p == null)
                    {
                        continue;
                    }

                    var exprText   = m.Groups [1].Value.Trim();
                    var parser     = new CSharpParser();
                    var syntaxTree = parser.Parse("class C { void F() { var __r = " + exprText + "; } }");

                    if (syntaxTree.Errors.Count > 0)
                    {
                        continue;
                    }
                    var t    = syntaxTree.Members.OfType <TypeDeclaration> ().First();
                    var expr = t.Descendants.OfType <VariableInitializer> ().First().Initializer;

                    var id         = Guid.NewGuid().ToString();
                    var instrument = GetWatchInstrument(id, expr.Clone());
                    p.InsertChildBefore(d, instrument, BlockStatement.StatementRole);
                    watches.Add(new WatchVariable {
                        Id                 = id,
                        Expression         = exprText,
                        ExplicitExpression = m.Groups[1].Value,
                        FilePath           = doc.FullPath,
                        FileLine           = d.StartLocation.Line,
                        FileColumn         = d.StartLocation.Column,
                    });
                }

                //
                // All done
                //
                var instrumentedCode = typedecl.ToString();

                return(TypeCode.Set(name, usings, rawCode, instrumentedCode, deps, nsName, watches));
            }