// adds an analysis method to the enclosing class private async Task<Document> MissingAnalysisMethodAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken c) { SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document); string analysisMethodName = CodeFixHelper.AnalysisMethodName(declaration); SemanticModel semanticModel = await document.GetSemanticModelAsync(); var newAnalysisMethod = CodeFixHelper.CreateAnalysisMethod(generator, analysisMethodName, semanticModel) as MethodDeclarationSyntax; ClassDeclarationSyntax classDeclaration = declaration.Ancestors().OfType<ClassDeclarationSyntax>().First(); return await ReplaceNode(classDeclaration, classDeclaration.AddMembers(newAnalysisMethod), document); }
/// <summary> /// returns fullname of a methoddeclaration /// </summary> /// <param name="m"></param> /// <returns></returns> private string MethodFullName(MethodDeclarationSyntax m) { //may be a simplest way exists, but it works string result = m.Identifier.Text; var ancestors = m.Ancestors(); SyntaxNode classOrStruct; var classe = ancestors.OfType<ClassDeclarationSyntax>().FirstOrDefault(); if (classe != null) { result = classe.Identifier.Text + "." + result; classOrStruct = classe; } else { var structe = ancestors.OfType<StructDeclarationSyntax>().FirstOrDefault(); if (structe == null) { //it's just a peace of code with out class or struct return result; } result = structe.Identifier.Text + "." + result; classOrStruct = structe; } var ns = classOrStruct.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault(); var nsString = ns?.Name.ToString(); result = nsString + "." + result; return result; }
/// <summary> /// Creates a sync method variant for the given async method. /// </summary> /// <param name="document">The code analysis document.</param> /// <param name="methodDecl">The async method.</param> /// <param name="semanticModel">A semantic model to look for the type in.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>The modified document.</returns> private async Task<Document> CreateMethodSyncVariant(Document document, MethodDeclarationSyntax methodDecl, SemanticModel semanticModel, CancellationToken cancellationToken) { // Setup MethodDeclarationSyntax syncMethodDecl; // Are we dealing with interface or class? var typeDecl = methodDecl.Ancestors().OfType<TypeDeclarationSyntax>().FirstOrDefault(); if (typeDecl is ClassDeclarationSyntax) { // Create the async method signature syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariant(document, methodDecl, semanticModel, cancellationToken); } else { // Create the async method signature syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariantSignature(document, methodDecl, semanticModel, cancellationToken); } // Update the solution var root = await document.GetSyntaxRootAsync(cancellationToken); // Insert document = document.WithSyntaxRoot(root.InsertNodesBefore(methodDecl, new[] { syncMethodDecl })); // Format document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false); document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false); return document; }