1+ using EmmyLua . CodeAnalysis . Compilation ;
2+ using EmmyLua . CodeAnalysis . Compilation . Declaration ;
3+ using EmmyLua . CodeAnalysis . Compilation . Infer ;
4+ using EmmyLua . CodeAnalysis . Compilation . Semantic . Render ;
5+ using EmmyLua . CodeAnalysis . Compilation . Semantic . Render . Renderer ;
6+ using EmmyLua . CodeAnalysis . Document ;
7+ using EmmyLua . CodeAnalysis . Syntax . Node . SyntaxNodes ;
8+ using EmmyLua . CodeAnalysis . Workspace . Module ;
9+
10+ namespace EmmyLua . Cli . DocGenerator . Markdown ;
11+
12+ public class ModuleDoc
13+ {
14+ private SearchContext SearchContext { get ; }
15+
16+ private LuaRenderContext RenderContext { get ; }
17+
18+ private LuaRenderBuilder RenderBuilder { get ; }
19+
20+ private ModuleIndex ModuleIndex { get ; }
21+
22+ private LuaRenderFeature Feature { get ; } = new LuaRenderFeature ( )
23+ {
24+ ShowTypeLink = false ,
25+ ExpandAlias = false ,
26+ } ;
27+
28+ public ModuleDoc ( LuaCompilation compilation , ModuleIndex moduleIndex )
29+ {
30+ SearchContext = new SearchContext ( compilation , new SearchContextFeatures ( ) ) ;
31+ RenderBuilder = new LuaRenderBuilder ( SearchContext ) ;
32+ ModuleIndex = moduleIndex ;
33+ RenderContext = new LuaRenderContext ( SearchContext , Feature ) ;
34+ }
35+
36+ public string Build ( )
37+ {
38+ RenderContext . AddH1Title ( ModuleIndex . ModulePath ) ;
39+ var document = SearchContext . Compilation . Workspace . GetDocument ( ModuleIndex . DocumentId ) ;
40+ if ( document is null )
41+ {
42+ return RenderContext . GetText ( ) ;
43+ }
44+
45+ RenderModuleDescription ( document ) ;
46+ RenderContext . AppendLine ( ) ;
47+
48+ RenderContext . AddH2Title ( "Public members:" ) ;
49+ RenderContext . AddSeparator ( ) ;
50+ RenderModuleMembers ( document ) ;
51+
52+ return RenderContext . GetText ( ) ;
53+ }
54+
55+ private void RenderModuleDescription ( LuaDocument document )
56+ {
57+ RenderContext . Append ( RenderBuilder . RenderModule ( document , Feature ) ) ;
58+ }
59+
60+ private IEnumerable < LuaFuncStatSyntax > GetModuleStats ( LuaDocument document )
61+ {
62+ if ( document . SyntaxTree . SyntaxRoot ? . Block is { StatList : { } statList } )
63+ {
64+ foreach ( var funcStat in statList . OfType < LuaFuncStatSyntax > ( ) )
65+ {
66+ yield return funcStat ;
67+ }
68+ }
69+ }
70+
71+ private void RenderModuleMembers ( LuaDocument document )
72+ {
73+ foreach ( var funcStat in GetModuleStats ( document ) )
74+ {
75+ if ( funcStat is { NameElement . Parent : { } node } )
76+ {
77+ var declaration = SearchContext . FindDeclaration ( node ) ;
78+ if ( declaration is not null )
79+ {
80+ RenderFuncDeclaration ( declaration , funcStat ) ;
81+ RenderContext . AddSeparator ( ) ;
82+ }
83+ }
84+ }
85+ }
86+
87+ private void RenderFuncDeclaration ( LuaDeclaration declaration , LuaFuncStatSyntax funcStat )
88+ {
89+ if ( declaration . IsLocal || declaration . IsPrivate )
90+ {
91+ return ;
92+ }
93+
94+ var asyncText = declaration . IsAsync ? "async " : string . Empty ;
95+
96+ if ( declaration . Info is MethodInfo methodInfo )
97+ {
98+ if ( methodInfo . IndexPtr . ToNode ( SearchContext ) is { } indexExpr )
99+ {
100+ RenderContext . WrapperLua ( ( ) =>
101+ {
102+ RenderContext . Append ( $ "{ asyncText } function { indexExpr . Text } ") ;
103+ LuaTypeRenderer . RenderFunc ( methodInfo . Method , RenderContext ) ;
104+ } ) ;
105+ }
106+ else if ( methodInfo . NamePtr . ToNode ( SearchContext ) is { } nameExpr )
107+ {
108+ RenderContext . WrapperLua ( ( ) =>
109+ {
110+ RenderContext . Append ( $ "{ asyncText } function { nameExpr . Text } ") ;
111+ LuaTypeRenderer . RenderFunc ( methodInfo . Method , RenderContext ) ;
112+ } ) ;
113+ }
114+
115+ var comments = funcStat . Comments ;
116+ foreach ( var comment in comments )
117+ {
118+ if ( comment . CommentText is { Length : > 0 } commentText )
119+ {
120+ // RenderContext.Append(" ");
121+ RenderContext . Append ( commentText ) ; //.Replace("\n", "\n "));
122+ }
123+
124+ RenderContext . AppendLine ( ) ;
125+ }
126+ }
127+ }
128+ }
0 commit comments