Skip to content

Commit 7293a94

Browse files
committed
Return string from FormatASTAsync api. (#2799)
* Return string from FormatASTAsync api. * Add identifier in code sample. * Add release notes.
1 parent 9b14355 commit 7293a94

File tree

9 files changed

+40
-24
lines changed

9 files changed

+40
-24
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## [6.0.0-alpha-007] - 2023-03-27
4+
5+
### Changed
6+
7+
* `CodeFormatter.FormatASTAsync` returns a string. [#2799](https://github.com/fsprojects/fantomas/pull/2799)
8+
* Cursor with defines. [#2774](https://github.com/fsprojects/fantomas/pull/2774)
9+
10+
### Removed
11+
12+
* Strict mode. [#2798](https://github.com/fsprojects/fantomas/pull/2798)
13+
314
## [6.0.0-alpha-006] - 2023-03-17
415

516
### Fixed

docs/docs/end-users/GeneratingCode.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let implementationSyntaxTree =
5050
false,
5151
None,
5252
None,
53-
Choice1Of2(IdentListNode([], Range.Zero)),
53+
Choice1Of2(IdentListNode([ IdentifierOrDot.Ident(SingleTextNode("a", Range.Zero)) ], Range.Zero)),
5454
None,
5555
[],
5656
None,

src/Fantomas.Core.Tests/FormatAstTests.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ let parseAndFormat sourceCode =
1515
let formattedCode =
1616
CodeFormatter.FormatASTAsync(ast, source = sourceCode)
1717
|> Async.RunSynchronously
18-
|> fun formatResult -> formatResult.Code
1918
|> String.normalizeNewLine
2019
|> fun s -> s.TrimEnd('\n')
2120

src/Fantomas.Core.Tests/SynLongIdentTests.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,4 @@ let ``backticks can be added from AST only scenarios`` () =
404404
InsertFinalNewline = false }
405405
)
406406
|> Async.RunSynchronously
407-
|> fun result -> result.Code
408407
|> should equal "``new``"

src/Fantomas.Core.Tests/TestHelpers.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Fantomas.Core.Tests.TestHelper
22

33
open System
44
open Fantomas.Core
5-
open Fantomas.Core.SyntaxOak
65
open NUnit.Framework
76
open FsUnit
87

@@ -42,13 +41,13 @@ let formatAST isFsiFile (source: string) config =
4241
let ast, _ =
4342
Fantomas.FCS.Parse.parseFile isFsiFile (FSharp.Compiler.Text.SourceText.ofString source) []
4443

45-
let! formatted = CodeFormatter.FormatASTAsync(ast, config = config)
46-
let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, formatted.Code)
44+
let! formattedCode = CodeFormatter.FormatASTAsync(ast, config = config)
45+
let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, formattedCode)
4746

4847
if not isValid then
49-
failwithf $"The formatted result is not valid F# code or contains warnings\n%s{formatted.Code}"
48+
failwithf $"The formatted result is not valid F# code or contains warnings\n%s{formattedCode}"
5049

51-
return formatted.Code.Replace("\r\n", "\n")
50+
return formattedCode.Replace("\r\n", "\n")
5251
}
5352
|> Async.RunSynchronously
5453

src/Fantomas.Core/CodeFormatter.fs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,31 @@ type CodeFormatter =
1212
return results |> Array.map (fun (ast, DefineCombination(defines)) -> ast, defines)
1313
}
1414

15-
static member FormatASTAsync(ast: ParsedInput) : Async<FormatResult> =
16-
CodeFormatterImpl.formatAST ast None FormatConfig.Default None |> async.Return
17-
18-
static member FormatASTAsync(ast: ParsedInput, config) : Async<FormatResult> =
19-
CodeFormatterImpl.formatAST ast None config None |> async.Return
15+
static member FormatASTAsync(ast: ParsedInput) : Async<string> =
16+
async {
17+
let result = CodeFormatterImpl.formatAST ast None FormatConfig.Default None
18+
return result.Code
19+
}
2020

21-
static member FormatASTAsync(ast: ParsedInput, source) : Async<FormatResult> =
22-
let sourceText = Some(CodeFormatterImpl.getSourceText source)
21+
static member FormatASTAsync(ast: ParsedInput, config) : Async<string> =
22+
async {
23+
let result = CodeFormatterImpl.formatAST ast None config None
24+
return result.Code
25+
}
2326

24-
CodeFormatterImpl.formatAST ast sourceText FormatConfig.Default None
25-
|> async.Return
27+
static member FormatASTAsync(ast: ParsedInput, source) : Async<string> =
28+
async {
29+
let sourceText = Some(CodeFormatterImpl.getSourceText source)
30+
let result = CodeFormatterImpl.formatAST ast sourceText FormatConfig.Default None
31+
return result.Code
32+
}
2633

2734
static member FormatASTAsync(ast: ParsedInput, source, config) : Async<FormatResult> =
28-
let sourceText = Some(CodeFormatterImpl.getSourceText source)
29-
CodeFormatterImpl.formatAST ast sourceText config None |> async.Return
35+
async {
36+
let sourceText = Some(CodeFormatterImpl.getSourceText source)
37+
let result = CodeFormatterImpl.formatAST ast sourceText config None
38+
return result
39+
}
3040

3141
static member FormatDocumentAsync(isSignature, source) =
3242
CodeFormatterImpl.formatDocument FormatConfig.Default isSignature (CodeFormatterImpl.getSourceText source) None

src/Fantomas.Core/CodeFormatter.fsi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ type CodeFormatter =
1010
static member ParseAsync: isSignature: bool * source: string -> Async<(ParsedInput * string list) array>
1111

1212
/// Format an abstract syntax tree
13-
static member FormatASTAsync: ast: ParsedInput -> Async<FormatResult>
13+
static member FormatASTAsync: ast: ParsedInput -> Async<string>
1414

1515
/// Format an abstract syntax tree using a given config
16-
static member FormatASTAsync: ast: ParsedInput * config: FormatConfig -> Async<FormatResult>
16+
static member FormatASTAsync: ast: ParsedInput * config: FormatConfig -> Async<string>
1717

1818
/// Format an abstract syntax tree with the original source for trivia processing
19-
static member FormatASTAsync: ast: ParsedInput * source: string -> Async<FormatResult>
19+
static member FormatASTAsync: ast: ParsedInput * source: string -> Async<string>
2020

2121
/// <summary>
2222
/// Format a source string using an optional config.

src/Fantomas.Core/CodeFormatterImpl.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module internal Fantomas.Core.CodeFormatterImpl
44
open FSharp.Compiler.Diagnostics
55
open FSharp.Compiler.Syntax
66
open FSharp.Compiler.Text
7-
open SyntaxOak
87
open MultipleDefineCombinations
98

109
let getSourceText (source: string) : ISourceText = source.TrimEnd() |> SourceText.ofString

src/Fantomas.Core/CodeFormatterImpl.fsi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module internal Fantomas.Core.CodeFormatterImpl
33

44
open FSharp.Compiler.Syntax
55
open FSharp.Compiler.Text
6-
open Fantomas.Core.SyntaxOak
76

87
val getSourceText: source: string -> ISourceText
98

0 commit comments

Comments
 (0)