From 1c86d7252300cb05a4612d2448e5d9165f0e114c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Mon, 22 Apr 2013 16:17:11 +0200 Subject: [PATCH] Revert "Simplified the scrip API a bit (insert cursor/links are no longer task" I do it different. This reverts commit 4fa3c33d6baf244f862ca08b824d6094a75d0f6a. --- .../ExtractMethod/ExtractMethodAction.cs | 22 ++++++++-- .../Refactoring/CreateChangedEvent.cs | 20 +++++----- .../Refactoring/DocumentScript.cs | 3 +- .../Refactoring/Script.cs | 40 +++++++------------ .../CodeActions/ContextActionTestBase.cs | 1 - .../CodeActions/TestRefactoringContext.cs | 23 ++++++----- 6 files changed, 55 insertions(+), 54 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs index 28dc68069b..a46ad59071 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs @@ -100,10 +100,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod invocation.Arguments.Add(argumentExpression); } - script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, delegate { + var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method); + + Action replaceStatements = delegate { script.Replace(expression, invocation); script.Link(target, method.NameToken); - }, method); + }; + + if (task.IsCompleted) { + replaceStatements (null); + } else { + task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ()); + } }, expression); } @@ -187,7 +195,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod)); invocation.Arguments.Add(argumentExpression); } - Action replaceStatements = delegate { + var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method); + Action replaceStatements = delegate { foreach (var node in statements.Skip (1)) { if (node is NewLineNode) continue; @@ -212,7 +221,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod script.Link(target, method.NameToken); }; - script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, replaceStatements, method); + + if (task.IsCompleted) { + replaceStatements (null); + } else { + task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ()); + } }, statements.First ().StartLocation, statements.Last ().EndLocation); } } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs index 051a6cc903..714256373b 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs @@ -30,7 +30,6 @@ using ICSharpCode.NRefactory.Semantics; using ICSharpCode.NRefactory.TypeSystem; using System.Threading; using System.Collections.Generic; -using System.Threading.Tasks; namespace ICSharpCode.NRefactory.CSharp.Refactoring { @@ -51,19 +50,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring yield return new CodeAction(context.TranslateString("Create changed event"), script => { var eventDeclaration = CreateChangedEventDeclaration (context, property); var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator (context, type, eventDeclaration, eventDeclaration.Variables.First (), resolvedType.GetDelegateInvokeMethod (), false); + var stmt = new ExpressionStatement (new InvocationExpression ( + new IdentifierExpression (methodDeclaration.Name), + new MemberReferenceExpression (new TypeReferenceExpression (context.CreateShortType("System", "EventArgs")), "Empty") + )); script.InsertWithCursor( context.TranslateString("Create event invocator"), Script.InsertPosition.After, - new AstNode[] { eventDeclaration, methodDeclaration }, - delegate { - var stmt = new ExpressionStatement (new InvocationExpression ( - new IdentifierExpression (methodDeclaration.Name), - new MemberReferenceExpression (new TypeReferenceExpression (context.CreateShortType("System", "EventArgs")), "Empty") - )); - script.InsertBefore (property.Setter.Body.RBraceToken, stmt); - script.FormatText ((AstNode)property.Setter.Body); - } - ); + new AstNode[] { eventDeclaration, methodDeclaration } + ).ContinueWith (delegate { + script.InsertBefore (property.Setter.Body.RBraceToken, stmt); + script.FormatText (stmt); + }); }, property.NameToken); } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs index 95564481ca..d4c11cf2d4 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs @@ -105,14 +105,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring var segments = new List(); foreach (var node in nodes.OrderByDescending (n => n.StartLocation)) { var segment = GetSegment(node); + formatter.AddFormattingRegion (new ICSharpCode.NRefactory.TypeSystem.DomRegion ( currentDocument.GetLocation (segment.Offset), currentDocument.GetLocation (segment.EndOffset) )); - Console.WriteLine(segment +"/"+currentDocument.GetText (segment)); segments.Add(segment); } - Console.WriteLine(segments.Count); if (segments.Count == 0) return; var changes = formatter.AnalyzeFormatting (currentDocument, syntaxTree); diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs index 32c5da1140..3c9015958a 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs @@ -177,17 +177,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring CorrectFormatting (null, newNode); } - public void Link (params AstNode[] nodes) - { - Link(null, nodes); - } - - public virtual void Link (Action continuation, params AstNode[] nodes) + public virtual Task Link (params AstNode[] nodes) { // Default implementation: do nothing // Derived classes are supposed to enter the text editor's linked state. + + // Immediately signal the task as completed: + var tcs = new TaskCompletionSource(); + tcs.SetResult(null); + return tcs.Task; } - + public void Replace (AstNode node, AstNode replaceWith) { var segment = GetSegment (node); @@ -238,36 +238,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring End } - public virtual void InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable node, Action continuation = null) + public virtual Task InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable node) { throw new NotImplementedException(); } - public virtual void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable node, Action continuation = null) + public virtual Task InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable node) { throw new NotImplementedException(); } - public void InsertWithCursor(string operation, InsertPosition defaultPosition, params AstNode[] nodes) - { - InsertWithCursor(operation, defaultPosition, (IEnumerable)nodes, null); - } - - public void InsertWithCursor(string operation, ITypeDefinition parentType, params AstNode[] nodes) - { - InsertWithCursor(operation, parentType, (IEnumerable)nodes, null); - } - - public void InsertWithCursor(string operation, InsertPosition defaultPosition, Action continuation, params AstNode[] nodes) + public Task InsertWithCursor(string operation, InsertPosition defaultPosition, params AstNode[] nodes) { - InsertWithCursor(operation, defaultPosition, (IEnumerable)nodes, continuation); + return InsertWithCursor(operation, defaultPosition, (IEnumerable)nodes); } - - public void InsertWithCursor(string operation, ITypeDefinition parentType, Action continuation, params AstNode[] nodes) + + public Task InsertWithCursor(string operation, ITypeDefinition parentType, params AstNode[] nodes) { - InsertWithCursor(operation, parentType, (IEnumerable)nodes, continuation); + return InsertWithCursor(operation, parentType, (IEnumerable)nodes); } - + protected virtual int GetIndentLevelAt (int offset) { return 0; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs index 1a6706ba73..8ebd281f79 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs @@ -30,7 +30,6 @@ using ICSharpCode.NRefactory.CSharp.Refactoring; using System.Threading; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace ICSharpCode.NRefactory.CSharp.CodeActions { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs index 803c0ed849..bd34adcd4a 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs @@ -92,28 +92,28 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions { this.context = context; } - - public override void Link(Action continuation, params AstNode[] nodes) + + public override Task Link (params AstNode[] nodes) { // check that all links are valid. foreach (var node in nodes) { Assert.IsNotNull (GetSegment (node)); } - if (continuation != null) - continuation(); + return new Task (() => {}); } - - public override void InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable nodes, Action continuation) + + public override Task InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable nodes) { var entity = context.GetNode(); foreach (var node in nodes) { InsertBefore(entity, node); } - if (continuation != null) - continuation(); + var tcs = new TaskCompletionSource (); + tcs.SetResult (null); + return tcs.Task; } - public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable nodes, Action continuation) + public override Task InsertWithCursor (string operation, ITypeDefinition parentType, IEnumerable nodes) { var unit = context.RootNode; var insertType = unit.GetNodeAt (parentType.Region.Begin); @@ -128,8 +128,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions } output.RegisterTrackedSegments (this, startOffset); } - if (continuation != null) - continuation(); + var tcs = new TaskCompletionSource (); + tcs.SetResult (null); + return tcs.Task; } void Rename (AstNode node, string newName)