Browse Source

Fixed object creation completion issue.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
45c8d0eb50
  1. 16
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 28
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs

16
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

@ -1570,29 +1570,31 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -1570,29 +1570,31 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
var state = GetState();
Func<IType, IType> pred = null;
if (hintType != null) {
if (hintType.Kind != TypeKind.Unknown) {
var lookup = new MemberLookup(ctx.CurrentTypeDefinition, Compilation.MainAssembly);
pred = t => {
// check if type is in inheritance tree.
if (hintType.GetDefinition() != null && !t.GetDefinition().IsDerivedFrom(hintType.GetDefinition())) {
return null;
}
// if (hintType.GetDefinition() != null && !t.GetDefinition().IsDerivedFrom(hintType.GetDefinition())) {
// return null;
//}
if (t.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array) {
return null;
}
// check for valid constructors
if (t.GetConstructors().Count() > 0) {
bool isProtectedAllowed = currentType != null ? currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) : false;
if (!t.GetConstructors().Any(m => lookup.IsAccessible(m, isProtectedAllowed)))
bool isProtectedAllowed = currentType != null ?
currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) :
false;
if (!t.GetConstructors().Any(m => lookup.IsAccessible(m, isProtectedAllowed))) {
return null;
}
}
var typeInference = new TypeInference(Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(new [] { t }, new [] { hintType });
wrapper.AddType(inferedType, amb.ConvertType(inferedType));
return null;
return t;
};
if (!(hintType.Kind == TypeKind.Interface && hintType.Kind != TypeKind.Array)) {
DefaultCompletionString = GetShortType(hintType, GetState());

28
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs

@ -1493,9 +1493,9 @@ public class AClass @@ -1493,9 +1493,9 @@ public class AClass
/// Bug 471937 - Code completion of 'new' showing invorrect entries
/// </summary>
[Test()]
public void TestBug471937 ()
public void TestBug471937()
{
CompletionDataList provider = CreateCtrlSpaceProvider (
CompletionDataList provider = CreateCtrlSpaceProvider(
@"
class B
{
@ -1514,7 +1514,7 @@ class A @@ -1514,7 +1514,7 @@ class A
Assert.IsNotNull (provider, "provider not found.");
Assert.IsNotNull (provider.Find ("A"), "class 'A' not found.");
Assert.AreEqual ("A", provider.DefaultCompletionString);
Assert.IsNull (provider.Find ("B"), "class 'B' found, but shouldn'tj.");
// Assert.IsNull (provider.Find ("B"), "class 'B' found, but shouldn'tj.");
}
/// <summary>
@ -4724,7 +4724,6 @@ class MainClass @@ -4724,7 +4724,6 @@ class MainClass
Assert.IsNotNull(provider.Find("List<string>"), "'List<string>' not found.");
Assert.IsNull(provider.Find("IEnumerable"), "'IEnumerable' found.");
Assert.IsNull(provider.Find("IEnumerable<string>"), "'IEnumerable<string>' found.");
Assert.IsNull (provider.Find ("Console"), "'Console' found.");
}
[Test()]
@ -5041,5 +5040,26 @@ public class Test @@ -5041,5 +5040,26 @@ public class Test
});
}
/// <summary>
/// Bug 4487 - Filtering possible types for new expressions a bit too aggressively
/// </summary>
[Test()]
public void TestBug4487()
{
// note 'string bar = new Test ().ToString ()' would be valid.
CombinedProviderTest(
@"public class Test
{
void TestFoo()
{
$string bar = new T$
}
}
", provider => {
Assert.IsNotNull(provider.Find("Test"), "'Test' not found.");
});
}
}
}

Loading…
Cancel
Save