public void ConvertSymbol(ISymbol symbol, TokenWriter writer, CSharpFormattingOptions formattingPolicy) { if (symbol == null) { throw new ArgumentNullException("symbol"); } if (writer == null) { throw new ArgumentNullException("writer"); } if (formattingPolicy == null) { throw new ArgumentNullException("formattingPolicy"); } TypeSystemAstBuilder astBuilder = CreateAstBuilder(); AstNode node = astBuilder.ConvertSymbol(symbol); EntityDeclaration entityDecl = node as EntityDeclaration; if (entityDecl != null) { PrintModifiers(entityDecl.Modifiers, writer); } if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword) { if (node is TypeDeclaration) { switch (((TypeDeclaration)node).ClassType) { case ClassType.Class: writer.WriteKeyword(Roles.ClassKeyword, "class"); break; case ClassType.Struct: writer.WriteKeyword(Roles.StructKeyword, "struct"); break; case ClassType.Interface: writer.WriteKeyword(Roles.InterfaceKeyword, "interface"); break; case ClassType.Enum: writer.WriteKeyword(Roles.EnumKeyword, "enum"); break; default: throw new Exception("Invalid value for ClassType"); } writer.Space(); } else if (node is DelegateDeclaration) { writer.WriteKeyword(Roles.DelegateKeyword, "delegate"); writer.Space(); } else if (node is EventDeclaration) { writer.WriteKeyword(EventDeclaration.EventKeywordRole, "event"); writer.Space(); } else if (node is NamespaceDeclaration) { writer.WriteKeyword(Roles.NamespaceKeyword, "namespace"); writer.Space(); } } if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType) { var rt = node.GetChildByRole(Roles.Type); if (!rt.IsNull) { rt.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); writer.Space(); } } if (symbol is ITypeDefinition) { WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy); } else if (symbol is IMember) { WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy); } else { writer.WriteIdentifier(Identifier.Create(symbol.Name), BoxedTextColor.Text); } if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(symbol)) { writer.WriteTokenPunctuation(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "("); bool first = true; foreach (var param in node.GetChildrenByRole(Roles.Parameter)) { if (first) { first = false; } else { writer.WriteTokenPunctuation(Roles.Comma, ","); writer.Space(); } param.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); } writer.WriteTokenPunctuation(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")"); } if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration)) { IProperty property = symbol as IProperty; if (property != null) { writer.Space(); writer.WriteTokenBrace(Roles.LBrace, "{"); writer.Space(); if (property.CanGet) { writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get"); writer.WriteTokenPunctuation(Roles.Semicolon, ";"); writer.Space(); } if (property.CanSet) { writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set"); writer.WriteTokenPunctuation(Roles.Semicolon, ";"); writer.Space(); } writer.WriteTokenBrace(Roles.RBrace, "}"); } else { writer.WriteTokenPunctuation(Roles.Semicolon, ";"); } } }
public void ConvertSymbol(ISymbol symbol, TokenWriter writer, CSharpFormattingOptions formattingPolicy) { if (symbol == null) throw new ArgumentNullException("symbol"); if (writer == null) throw new ArgumentNullException("writer"); if (formattingPolicy == null) throw new ArgumentNullException("formattingPolicy"); TypeSystemAstBuilder astBuilder = CreateAstBuilder(); AstNode node = astBuilder.ConvertSymbol(symbol); EntityDeclaration entityDecl = node as EntityDeclaration; if (entityDecl != null) PrintModifiers(entityDecl.Modifiers, writer); if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword) { if (node is TypeDeclaration) { switch (((TypeDeclaration)node).ClassType) { case ClassType.Class: writer.WriteKeyword(Roles.ClassKeyword, "class"); break; case ClassType.Struct: writer.WriteKeyword(Roles.StructKeyword, "struct"); break; case ClassType.Interface: writer.WriteKeyword(Roles.InterfaceKeyword, "interface"); break; case ClassType.Enum: writer.WriteKeyword(Roles.EnumKeyword, "enum"); break; default: throw new Exception("Invalid value for ClassType"); } writer.Space(); } else if (node is DelegateDeclaration) { writer.WriteKeyword(Roles.DelegateKeyword, "delegate"); writer.Space(); } else if (node is EventDeclaration) { writer.WriteKeyword(EventDeclaration.EventKeywordRole, "event"); writer.Space(); } else if (node is NamespaceDeclaration) { writer.WriteKeyword(Roles.NamespaceKeyword, "namespace"); writer.Space(); } } if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType) { var rt = node.GetChildByRole(Roles.Type); if (!rt.IsNull) { rt.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); writer.Space(); } } if (symbol is ITypeDefinition) WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy); else if (symbol is IMember) WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy); else writer.WriteIdentifier(Identifier.Create(symbol.Name), TextTokenType.Text); if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(symbol)) { writer.WriteTokenOperator(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "("); bool first = true; foreach (var param in node.GetChildrenByRole(Roles.Parameter)) { if (first) { first = false; } else { writer.WriteTokenOperator(Roles.Comma, ","); writer.Space(); } param.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); } writer.WriteTokenOperator(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")"); } if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration)) { IProperty property = symbol as IProperty; if (property != null) { writer.Space(); writer.WriteTokenBrace(Roles.LBrace, "{"); writer.Space(); if (property.CanGet) { writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get"); writer.WriteTokenOperator(Roles.Semicolon, ";"); writer.Space(); } if (property.CanSet) { writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set"); writer.WriteTokenOperator(Roles.Semicolon, ";"); writer.Space(); } writer.WriteTokenBrace(Roles.RBrace, "}"); } else { writer.WriteTokenOperator(Roles.Semicolon, ";"); } } }