Skip to content

Commit

Permalink
Update watcher.fsx to support nullness
Browse files Browse the repository at this point in the history
I have zero experience on the Ionide codebase, but saw that VSCode was giving nullness warnings on starting FSI e.g.

```
ionide.ionide-fsharp-7.22.0\watcher\watcher.fsx(48,34): warning FS3261: Nullness warning: The types 'System.Reflection.MemberInfo' and 'System.Reflection.MemberInfo | null' do not have compatible nullability.
```

This PR removes the warnings, but someone should review them to make sure that there hasn't been any semantic change in behaviour.
  • Loading branch information
isaacabraham authored and baronfel committed Dec 6, 2024
1 parent 77e1b3f commit 5fcad29
Showing 1 changed file with 26 additions and 12 deletions.
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)
"")

0 comments on commit 5fcad29

Please sign in to comment.