示例#1
0
        private void Document_Saved(object sender, EventArgs e)
        {
            if (_skipEvent)
            {
                _skipEvent = false;
                return;
            }
            MonoDevelop.Ide.Gui.Document doc = IdeApp.Workbench.ActiveDocument;
            if (doc == null || doc.Editor == null)
            {
                return;
            }
            var success = IdeApp.CommandService.DispatchCommand(RemoveAndSortCommandId, MonoDevelop.Components.Commands.CommandSource.Keybinding);

            if (!success)
            {
                Console.WriteLine("SortAndRemoveOnSave: Cannot find or dispatch command {0}", RemoveAndSortCommandId);
            }
            else
            {
                _skipEvent = true;
                doc.Save();
            }
        }
        public async Task GenerateUnitTest(string unitTestName, MethodDeclarationSyntax currentMethod, MonoDevelop.Ide.Gui.Document document, GeneratedTest generatedTestModel)
        {
            var returnType = generatedTestModel.IsTask ? "Task" : "void";
            var modifiers  = SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            if (generatedTestModel.IsTask)
            {
                modifiers = SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword));
            }
            var config = await _configurationService.GetConfiguration();

            var annotation = "Test";

            if ("xunit".Equals(config.TestFramework))
            {
                annotation = "Fact";
            }
            var newMethod = GenerateUnitTestMethodDeclaration(returnType, modifiers, unitTestName, annotation, generatedTestModel);

            var analysisDoc = document.GetAnalysisDocument();
            var editor      = await DocumentEditor.CreateAsync(analysisDoc);

            var cuRoot = editor.SemanticModel.SyntaxTree.GetCompilationUnitRoot();

            if (cuRoot == null)
            {
                return;
            }

            //add required using statements that havent already been added
            if (generatedTestModel.RequiredNamespaces != null && generatedTestModel.RequiredNamespaces.Any())
            {
                var usingNames     = cuRoot.Usings.Select(u => u.Name.ToString());
                var requiredUsings = new List <UsingDirectiveSyntax>();
                foreach (var usingStatement in generatedTestModel.RequiredNamespaces)
                {
                    if (!usingNames.Contains(usingStatement))
                    {
                        requiredUsings.Add(GenerateUsingSyntax(usingStatement));
                    }
                }
                if (requiredUsings.Any())
                {
                    var updatedRoot = cuRoot.AddUsings(requiredUsings.ToArray());
                    editor.ReplaceNode(cuRoot, updatedRoot);
                    cuRoot = updatedRoot;
                }
            }

            var lastMethod = cuRoot.DescendantNodes().OfType <MethodDeclarationSyntax>().LastOrDefault();

            if (lastMethod != null)
            {
                editor.InsertAfter(lastMethod, newMethod);
            }
            else
            {
                var classDeclaration    = cuRoot.DescendantNodes().OfType <ClassDeclarationSyntax>().First();
                var newClassDeclaration = classDeclaration.AddMembers(newMethod);
                editor.ReplaceNode(classDeclaration, newClassDeclaration);
            }



            var newDocument = editor.GetChangedDocument();

            var newRoot = await newDocument.GetSyntaxRootAsync();

            var textBuffer = document.GetContent <ITextBuffer>();

            Microsoft.CodeAnalysis.Workspace.TryGetWorkspace(textBuffer.AsTextContainer(), out var workspace);
            newRoot = Formatter.Format(newRoot, Formatter.Annotation, workspace);
            workspace.TryApplyChanges(newDocument.WithSyntaxRoot(newRoot).Project.Solution);
            await document.Save();
        }