@@ -11,6 +11,8 @@ public abstract class DocBuilder
1111 protected readonly List < string > DescriptionLines = new ( ) ;
1212 protected readonly List < DocParam > Params = new ( ) ;
1313 protected readonly List < DocThrows > Throws = new ( ) ;
14+ protected readonly List < string > SeeAlso = new ( ) ;
15+ protected readonly List < DocExample > Examples = new ( ) ;
1416 protected DocReturn ? Return ;
1517
1618 public DocBuilder AddDescription ( string ? description )
@@ -49,7 +51,53 @@ public DocBuilder AddReturn(string type, string? description = null)
4951 return this ;
5052 }
5153
52- public bool IsEmpty => DescriptionLines . Count == 0 && Params . Count == 0 && Throws . Count == 0 && Return == null ;
54+ public DocBuilder AddSee ( string reference )
55+ {
56+ SeeAlso . Add ( reference ) ;
57+ return this ;
58+ }
59+
60+ public DocBuilder AddExample ( string code , string language = "typescript" )
61+ {
62+ Examples . Add ( new DocExample ( code , language ) ) ;
63+ return this ;
64+ }
65+
66+ /// <summary>
67+ /// Returns the preferred languages for examples in priority order.
68+ /// Override in language-specific builders.
69+ /// </summary>
70+ protected virtual string [ ] GetPreferredLanguages ( ) => [ ] ;
71+
72+ /// <summary>
73+ /// Filters examples to prefer the target language.
74+ /// Returns examples matching the first preferred language that has any,
75+ /// or all examples if no preferred languages match.
76+ /// </summary>
77+ protected IEnumerable < DocExample > GetFilteredExamples ( )
78+ {
79+ if ( Examples . Count == 0 )
80+ return Examples ;
81+
82+ var preferred = GetPreferredLanguages ( ) ;
83+ if ( preferred . Length == 0 )
84+ return Examples ;
85+
86+ // Try each preferred language in order
87+ foreach ( var lang in preferred )
88+ {
89+ var matching = Examples . Where ( e =>
90+ e . Language . Equals ( lang , StringComparison . OrdinalIgnoreCase ) ) . ToList ( ) ;
91+
92+ if ( matching . Count > 0 )
93+ return matching ;
94+ }
95+
96+ // No preferred match - return all examples
97+ return Examples ;
98+ }
99+
100+ public bool IsEmpty => DescriptionLines . Count == 0 && Params . Count == 0 && Throws . Count == 0 && Return == null && SeeAlso . Count == 0 && Examples . Count == 0 ;
53101
54102 /// <summary>
55103 /// Renders the documentation to the CodeBuilder.
@@ -89,6 +137,8 @@ private static string EscapeJsDoc(string text)
89137 return text . Replace ( "*/" , "*\\ /" ) ;
90138 }
91139
140+ protected override string [ ] GetPreferredLanguages ( ) => [ "typescript" , "ts" , "javascript" , "js" ] ;
141+
92142 public override void Render ( CodeBuilder cb )
93143 {
94144 if ( IsEmpty ) return ;
@@ -121,6 +171,22 @@ public override void Render(CodeBuilder cb)
121171 cb . AppendLine ( $ " * @returns{ desc } ") ;
122172 }
123173
174+ foreach ( var see in SeeAlso )
175+ {
176+ cb . AppendLine ( $ " * @see {{{see}}}") ;
177+ }
178+
179+ foreach ( var example in GetFilteredExamples ( ) )
180+ {
181+ cb . AppendLine ( " * @example" ) ;
182+ cb . AppendLine ( $ " * ```{ example . Language } ") ;
183+ foreach ( var line in example . Code . Split ( '\n ' ) )
184+ {
185+ cb . AppendLine ( $ " * { EscapeJsDoc ( line ) } ") ;
186+ }
187+ cb . AppendLine ( " * ```" ) ;
188+ }
189+
124190 cb . AppendLine ( " */" ) ;
125191 }
126192}
@@ -130,6 +196,8 @@ public override void Render(CodeBuilder cb)
130196/// </summary>
131197public class LuaDocBuilder : DocBuilder
132198{
199+ protected override string [ ] GetPreferredLanguages ( ) => [ "lua" ] ;
200+
133201 public override void Render ( CodeBuilder cb )
134202 {
135203 foreach ( var line in DescriptionLines )
@@ -148,6 +216,23 @@ public override void Render(CodeBuilder cb)
148216 var desc = string . IsNullOrEmpty ( Return . Description ) ? "" : $ " { Return . Description } ";
149217 cb . AppendLine ( $ "---@return { Return . Type } { desc } ") ;
150218 }
219+
220+ foreach ( var see in SeeAlso )
221+ {
222+ cb . AppendLine ( $ "---@see { see } ") ;
223+ }
224+
225+ foreach ( var example in GetFilteredExamples ( ) )
226+ {
227+ cb . AppendLine ( "---" ) ;
228+ cb . AppendLine ( "---Example:" ) ;
229+ cb . AppendLine ( "---```" + example . Language ) ;
230+ foreach ( var line in example . Code . Split ( '\n ' ) )
231+ {
232+ cb . AppendLine ( $ "---{ line } ") ;
233+ }
234+ cb . AppendLine ( "---```" ) ;
235+ }
151236 }
152237}
153238
@@ -183,3 +268,4 @@ public override void Render(CodeBuilder cb)
183268public record DocParam ( string Name , string Type , string Description ) ;
184269public record DocReturn ( string Type , string ? Description ) ;
185270public record DocThrows ( string Type , string Description ) ;
271+ public record DocExample ( string Code , string Language ) ;
0 commit comments