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

Update watcher.fsx to support nullness #2055

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
38 changes: 26 additions & 12 deletions release/watcher/watcher.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ fsi.AddPrinter (fun (_: obj) ->
let fsiAsm = "FSI-ASSEMBLY"

let asmNum (asm:System.Reflection.Assembly) =
asm.GetName().Name.Replace(fsiAsm, "") |> System.Int32.TryParse |> fun (b,v) -> if b then v else 0
match asm.GetName().Name with
| null -> 0
| name -> name.Replace(fsiAsm, "") |> System.Int32.TryParse |> fun (b,v) -> if b then v else 0

let fsiAssemblies =
// use multiple assemblies (FSI-ASSEMBLY1, FSI-ASSEMBLY2...) if single isn't found
let fsiAsms =
System.AppDomain.CurrentDomain.GetAssemblies()
|> Array.filter (fun asm -> asm.GetName().Name.StartsWith fsiAsm)
|> Array.filter (fun asm ->
match asm.GetName().Name with
| null -> false
| name -> name.StartsWith fsiAsm
)

fsiAsms
|> Array.tryFind (fun asm -> asm.GetName().Name = fsiAsm)
|> function
Expand All @@ -30,9 +37,14 @@ fsi.AddPrinter (fun (_: obj) ->
|> Seq.sortBy (fun (_, (i, _)) -> i) //order by original index
|> Seq.map (fun (_, (_, pi)) -> pi.Name, pi.GetValue(null, Array.empty), pi.PropertyType) //discard ordering index, project usuable watch value

let isFsiAssembly (ty:System.Type) =
match ty.FullName with
| null -> false
| name -> name.StartsWith("FSI")

let getRecords (fsiAssembly:System.Reflection.Assembly) =
fsiAssembly.GetTypes()
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
|> Seq.filter isFsiAssembly
|> Seq.filter (Reflection.FSharpType.IsRecord)
|> Seq.map (fun ty ->
let flds =
Expand All @@ -43,9 +55,12 @@ fsi.AddPrinter (fun (_: obj) ->

let getUnions (fsiAssembly:System.Reflection.Assembly) =
fsiAssembly.GetTypes()
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
|> Seq.filter isFsiAssembly
|> Seq.filter (Reflection.FSharpType.IsUnion)
|> Seq.filter (fun ty -> ty.BaseType.Name = "Object") //find DU declaration not DU cases
|> Seq.filter (fun ty ->
match ty.BaseType with
| null -> false
| ty -> ty.Name = "Object") //find DU declaration not DU cases
|> Seq.map (fun ty ->
let flds =
Reflection.FSharpType.GetUnionCases ty
Expand All @@ -60,7 +75,7 @@ fsi.AddPrinter (fun (_: obj) ->

let getFuncs (fsiAssembly:System.Reflection.Assembly) =
fsiAssembly.GetTypes()
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
|> Seq.filter isFsiAssembly
|> Seq.filter (Reflection.FSharpType.IsModule)
|> Seq.choose (fun ty ->
let meth =
Expand Down Expand Up @@ -97,10 +112,9 @@ fsi.AddPrinter (fun (_: obj) ->

let fromAssemblies folder =
fsiAssemblies
|> Array.toList
|> Seq.map (fun asm -> asmNum asm, asm)
|> Seq.sortByDescending fst // assume later/higher assembly # always shadows
|> Seq.fold folder (Set.empty, Seq.empty)
|> Array.map (fun asm -> asmNum asm, asm)
|> Array.sortByDescending fst // assume later/higher assembly # always shadows
|> Array.fold folder (Set.empty, Seq.empty)
|> snd

let arrangeVars (state: Set<string> * seq<string>) (step, asm) =
Expand All @@ -123,7 +137,7 @@ fsi.AddPrinter (fun (_: obj) ->
let shadowed = (fst state).Contains name
let parms =
parms
|> Seq.map (fun (n, t) -> n + ": " + t)
|> Seq.map (fun (n, t) -> $"{n}: {t}")
|> String.concat "; "
formatVarsAndFuncs name parms typ step shadowed, name)
let names =
Expand Down Expand Up @@ -199,4 +213,4 @@ fsi.AddPrinter (fun (_: obj) ->
}
|> Async.Start

null)
"")