Skip to content

Commit

Permalink
Fantomas format
Browse files Browse the repository at this point in the history
  • Loading branch information
amine-mej-dedge committed Dec 21, 2022
1 parent 4fd2975 commit 64cb126
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 172 deletions.
16 changes: 7 additions & 9 deletions src/Diffract/DictionaryShape.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ open System.Collections.Generic
open TypeShape.Core

type IDictionaryVisitor<'R> =
abstract Visit<'Dict, 'K, 'V when 'K : equality and 'Dict :> IDictionary<'K, 'V>> : unit -> 'R
abstract Visit<'Dict, 'K, 'V when 'K: equality and 'Dict :> IDictionary<'K, 'V>> : unit -> 'R

type IShapeDictionary =
abstract Key : TypeShape
abstract Value : TypeShape
abstract Accept : visitor: IDictionaryVisitor<'R> -> 'R
abstract Key: TypeShape
abstract Value: TypeShape
abstract Accept: visitor: IDictionaryVisitor<'R> -> 'R

type private ShapeDictionary<'Dict, 'K, 'V when 'K : equality and 'Dict :> IDictionary<'K, 'V>>() =
type private ShapeDictionary<'Dict, 'K, 'V when 'K: equality and 'Dict :> IDictionary<'K, 'V>>() =
interface IShapeDictionary with
member _.Key = shapeof<'K> :> _
member _.Value = shapeof<'V> :> _
Expand All @@ -26,13 +26,11 @@ module Shape =
match s.ShapeInfo with
| Generic (td, ta) when td.FullName = fullName -> Some ta
| _ -> None
| iface ->
Some (iface.GetGenericArguments())
| iface -> Some(iface.GetGenericArguments())

let (|Dictionary|_|) (s: TypeShape) =
match s with
| GenericInterface "System.Collections.Generic.IDictionary`2" ta ->
Activator.CreateInstanceGeneric<ShapeDictionary<_, _, _>>(Array.append [|s.Type|] ta)
:?> IShapeDictionary
Activator.CreateInstanceGeneric<ShapeDictionary<_, _, _>>(Array.append [| s.Type |] ta) :?> IShapeDictionary
|> Some
| _ -> None
40 changes: 27 additions & 13 deletions src/Diffract/DiffPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ open System.IO

let toStreamImpl (w: TextWriter) param (d: Diff) =
let originalParam = param
let param = if originalParam.ensureFirstLineIsAligned then { originalParam with ensureFirstLineIsAligned = false } else originalParam

let addPathField path field = if path = "" then field else (path + "." + field)
let param =
if originalParam.ensureFirstLineIsAligned then
{ originalParam with ensureFirstLineIsAligned = false }
else
originalParam

let addPathField path field =
if path = "" then field else (path + "." + field)

let addPathIndex path index = path + "[" + index + "]"
let indentLike str = String.replicate (String.length str) " "
let displayPath path = if path = "" then param.neutralName else path

let indentLike str =
String.replicate (String.length str) " "

let displayPath path =
if path = "" then param.neutralName else path

let printValue indent path x1 x2 =
let dpath = if path = "" then "" else path + " "
w.WriteLine($"%s{indent}%s{dpath}%s{param.x1Name} = %A{x1}")
w.WriteLine($"%s{indent}%s{indentLike dpath}%s{param.x2Name} = %A{x2}")

let rec loop (indent: string) (path: string) (d: Diff) =
match d with
| Diff.Value (x1, x2) ->
printValue indent path x1 x2
| Diff.Record fields when fields.Count = 1 ->
loop indent (addPathField path fields.[0].Name) fields.[0].Diff
| Diff.Value (x1, x2) -> printValue indent path x1 x2
| Diff.Record fields when fields.Count = 1 -> loop indent (addPathField path fields.[0].Name) fields.[0].Diff
| Diff.Record fields ->
w.WriteLine($"%s{indent}%s{displayPath path} differs by %i{fields.Count} fields:")
let indent = indent + param.indent

for field in fields do
loop indent (addPathField path field.Name) field.Diff
| Diff.UnionCase (caseName1, caseName2) ->
Expand All @@ -37,35 +47,39 @@ let toStreamImpl (w: TextWriter) param (d: Diff) =
| Diff.UnionField (case, fields) ->
w.WriteLine($"%s{indent}%s{displayPath path} differs by union case %s{case} fields:")
let indent = indent + param.indent

for field in fields do
loop indent (addPathField path field.Name) field.Diff
| Diff.Collection (c1, c2, diffs) ->
w.WriteLine($"%s{indent}%s{displayPath path} collection differs:")
let indent = indent + param.indent

if c1 <> c2 then
let countPath = addPathField path "Count"
w.WriteLine($"%s{indent}%s{countPath} %s{param.x1Name} = %i{c1}")
w.WriteLine($"%s{indent}%s{indentLike countPath} %s{param.x2Name} = %i{c2}")

for item in diffs do
loop indent (addPathIndex path item.Name) item.Diff
| Diff.Custom cd ->
cd.WriteTo(w, param, indent, path, loop)
| Diff.Custom cd -> cd.WriteTo(w, param, indent, path, loop)
| Diff.Dictionary (keysInX1, keysInX2, common) ->
w.WriteLine($"%s{indent}%s{displayPath path} dictionary differs:")
let indent = indent + param.indent

for k in keysInX1 do
w.WriteLine($"%s{indent}%s{param.x2Name}[%s{k}] is missing")

for k in keysInX2 do
w.WriteLine($"%s{indent}%s{param.x1Name}[%s{k}] is missing")

for item in common do
loop indent (addPathIndex path item.Name) item.Diff

match originalParam.ensureFirstLineIsAligned, d with
| true, Diff.Value (x1, x2) ->
w.WriteLine()
printValue "" "" x1 x2
| true, Diff.Custom cd ->
cd.WriteTo(w, originalParam, "", "", loop)
| true, Diff.Custom cd -> cd.WriteTo(w, originalParam, "", "", loop)
| _ -> loop "" "" d

let write (param: PrintParams) (w: TextWriter) (d: Diff option) =
Expand Down
Loading

0 comments on commit 64cb126

Please sign in to comment.