Exemplo n.º 1
0
		void AddTypesAndNamespaces (CompletionDataWrapper wrapper, CppResolver state, AstNode node, Predicate<IType> typePred = null, Predicate<IMember> memberPred = null)
		{
			if (currentType != null) {
				for (var ct = currentType; ct != null; ct = ct.DeclaringTypeDefinition) {
					foreach (var nestedType in ct.NestedTypes) {
						if (typePred == null || typePred (nestedType.Resolve (ctx))) {
							string name = nestedType.Name;
							if (node is Attribute && name.EndsWith ("Attribute") && name.Length > "Attribute".Length)
								name = name.Substring (0, name.Length - "Attribute".Length);
							wrapper.AddType (nestedType, name);
						}
					}
				}
				if (this.currentMember != null && !(node is AstType)) {
					var def = ctx.CurrentTypeDefinition ?? Compilation.MainAssembly.GetTypeDefinition (currentType);
					if (def != null) {
						foreach (var member in def.GetMembers ()) {
							if (member is IMethod && ((IMethod)member).FullName == "System.Object.Finalize")
								continue;
							if (member.EntityType == EntityType.Operator)
								continue;
							if (memberPred == null || memberPred (member))
								wrapper.AddMember (member);
						}
						var declaring = def.DeclaringTypeDefinition;
						while (declaring != null) {
							foreach (var member in declaring.GetMembers (m => m.IsStatic)) {
								if (memberPred == null || memberPred (member))
									wrapper.AddMember (member);
							}
							declaring = declaring.DeclaringTypeDefinition;
						}
					}
				}
				foreach (var p in currentType.TypeParameters) {
					wrapper.AddTypeParameter (p);
				}
			}
			var scope = CSharpParsedFile.GetUsingScope (location).Resolve (Compilation);
			
			for (var n = scope; n != null; n = n.Parent) {
				foreach (var pair in n.UsingAliases) {
					wrapper.AddNamespace (pair.Key);
				}
				foreach (var u in n.Usings) {
					foreach (var type in u.Types) {
						if (typePred == null || typePred (type)) {
							string name = type.Name;
							if (node is Attribute && name.EndsWith ("Attribute") && name.Length > "Attribute".Length)
								name = name.Substring (0, name.Length - "Attribute".Length);
							wrapper.AddType (type, name);
						}
					}
				}
				
				foreach (var type in n.Namespace.Types) {
					if (typePred == null || typePred (type))
						wrapper.AddType (type, type.Name);
				}
				
				foreach (var curNs in n.Namespace.ChildNamespaces) {
					wrapper.AddNamespace (curNs.Name);
				}
			}
		}
Exemplo n.º 2
0
		IEnumerable<ICompletionData> CreateCompletionData (TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CppResolver state)
		{
			if (resolveResult == null /*|| resolveResult.IsError*/)
				return null;
			
			if (resolveResult is NamespaceResolveResult) {
				var nr = (NamespaceResolveResult)resolveResult;
				var namespaceContents = new CompletionDataWrapper (this);
				
				foreach (var cl in nr.Namespace.Types) {
					namespaceContents.AddType (cl, cl.Name);
				}
				
				foreach (var ns in nr.Namespace.ChildNamespaces) {
					namespaceContents.AddNamespace (ns.Name);
				}
				return namespaceContents.Result;
			}
			
			IType type = resolveResult.Type;
			var typeDef = resolveResult.Type.GetDefinition ();
			var result = new CompletionDataWrapper (this);
			bool includeStaticMembers = false;
			
			if (resolveResult is LocalResolveResult) {
				if (resolvedNode is IdentifierExpression) {
					var mrr = (LocalResolveResult)resolveResult;
					includeStaticMembers = mrr.Variable.Name == mrr.Type.Name;
				}
			}
			if (resolveResult is TypeResolveResult && type.Kind == TypeKind.Enum) {
				foreach (var field in type.GetFields ()) {
					result.AddMember (field);
				}
				foreach (var m in type.GetMethods ()) {
					if (m.Name == "TryParse")
						result.AddMember (m);
				}
				return result.Result;
			}
			
			if (resolveResult is MemberResolveResult && resolvedNode is IdentifierExpression) {
				var mrr = (MemberResolveResult)resolveResult;
				includeStaticMembers = mrr.Member.Name == mrr.Type.Name;
				
				// ADD Aliases
				var scope = CSharpParsedFile.GetUsingScope (location).Resolve (Compilation);
			
				for (var n = scope; n != null; n = n.Parent) {
					foreach (var pair in n.UsingAliases) {
						if (pair.Key == mrr.Member.Name) {
							foreach (var r in CreateCompletionData (location, pair.Value, resolvedNode, state)) {
								if (r is IEntityCompletionData && ((IEntityCompletionData)r).Entity is IMember) {
									result.AddMember ((IMember)((IEntityCompletionData)r).Entity);
								} else {
									result.Add (r);
								}
							}
						}
					}
				}				
				
				
			}
			if (resolveResult is TypeResolveResult && (resolvedNode is IdentifierExpression || resolvedNode is MemberReferenceExpression)) {
				includeStaticMembers = true;
			}
			
//			Console.WriteLine ("type:" + type +"/"+type.GetType ());
//			Console.WriteLine ("current:" + ctx.CurrentTypeDefinition);
//			Console.WriteLine ("IS PROT ALLOWED:" + isProtectedAllowed + " static: "+ includeStaticMembers);
//			Console.WriteLine (resolveResult);
//			Console.WriteLine ("node:" + resolvedNode);
//			Console.WriteLine (currentMember !=  null ? currentMember.IsStatic : "currentMember == null");
			
			if (resolvedNode.Annotation<ObjectCreateExpression> () == null) { //tags the created expression as part of an object create expression.
				var lookup = new MemberLookup (ctx.CurrentTypeDefinition, Compilation.MainAssembly);
				bool isProtectedAllowed = resolveResult is ThisResolveResult ? true : lookup.IsProtectedAccessAllowed (type);
			
				var filteredList = new List<IMember> ();
				foreach (var member in type.GetMembers ()) {
//					Console.WriteLine ("member:" + member + member.IsShadowing);
					if (!lookup.IsAccessible (member, isProtectedAllowed)) {
//						Console.WriteLine ("skip access: " + member.FullName);
						continue;
					}
					if (resolvedNode is BaseReferenceExpression && member.IsAbstract)
						continue;
					bool memberIsStatic = member.IsStatic;
					if (!includeStaticMembers && memberIsStatic && !(resolveResult is TypeResolveResult)) {
//						Console.WriteLine ("skip static member: " + member.FullName);
						continue;
					}
					var field = member as IField;
					if (field != null)
						memberIsStatic |= field.IsConst;
					
					if (!memberIsStatic && (resolveResult is TypeResolveResult)) {
						continue;
					}
					
					if (member is IMethod && ((IMethod)member).FullName == "System.Object.Finalize")
						continue;
					if (member.EntityType == EntityType.Operator)
						continue;
					if (member.IsShadowing)
						filteredList.RemoveAll (m => m.Name == member.Name);
					
					filteredList.Add (member);
				}
				
				foreach (var member in filteredList) {
//					Console.WriteLine ("add : "+ member.FullName + " --- " + member.IsStatic);
					result.AddMember (member);
				}
			}
			
			if (resolveResult is TypeResolveResult || includeStaticMembers) {
				foreach (var nested in type.GetNestedTypes ()) {
					result.AddType (nested, nested.Name);
				}
				
			} else {
				foreach (var meths in state.GetExtensionMethods (type)) {
					foreach (var m in meths) {
						result.AddMember (m);
					}
				}
			}
			
//			IEnumerable<object> objects = resolveResult.CreateResolveResult (dom, resolver != null ? resolver.CallingMember : null);
//			CompletionDataCollector col = new CompletionDataCollector (this, dom, result, Document.CompilationUnit, resolver != null ? resolver.CallingType : null, location);
//			col.HideExtensionParameter = !resolveResult.StaticResolve;
//			col.NamePrefix = expressionResult.Expression;
//			bool showOnlyTypes = expressionResult.Contexts.Any (ctx => ctx == ExpressionContext.InheritableType || ctx == ExpressionContext.Constraints);
//			if (objects != null) {
//				foreach (object obj in objects) {
//					if (expressionResult.ExpressionContext != null && expressionResult.ExpressionContext.FilterEntry (obj))
//						continue;
//					if (expressionResult.ExpressionContext == ExpressionContext.NamespaceNameExcepted && !(obj is Namespace))
//						continue;
//					if (showOnlyTypes && !(obj is IType))
//						continue;
//					CompletionData data = col.Add (obj);
//					if (data != null && expressionResult.ExpressionContext == ExpressionContext.Attribute && data.CompletionText != null && data.CompletionText.EndsWith ("Attribute")) {
//						string newText = data.CompletionText.Substring (0, data.CompletionText.Length - "Attribute".Length);
//						data.SetText (newText);
//					}
//				}
//			}
			
			return result.Result;
		}
Exemplo n.º 3
0
		IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData (TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CppResolver state)
		{
			if (resolveResult == null || resolveResult.IsError)
				return null;
			var result = new CompletionDataWrapper (this);
			if (resolveResult is NamespaceResolveResult) {
				var nr = (NamespaceResolveResult)resolveResult;
				foreach (var cl in nr.Namespace.Types) {
					result.AddType (cl, cl.Name);
				}
				foreach (var ns in nr.Namespace.ChildNamespaces) {
					result.AddNamespace (ns.Name);
				}
			} else if (resolveResult is TypeResolveResult) {
				var type = resolveResult.Type;
				foreach (var nested in type.GetNestedTypes ()) {
					result.AddType (nested, nested.Name);
				}
			}
			return result.Result;
		}
Exemplo n.º 4
0
		void AddEnumMembers (CompletionDataWrapper completionList, IType resolvedType, CppResolver state)
		{
			if (resolvedType.Kind != TypeKind.Enum)
				return;
			string typeString = GetShortType (resolvedType, state);
			if (typeString.Contains ("."))
				completionList.AddType (resolvedType, typeString);
			foreach (var field in resolvedType.GetFields ()) {
				if (field.IsConst || field.IsStatic)
					completionList.Result.Add (factory.CreateEntityCompletionData (field, typeString + "." + field.Name));
			}
			DefaultCompletionString = typeString;
		}
Exemplo n.º 5
0
		IEnumerable<ICompletionData> CreateTypeCompletionData (IType hintType, AstType hintTypeAst)
		{
			var wrapper = new CompletionDataWrapper (this);
			var state = GetState ();
			Predicate<IType> pred = null;
			if (hintType != null) {
				
				if (hintType.Kind != TypeKind.Unknown) {
					var lookup = new MemberLookup (ctx.CurrentTypeDefinition, Compilation.MainAssembly);
					pred = t => {
						// check if type is in inheritance tree.
						if (hintType.GetDefinition () != null && !t.GetDefinition ().IsDerivedFrom (hintType.GetDefinition ()))
							return false;
						
						// check for valid constructors
						if (t.GetConstructors ().Count () == 0)
							return true;
						bool isProtectedAllowed = currentType != null ? currentType.Resolve (ctx).GetDefinition ().IsDerivedFrom (t.GetDefinition ()) : false;
						return t.GetConstructors ().Any (m => lookup.IsAccessible (m, isProtectedAllowed));
					};
					DefaultCompletionString = GetShortType (hintType, GetState ());
					wrapper.AddType (hintType, DefaultCompletionString);
				} else {
					DefaultCompletionString = hintTypeAst.ToString ();
					wrapper.AddType (hintType, DefaultCompletionString);
				}
			} 
			AddTypesAndNamespaces (wrapper, state, null, pred, m => false);
			AddKeywords (wrapper, primitiveTypesKeywords.Where (k => k != "void"));
			CloseOnSquareBrackets = true;
			AutoCompleteEmptyMatch = true;
			return wrapper.Result;
		}