Ejemplo n.º 1
0
 public void WeaveTest()
 {
     var sourceCode = @"bool TyGet(out int a, out int b)
     {
     a = 123;
     b = 567;
     return false;
     }";
     var docWeaver = new DocumentWeaver(CSharpSyntaxTree.ParseText(sourceCode));
     var newSyntaxTree = docWeaver.Weave();
     var newCode = newSyntaxTree.ToString();
     Assert.IsNotNull(newCode);
     Assert.IsTrue(newCode.Contains("try//$PGO$/0X200X150X080X31/$PGO$\r"));
     Assert.IsTrue(newCode.Contains("WeaverReport.Logger.BeginReport"));
     Assert.IsTrue(newCode.Contains("WeaverReport.Logger.EndReport"));
     Assert.IsTrue(newCode.Contains("WeaverReport.Logger.PushOutArgs"));
     Assert.IsFalse(newCode.Contains("WeaverReport.Logger.PushArgs"));
     Assert.IsTrue(newCode.Contains("WeaverReport.Logger.PushResult"));
 }
Ejemplo n.º 2
0
        private static WeaveEditorResult InternalWeaveOrUnWeaveFromEditor(Func<DocumentWeaver, SyntaxNode, SyntaxTree> weaveOrunwaveFun)
        {
            Document doc;
            IWpfTextView wpfView;
            var caretContent = GetCaretTrivia(out doc, out wpfView);
            if (caretContent == null) return WeaveEditorResult.ActiveDocumentGotFocusFailed;

            var syntaxTree = doc.GetSyntaxTreeAsync().Result;
            SyntaxNode mOrc = caretContent.GetMethodDeclarationFromTrivia();
            if (mOrc == null) mOrc = caretContent.GetClassDeclarationFromTrivia();
            if (mOrc == null) return WeaveEditorResult.NothingToWeave;
            var weaver = new DocumentWeaver(syntaxTree);
            var newSyntaxTree = weaveOrunwaveFun(weaver, mOrc);
            if (newSyntaxTree == null) return WeaveEditorResult.AllReadyWeaveOrUnweave;
            var root = newSyntaxTree.GetRoot();
            var newDoc = Formatter.FormatAsync(doc.WithSyntaxRoot(root)).Result;
            wpfView.TextBuffer.GetWorkspace().TryApplyChanges(newDoc.Project.Solution);

            return WeaveEditorResult.Ok;
        }
Ejemplo n.º 3
0
        public static void WeaveOrUnWeave(string rootText, Func<DocumentWeaver, SyntaxTree> weaveOrunwaveFun)
        {
            using (var session = StartWaitDialog("Weaving selected project(s)", "Start weaving", "Weaving selected project(s)"))
            {
                Report(session, "Opening solution with Roslyn");

                using (MSBuildWorkspace workspace = MSBuildWorkspace.Create())
                {
                    var originalSolution = workspace.OpenSolutionAsync(VSTools.SelectedSolution()).Result;
                    var newSolution = originalSolution;

                    foreach (var project in FilterProjects(originalSolution))
                    {
                        Report(session, rootText + " " + project.Name);
                        foreach (var documentid in project.DocumentIds)
                        {
                            var document = newSolution.GetDocument(documentid);
                            Report(session, rootText + " " + document.FilePath);
                            var weaver = new DocumentWeaver(document.GetSyntaxTreeAsync().Result);
                            var newRoot = weaveOrunwaveFun(weaver).GetRoot();
                            var newDoc = Formatter.FormatAsync(document.WithSyntaxRoot(newRoot)).Result;
                            newSolution = newDoc.Project.Solution;
                        }
                        if (workspace.TryApplyChanges(newSolution))
                        {
                            //
                            //MessageBox("Done successfully");
                            Trace.WriteLine("Changes are done for project " + project.Name);
                        }
                    }
                }
            }
        }