コード例 #1
0
ファイル: PythonEnumsGenerator.cs プロジェクト: icedland/iced
        void WriteEnumCore(FileWriter writer, EnumType enumType, PythonDocCommentWriter docWriter)
        {
            bool mustHaveDocs     = enumType.TypeId != TypeIds.Register && enumType.TypeId != TypeIds.Mnemonic;
            bool uppercaseRawName = PythonUtils.UppercaseEnum(enumType.TypeId.Id1);
            var  enumTypeName     = enumType.Name(pythonIdConverter);
            var  firstVersion     = new Version(1, 13, 0);

            // *****************************************************************************
            // For PERF reasons, we do NOT use Enums. They're incredibly slow to load!
            // Eg. loading 'class Code(IntEnum)' (plus other non-Mnemonic enums and some random
            // code) took ~850ms and when I converted them to constants, it took ~43ms!
            // *****************************************************************************
            foreach (var value in enumType.Values)
            {
                if (value.DeprecatedInfo.IsDeprecated && value.DeprecatedInfo.Version < firstVersion)
                {
                    continue;
                }

                var docs = value.Documentation.GetComment(TargetLanguage.Python);
                // Sphinx doesn't include the public enum items (global vars in a module) if they're not documented
                if (string.IsNullOrEmpty(docs))
                {
                    if (mustHaveDocs)
                    {
                        throw new InvalidOperationException();
                    }
                    docs = "<no docs>";
                }

                var(valueName, numStr) = PythonUtils.GetEnumNameValue(pythonIdConverter, value, uppercaseRawName);
                writer.WriteLine($"{valueName}: {enumTypeName} = {numStr} # type: ignore");
                if (value.DeprecatedInfo.IsDeprecated)
                {
                    string?extra;
                    if (value.DeprecatedInfo.NewName is not null)
                    {
                        extra = $"Use {value.DeprecatedInfo.NewName} instead";
                    }
                    else
                    {
                        extra = null;
                    }

                    if (extra is null)
                    {
                        extra = string.Empty;
                    }
                    else
                    {
                        extra = $": {extra}";
                    }
                    docs = $"DEPRECATED({value.DeprecatedInfo.VersionStr}){extra}";
                }
                docWriter.WriteSummary(writer, docs, enumType.RawName);
            }
        }