Browse Source

[CodeActions] Finished first implement interface implementation.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
286091be90
  1. 36
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceAction.cs
  2. 4
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceExplicitAction.cs
  3. 5
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceExplicitTests.cs
  4. 68
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.cs

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

@ -32,7 +32,7 @@ using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Refactoring namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
// [ContextAction("Implement interface", Description = "Creates an interface implementation.")] [ContextAction("Implement interface", Description = "Creates an interface implementation.")]
public class ImplementInterfaceAction : ICodeActionProvider public class ImplementInterfaceAction : ICodeActionProvider
{ {
public IEnumerable<CodeAction> GetActions(RefactoringContext context) public IEnumerable<CodeAction> GetActions(RefactoringContext context)
@ -48,9 +48,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var resolveResult = context.Resolve(type); var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface) if (resolveResult.Type.Kind != TypeKind.Interface)
yield break; yield break;
var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type, false); var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type, false);
if (toImplement.Count == 0) if (toImplement.Count == 0)
yield break; yield break;
yield return new CodeAction(context.TranslateString("Implement interface"), script => { yield return new CodeAction(context.TranslateString("Implement interface"), script => {
script.InsertWithCursor( script.InsertWithCursor(
context.TranslateString ("Implement Interface"), context.TranslateString ("Implement Interface"),
@ -60,7 +62,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}); });
} }
public static IEnumerable<AstNode> GenerateImplementation(RefactoringContext context, List<Tuple<IMember, bool>> toImplement) public static IEnumerable<AstNode> GenerateImplementation(RefactoringContext context, IEnumerable<Tuple<IMember, bool>> toImplement)
{ {
var nodes = new Dictionary<IType, List<AstNode>>(); var nodes = new Dictionary<IType, List<AstNode>>();
@ -116,6 +118,29 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
foreach (var typeParam in method.TypeParameters) { foreach (var typeParam in method.TypeParameters) {
result.TypeParameters.Add(new TypeParameterDeclaration(typeParam.Name)); result.TypeParameters.Add(new TypeParameterDeclaration(typeParam.Name));
var constraint = new Constraint() {
TypeParameter = new SimpleType(typeParam.Name)
};
if (typeParam.HasDefaultConstructorConstraint) {
constraint.BaseTypes.Add (new PrimitiveType("new"));
} else if (typeParam.HasReferenceTypeConstraint) {
constraint.BaseTypes.Add (new PrimitiveType("class"));
} else if (typeParam.HasValueTypeConstraint) {
constraint.BaseTypes.Add (new PrimitiveType("struct"));
}
foreach (var type in typeParam.DirectBaseTypes) {
if (type.FullName == "System.Object")
continue;
if (type.FullName == "System.ValueType")
continue;
constraint.BaseTypes.Add (context.CreateShortType (type));
}
if (constraint.BaseTypes.Count == 0)
continue;
result.Constraints.Add (constraint);
} }
foreach (var p in method.Parameters) { foreach (var p in method.Parameters) {
@ -141,6 +166,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var def = interfaceType.GetDefinition(); var def = interfaceType.GetDefinition();
List<Tuple<IMember, bool>> toImplement = new List<Tuple<IMember, bool>>(); List<Tuple<IMember, bool>> toImplement = new List<Tuple<IMember, bool>>();
bool alreadyImplemented; bool alreadyImplemented;
// Stub out non-implemented events defined by @iface // Stub out non-implemented events defined by @iface
foreach (var ev in interfaceType.GetEvents (e => !e.IsSynthetic && e.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) { foreach (var ev in interfaceType.GetEvents (e => !e.IsSynthetic && e.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) {
bool needsExplicitly = explicitly; bool needsExplicitly = explicitly;
@ -151,9 +177,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!alreadyImplemented) if (!alreadyImplemented)
toImplement.Add(new Tuple<IMember, bool>(ev, needsExplicitly)); toImplement.Add(new Tuple<IMember, bool>(ev, needsExplicitly));
} }
Console.WriteLine("foo!!!!");
// Stub out non-implemented methods defined by @iface // Stub out non-implemented methods defined by @iface
foreach (var method in interfaceType.GetMethods (d => !d.IsSynthetic && d.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) { foreach (var method in interfaceType.GetMethods (d => !d.IsSynthetic /* && d.DeclaringTypeDefinition.ReflectionName == def.ReflectionName*/)) {
bool needsExplicitly = explicitly; bool needsExplicitly = explicitly;
alreadyImplemented = false; alreadyImplemented = false;
@ -165,6 +191,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
alreadyImplemented |= !needsExplicitly /*|| cmet.InterfaceImplementations.Any (impl => impl.InterfaceType.Equals (interfaceType))*/; alreadyImplemented |= !needsExplicitly /*|| cmet.InterfaceImplementations.Any (impl => impl.InterfaceType.Equals (interfaceType))*/;
} }
} }
if (toImplement.Where (t => t.Item1 is IMethod).Any (t => CompareMethods (method, (IMethod)t.Item1)))
needsExplicitly = true;
if (!alreadyImplemented) if (!alreadyImplemented)
toImplement.Add(new Tuple<IMember, bool>(method, needsExplicitly)); toImplement.Add(new Tuple<IMember, bool>(method, needsExplicitly));
} }

4
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ImplementInterfaceExplicitAction.cs

@ -32,7 +32,7 @@ using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Refactoring namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
// [ContextAction("Implement interface explicit", Description = "Creates an interface implementation.")] [ContextAction("Implement interface explicit", Description = "Creates an interface implementation.")]
public class ImplementInterfaceExplicitAction : ICodeActionProvider public class ImplementInterfaceExplicitAction : ICodeActionProvider
{ {
public IEnumerable<CodeAction> GetActions(RefactoringContext context) public IEnumerable<CodeAction> GetActions(RefactoringContext context)
@ -60,7 +60,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
script.InsertWithCursor( script.InsertWithCursor(
context.TranslateString("Implement Interface"), context.TranslateString("Implement Interface"),
state.CurrentTypeDefinition, state.CurrentTypeDefinition,
ImplementInterfaceAction.GenerateImplementation (context, toImplement) ImplementInterfaceAction.GenerateImplementation (context, toImplement.Select (t => Tuple.Create (t.Item1, true)))
); );
}); });
} }

5
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceExplicitTests.cs

@ -29,7 +29,6 @@ using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions namespace ICSharpCode.NRefactory.CSharp.CodeActions
{ {
[Ignore("TODO")]
[TestFixture] [TestFixture]
public class ImplementInterfaceExplicitTests : ContextActionTestBase public class ImplementInterfaceExplicitTests : ContextActionTestBase
{ {
@ -44,9 +43,9 @@ class Foo : $IDisposable
class Foo : IDisposable class Foo : IDisposable
{ {
#region IDisposable implementation #region IDisposable implementation
void IDisposable.Dispose() void IDisposable.Dispose ()
{ {
throw new NotImplementedException(); throw new NotImplementedException ();
} }
#endregion #endregion
} }

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

@ -56,7 +56,6 @@ class Foo : IDisposable
/// <summary> /// <summary>
/// Bug 663842 - Interface implementation does not include constraints /// Bug 663842 - Interface implementation does not include constraints
/// </summary> /// </summary>
[Ignore("TODO")]
[Test()] [Test()]
public void TestBug663842() public void TestBug663842()
{ {
@ -82,21 +81,21 @@ interface ITest {
class Foo : ITest class Foo : ITest
{ {
#region ITest implementation #region ITest implementation
public void MyMethod1<T> (T t) where T : new () public void MyMethod1<T> (T t) where T : new()
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
public void MyMethod2<T> (T t) where T : class public void MyMethod2<T> (T t) where T : class
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
public void MyMethod3<T> (T t) where T : struct public void MyMethod3<T> (T t) where T : struct
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
public void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider public void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
#endregion #endregion
} }
@ -206,7 +205,6 @@ class Foo : ITest
/// <summary> /// <summary>
/// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly /// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly
/// </summary> /// </summary>
[Ignore("TODO")]
[Test()] [Test()]
public void TestBug3365() public void TestBug3365()
{ {
@ -238,20 +236,68 @@ public interface ITest : IA, IEnumerable
class Foo : ITest class Foo : ITest
{ {
#region ITest implementation #region IA implementation
public bool GetEnumerator () public bool GetEnumerator ()
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
#endregion #endregion
#region IEnumerable implementation #region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () IEnumerator IEnumerable.GetEnumerator ()
{ {
throw new System.NotImplementedException (); throw new NotImplementedException ();
} }
#endregion #endregion
}"); }");
} }
/// <summary>
/// Bug 4818 - Implement implicit does not handle 'params' types
/// </summary>
[Test()]
public void TestBug4818()
{
Test<ImplementInterfaceAction>(@"using System;
interface ITest {
void OnScenesAdded (params ITest[] scenes);
}
class Foo : $ITest
{
}
", @"using System;
interface ITest {
void OnScenesAdded (params ITest[] scenes);
}
class Foo : ITest
{
#region ITest implementation
public void OnScenesAdded (params ITest[] scenes)
{
throw new NotImplementedException ();
}
#endregion
}
");
TestWrongContext<ImplementInterfaceAction>(@"using System;
interface ITest {
void OnScenesAdded (params ITest[] scenes);
}
class Foo : $ITest
{
#region ITest implementation
public void OnScenesAdded (params ITest[] scenes)
{
throw new NotImplementedException ();
}
#endregion
}
");
}
} }
} }

Loading…
Cancel
Save