/// <summary> /// Compute the label from the prototype elements /// </summary> /// <param name="knownCodeTypes"></param> /// <param name="name"></param> /// <param name="returnType"></param> /// <param name="parameters"></param> public static string ComputeLabel(KnownCodeTypes knownCodeTypes, string name, string returnType, List<KeyValuePair<string, string>> parameters) { List<string> parameterString = new List<string>(); foreach (KeyValuePair<string, string> parameter in parameters) { string parameterName = parameter.Key; string fullTypeName = parameter.Value; string shortTypeName = VariableLabel.SimplifyTypeName(knownCodeTypes, fullTypeName); parameterString.Add(shortTypeName + " " + parameterName); } string returnShortType = VariableLabel.SimplifyTypeName(knownCodeTypes, returnType); /* if (knownCodeTypes != null) { CodeType codeType = knownCodeTypes.GetFullNamedType(returnType); if (codeType != null) returnShortType = KnownCodeTypes.GetShortTypeName(codeType); } */ StringBuilder sb = new StringBuilder(); if (!string.IsNullOrEmpty(returnShortType)) sb.Append(returnShortType.Trim() + " "); sb.Append(name); sb.Append('('); sb.Append(string.Join(", ", parameterString.ToArray())); sb.Append(')'); return sb.ToString(); }
/// <summary> /// Computes the label from name and full type name /// </summary> /// <param name="knownCodeTypes">Known code types</param> /// <param name="fullTypeName">Full type name for the variable</param> /// <param name="name">Name of the variable</param> /// <returns>The label</returns> public static string ComputeLabel(KnownCodeTypes knownCodeTypes, string fullTypeName, string name) { string label = name; if (!string.IsNullOrEmpty(fullTypeName)) { if (knownCodeTypes == null) label += " : " + KnownCodeTypes.SimplifyForCSharp(fullTypeName); else { string simpleType = SimplifyTypeName(knownCodeTypes, fullTypeName); label += " : " + simpleType; } } return label; }
/// <summary> /// Get the full type name from the simplifier type name /// </summary> /// <param name="knownCodeTypes">Known code types</param> /// <param name="simplifiedType">Simplified type</param> /// <returns>Full type name</returns> public static string GetFullTypeName(KnownCodeTypes knownCodeTypes, string simplifiedType) { string simplifiedTypeOriginal = simplifiedType; string suffix = ""; bool modified = true; while (modified) { modified = false; if (simplifiedType.EndsWith("[]")) { simplifiedType = simplifiedType.Substring(0, simplifiedType.Length - 2); suffix = "[]" + suffix; modified = true; } else if (simplifiedType.EndsWith("[,]")) { simplifiedType = simplifiedType.Substring(0, simplifiedType.Length - 3); suffix = "[,]" + suffix; modified = true; } } List<CodeType> possibleTypes; if (knownCodeTypes != null) possibleTypes = InferType(knownCodeTypes, simplifiedType); else possibleTypes = new List<CodeType>(); string type; if (possibleTypes.Count == 0) type = simplifiedTypeOriginal; else if (possibleTypes.Count == 1) type = KnownCodeTypes.SimplifyForCSharp(possibleTypes[0].FullName.Replace("+", ".")) + suffix; else type = ChooseType(simplifiedType, possibleTypes) + suffix; return type; }
/// <summary> /// Get the KnownCodeTypes associated to a project. /// </summary> /// <param name="project"></param> /// <returns></returns> public static KnownCodeTypes FromProject(Project project) { if (project.CodeModel == null) return null; KnownCodeTypes knownCodeTypes = new KnownCodeTypes(project); return knownCodeTypes; }
/// <summary> /// Parse the label /// </summary> /// <param name="knownCodeTypes"></param> /// <param name="label"></param> /// <param name="name"></param> /// <param name="returnType"></param> /// <param name="parameters"></param> public static void ParseLabel(KnownCodeTypes knownCodeTypes, string label, out string name, out string returnType, out List<KeyValuePair<string, string>> parameters) { // Verify parenthesis int indexBeginningOfParameters = label.IndexOf('('); int indexEndOfParametes = label.LastIndexOf(')'); if ((indexBeginningOfParameters == -1) || (indexEndOfParametes == -1)) throw new ArgumentException(string.Format("Method signature '{0}' is incorrect : missing parenthesis", label)); // Find return type, and name int indexFirstSpace = label.IndexOf(' '); if ((indexFirstSpace == -1) || (indexFirstSpace > indexBeginningOfParameters)) { returnType = "void"; name = label.Substring(0, indexBeginningOfParameters - 1).Trim(); } else { returnType = label.Substring(0, indexFirstSpace).Trim(); if ((knownCodeTypes!=null) && (knownCodeTypes.GetNamedTypes(returnType).Length == 1)) returnType = knownCodeTypes.GetNamedTypes(returnType)[0].FullName; name = label.Substring(indexFirstSpace + 1, indexBeginningOfParameters - 1 - indexFirstSpace).Trim(); } // Parse parameters parameters = new List<KeyValuePair<string, string>>(); if (indexEndOfParametes != -1) { string parameterString = label.Substring(indexBeginningOfParameters + 1, indexEndOfParametes - 1 - indexBeginningOfParameters); string[] parameterArray = parameterString.Split(','); if (!string.IsNullOrEmpty(parameterString)) { int parameterIndex = 0; foreach (string parameter in parameterArray) { string aParameterString = parameter.Trim(); string[] parts = aParameterString.Split(' '); string parameterType; string parameterName; if (parts.Length >= 2) { parameterType = parts[0]; parameterName = parts[1]; } else if (parts.Length > 0) { parameterType = parts[0]; parameterName = "p" + (parameterIndex + 1).ToString(); } else throw new ArgumentException(string.Format("Method signature '{0}' is incorrect (parameter #{1})", label, parameterIndex)); parameterIndex++; if ((knownCodeTypes != null) && (knownCodeTypes.GetNamedTypes(parameterType).Length == 1)) parameterType = knownCodeTypes.GetNamedTypes(parameterType)[0].FullName; parameters.Add(new KeyValuePair<string, string>(parameterName, parameterType)); } } } }
/// <summary> /// Simplify a Type name /// </summary> /// <param name="knownCodeTypes"></param> /// <param name="fullTypeName"></param> /// <returns></returns> public static string SimplifyTypeName(KnownCodeTypes knownCodeTypes, string fullTypeName) { if (knownCodeTypes == null) return fullTypeName; string post = ""; while (fullTypeName.EndsWith("[]")) { fullTypeName = fullTypeName.Substring(0, fullTypeName.Length - 2); post = "[]" + post; } CodeType theType = knownCodeTypes.GetFullNamedType(fullTypeName); string simpleType; if (theType != null) simpleType = KnownCodeTypes.GetShortTypeName(theType) + post; else simpleType = fullTypeName + post; return simpleType; }
/// <summary> /// Parse a label in order to get the name of the variable and its full type name /// </summary> /// <param name="knownCodeTypes"></param> /// <param name="newValue"></param> /// <param name="name"></param> /// <param name="type"></param> public static void ParseLabel(KnownCodeTypes knownCodeTypes, string newValue, out string name, ref string type) { string[] composants = newValue.Split(':'); if (composants.Length == 0) name = type = ""; else if (composants.Length == 1) name = newValue; else { name = composants[0].Trim(); string simplifiedType = composants[1].Trim(); type = GetFullTypeName(knownCodeTypes, simplifiedType); } }
/// <summary> /// Infers the types from a single type name /// </summary> /// <param name="knownCodeTypes"></param> /// <param name="type"></param> /// <returns></returns> public static List<CodeType> InferType(KnownCodeTypes knownCodeTypes, string type) { return new List<CodeType>(knownCodeTypes.GetNamedTypes(type)); }