diff options
author | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
commit | 95408b1a41b31b645f3a8c3323ab7d3ca4bff26b (patch) | |
tree | f8f137cddcb9a40499d303be95722fd2c6081bce /main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs | |
parent | b70e4e04b4e41b8ec28d7e4b220b401ecf467e7a (diff) | |
parent | de043d330a8bf5855d4d983c804cd36727cb2406 (diff) |
Merge remote-tracking branch 'origin/roslyn' into new-project-model
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs | 98 |
1 files changed, 72 insertions, 26 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs index 57b241976a..1999cf900f 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/OverrideMembersGenerator.cs @@ -28,9 +28,12 @@ using Gtk; using System.Collections.Generic; using MonoDevelop.Core; using MonoDevelop.Refactoring; -using ICSharpCode.NRefactory.CSharp; -using ICSharpCode.NRefactory.TypeSystem; using MonoDevelop.Ide.TypeSystem; +using ICSharpCode.NRefactory6.CSharp; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis; +using MonoDevelop.CSharp.Refactoring; namespace MonoDevelop.CodeGeneration { @@ -41,62 +44,105 @@ namespace MonoDevelop.CodeGeneration return "md-method"; } } - + public string Text { get { return GettextCatalog.GetString ("Override members"); } } - + public string GenerateDescription { get { return GettextCatalog.GetString ("Select members to be overridden."); } } - + public bool IsValid (CodeGenerationOptions options) { return new OverrideMethods (options).IsValid (); } - + public IGenerateAction InitalizeSelection (CodeGenerationOptions options, Gtk.TreeView treeView) { OverrideMethods overrideMethods = new OverrideMethods (options); overrideMethods.Initialize (treeView); return overrideMethods; } - + class OverrideMethods : AbstractGenerateAction { public OverrideMethods (CodeGenerationOptions options) : base (options) { } - + protected override IEnumerable<object> GetValidMembers () { - var type = Options.EnclosingType; - if (type == null || Options.EnclosingMember != null) - yield break; - HashSet<string> memberName = new HashSet<string> (); - foreach (var member in Options.EnclosingType.GetMembers ()) { - if (member.IsSynthetic) - continue; - if (member.IsOverridable) { - string id = AmbienceService.DefaultAmbience.GetString (member, OutputFlags.ClassBrowserEntries); - if (memberName.Contains (id)) - continue; - memberName.Add (id); - yield return member; + var encType = Options.EnclosingType as INamedTypeSymbol; + if (encType == null || Options.EnclosingMember != null) + return Enumerable.Empty<object> (); + + + var result = new HashSet<ISymbol> (); + var cancellationToken = default(CancellationToken); + var baseTypes = encType.GetBaseTypes ().Reverse (); + foreach (var type in baseTypes) { + RemoveOverriddenMembers (result, type, cancellationToken); + + AddOverridableMembers (result, encType, type, cancellationToken); + } + RemoveOverriddenMembers (result, encType, cancellationToken); + return result; + } + + static void AddOverridableMembers (HashSet<ISymbol> result, INamedTypeSymbol containingType, INamedTypeSymbol type, CancellationToken cancellationToken) + { + foreach (var member in type.GetMembers()) { + if (IsOverridable (member, containingType)) { + result.Add (member); + } + } + } + + protected static void RemoveOverriddenMembers (HashSet<ISymbol> result, INamedTypeSymbol containingType, CancellationToken cancellationToken) + { + foreach (var member in containingType.GetMembers()) { + var overriddenMember = member.OverriddenMember (); + if (overriddenMember != null) { + result.Remove (overriddenMember); } } } - + + public static bool IsOverridable (ISymbol member, INamedTypeSymbol containingType) + { + if (member.IsAbstract || member.IsVirtual || member.IsOverride) { + if (member.IsSealed) { + return false; + } + + if (!member.IsAccessibleWithin (containingType)) { + return false; + } + + switch (member.Kind) { + case SymbolKind.Event: + return true; + case SymbolKind.Method: + return ((IMethodSymbol)member).MethodKind == MethodKind.Ordinary; + case SymbolKind.Property: + return !((IPropertySymbol)member).IsWithEvents; + } + } + return false; + } + protected override IEnumerable<string> GenerateCode (List<object> includedMembers) { - var generator = Options.CreateCodeGenerator (); - generator.AutoIndent = false; - foreach (IMember member in includedMembers) - yield return generator.CreateMemberImplementation (Options.EnclosingType, Options.EnclosingPart, member, false).Code; + var currentType = Options.EnclosingType as INamedTypeSymbol; + + foreach (ISymbol member in includedMembers) { + yield return CSharpCodeGenerator.CreateOverridenMemberImplementation (Options.DocumentContext, Options.Editor, currentType, currentType.Locations.First (), member, false).Code; + } } } } |