From 9ce0d70a81c96966f02b83313575272462fcf2ec Mon Sep 17 00:00:00 2001 From: Andreas Weizel Date: Mon, 24 Jun 2013 00:46:20 +0200 Subject: [PATCH] Closing of AbstractInlineRefactorDialog-based dialogs on Undo command is now handled without faking input. --- .../AbstractInlineRefactorDialog.cs | 33 ++++++++++++++++++- .../Src/Refactoring/CreateProperties.cs | 10 +++--- .../CreatePropertiesDialog.xaml.cs | 10 ++++-- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs index 624570233c..c343f0a2a0 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/AbstractInlineRefactorDialog.cs @@ -3,18 +3,21 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; +using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Snippets; using ICSharpCode.Core.Presentation; using ICSharpCode.NRefactory.CSharp; using ICSharpCode.NRefactory.CSharp.Refactoring; using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.TypeSystem; +using ICSharpCode.SharpDevelop; using CSharpBinding.Refactoring; using ICSharpCode.SharpDevelop.Editor; @@ -124,6 +127,29 @@ namespace CSharpBinding.Refactoring protected virtual void Initialize() { this.refactoringContext = SDRefactoringContext.Create(editor, CancellationToken.None); + + TextDocument textDocument = editor.Document as TextDocument; + if (textDocument != null) { + textDocument.UndoStack.PropertyChanged += UndoStackPropertyChanged; + } + } + + void UndoStackPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == "CanRedo") { + // Undo command has been triggered? + OnUndoTriggered(); + + // Unregister from event, again + TextDocument textDocument = editor.Document as TextDocument; + if (textDocument != null) { + textDocument.UndoStack.PropertyChanged -= UndoStackPropertyChanged; + } + } + } + + protected virtual void OnUndoTriggered() + { } protected virtual void OnInsertionCompleted() @@ -139,6 +165,11 @@ namespace CSharpBinding.Refactoring return; } + TextDocument textDocument = editor.Document as TextDocument; + if (textDocument != null) { + textDocument.UndoStack.PropertyChanged -= UndoStackPropertyChanged; + } + if (e.Reason == DeactivateReason.ReturnPressed) OKButtonClick(null, null); @@ -150,7 +181,7 @@ namespace CSharpBinding.Refactoring bool deactivated; - void Deactivate() + protected void Deactivate() { if (Element == null) throw new InvalidOperationException("no IInlineUIElement set!"); diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs index 7a35a6aba5..6a9b7b000c 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreateProperties.cs @@ -35,13 +35,13 @@ namespace CSharpBinding.Refactoring return null; ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition); - anchor.MovementType = AnchorMovementType.AfterInsertion; + anchor.MovementType = AnchorMovementType.BeforeInsertion; // Since this snippet doesn't insert anything, fake insertion of 1 character to allow proper Ctrl+Z reaction - if (context.StartPosition == context.InsertionPosition) { - textEditor.Document.Insert(context.InsertionPosition, " "); - context.InsertionPosition++; - } +// if (context.StartPosition == context.InsertionPosition) { +// textEditor.Document.Insert(context.InsertionPosition, " "); +// context.InsertionPosition++; +// } CreatePropertiesDialog dialog = new CreatePropertiesDialog(context, textEditor, anchor); diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs index 48f41480fb..0bab18417e 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CreatePropertiesDialog.xaml.cs @@ -56,6 +56,14 @@ namespace CSharpBinding.Refactoring SelectAllUnchecked(); } + protected override void OnUndoTriggered() + { + base.OnUndoTriggered(); + + // Close this dialog on Undo + Deactivate(); + } + static IEnumerable FindFields(IType sourceClass) { int i = 0; @@ -89,7 +97,6 @@ namespace CSharpBinding.Refactoring if (!hasOnPropertyChanged) { var nodes = new List(); if (!currentClass.GetAllBaseTypeDefinitions().Any(bt => bt.FullName == "System.ComponentModel.INotifyPropertyChanged")) { -// int insertion = editor.Document.GetOffset(currentClass.BodyRegion.BeginLine, currentClass.BodyRegion.BeginColumn); AstNode nodeBeforeClassBlock = currentClassDeclaration.LBraceToken; if (nodeBeforeClassBlock.PrevSibling is NewLineNode) { // There's a new line before the brace, insert before it! @@ -99,7 +106,6 @@ namespace CSharpBinding.Refactoring AstType interfaceTypeNode = refactoringContext.CreateShortType("System.ComponentModel", "INotifyPropertyChanged", 0); var directBaseTypes = currentClass.DirectBaseTypes.Where(t => t.FullName != "System.Object"); -// if ((directBaseTypes != null) && (directBaseTypes.Count() > 0)) { if (currentClassDeclaration.BaseTypes.Count > 0) { script.InsertText(insertion, ", " + interfaceTypeNode.GetText() + " "); } else {