Browse Source

Revert "Simplified the scrip API a bit (insert cursor/links are no longer task"

I do it different.

This reverts commit 4fa3c33d6b.
pull/32/merge
Mike Krüger 12 years ago
parent
commit
1c86d72523
  1. 22
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs
  2. 20
      ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs
  3. 3
      ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs
  4. 40
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  5. 1
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
  6. 23
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

22
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs

@ -100,10 +100,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -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<Task> 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 @@ -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<Task> replaceStatements = delegate {
foreach (var node in statements.Skip (1)) {
if (node is NewLineNode)
continue;
@ -212,7 +221,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -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);
}
}

20
ICSharpCode.NRefactory.CSharp/Refactoring/CreateChangedEvent.cs

@ -30,7 +30,6 @@ using ICSharpCode.NRefactory.Semantics; @@ -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 @@ -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);
}

3
ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs

@ -105,14 +105,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -105,14 +105,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var segments = new List<ISegment>();
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);

40
ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

@ -177,17 +177,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -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<object>();
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 @@ -238,36 +238,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
End
}
public virtual void InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable<AstNode> node, Action continuation = null)
public virtual Task InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable<AstNode> node)
{
throw new NotImplementedException();
}
public virtual void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable<AstNode> node, Action continuation = null)
public virtual Task InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable<AstNode> node)
{
throw new NotImplementedException();
}
public void InsertWithCursor(string operation, InsertPosition defaultPosition, params AstNode[] nodes)
{
InsertWithCursor(operation, defaultPosition, (IEnumerable<AstNode>)nodes, null);
}
public void InsertWithCursor(string operation, ITypeDefinition parentType, params AstNode[] nodes)
{
InsertWithCursor(operation, parentType, (IEnumerable<AstNode>)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<AstNode>)nodes, continuation);
return InsertWithCursor(operation, defaultPosition, (IEnumerable<AstNode>)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<AstNode>)nodes, continuation);
return InsertWithCursor(operation, parentType, (IEnumerable<AstNode>)nodes);
}
protected virtual int GetIndentLevelAt (int offset)
{
return 0;

1
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs

@ -30,7 +30,6 @@ using ICSharpCode.NRefactory.CSharp.Refactoring; @@ -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
{

23
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

@ -92,28 +92,28 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -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<AstNode> nodes, Action continuation)
public override Task InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable<AstNode> nodes)
{
var entity = context.GetNode<EntityDeclaration>();
foreach (var node in nodes) {
InsertBefore(entity, node);
}
if (continuation != null)
continuation();
var tcs = new TaskCompletionSource<object> ();
tcs.SetResult (null);
return tcs.Task;
}
public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable<AstNode> nodes, Action continuation)
public override Task InsertWithCursor (string operation, ITypeDefinition parentType, IEnumerable<AstNode> nodes)
{
var unit = context.RootNode;
var insertType = unit.GetNodeAt<TypeDeclaration> (parentType.Region.Begin);
@ -128,8 +128,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -128,8 +128,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
}
output.RegisterTrackedSegments (this, startOffset);
}
if (continuation != null)
continuation();
var tcs = new TaskCompletionSource<object> ();
tcs.SetResult (null);
return tcs.Task;
}
void Rename (AstNode node, string newName)

Loading…
Cancel
Save