Browse Source

[CodeActions] Worked on implement interface.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
cf9d360be7
  1. 10
      ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/PreProcessorDirective.cs
  2. 20
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs
  3. 27
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs
  4. 2
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

10
ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/PreProcessorDirective.cs

@ -86,13 +86,19 @@ namespace ICSharpCode.NRefactory.CSharp @@ -86,13 +86,19 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
public PreProcessorDirective (PreProcessorDirectiveType type, TextLocation startLocation, TextLocation endLocation)
public PreProcessorDirective(PreProcessorDirectiveType type, TextLocation startLocation, TextLocation endLocation)
{
this.Type = type;
this.startLocation = startLocation;
this.endLocation = endLocation;
}
public PreProcessorDirective(PreProcessorDirectiveType type, string argument = null)
{
this.Type = type;
this.Argument = argument;
}
public override void AcceptVisitor (IAstVisitor visitor)
{
visitor.VisitPreProcessorDirective (this);

20
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs

@ -40,6 +40,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -40,6 +40,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
@ -47,11 +48,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -47,11 +48,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface)
yield break;
var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type, false);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(context.TranslateString("Implement interface"), script => {
script.InsertWithCursor(
context.TranslateString ("Implement Interface"),
@ -63,8 +62,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -63,8 +62,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public static IEnumerable<AstNode> GenerateImplementation(RefactoringContext context, List<Tuple<IMember, bool>> toImplement)
{
var nodes = new Dictionary<IType, List<AstNode>>();
foreach (var member in toImplement) {
yield return GenerateMemberImplementation(context, member);
if (!nodes.ContainsKey(member.Item1.DeclaringType))
nodes [member.Item1.DeclaringType] = new List<AstNode>();
nodes [member.Item1.DeclaringType].Add(GenerateMemberImplementation(context, member));
}
foreach (var kv in nodes) {
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Region,
string.Format("{0} implementation", kv.Key.Name));
foreach (var member in kv.Value)
yield return member;
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Endregion
);
}
}

27
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs

@ -29,7 +29,6 @@ using ICSharpCode.NRefactory.CSharp.Refactoring; @@ -29,7 +29,6 @@ using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions
{
[Ignore("TODO")]
[TestFixture]
public class ImplementInterfaceTests : ContextActionTestBase
{
@ -44,9 +43,9 @@ class Foo : $IDisposable @@ -44,9 +43,9 @@ class Foo : $IDisposable
class Foo : IDisposable
{
#region IDisposable implementation
public void Dispose()
public void Dispose ()
{
throw new NotImplementedException();
throw new NotImplementedException ();
}
#endregion
}
@ -57,6 +56,7 @@ class Foo : IDisposable @@ -57,6 +56,7 @@ class Foo : IDisposable
/// <summary>
/// Bug 663842 - Interface implementation does not include constraints
/// </summary>
[Ignore("TODO")]
[Test()]
public void TestBug663842()
{
@ -79,24 +79,21 @@ interface ITest { @@ -79,24 +79,21 @@ interface ITest {
void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider;
}
class Foo : $ITest
class Foo : ITest
{
#region ITest implementation
public void MyMethod1<T> (T t) where T : new ()
{
throw new System.NotImplementedException ();
}
public void MyMethod2<T> (T t) where T : class
{
throw new System.NotImplementedException ();
}
public void MyMethod3<T> (T t) where T : struct
{
throw new System.NotImplementedException ();
}
public void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider
{
throw new System.NotImplementedException ();
@ -119,8 +116,7 @@ class Foo : $ITest @@ -119,8 +116,7 @@ class Foo : $ITest
class Foo : $ITest
{
}
", @"interface ITest {
}", @"interface ITest {
void M1();
void M1(int x);
}
@ -132,7 +128,6 @@ class Foo : ITest @@ -132,7 +128,6 @@ class Foo : ITest
{
throw new System.NotImplementedException ();
}
public void M1 (int x)
{
throw new System.NotImplementedException ();
@ -167,7 +162,6 @@ class Foo : ITest @@ -167,7 +162,6 @@ class Foo : ITest
{
throw new System.NotImplementedException ();
}
public void Inc (string message)
{
throw new System.NotImplementedException ();
@ -191,28 +185,28 @@ class Foo : ITest @@ -191,28 +185,28 @@ class Foo : ITest
class Foo : $ITest
{
public void Method2 () {}
public void Method2 () {}
}", @"interface ITest {
void Method1 ();
void Method2 ();
}
class Foo : $ITest
class Foo : ITest
{
public void Method2 () {}
#region ITest implementation
public void Method1 ()
{
throw new System.NotImplementedException ();
}
#endregion
public void Method2 () {}
}");
}
/// <summary>
/// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly
/// </summary>
[Ignore("TODO")]
[Test()]
public void TestBug3365()
{
@ -242,7 +236,7 @@ public interface ITest : IA, IEnumerable @@ -242,7 +236,7 @@ public interface ITest : IA, IEnumerable
{
}
class Foo : $ITest
class Foo : ITest
{
#region ITest implementation
public bool GetEnumerator ()
@ -250,7 +244,6 @@ class Foo : $ITest @@ -250,7 +244,6 @@ class Foo : $ITest
throw new System.NotImplementedException ();
}
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{

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

@ -111,7 +111,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -111,7 +111,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
var insertType = unit.GetNodeAt<TypeDeclaration> (parentType.Region.Begin);
var startOffset = GetCurrentOffset (insertType.LBraceToken.EndLocation);
foreach (var node in nodes) {
foreach (var node in nodes.Reverse ()) {
var output = OutputNode (1, node, true);
InsertText (startOffset, output.Text);
output.RegisterTrackedSegments (this, startOffset);

Loading…
Cancel
Save