bool Fix(TP X) { Log("Trying to fix " + X); Log(" bounds = ", X.Bounds); List <IReturnType> candidates = new List <IReturnType>(X.Bounds); foreach (IReturnType U in X.Bounds) { candidates.RemoveAll((IReturnType candidate) => !MemberLookupHelper.ConversionExists(U, candidate)); } Log(" candidates after removal round = ", candidates); if (candidates.Count == 0) { return(false); } var results = candidates.Where( c1 => candidates.All(c2 => MemberLookupHelper.ConversionExists(c1, c2)) ).ToList(); Log(" possible solutions (should be exactly one) = ", candidates); if (results.Count == 1) { X.FixedTo = results[0]; return(true); } else { return(false); } }
public void ArrayOfStringIsApplicableOnArrayOfT() { Assert.IsTrue(IsApplicable(new ArrayReturnType(msc, msc.SystemTypes.String, 1), new ArrayReturnType(msc, CreateT(), 1))); Assert.IsFalse(MemberLookupHelper.ConversionExists(new ArrayReturnType(msc, msc.SystemTypes.String, 1), new ArrayReturnType(msc, CreateT(), 1))); }
public void DisposableClassIsApplicableOnDisposableT() { Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.GetClass("System.CharEnumerator", 0).DefaultReturnType, CreateTWithDisposableConstraint())); Assert.IsTrue(IsApplicable(msc.GetClass("System.CharEnumerator", 0).DefaultReturnType, CreateTWithDisposableConstraint())); }
public void NoConversionExistsFromParameterlessAnonymousDelegateToSystemPredicate() { AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc)); amrt.MethodParameters = new List <IParameter>(); Assert.IsFalse(MemberLookupHelper.ConversionExists( amrt, new GetClassReturnType(msc, "System.Predicate", 1) )); }
public void NoConversionExistsFromStringToDisposableT() { // no conversion exists Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.SystemTypes.String, CreateTWithDisposableConstraint())); // but it is applicable (applicability ignores constraints) Assert.IsTrue(IsApplicable(msc.SystemTypes.String, CreateTWithDisposableConstraint())); }
public void StringIsApplicableOnT() { // no conversion exists Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.SystemTypes.String, CreateT())); // but it is applicable Assert.IsTrue(IsApplicable(msc.SystemTypes.String, CreateT())); }
public void ConversionDoesNotExistFromAnonymousDelegateWithParameterToSystemPredicateWhenParameterTypeIsIncompatible() { AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc)); amrt.MethodReturnType = msc.SystemTypes.Boolean; amrt.MethodParameters = new List <IParameter>(); amrt.MethodParameters.Add(new DefaultParameter("test", msc.SystemTypes.String, DomRegion.Empty)); Assert.IsFalse(MemberLookupHelper.ConversionExists( amrt, new ConstructedReturnType(new GetClassReturnType(msc, "System.Predicate", 1), new IReturnType[] { msc.SystemTypes.Int32 }) )); }
public void ConversionExistsFromClassDerivingFromListOfStringToListOfString() { Assert.IsTrue(MemberLookupHelper.ConversionExists(CreateClassDerivingFromListOfString().DefaultReturnType, ListOf(msc.SystemTypes.String))); }
public void ConversionExistsFromStringListToStringEnumerable() { Assert.IsTrue(MemberLookupHelper.ConversionExists(ListOf(msc.SystemTypes.String), EnumerableOf(msc.SystemTypes.String))); }
public void NoConversionExistsFromStringIListToIntEnumerable() { Assert.IsFalse(MemberLookupHelper.ConversionExists(IListOf(msc.SystemTypes.String), EnumerableOf(msc.SystemTypes.Int32))); }
public void NoConversionExistsFromStringEnumerableToObjectEnumerable() { Assert.IsFalse(MemberLookupHelper.ConversionExists(EnumerableOf(msc.SystemTypes.String), EnumerableOf(msc.SystemTypes.Object))); }
public void ConversionExistsFromStringArrayToStringEnumerable() { Assert.IsTrue(MemberLookupHelper.ConversionExists(new ArrayReturnType(msc, msc.SystemTypes.String, 1), EnumerableOf(msc.SystemTypes.String))); }
public void NoConversionExistsFromObjectArrayToStringArray() { Assert.IsFalse(MemberLookupHelper.ConversionExists(new ArrayReturnType(msc, msc.SystemTypes.Object, 1), new ArrayReturnType(msc, msc.SystemTypes.String, 1))); }
/// <summary> /// Generates the completion data. This method is called by the text editor control. /// </summary> public override ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) { List <ICompletionData> completionData = new List <ICompletionData>(); // delegate { } completionData.Add(new DelegateCompletionData("delegate { };", 3, "${res:CSharpBinding.InsertAnonymousMethod}")); CSharpAmbience ambience = new CSharpAmbience(); // get eventHandler type name incl. type argument list ambience.ConversionFlags = ConversionFlags.ShowParameterNames | ConversionFlags.ShowTypeParameterList | ConversionFlags.UseFullyQualifiedTypeNames; string eventHandlerFullyQualifiedTypeName = ambience.Convert(resolvedReturnType); ambience.ConversionFlags = ConversionFlags.ShowParameterNames | ConversionFlags.ShowTypeParameterList; string eventHandlerTypeName = ambience.Convert(resolvedReturnType); // retrieve Invoke method from resolvedReturnType instead of resolvedClass to get a method where // type arguments are substituted. IMethod invoke = resolvedReturnType.GetMethods().Find(delegate(IMethod m) { return(m.Name == "Invoke"); }); StringBuilder parameterString = new StringBuilder(); if (invoke != null) { // build the parameter string for (int i = 0; i < invoke.Parameters.Count; ++i) { if (i > 0) { parameterString.Append(", "); } parameterString.Append(ambience.Convert(invoke.Parameters[i])); } // delegate(object sender, EventArgs e) { }; StringBuilder anonMethodWithParametersBuilder = new StringBuilder("delegate(").Append(parameterString.ToString()).Append(") { };"); completionData.Add(new DelegateCompletionData(anonMethodWithParametersBuilder.ToString(), 3, "${res:CSharpBinding.InsertAnonymousMethodWithParameters}")); // new EventHandler(ClassName_EventName); IClass callingClass = resolveResult.CallingClass; bool inStatic = false; if (resolveResult.CallingMember != null) { inStatic = resolveResult.CallingMember.IsStatic; } // ...build the new handler name... string newHandlerName = BuildHandlerName(); if (newHandlerName == null) { MemberResolveResult mrr = resolveResult as MemberResolveResult; IEvent eventMember = (mrr != null ? mrr.ResolvedMember as IEvent : null); newHandlerName = ((callingClass != null) ? callingClass.Name : "callingClass") + "_" + ((eventMember != null) ? eventMember.Name : "eventMember"); } // ...build the completion text... StringBuilder newHandlerTextBuilder = new StringBuilder("new ").Append(eventHandlerTypeName).Append("("); newHandlerTextBuilder.Append(newHandlerName).Append(");"); // ...build the optional new method text... StringBuilder newHandlerCodeBuilder = new StringBuilder(); newHandlerCodeBuilder.AppendLine().AppendLine(); if (inStatic) { newHandlerCodeBuilder.Append("static "); } newHandlerCodeBuilder.Append(ambience.Convert(invoke.ReturnType)).Append(" ").Append(newHandlerName); newHandlerCodeBuilder.Append("(").Append(parameterString.ToString()).AppendLine(")"); newHandlerCodeBuilder.AppendLine("{"); newHandlerCodeBuilder.AppendLine("throw new NotImplementedException();"); newHandlerCodeBuilder.Append("}"); // ...and add it to the completionData. completionData.Add(new NewEventHandlerCompletionData( newHandlerTextBuilder.ToString(), 2 + newHandlerName.Length, newHandlerName.Length, "new " + eventHandlerFullyQualifiedTypeName + "(" + newHandlerName + StringParser.Parse(")\n${res:CSharpBinding.GenerateNewHandlerInstructions}\n") + CodeCompletionData.ConvertDocumentation(resolvedClass.Documentation), resolveResult, newHandlerCodeBuilder.ToString() )); if (callingClass != null) { foreach (IMethod method in callingClass.DefaultReturnType.GetMethods()) { if (inStatic && !method.IsStatic) { continue; } if (!method.IsAccessible(callingClass, true)) { continue; } if (method.Parameters.Count != invoke.Parameters.Count) { continue; } // check return type compatibility: if (!MemberLookupHelper.ConversionExists(method.ReturnType, invoke.ReturnType)) { continue; } bool ok = true; for (int i = 0; i < invoke.Parameters.Count; i++) { if (!MemberLookupHelper.ConversionExists(invoke.Parameters[i].ReturnType, method.Parameters[i].ReturnType)) { ok = false; break; } } if (ok) { completionData.Add(new CodeCompletionData(method)); } } } } return(completionData.ToArray()); }