void AppendConstant (StringBuilder sb, ITypeSymbol constantType, object constantValue, bool useNumericalEnumValue = false)
{
if (constantValue is string) {
sb.Append (Highlight ("\"" + MonoDevelop.Ide.TypeSystem.Ambience.EscapeText ((string)constantValue) + "\"", colorStyle.String));
return;
}
if (constantValue is char) {
sb.Append (Highlight ("'" + constantValue + "'", colorStyle.String));
return;
}
if (constantValue is bool) {
sb.Append (Highlight ((bool)constantValue ? "true" : "false", colorStyle.KeywordConstants));
return;
}
if (constantValue == null) {
if (constantType.IsValueType) {
// structs can never be == null, therefore it's the default value.
sb.Append (Highlight ("default", colorStyle.KeywordSelection) + "(" + GetTypeReferenceString (constantType) + ")");
} else {
sb.Append (Highlight ("null", colorStyle.KeywordConstants));
}
return;
}
// TODOδ
// while (IsNullableType (constantType))
// constantType = NullableType.GetUnderlyingType (constantType);
if (constantType.TypeKind == TypeKind.Enum) {
foreach (var field in constantType.GetMembers ().OfType<IFieldSymbol> ()) {
if (field.ConstantValue == constantValue) {
if (useNumericalEnumValue) {
sb.Append (Highlight (string.Format ("0x{0:X}", field.ConstantValue), colorStyle.Number));
} else {
sb.Append (GetTypeReferenceString (constantType) + "." + FilterEntityName (field.Name));
}
return;
}
}
// try to decompose flags
if (constantType.GetAttributes ().Any (attr => attr.AttributeClass.Name == "FlagsAttribute" && attr.AttributeClass.ContainingNamespace.Name == "System")) {
var val = GetUlong (constantValue.ToString ());
var outVal = 0UL;
var fields = new List<IFieldSymbol> ();
foreach (var field in constantType.GetMembers ().OfType<IFieldSymbol> ()) {
if (field.ConstantValue == null)
continue;
var val2 = GetUlong (field.ConstantValue.ToString ());
if ((val & val2) == val2) {
fields.Add (field);
outVal |= val2;
}
}
if (val == outVal && fields.Count > 1) {
for (int i = 0; i < fields.Count; i++) {
if (i > 0)
sb.Append (" | ");
var field = fields [i];
sb.Append (GetTypeReferenceString (constantType) + "." + FilterEntityName (field.Name));
}
return;
}
}
sb.Append ("(" + GetTypeReferenceString (constantType) + ")" + Highlight (constantValue.ToString (), colorStyle.Number));
return;
}
sb.Append (Highlight (MonoDevelop.Ide.TypeSystem.Ambience.EscapeText (constantValue.ToString ()), colorStyle.Number));
}