Skip to content

Commit

Permalink
Merge pull request #1 from dawedawe/reflecteddef
Browse files Browse the repository at this point in the history
Make use of [<ReflectedDefinition>]
  • Loading branch information
dawedawe authored Jan 16, 2024
2 parents a6bf5b9 + f8d7e0d commit 1447b5d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [0.2.0] - 2024-01-16

### Fixed

* Fixed a caching issue which caused the loss of xml fragments.

### Added

* Added `H.H` to apply directly to expressions without the need for quotation wrapping.

## [0.1.1] - 2023-12-25

### Fixed
Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Fsih
====

Fsih provides you with the `h` function, meant to be used in the F# REPL [fsi](https://learn.microsoft.com/en-us/dotnet/fsharp/tools/fsharp-interactive/).
Fsih provides you with the `h` and `H.H` functions, meant to be used in the F# REPL [fsi](https://learn.microsoft.com/en-us/dotnet/fsharp/tools/fsharp-interactive/).
It's modeled after the `h` function in the Elixir [iex](https://hexdocs.pm/iex/1.16.0/IEx.html) REPL.

To use it, just start an fsi session with `dotnet fsi`.
Expand All @@ -11,9 +11,30 @@ Load the package and open the namespace:
open Fsih;;
```

Apply h to any function wrapped in an FSharp [quotation](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/code-quotations) to get its documentation:
Apply h to any expression wrapped in an FSharp [quotation](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/code-quotations) to get its documentation:
```fsharp
h <@ fst @>
h <@ fst @>;;
```

```
Description:
Return the first element of a tuple, fst (a,b) = a.
Parameters:
- tuple: The input tuple.
Returns:
The first value.
Examples:
fst ("first", 2) // Evaluates to "first"
Full name: Microsoft.FSharp.Core.Operators.fst
Assembly: FSharp.Core.dll
```

Or apply H.H to any expression directly to get its documentation:
```fsharp
H.H fst;;
```

```
Expand Down
23 changes: 16 additions & 7 deletions src/Fsih.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ let ``full info is as expected for Seq.splitInto`` () =
Assert.Equal("Microsoft.FSharp.Collections.SeqModule.splitInto", fullName)
Assert.Equal("FSharp.Core.dll", assembly)

| Some _ -> Assert.False(true, sprintf "unexpected help")
| None -> Assert.False(true, sprintf "no docs for Seq.splitInto")
| Some _ -> Assert.False(true, "unexpected help")
| None -> Assert.False(true, "no docs for Seq.splitInto")

[<Fact>]
let ``returns is as expected for HashIdentity.FromFunctions`` () =
Expand All @@ -96,17 +96,17 @@ let ``returns is as expected for HashIdentity.FromFunctions`` () =
"An object implementing System.Collections.Generic.IEqualityComparer using the given functions.",
returns
)
| Some _ -> Assert.False(true, sprintf "unexpected help")
| None -> Assert.False(true, sprintf "no docs for Seq.splitInto")
| Some _ -> Assert.False(true, "unexpected help")
| None -> Assert.False(true, "no docs for HashIdentity.FromFunctions")

[<Fact>]
let ``remarks is as expected for List.reduce`` () =
let doc = tryGetDocumentation <@ List.reduce @>

match doc with
| Some { Remarks = Some remarks } -> Assert.Equal("Raises System.ArgumentException if list is empty", remarks)
| Some _ -> Assert.False(true, sprintf "unexpected help")
| None -> Assert.False(true, sprintf "no docs for Seq.splitInto")
| Some _ -> Assert.False(true, "unexpected help")
| None -> Assert.False(true, "no docs for List.reduce")

[<Fact>]
let ``summary is as expected for Array.sortDescending`` () =
Expand All @@ -118,4 +118,13 @@ let ``summary is as expected for Array.sortDescending`` () =
"Sorts the elements of an array, in descending order, returning a new array. Elements are compared using Microsoft.FSharp.Core.Operators.compare.",
summary
)
| None -> Assert.False(true, sprintf "no docs for Seq.splitInto")
| None -> Assert.False(true, "no docs for Array.sortDescending")

[<Fact>]
let ``ReflectedDefinition works as expected`` () =
let docReflected = H.TryGetDocumentation(id)
let docQuoted = tryGetDocumentation <@ id @>

match docReflected, docQuoted with
| Some r, Some q -> Assert.True((r = q))
| _ -> Assert.False(true, "no docs for id")
10 changes: 7 additions & 3 deletions src/Fsih/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,23 @@ let trimDotNet (s: string) =
let s = if idx > 0 then s.Substring(0, idx) else s
s

let xmlDocCache = Collections.Generic.Dictionary<string, XmlDocument>()
// WTF, seems like we loose inner xml (example content) if we cache an XmlDocument
let xmlDocCache = Collections.Generic.Dictionary<string, string>()

let getXmlDocument xmlPath =
#if DEBUG
printfn $"xml file: %s{xmlPath}"
#endif
match xmlDocCache.TryGetValue(xmlPath) with
| true, value -> value
| true, value ->
let xmlDocument = XmlDocument()
xmlDocument.LoadXml(value)
xmlDocument
| _ ->
let rawXml = File.ReadAllText(xmlPath)
let xmlDocument = XmlDocument()
xmlDocument.LoadXml(rawXml)
xmlDocCache.Add(xmlPath, xmlDocument)
xmlDocCache.Add(xmlPath, rawXml)
xmlDocument

let getTexts (node: Xml.XmlNode) =
Expand Down
34 changes: 9 additions & 25 deletions src/Fsih/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,17 @@ module Logic =
| None -> printfn "unable to get documentation"
| Some d -> d.Print()

module Program =
type H() =
static member H([<ReflectedDefinition>] expr: Quotations.Expr<_>) = h expr
static member TryGetDocumentation([<ReflectedDefinition>] expr: Quotations.Expr<_>) = tryGetDocumentation expr

open Logic
module Program =

[<EntryPoint>]
let main argv =
// h <@ Option.isNone @>
// h <@ Option.defaultValue @>
// h <@ Seq.allPairs @>
// h <@ Seq.iter @>
// h <@ Seq.average [ 1. ] @>
// h <@ Seq.append @>
// h <@ Seq.average [ 1. ] @>
// h <@ printfn @>
// h <@ None @>
// h <@ System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes @>
// h <@ Collections.ResizeArray<int> [] @>
// h <@ [] @>
// h <@ "" @>
// h <@ 1 :: List.empty @>
// h <@ Map @>
// h <@ [| 1 |] @>
// h <@ Array.Parallel.tryFind @>
// h <@ Seq.head @>
// h <@ Seq.delay @>
// h <@ Seq.countBy @>
// h <@ Microsoft.FSharp.Collections.HashIdentity.FromFunctions @>
// h <@ List.reduce @>
let main _argv =
// h <@ id @>
// H.H id
h <@ List.map @>
H.H List.map

0

0 comments on commit 1447b5d

Please sign in to comment.