Skip to content

Commit

Permalink
V6.1 (#2913)
Browse files Browse the repository at this point in the history
* Expose TransformAST api. (#2868)

* Expose TransformAST api.

* Add additional unit test.

* Revert changelog entry

* Add Changelog for 6.1 alpha 1

* Enrich transformed Oak. (#2869)

* Add Changelog for 6.1 alpha 2

* Rename FSharp.Compiler to Fantomas.FCS (#2894)

* Rename FSharp.Compiler to Fantomas.FCS

* Add release notes for 6.1.0-alpha-003.

* Update editorconfig package (#2895)

* Use EditorConfigCache.

* Clean up EditorConfig.fs(i)

* Add 6.1.0-alpha-004 to changelog.

* Add 6.1.0-alpha-005 to changelog.

* Add 6.1.0-alpha-006 to changelog.

* Add stable release
  • Loading branch information
nojaf committed Jun 28, 2023
1 parent d1295a0 commit e671f3d
Show file tree
Hide file tree
Showing 43 changed files with 232 additions and 119 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## 6.1.0 - 2023-06-27

Stable release

## 6.1.0-alpha-006 - 2023-06-20

Contains fixes of 6.0.8

## 6.1.0-alpha-005 - 2023-06-20

Contains fixes of 6.0.7

## 6.1.0-alpha-004 - 2023-06-19

Contains fixes of 6.0.6

## 6.1.0-alpha-003 - 2023-06-02

### Changed
* Rename `namespace FSharp.Compiler` to `namespace Fantomas.FCS` for `Fantomas.FCS`. [#2894](https://github.com/fsprojects/fantomas/pull/2894)

## 6.1.0-alpha-002 - 2023-05-02

### Changed
* Enrich transformed Oak. [#2869](https://github.com/fsprojects/fantomas/pull/2869)

## 6.1.0-alpha-001 - 2023-05-02

### Added
* TransformAST in CodeFormatter. [#2868](https://github.com/fsprojects/fantomas/pull/2868)

## [6.0.8] - 2023-06-20

### Fixed
Expand Down
13 changes: 13 additions & 0 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ let fsharpCompilerHash =
let xDoc = XElement.Load(__SOURCE_DIRECTORY__ </> "Directory.Build.props")
xDoc.XPathSelectElements("//FCSCommitHash") |> Seq.head |> (fun xe -> xe.Value)

let updateFileRaw (file: FileInfo) =
let lines = File.ReadAllLines file.FullName
let updatedLines =
lines
|> Array.map (fun line ->
if line.Contains("FSharp.Compiler") then
line.Replace("FSharp.Compiler", "Fantomas.FCS")
else
line)
File.WriteAllLines(file.FullName, updatedLines)

let downloadCompilerFile commitHash relativePath =
async {
let file = FileInfo(deps </> commitHash </> relativePath)
Expand All @@ -213,6 +224,8 @@ let downloadCompilerFile commitHash relativePath =
printfn $"Could not download %s{relativePath}"
do! Async.AwaitTask(response.ResponseStream.CopyToAsync(fs))
fs.Close()

updateFileRaw file
}

pipeline "Init" {
Expand Down
11 changes: 4 additions & 7 deletions docs/docs/end-users/GeneratingCode.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ To illustrate the API, lets generate a simple value binding: `let a = 0`.
*)

#r "../../../src/Fantomas/bin/Release/net6.0/Fantomas.FCS.dll"
#r "../../../src/Fantomas/bin/Release/net6.0/Fantomas.Core.dll" // In production use #r "nuget: Fantomas.Core, 6.0-alpha-*"
#r "../../../src/Fantomas/bin/Release/net6.0/Fantomas.Core.dll" // In production use #r "nuget: Fantomas.Core, 6.*"

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

let implementationSyntaxTree =
Expand Down Expand Up @@ -88,11 +88,8 @@ The more you interact with AST/Oak, the easier you pick up which node represents
### Fantomas.FCS
When looking at the example, we notice that we've opened `FSharp.Compiler.Text`.
Don't be fooled by this, `Fantomas.Core` and `Fantomas.FCS` **do not reference [FSharp.Compiler.Service](https://www.nuget.org/packages/FSharp.Compiler.Service)**!
Instead, `Fantomas.FCS` is a custom version of the F# compiler (built from source) that only exposes the F# parser and the syntax tree.
`Fantomas.FCS` exposes the exact same namespaces because it builds from the exact same F# compiler source code.
When looking at the example, we notice that we've opened `Fantomas.FCS.Text`.
`Fantomas.FCS` is a custom version of the F# compiler (built from source) that only exposes the F# parser and the syntax tree.
The key difference is that `Fantomas.FCS` will most likely contain a more recent version of the F# parser.
You can read the [CHANGELOG](https://github.com/fsprojects/fantomas/blob/main/CHANGELOG.md) to see what git commit was used to build `Fantomas.FCS`.
Expand Down
8 changes: 4 additions & 4 deletions src/Fantomas.Core.Tests/ASTTransformerTests.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Fantomas.Core.Tests.ASTTransformerTests

open NUnit.Framework
open FSharp.Compiler.Text
open FSharp.Compiler.Xml
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open Fantomas.FCS.Text
open Fantomas.FCS.Xml
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
open Fantomas.Core

[<Test>]
Expand Down
62 changes: 61 additions & 1 deletion src/Fantomas.Core.Tests/CodeFormatterTests.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Fantomas.Core.Tests.CodeFormatterTests

open NUnit.Framework
open Fantomas.FCS.Text
open Fantomas.Core
open Fantomas.Core.SyntaxOak
open Fantomas.Core.Tests.TestHelpers

[<Test>]
Expand Down Expand Up @@ -39,7 +41,7 @@ let main _ =
|> ignore

[<Test>]
let ``trivia is parsed for Oak`` () =
let ``trivia is transformed to Oak`` () =
let oak =
CodeFormatter.ParseOakAsync(false, "let a = 0\n // foo")
|> Async.RunSynchronously
Expand Down Expand Up @@ -67,3 +69,61 @@ let ``parsed oak can be formatted back to source`` () =
|> Async.RunSynchronously

Assert.AreEqual(source, formatted)

[<Test>]
let ``transform parsedInput to Oak`` () =
let source =
"""
module A
#if DEBUG
let b = 0
#endif
"""

let ast, _ =
Fantomas.FCS.Parse.parseFile false (SourceText.ofString source) [ "DEBUG" ]

let oak = CodeFormatter.TransformAST(ast, source)

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding _ -> Assert.Pass()
| _ -> Assert.Fail()

[<Test>]
let ``transform parsedInput created with additional defines to Oak`` () =
let source =
"""
module A
#if DEBUG
let b = 0
#endif
"""

let ast, _ =
Fantomas.FCS.Parse.parseFile false (SourceText.ofString source) [ "DEBUG"; "FOO"; "BAR" ]

let oak = CodeFormatter.TransformAST ast

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding _ -> Assert.Pass()
| _ -> Assert.Fail()

[<Test>]
let ``transform parsedInput contains trivia in Oak`` () =
let source =
"""
module A
// foo
let b = 0
"""

let ast, _ = Fantomas.FCS.Parse.parseFile false (SourceText.ofString source) []

let oak = CodeFormatter.TransformAST(ast, source)

match oak.ModulesOrNamespaces.[0].Declarations.[0] with
| ModuleDecl.TopLevelBinding node -> Assert.True node.HasContentBefore
| _ -> Assert.Fail()
4 changes: 2 additions & 2 deletions src/Fantomas.Core.Tests/CodePrinterHelperFunctionsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ let a =

// Let's create a dummy Oak
// In practise, a FCS Syntax tree will be transformed to an Oak
let zeroRange = FSharp.Compiler.Text.Range.Zero
let zeroRange = Fantomas.FCS.Text.Range.Zero
let stn text = SingleTextNode(text, zeroRange)

let tree =
Expand Down Expand Up @@ -321,7 +321,7 @@ let b = 2
"""

// Imagine that we always want to print a new line between let bindings.
let zeroRange = FSharp.Compiler.Text.Range.Zero
let zeroRange = Fantomas.FCS.Text.Range.Zero
let stn text = SingleTextNode(text, zeroRange)

let mkBinding name body =
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/CursorTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Fantomas.Core.Tests.CursorTests

open FSharp.Compiler.Text
open Fantomas.FCS.Text
open NUnit.Framework
open FsUnit
open Fantomas.Core
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/DefinesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Fantomas.Core.Tests.TokenParserTests

open NUnit.Framework
open FsUnit
open FSharp.Compiler.Syntax
open Fantomas.FCS.Syntax
open Fantomas.Core
open Fantomas.Core.Defines
open Fantomas.Core.Tests.TestHelpers
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/FormattingSelectionOnlyTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Fantomas.Core.Tests.FormattingSelectionOnlyTests

open FSharp.Compiler.Text
open Fantomas.FCS.Text
open Fantomas.Core
open NUnit.Framework
open FsUnit
Expand Down
8 changes: 4 additions & 4 deletions src/Fantomas.Core.Tests/SynLongIdentTests.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Fantomas.Core.Tests.SynLongIdentTests

open FSharp.Compiler.Text
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open FSharp.Compiler.Xml
open Fantomas.FCS.Text
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
open Fantomas.FCS.Xml
open NUnit.Framework
open FsUnit
open Fantomas.Core
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let formatSourceString isFsiFile (s: string) config =
let formatAST isFsiFile (source: string) config =
async {
let ast, _ =
Fantomas.FCS.Parse.parseFile isFsiFile (FSharp.Compiler.Text.SourceText.ofString source) []
Fantomas.FCS.Parse.parseFile isFsiFile (Fantomas.FCS.Text.SourceText.ofString source) []

let! formattedCode = CodeFormatter.FormatASTAsync(ast, config = config)
let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, formattedCode)
Expand Down
10 changes: 5 additions & 5 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

open System.Collections.Generic
open System.Text.RegularExpressions
open FSharp.Compiler.Text
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open FSharp.Compiler.Xml
open Fantomas.FCS.Text
open Fantomas.FCS.Text.Range
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
open Fantomas.FCS.Xml
open Fantomas.Core.ISourceTextExtensions
open Fantomas.Core.RangePatterns
open Fantomas.Core.SyntaxOak
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Core/ASTTransformer.fsi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module internal Fantomas.Core.ASTTransformer

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

val mkOak: sourceText: ISourceText option -> ast: ParsedInput -> Oak
11 changes: 9 additions & 2 deletions src/Fantomas.Core/CodeFormatter.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Fantomas.Core

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

[<Sealed>]
Expand Down Expand Up @@ -78,6 +78,13 @@ type CodeFormatter =
oak, defines.Value)
}

static member TransformAST ast = ASTTransformer.mkOak None ast

static member TransformAST(ast, source) =
let sourceText = SourceText.ofString source
let oak = ASTTransformer.mkOak (Some sourceText) ast
Trivia.enrichTree FormatConfig.Default sourceText ast oak

static member FormatOakAsync(oak: Oak) : Async<string> =
async {
let context = Context.Context.Create FormatConfig.Default
Expand Down
10 changes: 8 additions & 2 deletions src/Fantomas.Core/CodeFormatter.fsi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Fantomas.Core

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

[<Sealed>]
Expand Down Expand Up @@ -67,6 +67,12 @@ type CodeFormatter =
/// Parse a source string to SyntaxOak
static member ParseOakAsync: isSignature: bool * source: string -> Async<(Oak * string list) array>

/// Transform a ParsedInput to an Oak
static member TransformAST: ast: ParsedInput -> Oak

/// Transform a ParsedInput to an Oak
static member TransformAST: ast: ParsedInput * source: string -> Oak

/// Format SyntaxOak to string
static member FormatOakAsync: oak: Oak -> Async<string>

Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core/CodeFormatterImpl.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[<RequireQualifiedAccess>]
module internal Fantomas.Core.CodeFormatterImpl

open FSharp.Compiler.Diagnostics
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open Fantomas.FCS.Diagnostics
open Fantomas.FCS.Syntax
open Fantomas.FCS.Text
open MultipleDefineCombinations

let getSourceText (source: string) : ISourceText = source.TrimEnd() |> SourceText.ofString
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Core/CodeFormatterImpl.fsi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[<RequireQualifiedAccess>]
module internal Fantomas.Core.CodeFormatterImpl

open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open Fantomas.FCS.Syntax
open Fantomas.FCS.Text

val getSourceText: source: string -> ISourceText

Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core/CodeFormatterTypes.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Fantomas.Core

open FSharp.Compiler.Text
open Fantomas.FCS.Text

type FormatResult =
{
Expand Down
Loading

0 comments on commit e671f3d

Please sign in to comment.