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 @@ -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 @@ -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 @@ -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 @@ -111,6 +116,8 @@ namespace SharpRefactoring.Gui
if (e.Reason == DeactivateReason.ReturnPressed)
OKButtonClick(null, null);
context.TextArea.Caret.Offset = insertionEndAnchor.Offset;
Deactivate();
}

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

@ -29,34 +29,17 @@ namespace SharpRefactoring.Gui @@ -29,34 +29,17 @@ namespace SharpRefactoring.Gui
/// </summary>
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)
{
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<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++;
}
if (!parameterList.Any())
Visibility = System.Windows.Visibility.Collapsed;
}
protected override string GenerateCode(LanguageProperties language, IClass currentClass)
@ -65,7 +48,7 @@ namespace SharpRefactoring.Gui @@ -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 @@ -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<AnchorElement>()
.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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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))]

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

@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
// </file>
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 @@ -55,14 +57,31 @@ namespace SharpRefactoring
if (current == null)
return null;
List<CtorParamWrapper> 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<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 @@ -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;

Loading…
Cancel
Save