|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | - |
6 | | -namespace SharpGLTF.CodeGen |
7 | | -{ |
8 | | - /// <summary> |
9 | | - /// Text processing extensions to facilitate source code emission. |
10 | | - /// </summary> |
11 | | - static class CodeEmitUtils |
12 | | - { |
13 | | - public static string Indent(this string text, int indent) |
14 | | - { |
15 | | - while (indent > 0) { text = $"\t{text}"; --indent; } |
16 | | - |
17 | | - return text; |
18 | | - } |
19 | | - |
20 | | - public static void EmitLine(this StringBuilder sb, int indent, string text) |
21 | | - { |
22 | | - text = text.Indent(indent); |
23 | | - sb.AppendLine(text); |
24 | | - } |
25 | | - |
26 | | - public static void EmitBlock(this StringBuilder sb, int indent, string body) |
27 | | - { |
28 | | - sb.EmitLine(indent, "{"); |
29 | | - |
30 | | - var lines = body.Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None); |
31 | | - |
32 | | - foreach (var line in lines) |
33 | | - { |
34 | | - sb.EmitLine(indent + 1, line); |
35 | | - } |
36 | | - |
37 | | - sb.EmitLine(indent, "}"); |
38 | | - } |
39 | | - |
40 | | - public static IEnumerable<string> AsCodeBlock(this IEnumerable<string> lines) |
41 | | - { |
42 | | - yield return "{"; |
43 | | - |
44 | | - foreach (var l in lines) yield return $"\t{l}"; |
45 | | - |
46 | | - yield return "}"; |
47 | | - } |
48 | | - |
49 | | - public static IEnumerable<string> Indent(this IEnumerable<string> lines, int indent) |
50 | | - { |
51 | | - foreach (var l in lines) yield return l.Indent(indent); |
52 | | - } |
53 | | - |
54 | | - public static IEnumerable<string> EmitSummary(this string description, int indent) |
55 | | - { |
56 | | - if (string.IsNullOrWhiteSpace(description)) yield break; |
57 | | - |
58 | | - description = _ReplaceDescriptionKeywords(description); |
59 | | - |
60 | | - var lines = description |
61 | | - .Split(" ") |
62 | | - .Select(item => item.Trim()) |
63 | | - .ToList(); |
64 | | - |
65 | | - yield return "/// <summary>".Indent(indent); |
66 | | - foreach(var l in lines) yield return $"/// {l}"; |
67 | | - yield return "/// </summary>".Indent(indent); |
68 | | - } |
69 | | - |
70 | | - private static string _ReplaceDescriptionKeywords(string description) |
71 | | - { |
72 | | - while(true) |
73 | | - { |
74 | | - var (start, len) = _FindDescriptionKeyword(description); |
75 | | - if (start < 0) return description; |
76 | | - |
77 | | - var block = description.Substring(start , len); |
78 | | - var name = block.Substring(2, block.Length - 4); |
79 | | - |
80 | | - description = description.Replace(block, $"<see cref=\"{name}\"/>", StringComparison.Ordinal); |
81 | | - } |
82 | | - } |
83 | | - |
84 | | - private static (int start,int len) _FindDescriptionKeyword(string description) |
85 | | - { |
86 | | - int start = description.IndexOf("`\""); |
87 | | - var end = description.IndexOf("\"`"); |
88 | | - if (start < 0 || end < 0 || start >= end) return (-1, -1); |
89 | | - return (start, end - start + 2); |
90 | | - } |
91 | | - } |
92 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace SharpGLTF.CodeGen |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Text processing extensions to facilitate source code emission. |
| 10 | + /// </summary> |
| 11 | + static class CodeEmitUtils |
| 12 | + { |
| 13 | + public static string Indent(this string text, int indent) |
| 14 | + { |
| 15 | + while (indent > 0) { text = $"\t{text}"; --indent; } |
| 16 | + |
| 17 | + return text; |
| 18 | + } |
| 19 | + |
| 20 | + public static void EmitLine(this StringBuilder sb, int indent, string text) |
| 21 | + { |
| 22 | + text = text.Indent(indent); |
| 23 | + sb.AppendLine(text); |
| 24 | + } |
| 25 | + |
| 26 | + public static void EmitBlock(this StringBuilder sb, int indent, string body) |
| 27 | + { |
| 28 | + sb.EmitLine(indent, "{"); |
| 29 | + |
| 30 | + var lines = body.Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None); |
| 31 | + |
| 32 | + foreach (var line in lines) |
| 33 | + { |
| 34 | + sb.EmitLine(indent + 1, line); |
| 35 | + } |
| 36 | + |
| 37 | + sb.EmitLine(indent, "}"); |
| 38 | + } |
| 39 | + |
| 40 | + public static IEnumerable<string> AsCodeBlock(this IEnumerable<string> lines) |
| 41 | + { |
| 42 | + yield return "{"; |
| 43 | + |
| 44 | + foreach (var l in lines) yield return $"\t{l}"; |
| 45 | + |
| 46 | + yield return "}"; |
| 47 | + } |
| 48 | + |
| 49 | + public static IEnumerable<string> Indent(this IEnumerable<string> lines, int indent) |
| 50 | + { |
| 51 | + foreach (var l in lines) yield return l.Indent(indent); |
| 52 | + } |
| 53 | + |
| 54 | + public static IEnumerable<string> EmitSummary(this string description, int indent) |
| 55 | + { |
| 56 | + if (string.IsNullOrWhiteSpace(description)) yield break; |
| 57 | + |
| 58 | + description = _ReplaceDescriptionKeywords(description); |
| 59 | + |
| 60 | + var lines = description |
| 61 | + .Split(" ") |
| 62 | + .Select(item => item.Trim()) |
| 63 | + .ToList(); |
| 64 | + |
| 65 | + yield return "/// <summary>".Indent(indent); |
| 66 | + foreach(var l in lines) yield return $"/// {l}"; |
| 67 | + yield return "/// </summary>".Indent(indent); |
| 68 | + } |
| 69 | + |
| 70 | + private static string _ReplaceDescriptionKeywords(string description) |
| 71 | + { |
| 72 | + while(true) |
| 73 | + { |
| 74 | + var (start, len) = _FindDescriptionKeyword(description); |
| 75 | + if (start < 0) return description; |
| 76 | + |
| 77 | + var block = description.Substring(start , len); |
| 78 | + var name = block.Substring(2, block.Length - 4); |
| 79 | + |
| 80 | + description = description.Replace(block, $"<see cref=\"{name}\"/>", StringComparison.Ordinal); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static (int start,int len) _FindDescriptionKeyword(string description) |
| 85 | + { |
| 86 | + int start = description.IndexOf("`\""); |
| 87 | + var end = description.IndexOf("\"`"); |
| 88 | + if (start < 0 || end < 0 || start >= end) return (-1, -1); |
| 89 | + return (start, end - start + 2); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments