Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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  firstVersion     = new Version(1, 9, 1);

            // *****************************************************************************
            // 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;
                // 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    numStr = enumType.IsFlags ? NumberFormatter.FormatHexUInt32WithSep(value.Value) : value.Value.ToString();
                string valueName;
                if (uppercaseRawName)
                {
                    valueName = value.RawName.ToUpperInvariant();
                }
                else
                {
                    valueName = value.Name(pythonIdConverter);
                }
                writer.WriteLine($"{valueName}: int = {numStr}");
                if (value.DeprecatedInfo.IsDeprecated)
                {
                    docs = $"DEPRECATED({value.DeprecatedInfo.VersionStr}): {docs}";
                }
                docWriter.WriteSummary(writer, docs, enumType.RawName);
            }
        }