diff options
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs | 97 |
1 files changed, 58 insertions, 39 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs index 9d7913d8ae..516a34c15c 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ToStringGenerator.cs @@ -7,7 +7,7 @@ // Copyright (c) 2009 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal +// of this software and associated documentation files (the "Software" ), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is @@ -25,75 +25,79 @@ // THE SOFTWARE. using System; -using MonoDevelop.Components; -using Gtk; -using MonoDevelop.Ide.Gui; using System.Collections.Generic; -using ICSharpCode.NRefactory.CSharp; using System.Text; using MonoDevelop.Core; -using MonoDevelop.Refactoring; -using ICSharpCode.NRefactory.TypeSystem; -using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Simplification; namespace MonoDevelop.CodeGeneration { class ToStringGenerator : ICodeGenerator { - public string Icon { - get { + public string Icon + { + get + { return "md-newmethod"; } } - - public string Text { - get { + + public string Text + { + get + { return GettextCatalog.GetString ("ToString() implementation"); } } - - public string GenerateDescription { - get { + + public string GenerateDescription + { + get + { return GettextCatalog.GetString ("Select members to be outputted."); } } - + public bool IsValid (CodeGenerationOptions options) { return new CreateToString (options).IsValid (); } - + public IGenerateAction InitalizeSelection (CodeGenerationOptions options, Gtk.TreeView treeView) { CreateToString createToString = new CreateToString (options); createToString.Initialize (treeView); return createToString; } - + class CreateToString : AbstractGenerateAction { public CreateToString (CodeGenerationOptions options) : base (options) { } - + protected override IEnumerable<object> GetValidMembers () { if (Options.EnclosingType == null || Options.EnclosingMember != null) yield break; - foreach (IField field in Options.EnclosingType.Fields) { - if (field.IsSynthetic) + + foreach (var field in Options.EnclosingType.GetMembers ().OfType<IFieldSymbol> ()) { + if (field.IsImplicitlyDeclared) continue; yield return field; } - foreach (IProperty property in Options.EnclosingType.Properties) { - if (property.IsSynthetic) + foreach (var property in Options.EnclosingType.GetMembers ().OfType<IPropertySymbol> ()) { + if (property.IsImplicitlyDeclared) continue; - if (property.CanGet) + if (property.GetMethod != null) yield return property; } } - + string GetFormatString (IEnumerable<object> includedMembers) { var format = new StringBuilder (); @@ -101,7 +105,7 @@ namespace MonoDevelop.CodeGeneration format.Append (Options.EnclosingType.Name); format.Append (": "); int i = 0; - foreach (IEntity member in includedMembers) { + foreach (ISymbol member in includedMembers) { if (i > 0) format.Append (", "); format.Append (member.Name); @@ -112,22 +116,37 @@ namespace MonoDevelop.CodeGeneration format.Append ("]"); return format.ToString (); } - + protected override IEnumerable<string> GenerateCode (List<object> includedMembers) { - yield return new MethodDeclaration () { - Name = "ToString", - ReturnType = new PrimitiveType ("string"), - Modifiers = Modifiers.Public | Modifiers.Override, - Body = new BlockStatement () { - new ReturnStatement ( - new InvocationExpression ( - new MemberReferenceExpression (new TypeReferenceExpression (new PrimitiveType ("string")), "Format"), - new Expression [] { new PrimitiveExpression (GetFormatString (includedMembers)) }.Concat (includedMembers.Select (member => new IdentifierExpression (((IEntity)member).Name))) + List<ArgumentSyntax> arguments = new List<ArgumentSyntax> (); + arguments.Add (SyntaxFactory.Argument (SyntaxFactory.LiteralExpression (SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal (GetFormatString (includedMembers))))); + foreach (ISymbol member in includedMembers) { + arguments.Add (SyntaxFactory.Argument (SyntaxFactory.IdentifierName (member.Name))); + } + var node = SyntaxFactory.MethodDeclaration ( + SyntaxFactory.List<AttributeListSyntax>(), + SyntaxFactory.TokenList (SyntaxFactory.Token (SyntaxKind.PublicKeyword), SyntaxFactory.Token (SyntaxKind.OverrideKeyword)), + SyntaxFactory.ParseTypeName ("string"), + null, + SyntaxFactory.Identifier ("ToString"), + null, + SyntaxFactory.ParameterList (), + SyntaxFactory.List<TypeParameterConstraintClauseSyntax>(), + SyntaxFactory.Block ( + SyntaxFactory.ReturnStatement ( + SyntaxFactory.InvocationExpression ( + SyntaxFactory.MemberAccessExpression ( + SyntaxKind.SimpleMemberAccessExpression, + SyntaxFactory.ParseExpression ("string"), + SyntaxFactory.IdentifierName ("Format") + ), + SyntaxFactory.ArgumentList (SyntaxFactory.SeparatedList<ArgumentSyntax> (arguments)) ) ) - } - }.ToString (Options.FormattingOptions); + ), + null); + yield return Options.OutputNode (node).Result; } } } |