|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using static System.Net.WebUtility; |
| 6 | + |
| 7 | +namespace Libraries.Docs |
| 8 | +{ |
| 9 | + public class DocsApiReference |
| 10 | + { |
| 11 | + public bool IsOverload { get; private init; } |
| 12 | + |
| 13 | + public char? Prefix { get; private init; } |
| 14 | + |
| 15 | + public string Api { get; private init; } |
| 16 | + |
| 17 | + // Generic parameters need to support both single and double backtick conventions |
| 18 | + private const string GenericParameterPattern = @"`{1,2}(?<arity>\d+)"; |
| 19 | + private const string ApiChars = @"[A-Za-z0-9\-\._~:\/#\[\]\{\}@!\$&'\(\)\*\+,;`]"; |
| 20 | + private const string ApiReferencePattern = @"((?<prefix>[A-Za-z]):)?(?<api>(" + ApiChars + @")+)?(?<extraVars>\?(" + ApiChars + @")+=(" + ApiChars + @")+)?"; |
| 21 | + private static readonly Regex XrefPattern = new("<xref:(?<api>" + ApiReferencePattern + ")\\s*>", RegexOptions.Compiled); |
| 22 | + |
| 23 | + public DocsApiReference(string apiReference) |
| 24 | + { |
| 25 | + Api = UrlDecode(apiReference); |
| 26 | + var match = Regex.Match(Api, ApiReferencePattern); |
| 27 | + |
| 28 | + if (match.Success) |
| 29 | + { |
| 30 | + Api = match.Groups["api"].Value; |
| 31 | + |
| 32 | + if (match.Groups["prefix"].Success) |
| 33 | + { |
| 34 | + Prefix = match.Groups["prefix"].Value[0]; |
| 35 | + IsOverload = Prefix == 'O'; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (Api.EndsWith('*')) |
| 40 | + { |
| 41 | + IsOverload = true; |
| 42 | + Api = Api[..^1]; |
| 43 | + } |
| 44 | + |
| 45 | + Api = ReplacePrimitivesWithShorthands(Api); |
| 46 | + Api = ParseGenericTypes(Api); |
| 47 | + } |
| 48 | + |
| 49 | + public override string ToString() |
| 50 | + { |
| 51 | + if (Prefix is not null) |
| 52 | + { |
| 53 | + return $"{Prefix}:{Api}"; |
| 54 | + } |
| 55 | + |
| 56 | + return Api; |
| 57 | + } |
| 58 | + |
| 59 | + private static readonly Dictionary<string, string> PrimitiveTypes = new() |
| 60 | + { |
| 61 | + { "System.Boolean", "bool" }, |
| 62 | + { "System.Byte", "byte" }, |
| 63 | + { "System.Char", "char" }, |
| 64 | + { "System.Decimal", "decimal" }, |
| 65 | + { "System.Double", "double" }, |
| 66 | + { "System.Int16", "short" }, |
| 67 | + { "System.Int32", "int" }, |
| 68 | + { "System.Int64", "long" }, |
| 69 | + { "System.Object", "object" }, // Ambiguous: could be 'object' or 'dynamic' https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types |
| 70 | + { "System.SByte", "sbyte" }, |
| 71 | + { "System.Single", "float" }, |
| 72 | + { "System.String", "string" }, |
| 73 | + { "System.UInt16", "ushort" }, |
| 74 | + { "System.UInt32", "uint" }, |
| 75 | + { "System.UInt64", "ulong" }, |
| 76 | + { "System.Void", "void" } |
| 77 | + }; |
| 78 | + |
| 79 | + public static string ReplacePrimitivesWithShorthands(string apiReference) |
| 80 | + { |
| 81 | + foreach ((string key, string value) in PrimitiveTypes) |
| 82 | + { |
| 83 | + apiReference = Regex.Replace(apiReference, key, value); |
| 84 | + } |
| 85 | + |
| 86 | + return apiReference; |
| 87 | + } |
| 88 | + |
| 89 | + public static string ParseGenericTypes(string apiReference) |
| 90 | + { |
| 91 | + int genericParameterArity = 0; |
| 92 | + return Regex.Replace(apiReference, GenericParameterPattern, MapGenericParameter); |
| 93 | + |
| 94 | + string MapGenericParameter(Match match) |
| 95 | + { |
| 96 | + int arity = int.Parse(match.Groups["arity"].Value); |
| 97 | + |
| 98 | + if (genericParameterArity == 0) |
| 99 | + { |
| 100 | + // This is the first match that declares the generic parameter arity of the method |
| 101 | + // e.g. GenericMethod``3 ---> GenericMethod{T1,T2,T3}(...); |
| 102 | + Debug.Assert(arity > 0); |
| 103 | + genericParameterArity = arity; |
| 104 | + return WrapInCurlyBrackets(string.Join(",", Enumerable.Range(0, arity).Select(CreateGenericParameterName))); |
| 105 | + } |
| 106 | + |
| 107 | + // Subsequent matches are references to generic parameters in the method signature, |
| 108 | + // e.g. GenericMethod{T1,T2,T3}(..., List{``1} parameter, ...); ---> List{T2} parameter |
| 109 | + return CreateGenericParameterName(arity); |
| 110 | + |
| 111 | + // This naming scheme does not map to the exact generic parameter names; |
| 112 | + // however this is still accepted by intellisense and backporters can rename |
| 113 | + // manually with the help of tooling. |
| 114 | + string CreateGenericParameterName(int index) => genericParameterArity == 1 ? "T" : $"T{index + 1}"; |
| 115 | + |
| 116 | + static string WrapInCurlyBrackets(string input) => $"{{{input}}}"; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static string ReplaceMarkdownXrefWithSeeCref(string markdown) |
| 121 | + { |
| 122 | + return XrefPattern.Replace(markdown, match => |
| 123 | + { |
| 124 | + var api = new DocsApiReference(match.Groups["api"].Value); |
| 125 | + return @$"<see cref=""{api}"" />"; |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments