Browse Source

fixed cursor positioning after finishing InsertCtorDialog, display no dialog if no fields are available

pull/1/head
Siegfried Pammer 16 years ago
parent
commit
f4b26c61fb
  1. 13
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs
  2. 71
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs
  3. 21
      src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs
  4. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs

13
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs

@ -25,6 +25,7 @@ namespace SharpRefactoring.Gui
public abstract class AbstractInlineRefactorDialog : GroupBox, IOptionBindingContainer, IActiveElement public abstract class AbstractInlineRefactorDialog : GroupBox, IOptionBindingContainer, IActiveElement
{ {
protected ITextAnchor anchor; protected ITextAnchor anchor;
protected ITextAnchor insertionEndAnchor;
protected ITextEditor editor; protected ITextEditor editor;
ClassFinder classFinderContext; ClassFinder classFinderContext;
@ -34,15 +35,13 @@ namespace SharpRefactoring.Gui
protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor) protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor)
{ {
this.anchor = anchor; this.anchor = insertionEndAnchor = anchor;
this.editor = editor; this.editor = editor;
this.context = context; this.context = context;
this.classFinderContext = new ClassFinder(ParserService.ParseCurrentViewContent(), editor.Document.Text, anchor.Offset); this.classFinderContext = new ClassFinder(ParserService.ParseCurrentViewContent(), editor.Document.Text, anchor.Offset);
this.Background = SystemColors.ControlBrush; this.Background = SystemColors.ControlBrush;
FocusFirstElement();
} }
protected virtual void FocusFirstElement() protected virtual void FocusFirstElement()
@ -104,6 +103,12 @@ namespace SharpRefactoring.Gui
void IActiveElement.OnInsertionCompleted() void IActiveElement.OnInsertionCompleted()
{ {
OnInsertionCompleted();
}
protected virtual void OnInsertionCompleted()
{
FocusFirstElement();
} }
void IActiveElement.Deactivate(SnippetEventArgs e) void IActiveElement.Deactivate(SnippetEventArgs e)
@ -111,6 +116,8 @@ namespace SharpRefactoring.Gui
if (e.Reason == DeactivateReason.ReturnPressed) if (e.Reason == DeactivateReason.ReturnPressed)
OKButtonClick(null, null); OKButtonClick(null, null);
context.TextArea.Caret.Offset = insertionEndAnchor.Offset;
Deactivate(); Deactivate();
} }

71
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs

@ -29,34 +29,17 @@ namespace SharpRefactoring.Gui
/// </summary> /// </summary>
public partial class InsertCtorDialog : AbstractInlineRefactorDialog public partial class InsertCtorDialog : AbstractInlineRefactorDialog
{ {
protected IList<CtorParamWrapper> paramList; IList<CtorParamWrapper> parameterList;
public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current) public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList<CtorParamWrapper> possibleParameters)
: base(context, editor, anchor) : base(context, editor, anchor)
{ {
InitializeComponent(); InitializeComponent();
this.varList.ItemsSource = paramList = CreateCtorParams(current.Fields, current.Properties) this.varList.ItemsSource = parameterList = possibleParameters;
// "Add check for null" is checked for every item by default
//Select(w => { if(w.IsNullable) w.AddCheckForNull = true; return w; }).
.ToList();
FocusFirstElement(); if (!parameterList.Any())
} Visibility = System.Windows.Visibility.Collapsed;
IEnumerable<CtorParamWrapper> CreateCtorParams(IEnumerable<IField> fields, IEnumerable<IProperty> properties)
{
int i = 0;
foreach (var f in fields) {
yield return new CtorParamWrapper(f) { Index = i, IsSelected = !f.IsReadonly };
i++;
}
foreach (var p in properties.Where(prop => prop.CanSet && !prop.IsIndexer)) {
yield return new CtorParamWrapper(p) { Index = i, IsSelected = !p.IsReadonly };
i++;
}
} }
protected override string GenerateCode(LanguageProperties language, IClass currentClass) protected override string GenerateCode(LanguageProperties language, IClass currentClass)
@ -65,7 +48,7 @@ namespace SharpRefactoring.Gui
string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset); string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset);
var filtered = paramList.Where(p => p.IsSelected).OrderBy(p => p.Index).ToList(); var filtered = parameterList.Where(p => p.IsSelected).OrderBy(p => p.Index).ToList();
BlockStatement block = new BlockStatement(); BlockStatement block = new BlockStatement();
@ -108,11 +91,11 @@ namespace SharpRefactoring.Gui
foreach (CtorParamWrapper w in filtered) foreach (CtorParamWrapper w in filtered)
block.AddChild(new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName)))); block.AddChild(new ExpressionStatement(new AssignmentExpression(new MemberReferenceExpression(new ThisReferenceExpression(), w.MemberName), AssignmentOperatorType.Assign, new IdentifierExpression(w.ParameterName))));
AnchorElement parameterList = context.ActiveElements AnchorElement parameterListElement = context.ActiveElements
.OfType<AnchorElement>() .OfType<AnchorElement>()
.FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase)); .FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase));
if (parameterList != null) { if (parameterListElement != null) {
StringBuilder pList = new StringBuilder(); StringBuilder pList = new StringBuilder();
var parameters = filtered var parameters = filtered
@ -125,7 +108,7 @@ namespace SharpRefactoring.Gui
pList.Append(language.CodeGenerator.GenerateCode(parameters[i], "")); pList.Append(language.CodeGenerator.GenerateCode(parameters[i], ""));
} }
parameterList.Text = pList.ToString(); parameterListElement.Text = pList.ToString();
} }
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@ -144,13 +127,13 @@ namespace SharpRefactoring.Gui
if (selection <= 0) if (selection <= 0)
return; return;
var curItem = paramList.First(p => p.Index == selection); var curItem = parameterList.First(p => p.Index == selection);
var exchangeItem = paramList.First(p => p.Index == selection - 1); var exchangeItem = parameterList.First(p => p.Index == selection - 1);
curItem.Index = selection - 1; curItem.Index = selection - 1;
exchangeItem.Index = selection; exchangeItem.Index = selection;
varList.ItemsSource = paramList.OrderBy(p => p.Index); varList.ItemsSource = parameterList.OrderBy(p => p.Index);
varList.SelectedIndex = selection - 1; varList.SelectedIndex = selection - 1;
} }
@ -158,16 +141,16 @@ namespace SharpRefactoring.Gui
{ {
int selection = varList.SelectedIndex; int selection = varList.SelectedIndex;
if (selection < 0 || selection >= paramList.Count - 1) if (selection < 0 || selection >= parameterList.Count - 1)
return; return;
var curItem = paramList.First(p => p.Index == selection); var curItem = parameterList.First(p => p.Index == selection);
var exchangeItem = paramList.First(p => p.Index == selection + 1); var exchangeItem = parameterList.First(p => p.Index == selection + 1);
curItem.Index = selection + 1; curItem.Index = selection + 1;
exchangeItem.Index = selection; exchangeItem.Index = selection;
varList.ItemsSource = paramList.OrderBy(p => p.Index); varList.ItemsSource = parameterList.OrderBy(p => p.Index);
varList.SelectedIndex = selection + 1; varList.SelectedIndex = selection + 1;
} }
@ -178,6 +161,9 @@ namespace SharpRefactoring.Gui
void TryFocusAndSelectItem() void TryFocusAndSelectItem()
{ {
if (!parameterList.Any())
return;
object ctorParamWrapper = varList.Items.GetItemAt(0); object ctorParamWrapper = varList.Items.GetItemAt(0);
if (ctorParamWrapper != null) { if (ctorParamWrapper != null) {
ListBoxItem item = (ListBoxItem)varList.ItemContainerGenerator.ContainerFromItem(ctorParamWrapper); ListBoxItem item = (ListBoxItem)varList.ItemContainerGenerator.ContainerFromItem(ctorParamWrapper);
@ -188,6 +174,25 @@ namespace SharpRefactoring.Gui
Keyboard.Focus(item); Keyboard.Focus(item);
} }
} }
protected override void OnInsertionCompleted()
{
base.OnInsertionCompleted();
Dispatcher.BeginInvoke(
DispatcherPriority.Background,
(Action)(
() => {
if (!parameterList.Any())
context.Deactivate(null);
else {
insertionEndAnchor = editor.Document.CreateAnchor(anchor.Offset);
insertionEndAnchor.MovementType = AnchorMovementType.AfterInsertion;
}
}
)
);
}
} }
[ValueConversion(typeof(int), typeof(bool))] [ValueConversion(typeof(int), typeof(bool))]

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

@ -6,6 +6,8 @@
// </file> // </file>
using System; using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.AvalonEdit.Snippets; using ICSharpCode.AvalonEdit.Snippets;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom;
@ -55,14 +57,31 @@ namespace SharpRefactoring
if (current == null) if (current == null)
return null; return null;
List<CtorParamWrapper> parameters = CreateCtorParams(current).ToList();
ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition); ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition);
anchor.MovementType = AnchorMovementType.BeforeInsertion; anchor.MovementType = AnchorMovementType.BeforeInsertion;
InsertCtorDialog dialog = new InsertCtorDialog(context, textEditor, anchor, current); InsertCtorDialog dialog = new InsertCtorDialog(context, textEditor, anchor, current, parameters);
dialog.Element = uiService.CreateInlineUIElement(anchor, dialog); dialog.Element = uiService.CreateInlineUIElement(anchor, dialog);
return dialog; return dialog;
} }
IEnumerable<CtorParamWrapper> CreateCtorParams(IClass sourceClass)
{
int i = 0;
foreach (var f in sourceClass.Fields) {
yield return new CtorParamWrapper(f) { Index = i, IsSelected = !f.IsReadonly };
i++;
}
foreach (var p in sourceClass.Properties.Where(prop => prop.CanSet && !prop.IsIndexer)) {
yield return new CtorParamWrapper(p) { Index = i, IsSelected = !p.IsReadonly };
i++;
}
}
} }
} }

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs

@ -24,7 +24,7 @@ namespace ICSharpCode.AvalonEdit.Snippets
SetCaret(context); SetCaret(context);
} }
internal static void SetCaret(InsertionContext context) public static void SetCaret(InsertionContext context)
{ {
TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition); TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition);
pos.SurviveDeletion = true; pos.SurviveDeletion = true;

Loading…
Cancel
Save