/// <summary> /// Takes a synchronous ServiceContract interface and returns an asynchronous version /// </summary> /// <param name="originalInterface"></param> /// <returns></returns> private static InterfaceDeclarationSyntax ComputeNewServiceContractInterfaceNode(InterfaceDeclarationSyntax originalInterface) { var newMembers = new List <MemberDeclarationSyntax>( originalInterface.Members.OfType <MethodDeclarationSyntax>() .Where(m => m.GetAttribute <OperationContractAttribute>() != null) .SelectMany(ComputeNewOperationContractNodes)); return(originalInterface .WithIdentifier(Syntax.Identifier(originalInterface.Identifier.ValueText + "Async")) .WithMembers(Syntax.List <MemberDeclarationSyntax>(newMembers))); }
/// <summary> /// Takes a synchronous ServiceContract interface and returns an asynchronous version /// </summary> /// <param name="originalInterface"></param> /// <returns></returns> private static InterfaceDeclarationSyntax ComputeNewServiceContractInterfaceNode(InterfaceDeclarationSyntax originalInterface) { var newMembers = new List<MemberDeclarationSyntax>( originalInterface.Members.OfType<MethodDeclarationSyntax>() .Where(m => m.GetAttribute<OperationContractAttribute>() != null) .SelectMany(ComputeNewOperationContractNodes)); return originalInterface .WithIdentifier(Syntax.Identifier(originalInterface.Identifier.ValueText + "Async")) .WithMembers(Syntax.List<MemberDeclarationSyntax>(newMembers)); }
Document PerformAction(Document document, SemanticModel model, SyntaxNode root, InterfaceDeclarationSyntax interfaceDeclaration, string interfaceName) { // Applying a refactoring means replacing parts of syntax tree with our new elements. // Note: Every call to any .With...() method creates a completely new syntax tree object, // which is not part of current document anymore! Finally we create a completely new document // based on a new syntax tree containing the result of our refactoring. string newInterfaceName = "I" + interfaceName; var newRoot = root.ReplaceNode( interfaceDeclaration, interfaceDeclaration.WithIdentifier(SyntaxFactory.Identifier(newInterfaceName).WithAdditionalAnnotations(Formatter.Annotation))); return(document.WithSyntaxRoot(newRoot)); }
public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node) { var modifiers = GetModifiers(Command.Modifiers, Command.Partial); var baseTypes = GetBaseTypes(Command.ImplementedInterfaces); var interfaceNode = node.WithIdentifier(SyntaxFactory.Identifier(Command.Name)) .WithTypeParameterList(Command.GenericParameters) .WithConstraintClauses(Command.GenericParametersConstraints) .WithAttributeLists(Command.Attributes) .WithModifiers(modifiers) .WithBaseList(baseTypes) .WithAdditionalAnnotations(new SyntaxAnnotation($"{Id}")); DocumentEditor.ReplaceNode(node, interfaceNode); }
public static InterfaceDeclarationSyntax BuildAsyncInterfaceDeclFromSyncInterfaceDecl(string serviceContractName, string serviceContractAsyncName, InterfaceDeclarationSyntax existingInterfaceDecl, bool useExistingAsBase) { var newInterfaceIdentifier = SyntaxFactory.Identifier(serviceContractAsyncName).WithoutTrivia(); var newInterfaceDecl = existingInterfaceDecl.WithIdentifier(newInterfaceIdentifier) .WithMembers(new SyntaxList <MemberDeclarationSyntax>()); //Why not convert the members too? if (useExistingAsBase) { newInterfaceDecl = newInterfaceDecl.WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.Token(SyntaxKind.ColonToken), SyntaxFactory.SeparatedList <BaseTypeSyntax>() .Add(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(serviceContractName))))); } return(newInterfaceDecl.WithoutTrivia() .WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken)) .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken))); }
Document PerformAction(Document document, SemanticModel model, SyntaxNode root, InterfaceDeclarationSyntax interfaceDeclaration, string interfaceName) { // Applying a refactoring means replacing parts of syntax tree with our new elements. // Note: Every call to any .With...() method creates a completely new syntax tree object, // which is not part of current document anymore! Finally we create a completely new document // based on a new syntax tree containing the result of our refactoring. string newInterfaceName = "I" + interfaceName; var newRoot = root.ReplaceNode( interfaceDeclaration, interfaceDeclaration.WithIdentifier(SyntaxFactory.Identifier(newInterfaceName).WithAdditionalAnnotations(Formatter.Annotation))); return document.WithSyntaxRoot(newRoot); }