Skip to content

Commit

Permalink
Expose SyntaxOak API (#2758)
Browse files Browse the repository at this point in the history
* Expose SyntaxOak API

* Add changelog entry for 6.0 alpha 2.

---------

Co-authored-by: nojaf <[email protected]>
  • Loading branch information
edgarfgp and nojaf committed Mar 27, 2023
1 parent 0490b85 commit a85ed3c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/Fantomas.Core/CodeFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Fantomas.Core

open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open Fantomas.Core.SyntaxOak

[<Sealed>]
type CodeFormatter =
Expand Down Expand Up @@ -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

[<Experimental "Only for local development">]
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<string> =
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<string> =
async {
let context = Context.Context.Create config
let result = context |> CodePrinter.genFile oak |> Context.dump false
return result.Code
}
10 changes: 10 additions & 0 deletions src/Fantomas.Core/CodeFormatter.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Fantomas.Core

open FSharp.Compiler.Text
open FSharp.Compiler.Syntax
open Fantomas.Core.SyntaxOak

[<Sealed>]
type CodeFormatter =
Expand Down Expand Up @@ -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<string>

/// Format SyntaxOak to string using given config
static member FormatOakAsync: oak: Oak * config: FormatConfig -> Async<string>
2 changes: 1 addition & 1 deletion src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module internal rec Fantomas.Core.SyntaxOak
module rec Fantomas.Core.SyntaxOak

open System.Collections.Generic
open FSharp.Compiler.Text
Expand Down

0 comments on commit a85ed3c

Please sign in to comment.