New tests.
[mono.git] / mcs / mcs / complete.cs
1 //
2 // complete.cs: Expression that are used for completion suggestions.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2009 Novell, Inc.
10 //
11 // Completion* classes derive from ExpressionStatement as this allows
12 // them to pass through the parser in many conditions that require
13 // statements even when the expression is incomplete (for example
14 // completing inside a lambda
15 //
16 using System;
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Reflection.Emit;
20 using System.Text;
21 using Mono.CSharp.Linq;
22 using System.Linq;
23
24 namespace Mono.CSharp {
25
26         //
27         // A common base class for Completing expressions, it
28         // is just a very simple ExpressionStatement
29         //
30         public abstract class CompletingExpression : ExpressionStatement
31         {
32                 public static void AppendResults (List<string> results, string prefix, IEnumerable<string> names)
33                 {
34                         foreach (string name in names) {
35                                 if (name == null || prefix == null)
36                                         continue;
37
38                                 if (!name.StartsWith (prefix))
39                                         continue;
40
41                                 if (results.Contains (name))
42                                         continue;
43
44                                 if (prefix != null)
45                                         results.Add (name.Substring (prefix.Length));
46                                 else
47                                         results.Add (name);
48                         }
49                 }
50
51                 public override void EmitStatement (EmitContext ec)
52                 {
53                         // Do nothing
54                 }
55
56                 public override void Emit (EmitContext ec)
57                 {
58                         // Do nothing
59                 }
60
61                 public override Expression CreateExpressionTree (ResolveContext ec)
62                 {
63                         return null;
64                 }
65         }
66         
67         public class CompletionSimpleName : CompletingExpression {
68                 public string Prefix;
69                 
70                 public CompletionSimpleName (string prefix, Location l)
71                 {
72                         this.loc = l;
73                         this.Prefix = prefix;
74                 }
75                 
76                 protected override Expression DoResolve (ResolveContext ec)
77                 {
78                         var results = new List<string> ();
79
80                         AppendResults (results, Prefix, Evaluator.GetVarNames ());
81                         AppendResults (results, Prefix, ec.CurrentMemberDefinition.Parent.NamespaceEntry.CompletionGetTypesStartingWith (Prefix));
82                         AppendResults (results, Prefix, Evaluator.GetUsingList ());
83                         
84                         throw new CompletionResult (Prefix, results.ToArray ());
85                 }
86
87                 protected override void CloneTo (CloneContext clonectx, Expression t)
88                 {
89                         // Nothing
90                 }
91         }
92         
93         public class CompletionMemberAccess : CompletingExpression {
94                 Expression expr;
95                 string partial_name;
96                 TypeArguments targs;
97                 
98                 public CompletionMemberAccess (Expression e, string partial_name, Location l)
99                 {
100                         this.expr = e;
101                         this.loc = l;
102                         this.partial_name = partial_name;
103                 }
104
105                 public CompletionMemberAccess (Expression e, string partial_name, TypeArguments targs, Location l)
106                 {
107                         this.expr = e;
108                         this.loc = l;
109                         this.partial_name = partial_name;
110                         this.targs = targs;
111                 }
112                 
113                 protected override Expression DoResolve (ResolveContext ec)
114                 {
115                         Expression expr_resolved = expr.Resolve (ec,
116                                 ResolveFlags.VariableOrValue | ResolveFlags.Type |
117                                 ResolveFlags.Intermediate);
118
119                         if (expr_resolved == null)
120                                 return null;
121
122                         TypeSpec expr_type = expr_resolved.Type;
123                         if (expr_type.IsPointer || expr_type == TypeManager.void_type || expr_type == TypeManager.null_type || expr_type == InternalType.AnonymousMethod) {
124                                 Unary.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
125                                 return null;
126                         }
127
128                         if (targs != null) {
129                                 if (!targs.Resolve (ec))
130                                         return null;
131                         }
132
133                         var results = new List<string> ();
134                         if (expr_resolved is Namespace){
135                                 Namespace nexpr = expr_resolved as Namespace;
136                                 string namespaced_partial;
137
138                                 if (partial_name == null)
139                                         namespaced_partial = nexpr.Name;
140                                 else
141                                         namespaced_partial = nexpr.Name + "." + partial_name;
142
143 #if false
144                                 Console.WriteLine ("Workign with: namespaced partial {0}", namespaced_partial);
145                                 foreach (var x in ec.TypeContainer.NamespaceEntry.CompletionGetTypesStartingWith (ec.TypeContainer, namespaced_partial)){
146                                         Console.WriteLine ("    {0}", x);
147                                 }
148 #endif
149
150                                 CompletionSimpleName.AppendResults (
151                                         results,
152                                         partial_name, 
153                                         ec.CurrentMemberDefinition.Parent.NamespaceEntry.CompletionGetTypesStartingWith (namespaced_partial));
154                         } else {
155                                 var r = MemberCache.GetCompletitionMembers (expr_type, partial_name).Select (l => l.Name);
156                                 AppendResults (results, partial_name, r);
157                         }
158
159                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.ToArray ());
160                 }
161
162                 protected override void CloneTo (CloneContext clonectx, Expression t)
163                 {
164                         CompletionMemberAccess target = (CompletionMemberAccess) t;
165
166                         if (targs != null)
167                                 target.targs = targs.Clone ();
168
169                         target.expr = expr.Clone (clonectx);
170                 }
171         }
172
173         public class CompletionElementInitializer : CompletingExpression {
174                 string partial_name;
175                 
176                 public CompletionElementInitializer (string partial_name, Location l)
177                 {
178                         this.partial_name = partial_name;
179                         this.loc = l;
180                 }
181                 
182                 protected override Expression DoResolve (ResolveContext ec)
183                 {
184                         var members = MemberCache.GetCompletitionMembers (ec.CurrentInitializerVariable.Type, partial_name);
185
186 // TODO: Does this mean exact match only ?
187 //                      if (partial_name != null && results.Count > 0 && result [0] == "")
188 //                              throw new CompletionResult ("", new string [] { "=" });
189
190                         var results = members.Where (l => (l.Kind & (MemberKind.Field | MemberKind.Property)) != 0).Select (l => l.Name).ToList ();
191                         if (partial_name != null) {
192                                 var temp = new List<string> ();
193                                 AppendResults (temp, partial_name, results);
194                                 results = temp;
195                         }
196
197                         throw new CompletionResult (partial_name == null ? "" : partial_name, results.ToArray ());
198                 }
199
200                 protected override void CloneTo (CloneContext clonectx, Expression t)
201                 {
202                         // Nothing
203                 }
204         }
205         
206 }