From f4b26c61fb0f5d452a00bc8c5db0625377600771 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 2 Sep 2010 10:12:52 +0200 Subject: [PATCH] fixed cursor positioning after finishing InsertCtorDialog, display no dialog if no fields are available --- .../Src/Gui/AbstractInlineRefactorDialog.cs | 13 +++- .../Project/Src/Gui/InsertCtorDialog.xaml.cs | 71 ++++++++++--------- .../Src/InsertCtorSnippetRefactoring.cs | 21 +++++- .../Snippets/SnippetCaretElement.cs | 2 +- 4 files changed, 69 insertions(+), 38 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs index c8a9435601..b197d04339 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/AbstractInlineRefactorDialog.cs @@ -25,6 +25,7 @@ namespace SharpRefactoring.Gui public abstract class AbstractInlineRefactorDialog : GroupBox, IOptionBindingContainer, IActiveElement { protected ITextAnchor anchor; + protected ITextAnchor insertionEndAnchor; protected ITextEditor editor; ClassFinder classFinderContext; @@ -34,15 +35,13 @@ namespace SharpRefactoring.Gui protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor) { - this.anchor = anchor; + this.anchor = insertionEndAnchor = anchor; this.editor = editor; this.context = context; this.classFinderContext = new ClassFinder(ParserService.ParseCurrentViewContent(), editor.Document.Text, anchor.Offset); this.Background = SystemColors.ControlBrush; - - FocusFirstElement(); } protected virtual void FocusFirstElement() @@ -104,6 +103,12 @@ namespace SharpRefactoring.Gui void IActiveElement.OnInsertionCompleted() { + OnInsertionCompleted(); + } + + protected virtual void OnInsertionCompleted() + { + FocusFirstElement(); } void IActiveElement.Deactivate(SnippetEventArgs e) @@ -111,6 +116,8 @@ namespace SharpRefactoring.Gui if (e.Reason == DeactivateReason.ReturnPressed) OKButtonClick(null, null); + context.TextArea.Caret.Offset = insertionEndAnchor.Offset; + Deactivate(); } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs index 5f1265c153..4fcf50d034 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs @@ -29,34 +29,17 @@ namespace SharpRefactoring.Gui /// public partial class InsertCtorDialog : AbstractInlineRefactorDialog { - protected IList paramList; + IList parameterList; - public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current) + public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList possibleParameters) : base(context, editor, anchor) { InitializeComponent(); - this.varList.ItemsSource = paramList = CreateCtorParams(current.Fields, current.Properties) - // "Add check for null" is checked for every item by default - //Select(w => { if(w.IsNullable) w.AddCheckForNull = true; return w; }). - .ToList(); + this.varList.ItemsSource = parameterList = possibleParameters; - FocusFirstElement(); - } - - IEnumerable CreateCtorParams(IEnumerable fields, IEnumerable 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++; - } + if (!parameterList.Any()) + Visibility = System.Windows.Visibility.Collapsed; } protected override string GenerateCode(LanguageProperties language, IClass currentClass) @@ -65,7 +48,7 @@ namespace SharpRefactoring.Gui 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(); @@ -108,11 +91,11 @@ namespace SharpRefactoring.Gui foreach (CtorParamWrapper w in filtered) 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() .FirstOrDefault(item => item.Name.Equals("parameterList", StringComparison.OrdinalIgnoreCase)); - if (parameterList != null) { + if (parameterListElement != null) { StringBuilder pList = new StringBuilder(); var parameters = filtered @@ -125,7 +108,7 @@ namespace SharpRefactoring.Gui pList.Append(language.CodeGenerator.GenerateCode(parameters[i], "")); } - parameterList.Text = pList.ToString(); + parameterListElement.Text = pList.ToString(); } StringBuilder builder = new StringBuilder(); @@ -144,13 +127,13 @@ namespace SharpRefactoring.Gui if (selection <= 0) return; - var curItem = paramList.First(p => p.Index == selection); - var exchangeItem = paramList.First(p => p.Index == selection - 1); + var curItem = parameterList.First(p => p.Index == selection); + var exchangeItem = parameterList.First(p => p.Index == selection - 1); curItem.Index = selection - 1; exchangeItem.Index = selection; - varList.ItemsSource = paramList.OrderBy(p => p.Index); + varList.ItemsSource = parameterList.OrderBy(p => p.Index); varList.SelectedIndex = selection - 1; } @@ -158,16 +141,16 @@ namespace SharpRefactoring.Gui { int selection = varList.SelectedIndex; - if (selection < 0 || selection >= paramList.Count - 1) + if (selection < 0 || selection >= parameterList.Count - 1) return; - var curItem = paramList.First(p => p.Index == selection); - var exchangeItem = paramList.First(p => p.Index == selection + 1); + var curItem = parameterList.First(p => p.Index == selection); + var exchangeItem = parameterList.First(p => p.Index == selection + 1); curItem.Index = selection + 1; exchangeItem.Index = selection; - varList.ItemsSource = paramList.OrderBy(p => p.Index); + varList.ItemsSource = parameterList.OrderBy(p => p.Index); varList.SelectedIndex = selection + 1; } @@ -178,6 +161,9 @@ namespace SharpRefactoring.Gui void TryFocusAndSelectItem() { + if (!parameterList.Any()) + return; + object ctorParamWrapper = varList.Items.GetItemAt(0); if (ctorParamWrapper != null) { ListBoxItem item = (ListBoxItem)varList.ItemContainerGenerator.ContainerFromItem(ctorParamWrapper); @@ -188,6 +174,25 @@ namespace SharpRefactoring.Gui 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))] diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs index 7bcfb34962..761ee55bc6 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/InsertCtorSnippetRefactoring.cs @@ -6,6 +6,8 @@ // using System; +using System.Collections.Generic; +using System.Linq; using ICSharpCode.AvalonEdit.Snippets; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom; @@ -55,14 +57,31 @@ namespace SharpRefactoring if (current == null) return null; + List parameters = CreateCtorParams(current).ToList(); + ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition); 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); return dialog; } + + IEnumerable 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++; + } + } } } diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs index 2d11e4814b..d0532892e9 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs @@ -24,7 +24,7 @@ namespace ICSharpCode.AvalonEdit.Snippets SetCaret(context); } - internal static void SetCaret(InsertionContext context) + public static void SetCaret(InsertionContext context) { TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition); pos.SurviveDeletion = true;