/// <summary> /// Creates a field declaration matching an existing field symbol. /// </summary> public SyntaxNode FieldDeclaration(IFieldSymbol field, SyntaxNode initializer = null) { return(FieldDeclaration( field.Name, TypeExpression(field.Type), field.DeclaredAccessibility, SymbolModifiers.From(field), initializer)); }
/// <summary> /// Creates an indexer declaration matching an existing indexer symbol. /// </summary> public SyntaxNode IndexerDeclaration(IPropertySymbol indexer, IEnumerable <SyntaxNode> getterStatements = null, IEnumerable <SyntaxNode> setterStatements = null) { return(IndexerDeclaration( indexer.Parameters.Select(p => this.ParameterDeclaration(p)), TypeExpression(indexer.Type), indexer.DeclaredAccessibility, SymbolModifiers.From(indexer), getterStatements, setterStatements)); }
/// <summary> /// Creates a property declaration using an existing property symbol as a signature. /// </summary> public SyntaxNode PropertyDeclaration(IPropertySymbol property, IEnumerable <SyntaxNode> getterStatements = null, IEnumerable <SyntaxNode> setterStatements = null) { return(PropertyDeclaration( property.Name, TypeExpression(property.Type), property.DeclaredAccessibility, SymbolModifiers.From(property), getterStatements, setterStatements)); }
/// <summary> /// Create a constructor declaration using /// </summary> public SyntaxNode ConstructorDeclaration( IMethodSymbol constructorMethod, IEnumerable <SyntaxNode> baseConstructorArguments = null, IEnumerable <SyntaxNode> statements = null) { return(ConstructorDeclaration( constructorMethod.ContainingType != null ? constructorMethod.ContainingType.Name : "New", constructorMethod.Parameters.Select(p => ParameterDeclaration(p)), constructorMethod.DeclaredAccessibility, SymbolModifiers.From(constructorMethod), baseConstructorArguments, statements)); }
/// <summary> /// Creates a method declaration matching an existing method symbol. /// </summary> public SyntaxNode MethodDeclaration(IMethodSymbol method, IEnumerable <SyntaxNode> statements = null) { var decl = MethodDeclaration( method.Name, parameters: method.Parameters.Select(p => ParameterDeclaration(p)), returnType: TypeExpression(method.ReturnType), accessibility: method.DeclaredAccessibility, modifiers: SymbolModifiers.From(method), statements: statements); if (method.TypeParameters.Length > 0) { decl = this.WithTypeParametersAndConstraints(decl, method.TypeParameters); } return(decl); }
/// <summary> /// Creates a declaration matching an existing symbol. /// </summary> public SyntaxNode Declaration(ISymbol symbol) { switch (symbol.Kind) { case SymbolKind.Field: return(FieldDeclaration((IFieldSymbol)symbol)); case SymbolKind.Property: var property = (IPropertySymbol)symbol; if (property.IsIndexer) { return(IndexerDeclaration(property)); } else { return(PropertyDeclaration(property)); } case SymbolKind.Method: var method = (IMethodSymbol)symbol; switch (method.MethodKind) { case MethodKind.Constructor: case MethodKind.SharedConstructor: return(ConstructorDeclaration(method)); case MethodKind.Ordinary: return(MethodDeclaration(method)); } break; case SymbolKind.Parameter: return(ParameterDeclaration((IParameterSymbol)symbol)); case SymbolKind.NamedType: var type = (INamedTypeSymbol)symbol; SyntaxNode declaration = null; switch (type.TypeKind) { case TypeKind.Class: declaration = ClassDeclaration( type.Name, accessibility: type.DeclaredAccessibility, modifiers: SymbolModifiers.From(type), baseType: TypeExpression(type.BaseType), interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null, members: type.GetMembers().Select(m => Declaration(m))); break; case TypeKind.Struct: declaration = StructDeclaration( type.Name, accessibility: type.DeclaredAccessibility, modifiers: SymbolModifiers.From(type), interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null, members: type.GetMembers().Select(m => Declaration(m))); break; case TypeKind.Interface: declaration = InterfaceDeclaration( type.Name, accessibility: type.DeclaredAccessibility, interfaceTypes: type.Interfaces != null ? type.Interfaces.Select(i => TypeExpression(i)) : null, members: type.GetMembers().Select(m => Declaration(m))); break; case TypeKind.Enum: declaration = EnumDeclaration( type.Name, accessibility: type.DeclaredAccessibility, members: type.GetMembers().Select(m => Declaration(m))); break; } if (declaration != null) { return(WithTypeParametersAndConstraints(declaration, type.TypeParameters)); } break; } throw new ArgumentException("Symbol cannot be converted to a declaration"); }