Browse Source

Fixed interface return type tests.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
e058867fee
  1. 100
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

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

@ -176,7 +176,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (!(pexpr.Value is string || pexpr.Value is char) && !pexpr.LiteralValue.Contains ('.')) if (!(pexpr.Value is string || pexpr.Value is char) && !pexpr.LiteralValue.Contains ('.'))
return null; return null;
} }
resolveResult = ResolveExpression (expr); resolveResult = ResolveExpression (expr);
if (resolveResult == null) if (resolveResult == null)
return null; return null;
@ -439,7 +439,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (identifierStart != null && identifierStart.Node is VariableInitializer && location <= ((VariableInitializer)identifierStart.Node).NameToken.EndLocation) { if (identifierStart != null && identifierStart.Node is VariableInitializer && location <= ((VariableInitializer)identifierStart.Node).NameToken.EndLocation) {
return controlSpace ? HandleAccessorContext () ?? DefaultControlSpaceItems (identifierStart) : null; return controlSpace ? HandleAccessorContext () ?? DefaultControlSpaceItems (identifierStart) : null;
} }
if (identifierStart != null && identifierStart.Node is CatchClause) { if (identifierStart != null && identifierStart.Node is CatchClause) {
if (((CatchClause)identifierStart.Node).VariableNameToken.Contains (location)) if (((CatchClause)identifierStart.Node).VariableNameToken.Contains (location))
return null; return null;
@ -474,7 +474,7 @@ 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)
@ -486,7 +486,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (n != null && n.Parent is AnonymousTypeCreateExpression) { if (n != null && n.Parent is AnonymousTypeCreateExpression) {
AutoSelect = false; AutoSelect = false;
} }
// 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;
@ -506,7 +506,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (p != null) { if (p != null) {
var initializerResult = ResolveExpression (p, identifierStart.Unit); var initializerResult = ResolveExpression (p, identifierStart.Unit);
if (initializerResult != null && initializerResult.Item1.Type.Kind != TypeKind.Unknown) { if (initializerResult != null && initializerResult.Item1.Type.Kind != TypeKind.Unknown) {
foreach (var m in initializerResult.Item1.Type.GetMembers (m => m.IsPublic && (m.EntityType == EntityType.Property || m.EntityType == EntityType.Field))) { foreach (var m in initializerResult.Item1.Type.GetMembers (m => m.IsPublic && (m.EntityType == EntityType.Property || m.EntityType == EntityType.Field))) {
contextList.AddMember (m); contextList.AddMember (m);
} }
@ -517,7 +517,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
} }
} }
if (n != null && n.Parent is InvocationExpression) { if (n != null && n.Parent is InvocationExpression) {
var invokeParent = (InvocationExpression)n.Parent; var invokeParent = (InvocationExpression)n.Parent;
var invokeResult = ResolveExpression (invokeParent.Target, identifierStart.Unit); var invokeResult = ResolveExpression (invokeParent.Target, identifierStart.Unit);
@ -546,16 +546,14 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
AutoSelect = false; AutoSelect = false;
AutoCompleteEmptyMatch = false; AutoCompleteEmptyMatch = false;
} }
} }
} }
} }
} }
if (n != null && n.Parent is ObjectCreateExpression) { if (n != null && n.Parent is ObjectCreateExpression) {
var invokeResult = ResolveExpression (n.Parent, identifierStart.Unit); var invokeResult = ResolveExpression (n.Parent, identifierStart.Unit);
var mgr = invokeResult != null ? invokeResult.Item1 as ResolveResult : null; var mgr = invokeResult != null ? invokeResult.Item1 as ResolveResult : null;
if (mgr != null) { if (mgr != null) {
foreach (var constructor in mgr.Type.GetConstructors ()) { foreach (var constructor in mgr.Type.GetConstructors ()) {
foreach (var p in constructor.Parameters) { foreach (var p in constructor.Parameters) {
@ -564,11 +562,11 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
} }
} }
if (n is IdentifierExpression) { if (n is IdentifierExpression) {
var bop = n.Parent as BinaryOperatorExpression; var bop = n.Parent as BinaryOperatorExpression;
Expression evaluationExpr = null; Expression evaluationExpr = null;
if (bop != null && bop.Right == n && (bop.Operator == BinaryOperatorType.Equality || bop.Operator == BinaryOperatorType.InEquality)) { if (bop != null && bop.Right == n && (bop.Operator == BinaryOperatorType.Equality || bop.Operator == BinaryOperatorType.InEquality)) {
evaluationExpr = bop.Left; evaluationExpr = bop.Left;
} }
@ -605,7 +603,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
if (initalizerResult != null && initalizerResult.Item1.Type.Kind != TypeKind.Unknown) { if (initalizerResult != null && initalizerResult.Item1.Type.Kind != TypeKind.Unknown) {
foreach (var property in initalizerResult.Item1.Type.GetProperties ()) { foreach (var property in initalizerResult.Item1.Type.GetProperties ()) {
if (!property.IsPublic) if (!property.IsPublic)
continue; continue;
@ -1710,19 +1708,29 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
n = n.Parent; n = n.Parent;
return n is Attribute; return n is Attribute;
} }
IType GuessHintType (AstNode resolvedNode)
{
ObjectCreateExpression oce = resolvedNode.Parent as ObjectCreateExpression ?? (ObjectCreateExpression)resolvedNode.Ancestors.FirstOrDefault (n => n is ObjectCreateExpression);
if (oce != null && oce.Parent is ReturnStatement)
return ctx.CurrentMember != null ? ctx.CurrentMember.ReturnType : null;
return null;
}
IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData (TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state) IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData (TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state)
{ {
if (resolveResult == null || resolveResult.IsError) if (resolveResult == null || resolveResult.IsError)
return null; return null;
var hintType = GuessHintType (resolvedNode);
var result = new CompletionDataWrapper (this); var result = new CompletionDataWrapper (this);
if (resolveResult is NamespaceResolveResult) { if (resolveResult is NamespaceResolveResult) {
var nr = (NamespaceResolveResult)resolveResult; var nr = (NamespaceResolveResult)resolveResult;
if (!(resolvedNode.Parent is UsingDeclaration || resolvedNode.Parent != null && resolvedNode.Parent.Parent is UsingDeclaration)) { if (!(resolvedNode.Parent is UsingDeclaration || resolvedNode.Parent != null && resolvedNode.Parent.Parent is UsingDeclaration)) {
Console.WriteLine ("fooo");
foreach (var cl in nr.Namespace.Types) { foreach (var cl in nr.Namespace.Types) {
string name = cl.Name; string name = cl.Name;
if (hintType != null && hintType.Kind != TypeKind.Array && cl.Kind == TypeKind.Interface)
continue;
if (IsAttributeContext (resolvedNode) && name.EndsWith ("Attribute") && name.Length > "Attribute".Length) if (IsAttributeContext (resolvedNode) && name.EndsWith ("Attribute") && name.Length > "Attribute".Length)
name = name.Substring (0, name.Length - "Attribute".Length); name = name.Substring (0, name.Length - "Attribute".Length);
result.AddType (cl, name); result.AddType (cl, name);
@ -1734,12 +1742,14 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} else if (resolveResult is TypeResolveResult) { } else if (resolveResult is TypeResolveResult) {
var type = resolveResult.Type; var type = resolveResult.Type;
foreach (var nested in type.GetNestedTypes ()) { foreach (var nested in type.GetNestedTypes ()) {
if (hintType != null && hintType.Kind != TypeKind.Array && nested.Kind == TypeKind.Interface)
continue;
result.AddType (nested, nested.Name); result.AddType (nested, nested.Name);
} }
} }
return result.Result; return result.Result;
} }
IEnumerable<ICompletionData> CreateTypeList () IEnumerable<ICompletionData> CreateTypeList ()
{ {
foreach (var cl in Compilation.RootNamespace.Types) { foreach (var cl in Compilation.RootNamespace.Types) {
@ -2062,7 +2072,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
baseUnit = ParseStub ("a()"); baseUnit = ParseStub ("a()");
curNode = baseUnit.GetNodeAt (location); curNode = baseUnit.GetNodeAt (location);
} }
// Hack for handle object initializer continuation expressions // Hack for handle object initializer continuation expressions
if (curNode is EntityDeclaration || baseUnit.GetNodeAt<Expression> (location) == null) { if (curNode is EntityDeclaration || baseUnit.GetNodeAt<Expression> (location) == null) {
baseUnit = ParseStub ("a()};"); baseUnit = ParseStub ("a()};");
@ -2079,7 +2089,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (invoke != null) if (invoke != null)
mref = invoke.Target as MemberReferenceExpression; mref = invoke.Target as MemberReferenceExpression;
} }
Expression expr = null; AstNode expr = null;
if (mref != null) { if (mref != null) {
expr = mref.Target.Clone (); expr = mref.Target.Clone ();
mref.Parent.ReplaceWith (expr); mref.Parent.ReplaceWith (expr);
@ -2089,37 +2099,46 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (memberType == null) { if (memberType == null) {
memberType = baseUnit.GetNodeAt<MemberType> (location); memberType = baseUnit.GetNodeAt<MemberType> (location);
if (memberType != null) { if (memberType != null) {
tref = baseUnit.GetNodeAt<Expression> (location); if (memberType.Parent is ObjectCreateExpression) {
if (tref == null) { var mt = memberType.Target.Clone ();
tref = new TypeReferenceExpression (memberType.Clone ()); memberType.ReplaceWith (mt);
memberType.Parent.AddChild (tref, AstNode.Roles.Expression); expr = mt;
goto exit;
} else {
tref = baseUnit.GetNodeAt<Expression> (location);
if (tref == null) {
tref = new TypeReferenceExpression (memberType.Clone ());
memberType.Parent.AddChild (tref, AstNode.Roles.Expression);
}
if (tref is ObjectCreateExpression) {
expr = new TypeReferenceExpression (memberType.Target.Clone ());
expr.AddAnnotation (new ObjectCreateExpression ());
}
} }
} }
if (tref is ObjectCreateExpression) {
expr = new TypeReferenceExpression (memberType.Target.Clone ());
expr.AddAnnotation (new ObjectCreateExpression ());
}
} }
if (memberType == null) if (memberType == null)
return null; return null;
if (expr == null) if (expr == null) {
expr = new TypeReferenceExpression (memberType.Target.Clone ()); expr = new TypeReferenceExpression (memberType.Target.Clone ());
}
tref.ReplaceWith (expr); tref.ReplaceWith (expr);
} }
exit:
var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation); var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation);
var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation); var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation);
member2.Remove (); member2.Remove ();
member.ReplaceWith (member2); member.ReplaceWith (member2);
Print (member2);
return new ExpressionResult ((AstNode)expr, Unit); return new ExpressionResult ((AstNode)expr, Unit);
} }
ExpressionResult GetExpressionAtCursor () ExpressionResult GetExpressionAtCursor ()
{ {
// if (currentMember == null && currentType == null) // if (currentMember == null && currentType == null)
// return null; // return null;
TextLocation memberLocation; TextLocation memberLocation;
if (currentMember != null) { if (currentMember != null) {
memberLocation = currentMember.Region.Begin; memberLocation = currentMember.Region.Begin;
@ -2145,6 +2164,9 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
if (expr == null) { if (expr == null) {
baseUnit = ParseStub ("()"); baseUnit = ParseStub ("()");
expr = baseUnit.GetNodeAt<IdentifierExpression> (location.Line, location.Column - 1); expr = baseUnit.GetNodeAt<IdentifierExpression> (location.Line, location.Column - 1);
if (expr == null) {
expr = baseUnit.GetNodeAt<MemberType> (location.Line, location.Column - 1);
}
} }
if (expr == null) { if (expr == null) {
@ -2152,17 +2174,17 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
expr = baseUnit.GetNodeAt (location, n => n is IdentifierExpression || n is MemberReferenceExpression || n is CatchClause); expr = baseUnit.GetNodeAt (location, n => n is IdentifierExpression || n is MemberReferenceExpression || n is CatchClause);
} }
// try statement // try statement
if (expr == null) { if (expr == null) {
expr = tmpUnit.GetNodeAt<SwitchStatement> (location.Line, location.Column - 1); expr = tmpUnit.GetNodeAt<SwitchStatement> (location.Line, location.Column - 1);
baseUnit = tmpUnit; baseUnit = tmpUnit;
} }
if (expr == null) { if (expr == null) {
var block = tmpUnit.GetNodeAt<BlockStatement> (location); var block = tmpUnit.GetNodeAt<BlockStatement> (location);
var node = block != null ? block.Statements.LastOrDefault () : null; var node = block != null ? block.Statements.LastOrDefault () : null;
var forStmt = node != null ? node.PrevSibling as ForStatement : null; var forStmt = node != null ? node.PrevSibling as ForStatement : null;
if (forStmt != null && forStmt.EmbeddedStatement.IsNull) { if (forStmt != null && forStmt.EmbeddedStatement.IsNull) {
expr = forStmt; expr = forStmt;
@ -2185,19 +2207,19 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
expr = tmpUnit.GetNodeAt<VariableInitializer> (location.Line, location.Column - 1); expr = tmpUnit.GetNodeAt<VariableInitializer> (location.Line, location.Column - 1);
baseUnit = tmpUnit; baseUnit = tmpUnit;
} }
// try parameter declaration type // try parameter declaration type
if (expr == null) { if (expr == null) {
baseUnit = ParseStub (">", false, "{}"); baseUnit = ParseStub (">", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1); expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1);
} }
// try parameter declaration method // try parameter declaration method
if (expr == null) { if (expr == null) {
baseUnit = ParseStub ("> ()", false, "{}"); baseUnit = ParseStub ("> ()", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1); expr = baseUnit.GetNodeAt<TypeParameterDeclaration> (location.Line, location.Column - 1);
} }
// try expression in anonymous type "new { sample = x$" case // try expression in anonymous type "new { sample = x$" case
if (expr == null) { if (expr == null) {
baseUnit = ParseStub ("a", false); baseUnit = ParseStub ("a", false);
@ -2209,12 +2231,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
if (expr == null) if (expr == null)
return null; return null;
var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation); var member = Unit.GetNodeAt<EntityDeclaration> (memberLocation);
var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation); var member2 = baseUnit.GetNodeAt<EntityDeclaration> (memberLocation);
if (member != null && member2 != null) { if (member != null && member2 != null) {
member2.Remove (); member2.Remove ();
if (member is TypeDeclaration) { if (member is TypeDeclaration) {
member.AddChild (member2, TypeDeclaration.MemberRole); member.AddChild (member2, TypeDeclaration.MemberRole);
} else { } else {

Loading…
Cancel
Save