Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Resolver.cs « Parser « CSharpBinding « Extras - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e02f63f27475882f9d288a7018887eface356395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Andrea Paatz" email="andrea@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;

using MonoDevelop.Core;
using MonoDevelop.Projects.Parser;
using MonoDevelop.Projects;
using CSharpBinding.Parser.SharpDevelopTree;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory;
using ClassType = MonoDevelop.Projects.Parser.ClassType;

namespace CSharpBinding.Parser
{
	class Resolver
	{
		IParserContext parserContext;
		ICompilationUnit currentUnit;
		string currentFile;
		
		IClass callingClass;
		IMethod callingMethod;
		IIndexer callingIndexer;
		IProperty callingProperty;
		bool callingClassChecked;
		bool callingMethodChecked;
		bool callingIndexerChecked;
		bool callingPropertyChecked;
		
		LookupTableVisitor lookupTableVisitor;
		int caretLine;
		int caretColumn;
		
		public Resolver (IParserContext parserContext)
		{
			this.parserContext = parserContext;
		}
		
		public IParserContext ParserContext {
			get {
				return parserContext;
			}
		}
		
		public ICompilationUnit CompilationUnit {
			get {
				return currentUnit;
			}
		}
		
		public IClass CallingClass {
			get {
				return callingClass;
			}
		}
		
		bool showStatic = false;
		
		public bool ShowStatic {
			get {
				return showStatic;
			}
			
			set {
				showStatic = value;
			}
		}
		
		void SetCursorPosition (int caretLineNumber, int caretColumn)
		{
			this.caretLine = caretLineNumber;
			this.caretColumn = caretColumn;
			callingClass = null;
			callingMethod = null;
			callingIndexer = null;
			callingProperty = null;
			callingClassChecked = callingPropertyChecked = callingMethodChecked = callingIndexerChecked = false;
		}
		
		public IReturnType internalResolve (string expression, int caretLineNumber, int caretColumn, string fileName, string fileContent)
		{
			//Console.WriteLine("Start Resolving");
			if (expression == null) {
				return null;
			}
			expression = expression.TrimStart(null);
			if (expression == "") {
				return null;
			}
			
			SetCursorPosition (caretLineNumber, caretColumn);
			
			IParseInformation parseInfo = parserContext.GetParseInformation(fileName);
			ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
			if (fileCompilationUnit == null) {
//				ICSharpCode.NRefactory.Parser.Parser fileParser = new ICSharpCode.NRefactory.Parser.Parser();
//				fileParser.Parse(new Lexer(new StringReader(fileContent)));
				Console.WriteLine("Warning: no parse information!");
				return null;
			}
			/*
			//// try to find last expression in original string, it could be like " if (act!=null) act"
			//// in this case only "act" should be parsed as expression  
			!!is so!! don't change things that work
			Expression expr=null;	// tentative expression
			Lexer l=null;
			ICSharpCode.NRefactory.Parser.Parser p = new ICSharpCode.NRefactory.Parser.Parser();
			while (expression.Length > 0) {
				l = new Lexer(new StringReader(expression));
				expr = p.ParseExpression(l);
				if (l.LookAhead.val != "" && expression.LastIndexOf(l.LookAhead.val) >= 0) {
					if (expression.Substring(expression.LastIndexOf(l.LookAhead.val) + l.LookAhead.val.Length).Length > 0) 
						expression=expression.Substring(expression.LastIndexOf(l.LookAhead.val) + l.LookAhead.val.Length).Trim();
					else {
						expression=l.LookAhead.val.Trim();
						l=new Lexer(new StringReader(expression));
						expr=p.ParseExpression(l);
						break;
					}
				} else {
					if (l.Token.val!="" || expr!=null) break;
				}
			}
			//// here last subexpression should be fixed in expr
			if it should be changed in expressionfinder don't fix it here
			*/
			ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser (SupportedLanguage.CSharp, new StringReader(expression));
			Expression expr = p.ParseExpression();
			if (expr == null) {
				return null;
			}
			lookupTableVisitor = new LookupTableVisitor (SupportedLanguage.CSharp);
			lookupTableVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			
			TypeVisitor typeVisitor = new TypeVisitor(this);
			
			CSharpVisitor cSharpVisitor = new CSharpVisitor();
			currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			currentFile = fileName;
			
			if (currentUnit != null) {
				callingClass = GetInnermostClass();
//				Console.WriteLine("CallingClass is " + callingClass == null ? "null" : callingClass.Name);
			}
			
			// No completion inside enums
			if (callingClass != null && callingClass.ClassType == ClassType.Enum)
				return null;
			
			//Console.WriteLine("expression = " + expr.ToString());
			IReturnType type = expr.AcceptVisitor(typeVisitor, null) as IReturnType;
			//Console.WriteLine("type visited");
			if (type == null || type.PointerNestingLevel != 0) {
//				Console.WriteLine("Type == null || type.PointerNestingLevel != 0");
				if (type != null) {
					//Console.WriteLine("PointerNestingLevel is " + type.PointerNestingLevel);
				} else {
					//Console.WriteLine("Type == null");
				}
				//// when type is null might be file needs to be reparsed - some vars were lost
				fileCompilationUnit = parserContext.ParseFile (fileName, fileContent).MostRecentCompilationUnit.Tag 
					as ICSharpCode.NRefactory.Ast.CompilationUnit;
				lookupTableVisitor.VisitCompilationUnit (fileCompilationUnit,null);
				currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
				if (currentUnit != null) {
					// Reset cursor position data
					SetCursorPosition (caretLineNumber, caretColumn);
					callingClass = GetInnermostClass();
				}
				type=expr.AcceptVisitor(typeVisitor,null) as IReturnType;
				if (type==null)	return null;
			}
			//Console.WriteLine("Here: Type is " + type.FullyQualifiedName);
			return type;
		}
		
		public IClass GetCallingClass (int line, int col, string fileName, bool onlyClassDeclaration)
		{
			IParseInformation parseInfo = parserContext.GetParseInformation (fileName);
			ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
			if (fileCompilationUnit == null)
				return null;

			CSharpVisitor cSharpVisitor = new CSharpVisitor();
			currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			
			currentFile = fileName;
		
			SetCursorPosition (line, col);
			
			callingClass = GetInnermostClass();
			if (callingClass == null)
				return null;
				
			if (onlyClassDeclaration && GetMethod () != null)
				return null;
			
			return callingClass;
		}

		public IClass ResolveExpressionType (ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit, Expression expr, int line, int col)
		{
			CSharpVisitor cSharpVisitor = new CSharpVisitor();
			currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			currentFile = null;
			
			SetCursorPosition (line, col);
			
			callingClass = GetInnermostClass();
			
			lookupTableVisitor = new LookupTableVisitor (SupportedLanguage.CSharp);
			lookupTableVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			TypeVisitor typeVisitor = new TypeVisitor (this);
			
			IReturnType type = expr.AcceptVisitor (typeVisitor, null) as IReturnType;
			if (type != null)
				return SearchType (type, currentUnit);
			else
				return null;
		}

		public ILanguageItem ResolveIdentifier (IParserContext parserContext, string id, int line, int col, string fileName, string fileContent)
		{
			IParseInformation parseInfo = parserContext.GetParseInformation (fileName);
			ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
			currentFile = fileName;
			if (fileCompilationUnit == null)
				return null;
			return ResolveIdentifier (fileCompilationUnit, id, line, col);
		}
		
		public ILanguageItem ResolveIdentifier (ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit, string id, int line, int col)
		{
			ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser (SupportedLanguage.CSharp, new StringReader(id));
			Expression expr = p.ParseExpression ();
			if (expr == null)
				return null;
			
			CSharpVisitor cSharpVisitor = new CSharpVisitor();
			currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			
			SetCursorPosition (line, col);
			
			callingClass = GetInnermostClass();
			
			lookupTableVisitor = new LookupTableVisitor(SupportedLanguage.CSharp);
			lookupTableVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			
			LanguageItemVisitor itemVisitor = new LanguageItemVisitor (this);
			ILanguageItem item = expr.AcceptVisitor (itemVisitor, null) as ILanguageItem;
			
			if (item == null && expr is BinaryOperatorExpression && !id.EndsWith ("()")) {
				// The expression parser does not correctly parse individual generic type names.
				// Try resolving again but using a more complex expression.
				return ResolveIdentifier (fileCompilationUnit, id + "()", line, col);
			}
			
			return item;
		}

		public ResolveResult Resolve (string expression, int caretLineNumber, int caretColumn, string fileName, string fileContent) 
		{
			if (expression == null) {
				return null;
			}
			expression = expression.TrimStart(null);
			if (expression.Length == 0)
				return null;

			// disable the code completion for numbers like 3.47
			int nn;
			if (int.TryParse (expression, out nn))
				return null;
			
			if (expression.StartsWith("using ")) {
				// expression[expression.Length - 1] != '.'
				// the period that causes this Resove() is not part of the expression
				if (expression[expression.Length - 1] == '.') {
					return null;
				}
				int i;
				for (i = expression.Length - 1; i >= 0; --i) {
					if (!(Char.IsLetterOrDigit(expression[i]) || expression[i] == '_' || expression[i] == '.')) {
						break;
					}
				}
				// no Identifier before the period
				if (i == expression.Length - 1) {
					return null;
				}
				string t = expression.Substring(i + 1);
//				Console.WriteLine("in Using Statement");
				string[] namespaces = parserContext.GetNamespaceList (t);
				if (namespaces == null || namespaces.Length <= 0) {
					return null;
				}
				return new ResolveResult(namespaces);
			}
			
			//Console.WriteLine("Not in Using");
			IReturnType type = internalResolve (expression, caretLineNumber, caretColumn, fileName, fileContent);
			if (type == null)
				return null;

			// Needed to be able to find the array members
			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0)
				type = new ReturnType("System.Array");
			
			IClass returnClass = SearchType (type, currentUnit);
			if (returnClass == null) {
				// Try if type is Namespace:
				string n = SearchNamespace(type.FullyQualifiedName, currentUnit);
				if (n == null) {
					return null;
				}
				LanguageItemCollection content = parserContext.GetNamespaceContents (n,true);
				LanguageItemCollection classes = new LanguageItemCollection();
				for (int i = 0; i < content.Count; ++i) {
					if (content[i] is IClass) {
						classes.Add((IClass)content[i]);
					}
				}
				string[] namespaces = parserContext.GetNamespaceList (n, true, true);
				return new ResolveResult(namespaces, classes);
			}
			//Console.WriteLine("Returning Result!");
			if (returnClass.FullyQualifiedName == "System.Void")
				return null;
			LanguageItemCollection members = new LanguageItemCollection();
			ListMembers(members, returnClass, returnClass);
			if (returnClass.ClassType == ClassType.Interface) {
				IClass objType = SearchType ("System.Object", null, null);
				ListMembers (members, objType, objType);
			}
			
			return new ResolveResult(returnClass, members);
		}
		
		LanguageItemCollection ListMembers (LanguageItemCollection members, IClass qualifierClass, IClass curType)
		{
//			Console.WriteLine("LIST MEMBERS!!!");
//			Console.WriteLine("showStatic = " + showStatic);
//			Console.WriteLine(curType.InnerClasses.Count + " classes");
//			Console.WriteLine(curType.Properties.Count + " properties");
//			Console.WriteLine(curType.Methods.Count + " methods");
//			Console.WriteLine(curType.Events.Count + " events");
//			Console.WriteLine(curType.Fields.Count + " fields");
			
			if (showStatic) {
				if (curType.ClassType == ClassType.Enum) {
					// If the type is an enum, show the enum members only.
					// (it is correct to call static methods using an enum type reference,
					// but it doesn't make much sense)
					foreach (IField f in curType.Fields) {
						if (MustBeShown (qualifierClass, curType, f)) {
							members.Add(f);
						}
					}
					return members;
				}
				
				foreach (IClass c in curType.InnerClasses) {
					if (IsAccessible(qualifierClass, curType, c)) {
						members.Add(c);
					}
				}
			}
			foreach (IProperty p in curType.Properties) {
				if (MustBeShown (qualifierClass, curType, p)) {
					members.Add(p);
				}
			}
			foreach (IMethod m in curType.Methods) {
				if (MustBeShown (qualifierClass, curType, m)) {
					members.Add(m);
				}
			}
			
			foreach (IEvent e in curType.Events) {
				if (MustBeShown (qualifierClass, curType, e)) {
					members.Add(e);
				}
			}
			foreach (IField f in curType.Fields) {
				if (MustBeShown (qualifierClass, curType, f)) {
					members.Add(f);
				}
			}
//			Console.WriteLine("ClassType = " + curType.ClassType);
			if (curType.ClassType == ClassType.Interface && !showStatic) {
				foreach (IReturnType s in curType.BaseTypes) {
					IClass baseClass = parserContext.GetClass (s.FullyQualifiedName, s.GenericArguments, true, true);
					if (baseClass != null && baseClass.ClassType == ClassType.Interface) {
						ListMembers (members, qualifierClass, baseClass);
					}
				}
			} else {
				IClass baseClass = BaseClass(curType);
				if (baseClass != null) {
//					Console.WriteLine("Base Class = " + baseClass.FullyQualifiedName);
					ListMembers (members, qualifierClass, baseClass);
				}
			}
//			Console.WriteLine("listing finished");
			return members;
		}
		
		public IClass BaseClass(IClass curClass)
		{
			foreach (IReturnType s in curClass.BaseTypes) {
				IClass baseClass = parserContext.GetClass (s.FullyQualifiedName, s.GenericArguments, true, true);
				if (baseClass != null && baseClass.ClassType != ClassType.Interface) {
					return baseClass;
				}
			}
			return null;
		}
		
		bool IsAccessible (IClass qualifier, IClass c, IDecoration member)
		{
//			Console.WriteLine("member.Modifiers = " + member.Modifiers);
			if ((member.Modifiers & ModifierEnum.Internal) == ModifierEnum.Internal) {
				return true;
			}
			if ((member.Modifiers & ModifierEnum.Public) == ModifierEnum.Public) {
//				Console.WriteLine("IsAccessible");
				return true;
			}
			if ((member.Modifiers & ModifierEnum.Protected) == ModifierEnum.Protected && IsClassInInheritanceTree (callingClass, qualifier)) {
//				Console.WriteLine("IsAccessible");
				return true;
			}
			if (callingClass == null)
				return false;

			return c.FullyQualifiedName == callingClass.FullyQualifiedName;
		}
		
		bool MustBeShown (IClass qualifierClass, IClass c, IDecoration member)
		{
			if (c.ClassType == ClassType.Enum && (member is IField))
				return showStatic;
			bool memStatic = member.IsStatic || ((member is IField) && member.IsLiteral);
			if ((showStatic != memStatic) ||
			    (showStatic && member.IsStatic && member.IsSpecialName && member.Name.StartsWith ("op_"))
			    ) {
				//// enum type fields are not shown here - there is no info in member about enum field
				return false;
			}
			return IsAccessible (qualifierClass, c, member);
		}
		
		public ArrayList SearchMethod(IReturnType type, string memberName)
		{
			if (type == null || type.PointerNestingLevel != 0) {
				return new ArrayList();
			}
			IClass curType;
			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0) {
				curType = SearchType ("System.Array", null, null);
			} else {
				curType = SearchType (type, null);
				if (curType == null) {
					return new ArrayList();
				}
			}
			return SearchMethod(new ArrayList(), curType, memberName);
		}
		
		ArrayList SearchMethod (ArrayList methods, IClass curType, string memberName)
		{
			return SearchMethod (methods, curType, curType, memberName);
		}
		
		ArrayList SearchMethod (ArrayList methods, IClass qualifierClass, IClass curType, string memberName)
		{
			foreach (IMethod m in curType.Methods) {
				if (m.Name == memberName &&
				    MustBeShown (qualifierClass, curType, m) &&
				    !((m.Modifiers & ModifierEnum.Override) == ModifierEnum.Override)) {
					methods.Add(m);
				}
			}
			IClass baseClass = BaseClass(curType);
			if (baseClass != null && baseClass != curType) {
				return SearchMethod(methods, qualifierClass, baseClass, memberName);
			}
			showStatic = false;
			return methods;
		}
		
		public ArrayList SearchIndexer(IReturnType type)
		{
			IClass curType = SearchType (type, null);
			if (curType != null) {
				return SearchIndexer(new ArrayList(), curType, curType);
			}
			return new ArrayList();
		}
		
		public ArrayList SearchIndexer (ArrayList indexer, IClass qualifierClass, IClass curType)
		{
			foreach (IIndexer i in curType.Indexer) {
				if (MustBeShown(qualifierClass, curType, i) && !((i.Modifiers & ModifierEnum.Override) == ModifierEnum.Override)) {
					indexer.Add(i);
				}
			}
			IClass baseClass = BaseClass(curType);
			if (baseClass != null) {
				return SearchIndexer (indexer, qualifierClass, baseClass);
			}
			showStatic = false;
			return indexer;
		}
		
		// no methods or indexer
		public IReturnType SearchMember (IReturnType type, string memberName)
		{
			IClass curType;
			IDecoration member;
			
			if (!SearchClassMember (type, memberName, false, out curType, out member))
				return null;
			
			if (member is IField) {
				showStatic = false;
				if (curType.ClassType == ClassType.Enum)
					return type; // enum members have the type of the enum
				else
					return ((IField)member).ReturnType;
			}
			else if (member is IClass) {
				showStatic = true;
				return new ReturnType (((IClass)member).FullyQualifiedName);
			}
			else if (member is IProperty) {
				showStatic = false;
				return ((IProperty)member).ReturnType;
			}
			else if (member is IEvent) {
				showStatic = false;
				return ((IEvent)member).ReturnType;
			}
			
			throw new InvalidOperationException ("Unknown member type:" + member);
		}
		
		public IDecoration SearchClassMember (IReturnType type, string memberName, bool includeMethods)
		{
			IDecoration member;
			IClass curType;
			if (SearchClassMember (type, memberName, includeMethods, out curType, out member))
				return member;
			else
				return null;
		}
		
		bool SearchClassMember (IReturnType type, string memberName, bool includeMethods, out IClass curType, out IDecoration member)
		{
			curType = null;
			member = null;
			
			if (type == null || memberName == null || memberName == "")
				return false;
			
			curType = SearchType (type, currentUnit);
			if (curType == null)
				return false;

			if (type.PointerNestingLevel != 0)
				return false;

			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0)
				curType = SearchType ("System.Array", null, null);
				
			return SearchClassMember (curType, curType, memberName, includeMethods, out curType, out member);
		}
		
		bool SearchClassMember (IClass qualifierClass, IClass curType, string memberName, bool includeMethods, out IClass resultType, out IDecoration member)
		{
			resultType = curType;
			
			if (curType.ClassType == ClassType.Enum) {
				foreach (IField f in curType.Fields) {
					if (f.Name == memberName && MustBeShown (qualifierClass, curType, f)) {
						showStatic = false;
						member = f; // enum members have the type of the enum
						return true;
					}
				}
			}
			if (showStatic) {
				foreach (IClass c in curType.InnerClasses) {
					if (c.Name == memberName && IsAccessible (qualifierClass, curType, c)) {
						member = c;
						return true;
					}
				}
			}
			foreach (IProperty p in curType.Properties) {
				if (p.Name == memberName && MustBeShown (qualifierClass, curType, p)) {
					showStatic = false;
					member = p;
					return true;
				}
			}
			foreach (IField f in curType.Fields) {
				if (f.Name == memberName && MustBeShown (qualifierClass, curType, f)) {
					showStatic = false;
					member = f;
					return true;
				}
			}
			foreach (IEvent e in curType.Events) {
				if (e.Name == memberName && MustBeShown (qualifierClass, curType, e)) {
					showStatic = false;
					member = e;
					return true;
				}
			}
			if (includeMethods) {
				foreach (IMethod m in curType.Methods) {
					if (m.Name == memberName && MustBeShown (qualifierClass, curType, m)) {
						showStatic = false;
						member = m;
						return true;
					}
				}
			}
			
			// Don't look in interfaces, unless the base type is already an interface.
			
			foreach (IReturnType baseType in curType.BaseTypes) {
				IClass c = parserContext.GetClass (baseType.FullyQualifiedName, baseType.GenericArguments, true, true);
				if (c != null && (c.ClassType != ClassType.Interface || curType.ClassType == ClassType.Interface)) {
					if (SearchClassMember (qualifierClass, c, memberName, includeMethods, out resultType, out member))
						return true;
				}
			}
			
			member = null;
			return false;
		}
		
		bool IsInside(Location between, Location start, Location end)
		{
			if (between.Y < start.Y || between.Y > end.Y) {
//				Console.WriteLine("Y = {0} not between {1} and {2}", between.Y, start.Y, end.Y);
				return false;
			}
			if (between.Y > start.Y) {
				if (between.Y < end.Y) {
					return true;
				}
				// between.Y == end.Y
//				Console.WriteLine("between.Y = {0} == end.Y = {1}", between.Y, end.Y);
//				Console.WriteLine("returning {0}:, between.X = {1} <= end.X = {2}", between.X <= end.X, between.X, end.X);
				return between.X <= end.X;
			}
			// between.Y == start.Y
//			Console.WriteLine("between.Y = {0} == start.Y = {1}", between.Y, start.Y);
			if (between.X < start.X) {
				return false;
			}
			// start is OK and between.Y <= end.Y
			return between.Y < end.Y || between.X <= end.X;
		}
		
		LocalVariable SearchVariable (string name)
		{
			System.Collections.Generic.List<ICSharpCode.NRefactory.Visitors.LocalLookupVariable> variables;
			if (!lookupTableVisitor.Variables.TryGetValue (name, out variables) || variables.Count <= 0) {
				return null;
			}
			
			foreach (LocalLookupVariable v in variables) {
				if (IsInside(new Location(caretColumn, caretLine), v.StartPos, v.EndPos)) {
					// The call to GetFullTypeName will return a type name with generics decoration
					IClass c = SearchType (ReturnType.GetFullTypeName (v.TypeRef), null, CompilationUnit);
					DefaultRegion reg = new DefaultRegion (v.StartPos.Line, v.StartPos.Column, v.EndPos.Line, v.EndPos.Column);
					reg.FileName = currentFile;
					return new LocalVariable (name, new ReturnType (v.TypeRef, c), "", reg);
				}
			}
			return null;
		}
		
		/// <remarks>
		/// does the dynamic lookup for the id
		/// </remarks>
		public ILanguageItem IdentifierLookup (string id)
		{
			// try if it exists a variable named id
			LocalVariable variable = SearchVariable (id);
			if (variable != null) {
				return variable;
			}
			
			if (callingClass == null) {
				return null;
			}
			
			// try if typeName is a method parameter
			IParameter p = SearchMethodParameter (id);
			if (p != null) {
				return p;
			}
			
			//// somehow search in callingClass fields is not returning anything, so I am searching here once again
			foreach (IField f in callingClass.Fields) {
				if (f.Name == id) {
					return f;
				}
			}
		
			// check if typeName == value in set method of a property
			if (id == "value") {
				IProperty pr = SearchProperty();
				if (pr != null) {
					return pr;
				}
			}
			
			// try if there exists a nonstatic member named typeName
			showStatic = false;
			IClass cls;
			IDecoration member;
			if (SearchClassMember (callingClass == null ? null : new ReturnType(callingClass.FullyQualifiedName), id, true, out cls, out member)) {
				return member;
			}
			
			// try if there exists a static member named typeName
			showStatic = true;
			if (SearchClassMember (callingClass == null ? null : new ReturnType(callingClass.FullyQualifiedName), id, true, out cls, out member)) {
				showStatic = false;
				return member;
			}
			
			// try if there exists a static member in outer classes named typeName
			foreach (IClass c in GetOuterClasses()) {
				if (SearchClassMember (callingClass == null ? null : new ReturnType(c.FullyQualifiedName), id, true, out cls, out member)) {
					showStatic = false;
					return member;
				}
			}
			return null;
		}
		
		/// <remarks>
		/// does the dynamic lookup for the typeName
		/// </remarks>
		public IReturnType DynamicLookup(string typeName)
		{
//			Console.WriteLine("starting dynamic lookup");
//			Console.WriteLine("name == " + typeName);
			
			// try if it exists a variable named typeName
			LocalVariable variable = SearchVariable (typeName);
			if (variable != null) {
				showStatic = false;
				return variable.ReturnType;
			}
//			Console.WriteLine("No Variable found");
			
			if (callingClass == null) {
				return null;
			}
			
			// try if typeName is a method parameter
			IParameter p = SearchMethodParameter(typeName);
			if (p != null) {
//				Console.WriteLine("MethodParameter Found");
				showStatic = false;
				return p.ReturnType;
			}
//			Console.WriteLine("No Parameter found");
			
			//// somehow search in callingClass fields is not returning anything, so I am searching here once again
			foreach (IField f in callingClass.Fields) {
				if (f.Name == typeName) {
//					Console.WriteLine("Field found " + f.Name);
					return f.ReturnType;
				}
			}
			//// end of mod for search in Fields
		
			// check if typeName == value in set method of a property
			if (typeName == "value") {
				IProperty pr = SearchProperty();
				if (pr != null) {
					showStatic = false;
					return pr.ReturnType;
				}
			}
//			Console.WriteLine("No Property found");
			
			// try if there exists a nonstatic member named typeName
			showStatic = false;
			IReturnType t = SearchMember(callingClass == null ? null : new ReturnType(callingClass.FullyQualifiedName), typeName);
			if (t != null) {
				return t;
			}
//			Console.WriteLine("No nonstatic member found");
			
			// try if there exists a static member named typeName
			// SearchMember will reset the showStatic flag if necessary
			showStatic = true;
			t = SearchMember(callingClass == null ? null : new ReturnType(callingClass.FullyQualifiedName), typeName);
			if (t != null)
				return t;
//			Console.WriteLine("No static member found");
			
			// try if there exists a static member in outer classes named typeName
			foreach (IClass c in GetOuterClasses()) {
				t = SearchMember(callingClass == null ? null : new ReturnType(c.FullyQualifiedName), typeName);
				if (t != null)
					return t;
			}
//			Console.WriteLine("No static member in outer classes found");
//			Console.WriteLine("DynamicLookUp resultless");
			return null;
		}
		
		public IMember GetMember ()
		{
			if (callingClass == null)
				return null;
			IMember mem = GetMethod ();
			if (mem != null)
				return mem;
			mem = GetProperty ();
			if (mem != null)
				return mem;
			return GetIndexer ();
		}
		
		IProperty GetProperty()
		{
			if (callingPropertyChecked)
				return callingProperty;
			
			callingPropertyChecked = true;
			if (callingClass != null && callingClass.Properties != null) { 
				foreach (IProperty property in callingClass.Properties) {
					if (property.BodyRegion != null && property.BodyRegion.IsInside(caretLine, caretColumn)) {
						return callingProperty = property;
					}
				}
			}
			return null;
		}
		
		IMethod GetMethod()
		{
			if (callingMethodChecked)
				return callingMethod;
			
			callingMethodChecked = true;
			if (callingClass != null && callingClass.Methods != null) { 
				foreach (IMethod method in callingClass.Methods) {
					if (method.Region != null && method.Region.IsInside (caretLine, caretColumn))
						return callingMethod = method;
					
					if (method.BodyRegion != null && method.BodyRegion.IsInside(caretLine, caretColumn))
						return callingMethod = method;
				}
			}
			
			return null;
		}
		
		IIndexer GetIndexer()
		{
			if (callingIndexerChecked)
				return callingIndexer;
			
			callingIndexerChecked = true;
			if (callingClass != null && callingClass.Indexer != null) { 
				foreach (IIndexer indexer in callingClass.Indexer) {
					if (indexer.BodyRegion != null && indexer.BodyRegion.IsInside(caretLine, caretColumn)) {
						return callingIndexer = indexer;
					}
				}
			}
			return null;
		}
		
		IProperty SearchProperty ()
		{
			IProperty property = GetProperty ();
			if (property == null) {
				return null;
			}
			if (property.SetterRegion != null && property.SetterRegion.IsInside(caretLine, caretColumn)) {
				return property;
			}
			return null;
		}
		
		IParameter SearchMethodParameter(string parameter)
		{
			IMethod method = GetMethod();
			if (method == null)
				return null;
			
			foreach (IParameter p in method.Parameters) {
				if (p.Name == parameter) {
					return p;
				}
			}
			
			return null;
		}
		
		/// <remarks>
		/// use the usings to find the correct name of a namespace
		/// </remarks>
		public string SearchNamespace(string name, ICompilationUnit unit)
		{
			// If the name matches an alias, try using the alias first.
			if (unit != null) {
				IReturnType aliasResult = FindAlias (name, unit);
				if (aliasResult != null) {
					// Don't provide the compilation unit when trying to resolve the alias,
					// since aliases are not affected by other 'using' directives.
					string ns = SearchNamespace (aliasResult.FullyQualifiedName, null);
					if (ns != null)
						return ns;
				}
			}
			
			if (parserContext.NamespaceExists (name)) {
				return name;
			}
			if (unit == null) {
				return null;
			}
			foreach (IUsing u in unit.Usings) {
				if (u != null && (u.Region == null || u.Region.IsInside(caretLine, caretColumn))) {
					string nameSpace = parserContext.SearchNamespace (u, name);
					if (nameSpace != null) {
						return nameSpace;
					}
				}
			}
			return null;
		}
		
		public IClass SearchType (IReturnType type, ICompilationUnit unit)
		{
			return SearchType (type.FullyQualifiedName, type.GenericArguments, unit);
		}
		
		/// <remarks>
		/// use the usings and the name of the namespace to find a class
		/// </remarks>
		public IClass SearchType (string name, ReturnTypeList genericArguments, ICompilationUnit unit)
		{
//			Console.WriteLine("Searching Type " + name);
			if (name == null || name == String.Empty)
				return null;
			
			IClass c;
			
			// Check if the name matches a type parameter of the enclosing method
			IMethod met = GetMethod ();
			if (met != null && met.GenericParameters != null) {
				c = FindTypeParameter (met.GenericParameters, name, unit);
				if (c != null) return c;
			}
			
			if (callingClass != null && callingClass.GenericParameters != null) {
				c = FindTypeParameter (callingClass.GenericParameters, name, unit);
				if (c != null) return c;
			}
			
			// If the name matches an alias, try using the alias first.
			if (unit != null) {
				
				// If the type name has a namespace name, try to find an alias for the namespace
				int i = name.IndexOf ('.');
				c = null;
				if (i != -1) {
					string aname = name.Substring (0,i);
					string clsName = name.Substring (i);
					IReturnType aliasResult = FindAlias (aname, unit);
					if (aliasResult != null) {
						// Don't provide the compilation unit when trying to resolve the alias,
						// since aliases are not affected by other 'using' directives.
						c = SearchType (aliasResult.FullyQualifiedName + clsName, genericArguments, null);
					}
				} else {
					// If it is a type alias, there is no need to look further
					IReturnType aliasResult = FindAlias (name, unit);
					if (aliasResult != null) {
						c = SearchType (aliasResult, null);
					}
				}
				if (c != null)
					return c;
			}
			
			// Look for an exact match
			
			c = parserContext.GetClass (name, genericArguments);
			if (c != null)
				return c;
				

			// The enclosing namespace has preference over the using directives.
			// Check it now.

			if (callingClass != null)
			{
				string fullname = callingClass.FullyQualifiedName;
				string[] namespaces = fullname.Split(new char[] {'.'});
				string curnamespace = "";
				int i = 0;
				
				do {
					curnamespace += namespaces[i] + '.';
					c = parserContext.GetClass (curnamespace + name, genericArguments);
					if (c != null) {
						return c;
					}
					i++;
				}
				while (i < namespaces.Length);
			
				// It may be an inner class
				
				IClass parentc = callingClass;
				do {
					c = parserContext.GetClass (parentc.FullyQualifiedName + "." + name, genericArguments);
					if (c != null && (c.IsPublic || c.IsProtected || c.IsInternal))
						return c;
					parentc = BaseClass (parentc);
				}
				while (parentc != null);
			}
			
			// Now try to find the class using the included namespaces
			
			if (unit != null) {
				foreach (IUsing u in unit.Usings) {
					if (u != null && (u.Region == null || u.Region.IsInside(caretLine, caretColumn))) {
						c = parserContext.SearchType (u, name, genericArguments);
						if (c != null)
							return c;
					}
				}
			}
			
			return null;
		}
		
		IClass FindTypeParameter (GenericParameterList gparams, string name, ICompilationUnit unit)
		{
			foreach (MonoDevelop.Projects.Parser.GenericParameter gp in gparams) {
				if (gp.Name == name) {
					if (gp.BaseTypes != null)
						return CreateParameterTypeClass (gp.Name, gp.BaseTypes, unit);
					else
						return parserContext.GetClass ("System.Object", null);
				}
			}
			return null;
		}
		
		IReturnType FindAlias (string name, ICompilationUnit unit)
		{
			// If the name matches an alias, try using the alias first.
			if (unit == null)
				return null;
				
			foreach (IUsing u in unit.Usings) {
				if (u != null && (u.Region == null || u.Region.IsInside(caretLine, caretColumn))) {
					IReturnType rt = u.GetAlias (name);
					if (rt != null)
						return rt;
				}
			}
			return null;
		}
		
		public TypeNameResolver CreateTypeNameResolver ()
		{
			if (currentUnit == null)
				return new TypeNameResolver ();
			else
				return new TypeNameResolver (currentUnit, caretLine, caretColumn);
		}
		
		/// <remarks>
		/// Returns true, if class possibleBaseClass is in the inheritance tree from c
		/// </remarks>
		bool IsClassInInheritanceTree(IClass possibleBaseClass, IClass c)
		{
			if (possibleBaseClass == null || c == null) {
				return false;
			}
			if (possibleBaseClass.FullyQualifiedName == c.FullyQualifiedName) {
				return true;
			}
			foreach (IReturnType baseClass in c.BaseTypes) {
				IClass bc = parserContext.GetClass (baseClass.FullyQualifiedName, baseClass.GenericArguments, true, true);
				if (IsClassInInheritanceTree(possibleBaseClass, bc)) {
					return true;
				}
			}
			return false;
		}
		
		/// <remarks>
		/// Returns the innerst class in which the carret currently is, returns null
		/// if the carret is outside any class boundaries.
		/// </remarks>
		IClass GetInnermostClass()
		{
			if (currentUnit != null) {
				foreach (IClass c in currentUnit.Classes) {
					if (c != null && ((c.Region != null && c.Region.IsInside(caretLine, caretColumn)) ||
						              (c.BodyRegion != null && c.BodyRegion.IsInside(caretLine, caretColumn))))
					{
						return GetInnermostClass(c);
					}
				}
			}
			return null;
		}
		
		IClass GetInnermostClass(IClass curClass)
		{
			if (curClass == null) {
				return null;
			}
			if (curClass.InnerClasses == null) {
				return GetResolvedClass (curClass);
			}
			foreach (IClass c in curClass.InnerClasses) {
				if (c != null && ((c.Region != null && c.Region.IsInside(caretLine, caretColumn)) ||
					              (c.BodyRegion != null && c.BodyRegion.IsInside(caretLine, caretColumn))))
					return GetInnermostClass(c);
			}
			return GetResolvedClass (curClass);
		}
		
		/// <remarks>
		/// Returns all (nestet) classes in which the carret currently is exept
		/// the innermost class, returns an empty collection if the carret is in 
		/// no class or only in the innermost class.
		/// the most outer class is the last in the collection.
		/// </remarks>
		ClassCollection GetOuterClasses()
		{
			ClassCollection classes = new ClassCollection();
			if (currentUnit != null) {
				foreach (IClass c in currentUnit.Classes) {
					if (c != null && c.BodyRegion != null && c.BodyRegion.IsInside(caretLine, caretColumn)) {
						if (c != GetInnermostClass()) {
							GetOuterClasses(classes, c);
							classes.Add(GetResolvedClass (c));
						}
						break;
					}
				}
			}
			
			return classes;
		}
		
		void GetOuterClasses(ClassCollection classes, IClass curClass)
		{
			if (curClass != null) {
				foreach (IClass c in curClass.InnerClasses) {
					if (c != null && c.BodyRegion != null && c.BodyRegion.IsInside(caretLine, caretColumn)) {
						if (c != GetInnermostClass()) {
							GetOuterClasses(classes, c);
							classes.Add(GetResolvedClass (c));
						}
						break;
					}
				}
			}
		}
		
		public IClass GetResolvedClass (IClass cls)
		{
			// Returns an IClass in which all type names have been properly resolved
			return parserContext.GetClass (cls.FullyQualifiedName);
		}

		public LanguageItemCollection IsAsResolve (string expression, int caretLine, int caretColumn, string fileName, string fileContent, bool excludeInterfaces)
		{
			LanguageItemCollection result = new LanguageItemCollection ();
			SetCursorPosition (caretLine, caretColumn);
			
			IParseInformation parseInfo = parserContext.GetParseInformation (fileName);
			ICSharpCode.NRefactory.Ast.CompilationUnit fcu = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
			if (fcu == null)
				return null;
			ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser (SupportedLanguage.CSharp, new StringReader (expression));
			Expression expr = p.ParseExpression ();
			if (expr == null)
				return null;

			lookupTableVisitor = new LookupTableVisitor (SupportedLanguage.CSharp);
			lookupTableVisitor.VisitCompilationUnit (fcu, null);

			TypeVisitor typeVisitor = new TypeVisitor (this);

			CSharpVisitor csharpVisitor = new CSharpVisitor ();
			currentUnit = (ICompilationUnit)csharpVisitor.VisitCompilationUnit (fcu, null);
			currentFile = fileName;
			if (currentUnit != null) {
				callingClass = GetInnermostClass ();
			}
			IReturnType type = new ReturnType ("System.Object");
//			IReturnType type = expr.AcceptVisitor (typeVisitor, null) as IReturnType;
//			if (type == null || type.PointerNestingLevel != 0) {
//				fcu = parserContext.ParseFile (fileName, fileContent).MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
//				lookupTableVisitor.VisitCompilationUnit (fcu, null);
//				currentUnit = (ICompilationUnit)csharpVisitor.VisitCompilationUnit (fcu, null);
//
//				if (currentUnit != null) {
//					callingClass = GetInnermostClass ();
//				}
//				type = expr.AcceptVisitor (typeVisitor, null) as IReturnType;
//				if (type == null)
//					return null;
//			}
//			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0)
//				type = new ReturnType ("System.Array");

			IClass returnClass = SearchType (type, currentUnit);
//			IClass returnClass = parserContext.SearchType (type.FullyQualifiedName, null, currentUnit);
			if (returnClass == null)
				return null;
				
			// Get the list of namespaces where subclasses have to be searched.
			// Include all namespaces for which there is an "using".
			List<string> ns = new List<string> ();
			if (currentUnit != null && currentUnit.Usings != null) {
				foreach (IUsing us in currentUnit.Usings)
					ns.AddRange (us.Usings);
			}
			// Include the calling class namesapce and all its parent namespaces
			if (callingClass != null) {
				string[] namespaceParts = callingClass.Namespace.Split ('.');
				string cns = "";
				foreach (string s in namespaceParts) {
					if (cns.Length > 0)
						cns += ".";
					cns += s;
					ns.Add (cns);
				}
			}
//			Stack<IReturnType> baseTypes = new Stack<IReturnType> ();
//			baseTypes.Push (type);
//			do {
//				IClass c = SearchType (baseTypes.Pop (), currentUnit);
//				if (c != null) {
//					if (!result.Contains (c) && !(excludeInterfaces && (c.ClassType == ClassType.Interface || c.IsAbstract)))
//						result.Add (c);
//					foreach (IReturnType retType in c.BaseTypes) {
//						baseTypes.Push (retType);
//					}
//				}
//			} while (baseTypes.Count > 0);
			
			foreach (IClass iclass in parserContext.GetSubclassesTree (returnClass, ns.ToArray ())) {
				if (!result.Contains (iclass) && !(excludeInterfaces && (iclass.ClassType == ClassType.Interface || iclass.IsAbstract)))
					result.Add (iclass);
			}
			
			IMethod met = GetMethod ();
			if (met != null && met.GenericParameters != null)
				FindTypeParameterSubclasses (result, met.GenericParameters, returnClass, currentUnit);
			
			if (callingClass != null && callingClass.GenericParameters != null)
				FindTypeParameterSubclasses (result, callingClass.GenericParameters, returnClass, currentUnit);
			
			// Include all namespaces as well
			foreach (string nss in parserContext.GetNamespaceList ("", true, true))
				result.Add (new Namespace (nss));
			
			return result;
		}
		
		void FindTypeParameterSubclasses (LanguageItemCollection result, GenericParameterList gparams, IClass baseClass, ICompilationUnit unit)
		{
			foreach (MonoDevelop.Projects.Parser.GenericParameter gp in gparams) {
				if (gp.BaseTypes != null) {
					foreach (IReturnType rt in gp.BaseTypes) {
						IClass cls = SearchType (rt, unit);
						if (IsClassInInheritanceTree (baseClass, cls)) {
							result.Add (CreateParameterTypeClass (gp.Name, gp.BaseTypes, unit));
							break;
						}
					}
				}
				else {
					IClass cls = parserContext.GetClass ("System.Object", null);
					if (IsClassInInheritanceTree (baseClass, cls)) {
						result.Add (CreateParameterTypeClass (gp.Name, null, unit));
						break;
					}
				}
			}
		}
		
		IClass CreateParameterTypeClass (string name, ReturnTypeList btypes, ICompilationUnit unit)
		{
			DefaultClass c = new DefaultClass (unit);
			c.FullyQualifiedName = name;
			if (btypes != null)
				c.BaseTypes.AddRange (btypes);
			return c;
		}
		
		public LanguageItemCollection CtrlSpace (int caretLine, int caretColumn, string fileName)
		{
			LanguageItemCollection result = new LanguageItemCollection ();
// Why whas it here ? (I've removed it to remove dupes int/int for example) Mike			
//			foreach (System.Collections.Generic.KeyValuePair<string, string> pt in TypeReference.PrimitiveTypesCSharp) 
//				result.Add (new Namespace (pt.Key));

			SetCursorPosition (caretLine, caretColumn);
			IParseInformation parseInfo = parserContext.GetParseInformation (fileName);
			ICSharpCode.NRefactory.Ast.CompilationUnit fileCompilationUnit = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.NRefactory.Ast.CompilationUnit;
			if (fileCompilationUnit == null) {
				Console.WriteLine("!Warning: no parseinformation!");
				return null;
			}
			lookupTableVisitor = new LookupTableVisitor(SupportedLanguage.CSharp);
			lookupTableVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			CSharpVisitor cSharpVisitor = new CSharpVisitor();
			currentUnit = (ICompilationUnit)cSharpVisitor.VisitCompilationUnit (fileCompilationUnit, null);
			currentFile = fileName;
			if (currentUnit != null) {
				SetCursorPosition (caretLine, caretColumn);
				callingClass = GetInnermostClass();
//				Console.WriteLine("CallingClass is " + (callingClass == null ? "null" : callingClass.Name));
			}

			IMethod met = GetMethod ();
			Hashtable vars = new Hashtable ();
			if (met != null) {
				foreach (IParameter par in met.Parameters) {
					result.Add(par);
					vars [par.Name] = par;
				}
			}
			
			foreach (string name in lookupTableVisitor.Variables.Keys) {
				if (vars.Contains (name))
					continue;
				ICollection variables = lookupTableVisitor.Variables[name];
				if (variables != null && variables.Count > 0) {
					foreach (LocalLookupVariable v in variables) {
						if (IsInside(new Location(caretColumn, caretLine), v.StartPos, v.EndPos)) {
							result.Add(new DefaultParameter (null, name, new ReturnType (ReturnType.GetSystemType (v.TypeRef.Type))));
							break;
						}
					}
				}
			}
			if (callingClass != null) {
				showStatic = true;
				ListMembers (result, callingClass, callingClass);
				IProperty prop = GetProperty ();
				
				if (prop != null && prop.SetterRegion != null && prop.SetterRegion.IsInside (caretLine, caretColumn)) {
					result.Add (new DefaultParameter (null, "value", prop.ReturnType));					           
				}
				IIndexer indexer = GetIndexer();
				if ((met != null && !met.IsStatic) || (prop != null && !prop.IsStatic) || (indexer != null)) { 
					result.Add (new DefaultParameter (null, "this", new ReturnType(callingClass.FullyQualifiedName)));					            
					result.Add (new DefaultParameter (null, "base", new ReturnType(callingClass.BaseTypes.Count > 0 ? callingClass.BaseTypes[0].FullyQualifiedName : "object")));					            
					showStatic = false;
					ListMembers (result, callingClass, callingClass);
				}
				
				// Add classes from calling namespace
				if (callingClass.Namespace.Length > 0)
					result.AddRange (parserContext.GetNamespaceContents (callingClass.Namespace, true));
			}
			string n = "";
			
			// Add contents of the default namespace
			result.AddRange(parserContext.GetNamespaceContents (n, true));
			
			// Add contents of imported namespaces
			foreach (IUsing u in currentUnit.Usings) {
				if (u != null && (u.Region == null || u.Region.IsInside(caretLine, caretColumn))) {
					foreach (string name in u.Usings) {
						result.AddRange(parserContext.GetNamespaceContents (name, true));
					}
					foreach (string alias in u.Aliases) {
						result.Add(new Namespace (alias));
					}
				}
			}
			return result;
		}
	}
}