Browse Source

[Completion] Fixed bug in anonymous type creation.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
176a3a0540
  1. 12
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 42
      ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs

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

@ -619,7 +619,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (IsInsideCommentStringOrDirective()) { if (IsInsideCommentStringOrDirective()) {
tokenIndex = offset; tokenIndex = offset;
token = GetPreviousToken(ref tokenIndex, false); token = GetPreviousToken(ref tokenIndex, false);
if (IsInPreprocessorDirective() && (token.Length == 1 && char.IsLetter (completionChar) || controlSpace)) { if (IsInPreprocessorDirective() && (token.Length == 1 && char.IsLetter(completionChar) || controlSpace)) {
while (token != null && document.GetCharAt (tokenIndex - 1) != '#') { while (token != null && document.GetCharAt (tokenIndex - 1) != '#') {
token = GetPreviousToken(ref tokenIndex, false); token = GetPreviousToken(ref tokenIndex, false);
} }
@ -671,8 +671,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
new ExpressionResult( new ExpressionResult(
((MemberReferenceExpression)identifierStart.Node).Target, ((MemberReferenceExpression)identifierStart.Node).Target,
identifierStart.Unit identifierStart.Unit
) )
); );
} }
if (identifierStart.Node is Identifier) { if (identifierStart.Node is Identifier) {
@ -728,7 +728,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return HandleKeywordCompletion(tokenIndex, token); return HandleKeywordCompletion(tokenIndex, token);
} }
} }
if (identifierStart == null) { if (identifierStart == null) {
var accCtx = HandleAccessorContext(); var accCtx = HandleAccessorContext();
if (accCtx != null) { if (accCtx != null) {
@ -744,6 +743,10 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
AutoSelect = false; AutoSelect = false;
} }
// new { b$ }
if (n is IdentifierExpression && n.Parent is AnonymousTypeCreateExpression)
return null;
// Handle foreach (type name _ // Handle foreach (type name _
if (n is IdentifierExpression) { if (n is IdentifierExpression) {
var prev = n.GetPrevNode() as ForeachStatement; var prev = n.GetPrevNode() as ForeachStatement;
@ -755,7 +758,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return null; return null;
} }
} }
// Handle object/enumerable initialzer expressions: "new O () { P$" // Handle object/enumerable initialzer expressions: "new O () { P$"
if (n is IdentifierExpression && n.Parent is ArrayInitializerExpression && !(n.Parent.Parent is ArrayCreateExpression)) { if (n is IdentifierExpression && n.Parent is ArrayInitializerExpression && !(n.Parent.Parent is ArrayCreateExpression)) {
var result = HandleObjectInitializer(identifierStart.Unit, n); var result = HandleObjectInitializer(identifierStart.Unit, n);

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

@ -704,9 +704,49 @@ class C : S
Assert.IsNull(provider.Find("MinValue"), "'MinValue' found."); Assert.IsNull(provider.Find("MinValue"), "'MinValue' found.");
} }
[Test]
public void TestAnonymousTypes()
{
CodeCompletionBugTests.CombinedProviderTest(
@"class Test
{
public static void Main(string [] args)
{
var mm = new {
$b$
};
}
}
} ", provider => {
Assert.IsTrue(provider == null || provider.Count == 0);
});
}
[Test]
public void TestAnonymousTypesCase2()
{
CodeCompletionBugTests.CombinedProviderTest(
@"class Test
{
public static void Main(string [] args)
{
var mm = new {
$bar = a$
};
}
}
", provider => {
Assert.IsNotNull(provider.Find("args"), "'args' not found.");
});
}
}
} }

Loading…
Cancel
Save