public static T AddCSharpMember <T>(this T node, CSharp.Syntax.MemberDeclarationSyntax member, int index) where T : SyntaxNode { var newMembers = CSharp.SyntaxFactory.List(node.AddMember <CSharp.Syntax.MemberDeclarationSyntax>(member, index)); dynamic d = node; return((T)d.WithMembers(newMembers)); }
private static bool CanReuseTypeDeclaration(CSharp.Syntax.MemberDeclarationSyntax member) { if (member != null) { // on reuse valid type declaration (not bad namespace members) switch (member.Kind) { case SyntaxKind.JavaNormalClassDeclaration: case SyntaxKind.JavaNormalInterfaceDeclaration: case SyntaxKind.JavaAnnotationTypeDeclaration: case SyntaxKind.JavaEnumDeclaration: return(true); } } return(false); }
private static bool CanReuseMemberDeclaration(CSharp.Syntax.MemberDeclarationSyntax member, string typeName) { if (member != null) { switch (member.Kind) { case SyntaxKind.JavaNormalClassDeclaration: case SyntaxKind.JavaNormalInterfaceDeclaration: case SyntaxKind.JavaAnnotationTypeDeclaration: case SyntaxKind.JavaEnumDeclaration: case SyntaxKind.FieldDeclaration: return(true); } var parent = GetOldParent(member); var originalTypeDeclaration = parent as CSharp.Syntax.JavaTypeDeclarationSyntax; // originalTypeDeclaration can be null in the case of script code. In that case // the member declaration can be a child of a namespace/copmilation-unit instead of // a type. if (originalTypeDeclaration != null) { switch (member.Kind) { case SyntaxKind.MethodDeclaration: // can reuse a method as long as it *doesn't* match the type name. // // TODO(cyrusn): Relax this in the case of generic methods? var methodDeclaration = (CSharp.Syntax.MethodDeclarationSyntax)member; return(methodDeclaration.Identifier.ValueText != typeName); case SyntaxKind.ConstructorDeclaration: // fall through case SyntaxKind.DestructorDeclaration: // can reuse constructors or destructors if the name and type name still // match. return(originalTypeDeclaration.Identifier.ValueText == typeName); } } } return(false); }