Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 3 additions & 11 deletions src/Myriad.Plugins/DUCasesGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ module internal CreateDUModule =
let args = SynPat.CreateTyped(name, duType) |> SynPat.CreateParen
SynPat.CreateLongIdent(varIdent, [args])

let resolveCaseIdent (requiresQualifiedAccess: bool) (parent: LongIdent) (id: Fantomas.FCS.Syntax.Ident) : SynLongIdent =
let parts =
if requiresQualifiedAccess then
(parent |> List.map (fun i -> i.idText)) @ [id.idText]
else
[id.idText]
SynLongIdent.Create parts

let createMatchOnIdent (inputIdent: string) : SynExpr =
let ident = SynLongIdent.CreateString inputIdent
SynExpr.CreateLongIdent(false, ident, None)

let createCaseMatchClause (requiresQualifiedAccess: bool) (parent: LongIdent) (id: Ident) (hasFields: bool) (rhs: SynExpr) : SynMatchClause =
let indent = resolveCaseIdent requiresQualifiedAccess parent id
let ident = GeneratorHelpers.resolveCaseIdent requiresQualifiedAccess parent id
let args = if hasFields then [SynPat.CreateWild] else []
let p = SynPat.CreateLongIdent(indent, args)
let p = SynPat.CreateLongIdent(ident, args)
SynMatchClause.Create(p, None, rhs)

let createDuLetBinding (varName: string) (inputType: SynType) (returnType: SynType) (buildMatchClauses: unit -> SynMatchClause list) : SynModuleDecl =
Expand Down Expand Up @@ -65,7 +57,7 @@ module internal CreateDUModule =
let pat = SynPat.CreateConst(con)
let rhs =
let f = SynExpr.Ident (Ident("Some", range0))
let fullCaseName = resolveCaseIdent requiresQualifiedAccess parent id
let fullCaseName = GeneratorHelpers.resolveCaseIdent requiresQualifiedAccess parent id
let x = SynExpr.CreateLongIdent fullCaseName
SynExpr.App(ExprAtomicFlag.NonAtomic, false, f, x, range0)
SynMatchClause.Create(pat, None, rhs)
Expand Down
10 changes: 10 additions & 0 deletions src/Myriad.Plugins/GeneratorHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ module internal GeneratorHelpers =
| [] -> None
| types -> Some (ns, types))

/// Resolves a DU case identifier, optionally qualifying it with the parent type name
/// when RequireQualifiedAccess is present.
let resolveCaseIdent (requiresQualifiedAccess: bool) (parent: LongIdent) (id: Ident) : SynLongIdent =
let parts =
if requiresQualifiedAccess then
(parent |> List.map (fun i -> i.idText)) @ [id.idText]
else
[id.idText]
SynLongIdent.Create parts

/// Runs the standard generator pipeline: parse input AST, extract types, filter by attribute,
/// and collect modules. Eliminates the boilerplate shared by DUCasesGenerator and FieldsGenerator.
let generateModules<'Attr> (context: GeneratorContext) (extract: ParsedInput -> (LongIdent * SynTypeDefn list) list) (create: LongIdent -> SynTypeDefn -> (string * obj) seq -> SynModuleOrNamespace) : Output =
Expand Down
8 changes: 1 addition & 7 deletions src/Myriad.Plugins/LensesGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,8 @@ module internal CreateLenses =
let pattern =
SynPat.CreateLongIdent(SynLongIdent.CreateString "Lens'", [])

let matchCaseIdentParts =
if requiresQualifiedAccess then
(parent |> List.map (fun i -> i.idText)) @ [id.idText]
else
[id.idText]

// The name of the DU case, optionally preceded by the name of the DU itself, if fully qualified access is required
let fullCaseName = SynLongIdent.Create matchCaseIdentParts
let fullCaseName = GeneratorHelpers.resolveCaseIdent requiresQualifiedAccess parent id

let lensExpression =
let matchCase =
Expand Down
Loading