From e851d3f12c7c9b4d10d838e071f072f197a23e9c Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Wed, 1 Feb 2023 16:39:08 +0000 Subject: [PATCH] Expose SyntaxOak API (#2758) * Expose SyntaxOak API * Add changelog entry for 6.0 alpha 2. --------- Co-authored-by: nojaf --- CHANGELOG.md | 7 +++++++ src/Fantomas.Core/CodeFormatter.fs | 28 ++++++++++++++++++++++++++++ src/Fantomas.Core/CodeFormatter.fsi | 10 ++++++++++ src/Fantomas.Core/SyntaxOak.fs | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec182b3509..2b952bd131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [6.0.0-alpha-002] - 2023-02-01 + +### Changed +* Update output for copy-and-update expression for Stroustrup. [#2748](https://github.com/fsprojects/fantomas/pull/2748) +* Drop Experimental prefix from ExperimentalStroustrup. [#2755](https://github.com/fsprojects/fantomas/pull/2755) +* Expose initial Oak API. [#2758](https://github.com/fsprojects/fantomas/pull/2758) + ## [6.0.0-alpha-001] - 2023-01-24 ### Changed diff --git a/src/Fantomas.Core/CodeFormatter.fs b/src/Fantomas.Core/CodeFormatter.fs index 99d2d28cb6..f8e7b2d352 100644 --- a/src/Fantomas.Core/CodeFormatter.fs +++ b/src/Fantomas.Core/CodeFormatter.fs @@ -2,6 +2,7 @@ namespace Fantomas.Core open FSharp.Compiler.Syntax open FSharp.Compiler.Text +open Fantomas.Core.SyntaxOak [] type CodeFormatter = @@ -33,3 +34,30 @@ type CodeFormatter = Range.mkRange fileName (Position.mkPos startLine startCol) (Position.mkPos endLine endCol) static member MakePosition(line, column) = Position.mkPos line column + + [] + static member ParseOakAsync(isSignature: bool, source: string) : Async<(Oak * string list) array> = + async { + let sourceText = CodeFormatterImpl.getSourceText source + let! ast = CodeFormatterImpl.parse isSignature sourceText + + return + ast + |> Array.map (fun (ast, defines) -> + let oak = ASTTransformer.mkOak (Some sourceText) ast + oak, defines) + } + + static member FormatOakAsync(oak: Oak) : Async = + async { + let context = Context.Context.Create FormatConfig.Default + let result = context |> CodePrinter.genFile oak |> Context.dump false + return result.Code + } + + static member FormatOakAsync(oak: Oak, config: FormatConfig) : Async = + async { + let context = Context.Context.Create config + let result = context |> CodePrinter.genFile oak |> Context.dump false + return result.Code + } diff --git a/src/Fantomas.Core/CodeFormatter.fsi b/src/Fantomas.Core/CodeFormatter.fsi index c8b8a68aa0..c1b5a8ba9f 100644 --- a/src/Fantomas.Core/CodeFormatter.fsi +++ b/src/Fantomas.Core/CodeFormatter.fsi @@ -2,6 +2,7 @@ namespace Fantomas.Core open FSharp.Compiler.Text open FSharp.Compiler.Syntax +open Fantomas.Core.SyntaxOak [] type CodeFormatter = @@ -37,3 +38,12 @@ type CodeFormatter = /// Make a pos from line and column static member MakePosition: line: int * column: int -> pos + + /// Parse a source string to SyntaxOak + static member ParseOakAsync: isSignature: bool * source: string -> Async<(Oak * string list) array> + + /// Format SyntaxOak to string + static member FormatOakAsync: oak: Oak -> Async + + /// Format SyntaxOak to string using given config + static member FormatOakAsync: oak: Oak * config: FormatConfig -> Async diff --git a/src/Fantomas.Core/SyntaxOak.fs b/src/Fantomas.Core/SyntaxOak.fs index 536195510c..40593033d9 100644 --- a/src/Fantomas.Core/SyntaxOak.fs +++ b/src/Fantomas.Core/SyntaxOak.fs @@ -1,4 +1,4 @@ -module internal rec Fantomas.Core.SyntaxOak +module rec Fantomas.Core.SyntaxOak open System.Collections.Generic open FSharp.Compiler.Text