Browse Source

[Completion] Fixed bug in object initializer context.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
539db3bb43
  1. 21
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 58
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs

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

@ -263,10 +263,20 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -263,10 +263,20 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return contextList.Result;
}
foreach (var m in initializerResult.Item1.Type.GetMembers (m => m.IsPublic && (m.EntityType == EntityType.Property || m.EntityType == EntityType.Field))) {
contextList.AddMember(m);
var lookup = new MemberLookup(ctx.CurrentTypeDefinition, Compilation.MainAssembly);
bool isProtectedAllowed = ctx.CurrentTypeDefinition != null ?
ctx.CurrentTypeDefinition.IsDerivedFrom(initializerResult.Item1.Type.GetDefinition()) :
false;
foreach (var m in initializerResult.Item1.Type.GetMembers (m => m.EntityType == EntityType.Field)) {
if (lookup.IsAccessible (m, isProtectedAllowed))
contextList.AddMember(m);
}
foreach (IProperty m in initializerResult.Item1.Type.GetMembers (m => m.EntityType == EntityType.Property)) {
if (m.CanSet && lookup.IsAccessible (m.Setter, isProtectedAllowed))
contextList.AddMember(m);
}
if (prev != null && (prev is NamedExpression)) {
// case 2)
return contextList.Result;
@ -657,17 +667,20 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -657,17 +667,20 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return HandleCatchClauseType(identifierStart);
}
}
if (!(char.IsLetter(completionChar) || completionChar == '_') && (!controlSpace || identifierStart == null || !(identifierStart.Node.Parent is ArrayInitializerExpression))) {
if (!(char.IsLetter(completionChar) || completionChar == '_') && (!controlSpace || identifierStart == null)) {
return controlSpace ? HandleAccessorContext() ?? DefaultControlSpaceItems(identifierStart) : null;
}
char prevCh = offset > 2 ? document.GetCharAt(offset - 2) : ';';
char nextCh = offset < document.TextLength ? document.GetCharAt(offset) : ' ';
const string allowedChars = ";,.[](){}+-*/%^?:&|~!<>=";
if (!Char.IsWhiteSpace(nextCh) && allowedChars.IndexOf(nextCh) < 0) {
return null;
}
if (!(Char.IsWhiteSpace(prevCh) || allowedChars.IndexOf(prevCh) >= 0)) {
if (controlSpace && identifierStart != null && identifierStart.Node is IdentifierExpression)
return DefaultControlSpaceItems(identifierStart);
return null;
}
// Do not pop up completion on identifier identifier (should be handled by keyword completion).

58
ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs

@ -587,6 +587,64 @@ class MyTest @@ -587,6 +587,64 @@ class MyTest
);
}
/// <summary>
/// Bug 7383 - Object initializer completion inaccessible
/// </summary>
[Test()]
public void TestBug7383()
{
var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider(
@"using System.Runtime.InteropServices;
class S
{
public int Foo { get; protected set; }
public int Bar { get; set; }
}
class C
{
public static void Main ()
{
var s = new S () {
$Fo$
};
}
}
");
Assert.IsNull(provider.Find("Foo"), "'Foo' found.");
Assert.IsNotNull(provider.Find("Bar"), "'Bar' not found.");
}
[Test()]
public void TestBug7383Case2()
{
var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider(
@"using System.Runtime.InteropServices;
class S
{
public int Foo { get; protected set; }
public int Bar { get; set; }
}
class C : S
{
public static void Main ()
{
var s = new C () {
$Fo$
};
}
}
");
Assert.IsNotNull(provider.Find("Foo"), "'Foo' found.");
Assert.IsNotNull(provider.Find("Bar"), "'Bar' not found.");
}
}
}

Loading…
Cancel
Save