Files
WoofWare.Whippet/WoofWare.Whippet.Fantomas/Ast.fs
Patrick Stevens 8834d885de
Some checks failed
.NET / build (Debug) (push) Has been cancelled
.NET / build (Release) (push) Has been cancelled
.NET / analyzers (push) Has been cancelled
.NET / build-nix (push) Has been cancelled
.NET / check-dotnet-format (push) Has been cancelled
.NET / check-nix-format (push) Has been cancelled
.NET / Check links (push) Has been cancelled
.NET / Check flake (push) Has been cancelled
.NET / nuget-pack (push) Has been cancelled
.NET / expected-pack (push) Has been cancelled
.NET / all-required-checks-complete (push) Has been cancelled
Initial import of Fantomas client library (#6)
2024-10-04 15:13:49 +01:00

48 lines
1.6 KiB
Forth

namespace WoofWare.Whippet.Fantomas
open Fantomas.Core
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
/// Helper methods to convert between source code and FCS ASTs.
[<RequireQualifiedAccess>]
module Ast =
/// Given the contents of an F# source file, parse it into an AST. This is sync-over-async internally, which is
/// naughty.
let parse (fileContents : string) : ParsedInput =
CodeFormatter.ParseAsync (false, fileContents)
|> Async.RunSynchronously
|> Array.head
|> fst
/// Concatenate the input modules/namespaces and render them as a single F# source file.
///
/// This can return `None`, if the input was empty.
/// This is sync-over-async internally, which is naughty.
let render (contents : SynModuleOrNamespace list) : string option =
if contents.IsEmpty then
None
else
let parseTree =
ParsedInput.ImplFile (
ParsedImplFileInput.ParsedImplFileInput (
"file.fs",
false,
QualifiedNameOfFile.QualifiedNameOfFile (Ident.create "file"),
[],
[],
contents,
(false, false),
{
ParsedImplFileInputTrivia.CodeComments = []
ConditionalDirectives = []
},
Set.empty
)
)
let cfg = FormatConfig.Default
CodeFormatter.FormatASTAsync (parseTree, cfg) |> Async.RunSynchronously |> Some