Browse Source

add support for auto-implemented properties to override ToString code generator and adjust behaviour of dialog if list of available properties/fields is empty

pull/18/head
Siegfried Pammer 14 years ago
parent
commit
03bc938456
  1. 4
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/OverrideToStringMethodDialog.xaml.cs
  2. 3
      src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs
  3. 42
      src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs

4
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/OverrideToStringMethodDialog.xaml.cs

@ -24,13 +24,13 @@ namespace SharpRefactoring.Gui @@ -24,13 +24,13 @@ namespace SharpRefactoring.Gui
string baseCall;
string insertedCode;
public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor anchor, IList<IField> fields, string baseCall)
public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor anchor, IList<PropertyOrFieldWrapper> fields, string baseCall)
: base(context, editor, anchor)
{
InitializeComponent();
this.baseCall = baseCall;
this.listBox.ItemsSource = fields.Where(f => f.ReturnType != null).Select(f => new PropertyOrFieldWrapper(f)).ToList();
this.listBox.ItemsSource = fields;
listBox.SelectAll();
}

3
src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs

@ -91,7 +91,4 @@ namespace SharpRefactoring @@ -91,7 +91,4 @@ namespace SharpRefactoring
}
}
}
}

42
src/AddIns/Misc/SharpRefactoring/Project/Src/OverrideToStringMethodRefactoring.cs

@ -2,7 +2,9 @@ @@ -2,7 +2,9 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Snippets;
using ICSharpCode.NRefactory.Ast;
@ -46,6 +48,8 @@ namespace SharpRefactoring @@ -46,6 +48,8 @@ namespace SharpRefactoring
if (current == null)
return;
List<PropertyOrFieldWrapper> entities = FindFieldsAndProperties(current).ToList();
using (textEditor.Document.OpenUndoGroup()) {
ITextAnchor endAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset);
endAnchor.MovementType = AnchorMovementType.AfterInsertion;
@ -67,16 +71,42 @@ namespace SharpRefactoring @@ -67,16 +71,42 @@ namespace SharpRefactoring
InsertionContext insertionContext = new InsertionContext(textEditor.GetService(typeof(TextArea)) as TextArea, startAnchor.Offset);
AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, textEditor, startAnchor, insertionPos, current.Fields, codeForBaseCall.Trim());
dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);
textEditor.Document.InsertNormalized(endAnchor.Offset, Environment.NewLine + code.Substring(marker + codeForBaseCall.Length));
insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
if (entities.Any()) {
AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, textEditor, startAnchor, insertionPos, entities, codeForBaseCall.Trim());
dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);
textEditor.Document.InsertNormalized(endAnchor.Offset, Environment.NewLine + code.Substring(marker + codeForBaseCall.Length));
insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
} else {
int startIndex = endAnchor.Offset;
textEditor.Document.InsertNormalized(startIndex, code.Substring(marker));
textEditor.Select(startIndex, codeForBaseCall.TrimEnd().Length);
}
insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
}
}
IEnumerable<PropertyOrFieldWrapper> FindFieldsAndProperties(IClass sourceClass)
{
int i = 0;
foreach (var f in sourceClass.Fields.Where(field => !field.IsConst
&& field.IsStatic == sourceClass.IsStatic
&& field.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(f) { Index = i };
i++;
}
foreach (var p in sourceClass.Properties.Where(prop => prop.CanGet && !prop.IsIndexer
&& PropertyRefactoringMenuBuilder.IsAutomaticProperty(prop)
&& prop.IsStatic == sourceClass.IsStatic
&& prop.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(p) { Index = i };
i++;
}
}
public bool Handles(ICompletionItem item)
{
return item is OverrideCompletionItem && item.Text == "ToString()";

Loading…
Cancel
Save