From c29815a7c0375ad88de759486cc9808a605965c2 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 19 Nov 2009 19:25:20 +0000 Subject: [PATCH] - Removed OverrideEqualsGetHashCodeMethodsCommand and OverrideEqualsGetHashCodeMethodsCommand from Refactor menu - fixed ClassCodeGeneratorMenuBuilder - allow OptionBinding to be used from Code git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5272 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/InlineUIElementGenerator.cs | 2 +- .../SharpRefactoring/SharpRefactoring.addin | 5 +- .../SharpRefactoring/SharpRefactoring.csproj | 14 +++- .../Src/ClassCodeGeneratorMenuBuilder.cs | 7 +- ...log.cs => AbstractInlineRefactorDialog.cs} | 22 +++++- .../OverrideEqualsGetHashCodeMethodsDialog.cs | 78 +++++++++++++++++++ .../Src/Gui/OverrideToStringMethodDialog.xaml | 7 ++ .../Gui/OverrideToStringMethodDialog.xaml.cs | 30 +++++++ ....cs => OverrideToStringMethodDialogOld.cs} | 4 +- .../Misc/SharpRefactoring/Src/Options.cs | 38 +++++++++ ...OverrideEqualsGetHashCodeMethodsCommand.cs | 53 +++++++++++++ .../Src/OverrideToStringMethodCommand.cs | 6 +- .../OptionBinding.cs | 34 ++++---- .../Project/Src/MemberLookupHelper.cs | 2 +- 14 files changed, 273 insertions(+), 29 deletions(-) rename src/AddIns/Misc/SharpRefactoring/Src/Gui/{InlineRefactorDialog.cs => AbstractInlineRefactorDialog.cs} (82%) create mode 100644 src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.cs create mode 100644 src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml create mode 100644 src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml.cs rename src/AddIns/Misc/SharpRefactoring/Src/Gui/{OverrideToStringMethodDialog.cs => OverrideToStringMethodDialogOld.cs} (94%) create mode 100644 src/AddIns/Misc/SharpRefactoring/Src/Options.cs create mode 100644 src/AddIns/Misc/SharpRefactoring/Src/OverrideEqualsGetHashCodeMethodsCommand.cs diff --git a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/InlineUIElementGenerator.cs b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/InlineUIElementGenerator.cs index a05c5ee27d..27a758bc92 100644 --- a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/InlineUIElementGenerator.cs +++ b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/InlineUIElementGenerator.cs @@ -24,7 +24,7 @@ namespace ICSharpCode.AvalonEdit.AddIn this.textView = textView; this.element = element; this.anchor = anchor; - anchor.Deleted += delegate { Remove(); }; + this.anchor.Deleted += delegate { Remove(); }; } public override int GetFirstInterestedOffset(int startOffset) diff --git a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin index 3c77139e2f..c99fe5cff1 100644 --- a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin +++ b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.addin @@ -26,9 +26,12 @@ - + diff --git a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj index 4cbae80422..ba2df41cba 100644 --- a/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj +++ b/src/AddIns/Misc/SharpRefactoring/SharpRefactoring.csproj @@ -78,12 +78,19 @@ ExtractMethodForm.cs - - + + + + OverrideToStringMethodDialog.xaml + Code + + + + @@ -128,4 +135,7 @@ + + + \ No newline at end of file diff --git a/src/AddIns/Misc/SharpRefactoring/Src/ClassCodeGeneratorMenuBuilder.cs b/src/AddIns/Misc/SharpRefactoring/Src/ClassCodeGeneratorMenuBuilder.cs index 5458441bef..29f2dd6dae 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/ClassCodeGeneratorMenuBuilder.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/ClassCodeGeneratorMenuBuilder.cs @@ -64,7 +64,7 @@ namespace SharpRefactoring EventHandler eh = delegate { var d = FindReferencesAndRenameHelper.GetDocument(c); if (d != null) - ImplementAbstractClass(d, c, abstractClass); + ImplementAbstractClass(d, c, rtCopy); ParserService.ParseCurrentViewContent(); }; subItems.Add(new MenuCommand(ambience.Convert(abstractClass), eh)); @@ -83,14 +83,15 @@ namespace SharpRefactoring } #region Code generation - void ImplementAbstractClass(IDocument document, IClass target, IClass abstractClass) + void ImplementAbstractClass(IDocument document, IClass target, IReturnType abstractClass) { CodeGenerator generator = target.ProjectContent.Language.CodeGenerator; RefactoringDocumentAdapter doc = new RefactoringDocumentAdapter(document); var pos = doc.OffsetToPosition(doc.PositionToOffset(target.BodyRegion.EndLine, target.BodyRegion.EndColumn) - 1); ClassFinder context = new ClassFinder(target, pos.Line, pos.Column); - foreach (IMember member in abstractClass.AllMembers.Where(m => m.IsAbstract && !HasMember(m, target))) { + foreach (IMember member in MemberLookupHelper.GetAccessibleMembers(abstractClass, target, LanguageProperties.CSharp, true) + .Where(m => m.IsAbstract && !HasMember(m, target))) { generator.InsertCodeAtEnd(target.BodyRegion, doc, generator.GetOverridingMethod(member, context)); } } diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Gui/InlineRefactorDialog.cs b/src/AddIns/Misc/SharpRefactoring/Src/Gui/AbstractInlineRefactorDialog.cs similarity index 82% rename from src/AddIns/Misc/SharpRefactoring/Src/Gui/InlineRefactorDialog.cs rename to src/AddIns/Misc/SharpRefactoring/Src/Gui/AbstractInlineRefactorDialog.cs index cab89d21b2..ded7271a5d 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/Gui/InlineRefactorDialog.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/Gui/AbstractInlineRefactorDialog.cs @@ -14,6 +14,7 @@ using System.Windows.Controls; using System.Windows.Media; using ICSharpCode.Core; +using ICSharpCode.Core.Presentation; using ICSharpCode.NRefactory.Ast; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom; @@ -22,14 +23,14 @@ using ICSharpCode.SharpDevelop.Editor; namespace SharpRefactoring.Gui { - public abstract class InlineRefactorDialog : DockPanel + public abstract class AbstractInlineRefactorDialog : DockPanel, IOptionBindingContainer { protected ITextAnchor anchor; protected ITextEditor editor; public IInlineUIElement Element { get; set; } - public InlineRefactorDialog(ITextEditor editor, ITextAnchor anchor) + public AbstractInlineRefactorDialog(ITextEditor editor, ITextAnchor anchor) { this.anchor = anchor; this.editor = editor; @@ -39,7 +40,7 @@ namespace SharpRefactoring.Gui Initialize(); } - void Initialize() + protected virtual void Initialize() { UIElement content = CreateContentElement(); @@ -90,6 +91,11 @@ namespace SharpRefactoring.Gui editor.Document.Insert(anchor.Offset, GenerateCode(generator, current) ?? ""); } + if (optionBindings != null) { + foreach (OptionBinding binding in optionBindings) + binding.Save(); + } + Element.Remove(); } @@ -100,5 +106,15 @@ namespace SharpRefactoring.Gui Element.Remove(); } + + List optionBindings; + + public void AddBinding(OptionBinding binding) + { + if (optionBindings == null) + optionBindings = new List(); + + optionBindings.Add(binding); + } } } diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.cs b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.cs new file mode 100644 index 0000000000..e729cefdbf --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideEqualsGetHashCodeMethodsDialog.cs @@ -0,0 +1,78 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Linq; +using System.Windows; +using System.Windows.Controls; + +using ICSharpCode.Core; +using ICSharpCode.Core.Presentation; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Dom.Refactoring; +using ICSharpCode.SharpDevelop.Editor; + +namespace SharpRefactoring.Gui +{ + public class OverrideEqualsGetHashCodeMethodsDialog : AbstractInlineRefactorDialog + { + CheckBox addIEquatableCheckBox; + CheckBox addOperatorOverrides; + CheckBox implementAllIEquatables; + + IClass selectedClass; + + public OverrideEqualsGetHashCodeMethodsDialog(ITextEditor editor, ITextAnchor anchor, IClass selectedClass) + : base(editor, anchor) + { + if (selectedClass == null) + throw new ArgumentNullException("selectedClass"); + + this.selectedClass = selectedClass; + + addIEquatableCheckBox.Content = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.AddInterface}"), "IEquatable<" + selectedClass.Name + ">"); + addIEquatableCheckBox.IsEnabled = !selectedClass.BaseTypes.Any(type => { + if (!type.IsGenericReturnType) + return false; + var genericType = type.CastToGenericReturnType(); + var boundTo = genericType.TypeParameter.BoundTo; + if (boundTo == null) + return false; + return boundTo.Name == selectedClass.Name; + } + ); + + addIEquatableCheckBox.SetValueToExtension(CheckBox.IsCheckedProperty, + new OptionBinding(typeof(Options), "AddInterface") + ); + } + + protected override UIElement CreateContentElement() + { + addIEquatableCheckBox = new CheckBox(); + + StackPanel panel = new StackPanel() { + Orientation = Orientation.Vertical, + Children = { + addIEquatableCheckBox + } + }; + + return panel; + } + + protected override string GenerateCode(CodeGenerator generator, IClass currentClass) + { + throw new NotImplementedException(); + } + } + + public interface IInlineRefactoring + { + void GenerateCode(ITextEditor editor); + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml new file mode 100644 index 0000000000..41cd1fa95b --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml.cs b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml.cs new file mode 100644 index 0000000000..8976bdea7a --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.xaml.cs @@ -0,0 +1,30 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; + +namespace SharpRefactoring.Gui +{ + /// + /// Interaction logic for OverrideToStringMethodDialog.xaml + /// + public partial class OverrideToStringMethodDialog : UserControl + { + public OverrideToStringMethodDialog() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.cs b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialogOld.cs similarity index 94% rename from src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.cs rename to src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialogOld.cs index 8d79b23fdd..686565278b 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialog.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/Gui/OverrideToStringMethodDialogOld.cs @@ -22,12 +22,12 @@ using ICSharpCode.SharpDevelop.Editor; namespace SharpRefactoring.Gui { - public class OverrideToStringMethodDialog : InlineRefactorDialog + public class OverrideToStringMethodDialogOld : AbstractInlineRefactorDialog { ListBox listBox; List> fields; - public OverrideToStringMethodDialog(ITextEditor editor, ITextAnchor anchor, IList fields) + public OverrideToStringMethodDialogOld(ITextEditor editor, ITextAnchor anchor, IList fields) : base(editor, anchor) { this.fields = fields.Select(f => new Wrapper() { Entity = f }).ToList(); diff --git a/src/AddIns/Misc/SharpRefactoring/Src/Options.cs b/src/AddIns/Misc/SharpRefactoring/Src/Options.cs new file mode 100644 index 0000000000..6f75721b80 --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Src/Options.cs @@ -0,0 +1,38 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Diagnostics; +using ICSharpCode.Core; + +namespace SharpRefactoring +{ + /// + /// Description of Options. + /// + public static class Options + { + static Properties properties; + + public static Properties Properties { + get { + Debug.Assert(properties != null); + return properties; + } + } + + static Options() + { + properties = PropertyService.Get("SharpRefactoringOptions", new Properties()); + } + + public static bool AddInterface { + get { return properties.Get("AddInterface", false); } + set { properties.Set("AddInterface", value); } + } + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Src/OverrideEqualsGetHashCodeMethodsCommand.cs b/src/AddIns/Misc/SharpRefactoring/Src/OverrideEqualsGetHashCodeMethodsCommand.cs new file mode 100644 index 0000000000..b965c4c88f --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Src/OverrideEqualsGetHashCodeMethodsCommand.cs @@ -0,0 +1,53 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.SharpDevelop; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Dom.Refactoring; +using ICSharpCode.SharpDevelop.Editor; +using ICSharpCode.SharpDevelop.Refactoring; +using SharpRefactoring.Gui; + +namespace SharpRefactoring +{ + public class OverrideEqualsGetHashCodeMethodsCommand : AbstractRefactoringCommand + { + protected override void Run(ITextEditor textEditor, RefactoringProvider provider) + { + IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService; + + if (uiService == null) + return; + + ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName); + + if (parseInfo == null) + return; + + CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator; + IClass current = parseInfo.CompilationUnit.GetInnermostClass(textEditor.Caret.Line, textEditor.Caret.Column); + + if (current == null) + return; + + ITextAnchor anchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset); + anchor.MovementType = AnchorMovementType.AfterInsertion; + + var line = textEditor.Document.GetLineForOffset(textEditor.Caret.Offset); + + string indent = DocumentUtilitites.GetWhitespaceAfter(textEditor.Document, line.Offset); + +// textEditor.Document.Insert(anchor.Offset, "#region Equals and GetHashCode implementation\n" + indent); +// textEditor.Document.Insert(anchor.Offset + 1, indent + "#endregion\n"); +// +// AbstractInlineRefactorDialog dialog = new OverrideEqualsGetHashCodeMethodsDialog(textEditor, anchor, current); +// +// dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); + } + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Src/OverrideToStringMethodCommand.cs b/src/AddIns/Misc/SharpRefactoring/Src/OverrideToStringMethodCommand.cs index 8a3bde9526..7b0c9a38fc 100644 --- a/src/AddIns/Misc/SharpRefactoring/Src/OverrideToStringMethodCommand.cs +++ b/src/AddIns/Misc/SharpRefactoring/Src/OverrideToStringMethodCommand.cs @@ -48,9 +48,9 @@ namespace SharpRefactoring textEditor.Document.Insert(anchor.Offset, "public override string ToString()\n" + indent + "{\n" + indent + "\t"); textEditor.Document.Insert(anchor.Offset + 1, indent + "}\n"); - InlineRefactorDialog dialog = new OverrideToStringMethodDialog(textEditor, anchor, current.Fields); - - dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); +// AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(textEditor, anchor, current.Fields); +// +// dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); } } } diff --git a/src/Main/ICSharpCode.Core.Presentation/OptionBinding.cs b/src/Main/ICSharpCode.Core.Presentation/OptionBinding.cs index 6cda10e7db..d4a91d5c08 100644 --- a/src/Main/ICSharpCode.Core.Presentation/OptionBinding.cs +++ b/src/Main/ICSharpCode.Core.Presentation/OptionBinding.cs @@ -33,7 +33,7 @@ namespace ICSharpCode.Core.Presentation /// public class OptionBinding : MarkupExtension { - public string PropertyName { get; set; } + public string FullPropertyName { get; set; } static readonly Regex regex = new Regex("^.+\\:.+\\..+$", RegexOptions.Compiled); @@ -41,6 +41,7 @@ namespace ICSharpCode.Core.Presentation DependencyProperty dp; bool isStatic; Type propertyDeclaringType; + string propertyName; MemberInfo propertyInfo; @@ -49,7 +50,13 @@ namespace ICSharpCode.Core.Presentation if (!regex.IsMatch(propertyName)) throw new ArgumentException("parameter must have the following format: namespace:ClassName.FieldOrProperty", "propertyName"); - this.PropertyName = propertyName; + this.FullPropertyName = propertyName; + } + + public OptionBinding(Type container, string propertyName) + { + this.propertyDeclaringType = container; + this.propertyName = propertyName; } public override object ProvideValue(IServiceProvider provider) @@ -65,21 +72,22 @@ namespace ICSharpCode.Core.Presentation if (target == null || dp == null) return null; - string[] name = PropertyName.Split('.'); - IXamlTypeResolver typeResolver = provider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; - propertyDeclaringType = typeResolver.Resolve(name[0]); - if (propertyDeclaringType == null) - throw new ArgumentException("Could not find type " + name[0]); + if (FullPropertyName != null) { + string[] name = FullPropertyName.Split('.'); + IXamlTypeResolver typeResolver = provider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; + propertyDeclaringType = typeResolver.Resolve(name[0]); + propertyName = name[1]; + } - this.propertyInfo = propertyDeclaringType.GetProperty(name[1]); + this.propertyInfo = propertyDeclaringType.GetProperty(propertyName); if (this.propertyInfo != null) { isStatic = (propertyInfo as PropertyInfo).GetGetMethod().IsStatic; } else { - this.propertyInfo = propertyDeclaringType.GetField(name[1]); + this.propertyInfo = propertyDeclaringType.GetField(propertyName); if (this.propertyInfo != null) { isStatic = (propertyInfo as FieldInfo).IsStatic; } else { - throw new ArgumentException("Could not find property " + name[1]); + throw new ArgumentException("Could not find property " + propertyName); } } @@ -103,7 +111,7 @@ namespace ICSharpCode.Core.Presentation return ConvertOnDemand(result, dp.PropertyType); } catch (Exception e) { - throw new Exception("Failing to convert " + this.PropertyName + " to " + + throw new Exception("Failing to convert " + this.FullPropertyName + " to " + dp.OwnerType.Name + "." + dp.Name + " (" + dp.PropertyType + ")", e); } } @@ -140,13 +148,13 @@ namespace ICSharpCode.Core.Presentation return Convert.ChangeType(result, returnType); } - IOptionBindingContainer TryFindContainer(FrameworkElement start) + IOptionBindingContainer TryFindContainer(DependencyObject start) { if (start == null) return null; while (start != null && !(start is IOptionBindingContainer)) - start = start.Parent as FrameworkElement; + start = LogicalTreeHelper.GetParent(start); return start as IOptionBindingContainer; } diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/MemberLookupHelper.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/MemberLookupHelper.cs index 7594ae15f3..5faf5c954c 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/MemberLookupHelper.cs +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/MemberLookupHelper.cs @@ -22,7 +22,7 @@ namespace ICSharpCode.SharpDevelop.Dom public static class MemberLookupHelper { #region LookupMember / GetAccessibleMembers - static List GetAllMembers(IReturnType rt) + public static List GetAllMembers(IReturnType rt) { List members = new List(); if (rt != null) {