-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathDocumentationUtil.fs
226 lines (183 loc) · 8.16 KB
/
DocumentationUtil.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
namespace CSharpLanguageServer
open System
open System.Linq
open System.Xml.Linq
open System.Reflection
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.CSharp.Syntax
open CSharpLanguageServer.Conversions
module DocumentationUtil =
type TripleSlashComment =
{ Summary: XElement list
Params: (string * XElement) list
Exceptions: (string * XElement) list
Returns: XElement list
Types: (string * XElement) list
Remarks: XElement list
OtherLines: XElement list }
static member Default =
{ Summary = []
Params = []
Exceptions = []
Returns = []
Types = []
Remarks = []
OtherLines = [] }
let parseCref (cref: string) =
let parts = cref.Split(':')
match parts.Length with
| 1 -> cref
| _ -> String.Join(":", parts |> Seq.skip 1)
let normalizeWhitespace (s: string) =
let mutable modified = s
let mutable prevModified = ""
while modified <> prevModified do
prevModified <- modified
modified <- modified.Replace(" ", " ").Replace("\r\n", " ").Replace("\n", " ")
modified
let formatTextElement (n: XElement) =
let formatSeeElement (e: XElement) =
let crefMaybe =
e.Attribute(XName.Get("cref"))
|> Option.ofObj
|> Option.map (fun x -> x.Value)
|> Option.map parseCref
|> Option.map (fun s -> sprintf "`` %s ``" s)
|> Option.toList
let langWordMaybe =
e.Attribute(XName.Get("langword"))
|> Option.ofObj
|> Option.map (fun x -> sprintf "`` %s ``" x.Value)
|> Option.toList
crefMaybe |> Seq.append langWordMaybe |> List.ofSeq
let formatTextNode (subnode: XNode) =
match subnode with
| :? XElement as e ->
match e.Name.LocalName with
| "c" -> [ sprintf "`` %s ``" e.Value ]
| "see" -> formatSeeElement e
| "paramref" ->
e.Attribute(XName.Get("name"))
|> Option.ofObj
|> Option.map (fun x -> sprintf "`` %s ``" x.Value)
|> Option.toList
| _ -> [ e.Value ]
| :? XText as t -> [ t.Value |> normalizeWhitespace ]
| _ -> []
n.Nodes() |> Seq.collect formatTextNode |> (fun ss -> String.Join("", ss))
let extendCommentWithElement comment (n: XElement) =
match n.Name.LocalName with
| "summary" ->
let newSummary = comment.Summary |> List.append [ n ]
{ comment with Summary = newSummary }
| "remarks" ->
let newRemarks = comment.Remarks |> List.append [ n ]
{ comment with Remarks = newRemarks }
| "param" ->
let name = n.Attribute(XName.Get("name")).Value
{ comment with Params = comment.Params |> List.append [ (name, n) ] }
| "returns" ->
{ comment with Returns = comment.Returns |> List.append [ n ] }
| "exception" ->
let name = n.Attribute(XName.Get("cref")).Value |> parseCref
{ comment with
Exceptions = comment.Exceptions |> List.append [ (name, n) ] }
| "typeparam" ->
let name = n.Attribute(XName.Get("name")).Value
{ comment with Types = comment.Types |> List.append [ (name, n) ] }
| _ ->
{ comment with OtherLines = comment.OtherLines |> List.append [ n ] }
let parseComment xmlDocumentation: TripleSlashComment =
let doc = XDocument.Parse("<docroot>" + xmlDocumentation + "</docroot>")
let unwrapDocRoot (root: XElement) =
let elementNames (el: XElement) =
el.Elements() |> Seq.map (fun e -> e.Name.LocalName) |> List.ofSeq
match elementNames root with
| [ "member" ] -> root.Element(XName.Get("member"))
| _ -> root
doc.Root
|> unwrapDocRoot
|> fun r -> r.Elements()
|> Seq.fold extendCommentWithElement TripleSlashComment.Default
let formatComment model : string list =
let appendNamed name (kvs: (string * XElement) seq) markdownLines =
match Seq.isEmpty kvs with
| true -> markdownLines
| false ->
let formatItem (key, value) =
sprintf "- `` %s ``: %s" key (formatTextElement value)
markdownLines
|> List.append [ name + ":"; "" ]
|> List.append (kvs |> Seq.map formatItem |> List.ofSeq)
let appendFormatted name elms markdownLines =
let formattedLines =
elms
|> List.map formatTextElement
|> List.filter (not << String.IsNullOrWhiteSpace)
match Seq.isEmpty formattedLines with
| true -> markdownLines
| false ->
markdownLines
|> List.append [ "" ]
|> List.append (formattedLines |> List.map (fun s -> name + ": " + s))
[]
|> List.append (model.Summary |> List.map formatTextElement)
|> appendNamed "Parameters" model.Params
|> appendFormatted "Returns" model.Returns
|> appendNamed "Exceptions" model.Exceptions
|> appendNamed "Types" model.Types
|> appendFormatted "Remarks" model.Remarks
|> List.append (model.OtherLines |> List.map string)
|> List.rev
|> List.map (fun s -> s.Trim())
let formatDocXml xmlDocumentation =
String.Join("\n", xmlDocumentation |> parseComment |> formatComment)
let markdownDocForSymbol (sym: ISymbol) =
let comment = parseComment (sym.GetDocumentationCommentXml())
let formattedDocLines = formatComment comment
formattedDocLines |> (fun ss -> String.Join("\n", ss))
let markdownDocForSymbolWithSignature (sym: ISymbol) (semanticModel: SemanticModel) =
let symbolName = SymbolName.fromSymbol SymbolDisplayFormat.MinimallyQualifiedFormat sym
let symAssemblyName =
sym.ContainingAssembly
|> Option.ofObj
|> Option.map (fun a -> a.Name)
|> Option.defaultValue ""
match sym with
| :? IRangeVariableSymbol as rangeVarSym ->
let underlyingSymList = rangeVarSym.GetType().GetMember("_underlying", BindingFlags.Instance|||BindingFlags.NonPublic)
let underlyingSymType = underlyingSymList.Single() :?> FieldInfo |> _.FieldType
(sprintf "rangeVarSym.Type=%s; underlyingSym=%s" (rangeVarSym.GetType() |> string)
(underlyingSymType |> string))
//(sprintf "syntax=%s; type=%s" (syntax |> string) (syntax.GetType() |> string))
(*
let typeInfo = semanticModel.GetTypeInfo(syntax)
(sprintf "typeInfo=%s; typeInfo.Type=\"%s\"" (typeInfo |> string) (typeInfo.Type |> string))
let typeName = typeInfo.Type.ToDisplayString()
"o"
(sprintf "kind=%s, symbolName=%s, symAssemblyName=%s; typeName=%s" (string sym.Kind) symbolName symAssemblyName typeName)
*)
| _ -> "x"
(*
let symbolInfoLines =
match symbolName, symAssemblyName with
| "", "" -> []
| typeName, "" -> [ sprintf "`` %s ``" typeName ]
| _, _ ->
let docAssembly = semanticModel.Compilation.Assembly
if symAssemblyName = docAssembly.Name then
[ sprintf "`` %s ``" symbolName ]
else
[ sprintf "`` %s `` from assembly `` %s ``" symbolName symAssemblyName ]
let comment = parseComment (sym.GetDocumentationCommentXml())
let formattedDocLines = formatComment comment
formattedDocLines
|> Seq.append (
if symbolInfoLines.Length > 0 && formattedDocLines.Length > 0 then
[ "" ]
else
[]
)
|> Seq.append symbolInfoLines
|> (fun ss -> String.Join("\n", ss))
*)