Exemple #1
0
        private bool CreateEnumAndConversionMethods()
        {
            var parts = _item.OwnerClasses;
            var conflictsCodeMethod = Extensions_CheckConflictsCode(_item);

            var optionsToStringCode = new CsCodeWriter();

            optionsToStringCode.WriteLine("// generator : " + GetType().Name);
            if (!string.IsNullOrEmpty(conflictsCodeMethod))
            {
                optionsToStringCode.WriteLine($"{conflictsCodeMethod}({valueVariable});");
            }
            var fb = new EnumFlagsBuilder(MyEnum);

            fb.AddFlagsAttribute(MyNamespace);

            var enumSource = from q in _item.Options.Values
                             where string.IsNullOrEmpty(q.Parameter?.Value)
                             select q;
            var generate = false;

            foreach (var option in enumSource)
            {
                generate = true;
                var enumItem = new CsEnumItem(option.GetCsName())
                {
                    Description = option.FullDescription,
                };
                var stringValue = GetStringValue(preferLongNameVariable, option, ExtensionsClass);
                optionsToStringCode.WriteDescriptionComment(option);
                var condition = GetEnumCondition(valueVariable, MyEnum, enumItem);
                optionsToStringCode.SingleLineIf(condition, $"yield return {stringValue};");
                MyStruct_AddWithMethod(enumItem, fb.Value == 1);

                fb.Append(enumItem);

                {
                    apps.Add((w, r) =>
                    {
                        var stringValue = GetStringValue(preferLongNameVariable, option, ExtensionsClass);
                        w.WriteDescriptionComment(option);
                        var condition = GetEnumCondition(flagsPropertyName, MyEnum, enumItem);
                        w.SingleLineIf(condition, $"yield return {stringValue};");
                    });
                }
            }

            if (!generate)
            {
                return(false);
            }
            {
                var m = ExtensionsClass
                        .AddMethod("OptionsToString", MyNamespace.GetTypeName <IEnumerable <string> >())
                        .WithStatic()
                        .WithBody(optionsToStringCode);
                m.AddParam(valueVariable, MyEnumTypeName).UseThis = true;
                m.AddParam <OptionPreference>(preferLongNameVariable, ExtensionsClass).ConstValue =
                    ExtensionsClass.GetEnumValueCode(OptionPreference.Short);
            }

            AddEnumToOutput(parts, MyNamespace, MyEnum);
            return(true);
        }