Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight improvement of support for C# code #217

Merged
merged 1 commit into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/FSharp.MetadataFormat/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,30 @@ module Reader =
// Reading entities
// ----------------------------------------------------------------------------------------------

// -----------------------------------------------------------------------
// Hack for getting the xmldoc signature from C# assemblies
// FSC consistently returns emtpy strings in .XmlDocSig properties
// and as such Fsharp.formatting is unable to look up the correct
// comment.

let getXmlDocSigForType (typ:FSharpEntity) =
match (typ.XmlDocSig, typ.TryFullName) with
| "", None -> ""
| "", Some(n) -> sprintf "T:%s" n
| n , _ -> n

let getMemberXmlDocsSigPrefix (memb:FSharpMemberFunctionOrValue) =
if memb.IsProperty then "P" else "M"

let getXmlDocSigForMember (memb:FSharpMemberFunctionOrValue) =
match (memb.XmlDocSig, memb.EnclosingEntity.TryFullName) with
| "", None -> ""
| "", Some(n) -> sprintf "%s:%s.%s" (getMemberXmlDocsSigPrefix memb) n memb.CompiledName
| n, _ -> n

//
// ---------------------------------------------------------------------

/// Reads XML documentation comments and calls the specified function
/// to parse the rest of the entity, unless [omit] command is set.
/// The function is called with category name, commands & comment.
Expand All @@ -568,7 +592,7 @@ module Reader =
|> List.ofSeq

let tryReadMember (ctx:ReadingContext) kind (memb:FSharpMemberFunctionOrValue) =
readCommentsInto ctx memb.XmlDocSig (fun cat _ comment ->
readCommentsInto ctx (getXmlDocSigForMember memb) (fun cat _ comment ->
Member.Create(memb.DisplayName, kind, cat, readMemberOrVal ctx memb, comment))

let readAllMembers ctx kind (members:seq<FSharpMemberFunctionOrValue>) =
Expand Down Expand Up @@ -617,6 +641,7 @@ module Reader =
// Reading modules types (mutually recursive, because of nesting)
// ----------------------------------------------------------------------------------------------


// Create a xml documentation snippet and add it to the XmlMemberMap
let registerXmlDoc (ctx:ReadingContext) xmlDocSig (xmlDoc:string) =
let xmlDoc = if xmlDoc.Contains "<summary>" then xmlDoc else "<summary>" + xmlDoc + "</summary>"
Expand Down Expand Up @@ -646,15 +671,16 @@ module Reader =
and readType (ctx:ReadingContext) (typ:FSharpEntity) =
if typ.IsProvided && typ.XmlDoc.Count > 0 then
registerTypeProviderXmlDocs ctx typ
readCommentsInto ctx typ.XmlDocSig (fun cat cmds comment ->
let xmlDocSig = getXmlDocSigForType typ
readCommentsInto ctx xmlDocSig (fun cat cmds comment ->
let urlName = ctx.UniqueUrlName (sprintf "%s.%s" typ.AccessPath typ.CompiledName)

let rec getMembers (typ:FSharpEntity) = [
yield! typ.MembersFunctionsAndValues
match typ.BaseType with
| Some baseType ->
//TODO: would be better to reuse instead of reparsing the base type xml docs
let cmds, comment = readCommentAndCommands ctx baseType.TypeDefinition.XmlDocSig
let cmds, comment = readCommentAndCommands ctx (getXmlDocSigForType baseType.TypeDefinition)
match cmds with
| Command "omit" _ -> yield! getMembers baseType.TypeDefinition
| _ -> ()
Expand Down
25 changes: 24 additions & 1 deletion tests/FSharp.MetadataFormat.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,27 @@ let ``MetadataFormat handles c# dlls`` () =

#if INTERACTIVE
System.Diagnostics.Process.Start(output)
#endif
#endif

[<Test>]
let ``MetadataFormat processes C# types and includes xml comments in docs`` () =
let library = root @@ "files" @@ "CSharpFormat.dll"
let output = getOutputDir()
MetadataFormat.Generate
( library, output, layoutRoots, info, libDirs = [root @@ "../../lib"])
let fileNames = Directory.GetFiles(output)
let files = dict [ for f in fileNames -> Path.GetFileName(f), File.ReadAllText(f) ]
files.["index.html"] |> should contain "CLikeFormat"
files.["index.html"] |> should contain "Provides a base class for formatting languages similar to C."

[<Test>]
let ``MetadataFormat processes C# properties on types and includes xml comments in docs`` () =
let library = root @@ "files" @@ "CSharpFormat.dll"
let output = getOutputDir()
MetadataFormat.Generate
( library, output, layoutRoots, info, libDirs = [root @@ "../../lib"])
let fileNames = Directory.GetFiles(output)
let files = dict [ for f in fileNames -> Path.GetFileName(f), File.ReadAllText(f) ]

files.["manoli-utils-csharpformat-clikeformat.html"] |> should contain "CommentRegEx"
files.["manoli-utils-csharpformat-clikeformat.html"] |> should contain "Regular expression string to match single line and multi-line"