Skip to content

Commit 1447b5d

Browse files
authored
Merge pull request #1 from dawedawe/reflecteddef
Make use of [<ReflectedDefinition>]
2 parents a6bf5b9 + f8d7e0d commit 1447b5d

File tree

5 files changed

+66
-38
lines changed

5 files changed

+66
-38
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [0.2.0] - 2024-01-16
4+
5+
### Fixed
6+
7+
* Fixed a caching issue which caused the loss of xml fragments.
8+
9+
### Added
10+
11+
* Added `H.H` to apply directly to expressions without the need for quotation wrapping.
12+
313
## [0.1.1] - 2023-12-25
414

515
### Fixed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Fsih
22
====
33

4-
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/).
4+
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/).
55
It's modeled after the `h` function in the Elixir [iex](https://hexdocs.pm/iex/1.16.0/IEx.html) REPL.
66

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

14-
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:
14+
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:
1515
```fsharp
16-
h <@ fst @>
16+
h <@ fst @>;;
17+
```
18+
19+
```
20+
Description:
21+
Return the first element of a tuple, fst (a,b) = a.
22+
23+
Parameters:
24+
- tuple: The input tuple.
25+
Returns:
26+
The first value.
27+
28+
Examples:
29+
fst ("first", 2) // Evaluates to "first"
30+
31+
Full name: Microsoft.FSharp.Core.Operators.fst
32+
Assembly: FSharp.Core.dll
33+
```
34+
35+
Or apply H.H to any expression directly to get its documentation:
36+
```fsharp
37+
H.H fst;;
1738
```
1839

1940
```

src/Fsih.Tests/Tests.fs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ let ``full info is as expected for Seq.splitInto`` () =
8383
Assert.Equal("Microsoft.FSharp.Collections.SeqModule.splitInto", fullName)
8484
Assert.Equal("FSharp.Core.dll", assembly)
8585

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

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

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

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

111111
[<Fact>]
112112
let ``summary is as expected for Array.sortDescending`` () =
@@ -118,4 +118,13 @@ let ``summary is as expected for Array.sortDescending`` () =
118118
"Sorts the elements of an array, in descending order, returning a new array. Elements are compared using Microsoft.FSharp.Core.Operators.compare.",
119119
summary
120120
)
121-
| None -> Assert.False(true, sprintf "no docs for Seq.splitInto")
121+
| None -> Assert.False(true, "no docs for Array.sortDescending")
122+
123+
[<Fact>]
124+
let ``ReflectedDefinition works as expected`` () =
125+
let docReflected = H.TryGetDocumentation(id)
126+
let docQuoted = tryGetDocumentation <@ id @>
127+
128+
match docReflected, docQuoted with
129+
| Some r, Some q -> Assert.True((r = q))
130+
| _ -> Assert.False(true, "no docs for id")

src/Fsih/Parser.fs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,23 @@ let trimDotNet (s: string) =
6565
let s = if idx > 0 then s.Substring(0, idx) else s
6666
s
6767

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

7071
let getXmlDocument xmlPath =
7172
#if DEBUG
7273
printfn $"xml file: %s{xmlPath}"
7374
#endif
7475
match xmlDocCache.TryGetValue(xmlPath) with
75-
| true, value -> value
76+
| true, value ->
77+
let xmlDocument = XmlDocument()
78+
xmlDocument.LoadXml(value)
79+
xmlDocument
7680
| _ ->
7781
let rawXml = File.ReadAllText(xmlPath)
7882
let xmlDocument = XmlDocument()
7983
xmlDocument.LoadXml(rawXml)
80-
xmlDocCache.Add(xmlPath, xmlDocument)
84+
xmlDocCache.Add(xmlPath, rawXml)
8185
xmlDocument
8286

8387
let getTexts (node: Xml.XmlNode) =

src/Fsih/Program.fs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,17 @@ module Logic =
8686
| None -> printfn "unable to get documentation"
8787
| Some d -> d.Print()
8888

89-
module Program =
89+
type H() =
90+
static member H([<ReflectedDefinition>] expr: Quotations.Expr<_>) = h expr
91+
static member TryGetDocumentation([<ReflectedDefinition>] expr: Quotations.Expr<_>) = tryGetDocumentation expr
9092

91-
open Logic
93+
module Program =
9294

9395
[<EntryPoint>]
94-
let main argv =
95-
// h <@ Option.isNone @>
96-
// h <@ Option.defaultValue @>
97-
// h <@ Seq.allPairs @>
98-
// h <@ Seq.iter @>
99-
// h <@ Seq.average [ 1. ] @>
100-
// h <@ Seq.append @>
101-
// h <@ Seq.average [ 1. ] @>
102-
// h <@ printfn @>
103-
// h <@ None @>
104-
// h <@ System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes @>
105-
// h <@ Collections.ResizeArray<int> [] @>
106-
// h <@ [] @>
107-
// h <@ "" @>
108-
// h <@ 1 :: List.empty @>
109-
// h <@ Map @>
110-
// h <@ [| 1 |] @>
111-
// h <@ Array.Parallel.tryFind @>
112-
// h <@ Seq.head @>
113-
// h <@ Seq.delay @>
114-
// h <@ Seq.countBy @>
115-
// h <@ Microsoft.FSharp.Collections.HashIdentity.FromFunctions @>
116-
// h <@ List.reduce @>
96+
let main _argv =
97+
// h <@ id @>
98+
// H.H id
99+
h <@ List.map @>
100+
H.H List.map
117101

118102
0

0 commit comments

Comments
 (0)