| 1 | #Region "Microsoft.VisualBasic::a932a3b98d366ee02de011c3b4c836eb, Microsoft.VisualBasic.Core\Extensions\CodeDOM\CodeHelper.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module CodeHelper |
| 35 | ' |
| 36 | ' Function: EnumCodeHelper, EnumMember |
| 37 | ' |
| 38 | ' |
| 39 | ' /********************************************************************************/ |
| 40 | |
| 41 | #End Region |
| 42 | |
| 43 | Imports System.ComponentModel |
| 44 | Imports System.Runtime.CompilerServices |
| 45 | Imports System.Text |
| 46 | Imports Microsoft.VisualBasic.Scripting.SymbolBuilder.VBLanguage |
| 47 | Imports Microsoft.VisualBasic.Text |
| 48 | |
| 49 | Namespace Emit.CodeDOM_VBC |
| 50 | |
| 51 | Public Module CodeHelper |
| 52 | |
| 53 | ''' <summary> |
| 54 | ''' |
| 55 | ''' </summary> |
| 56 | ''' <param name="items"></param> |
| 57 | ''' <param name="enumName$"></param> |
| 58 | ''' <param name="type$"></param> |
| 59 | ''' <param name="pascalStyle"></param> |
| 60 | ''' <returns></returns> |
| 61 | <Extension> |
| 62 | Public Function EnumCodeHelper(items As IEnumerable(Of String), enumName$, Optional type$ = NameOf(Int32), Optional pascalStyle As Boolean = True) As String |
| 63 | Dim src$ = GetType(DescriptionAttribute).Namespace |
| 64 | Dim code As New StringBuilder |
| 65 | |
| 66 | Call code.AppendLine($"Imports {src}") |
| 67 | Call code.AppendLine() |
| 68 | Call code.AppendLine($"Public Enum {enumName} As {type}") |
| 69 | |
| 70 | ' 将字符串集合转换为枚举成员 |
| 71 | For Each member$ In items |
| 72 | ' xml code comments |
| 73 | Call code.AppendLine("<span class="xml_comment">''' <summary>")</span> |
| 74 | Call code.AppendLine("''' " & member) |
| 75 | Call code.AppendLine("<span class="xml_comment">''' </summary>")</span> |
| 76 | |
| 77 | ' add member name |
| 78 | Call code.AppendLine(vbTab & member.EnumMember(pascalStyle)) |
| 79 | Next |
| 80 | |
| 81 | Call code.AppendLine("End Enum") |
| 82 | |
| 83 | Return code.ToString |
| 84 | End Function |
| 85 | |
| 86 | <Extension> |
| 87 | Public Function EnumMember(member$, Optional pascalStyle As Boolean = True, Optional newLine As Boolean = False) As String |
| 88 | Dim src = $"<Description(""{member}"")>" |
| 89 | |
| 90 | ' generate valid enum member name |
| 91 | member = member.ReplaceChars(ASCII.Symbols, "_"c) |
| 92 | |
| 93 | If pascalStyle Then |
| 94 | member = member _ |
| 95 | .Split("_"c) _ |
| 96 | .Where(Function(s) Not s.StringEmpty) _ |
| 97 | .Select(AddressOf UpperCaseFirstChar) _ |
| 98 | .JoinBy("") |
| 99 | End If |
| 100 | |
| 101 | If Char.IsSymbol(member.First) AndAlso member.First <> "_"c Then |
| 102 | member = "_" & Mid(member, 2) |
| 103 | End If |
| 104 | If Char.IsDigit(member.First) Then |
| 105 | member = "_" & member |
| 106 | End If |
| 107 | |
| 108 | member = KeywordProcessor.AutoEscapeVBKeyword(member) |
| 109 | member = src & If(newLine, vbCrLf, " ") & member |
| 110 | |
| 111 | Return member |
| 112 | End Function |
| 113 | End Module |
| 114 | End Namespace |