Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

## [Unreleased]
## [8.0.1] - 2026-09-21

### Fixed

- Remove TailCall shim from Fantomas.FCS. [#3476](https://github.com/fsprojects/fantomas/pull/3476)
- Fantomas 8 with use bindings in fsx-file. A `use` at the top level of a script makes the parser warn that it is treated as `let`, and the check Fantomas runs over its own output refused the file on that warning. The warning is about the source as the author wrote it, not about anything the formatter changed, so it is now one of the warnings the check tolerates. [#3478](https://github.com/fsprojects/fantomas/issues/3478)
- Unexpected de-indent after trivia after expression. A comment at the end of a `match` that closes a computation expression, or a `let` body, pushed the closing brace and the next binding one level too far to the right. The unindent before the closing line was placed in front of the comment's newline, where the `match` still held the column it had claimed and restored its own indent on the way out, undoing the unindent. The newline now moves past those restores, so the line after the comment starts at the outer level. [#3481](https://github.com/fsprojects/fantomas/issues/3481)

## [8.0.0] - 2026-09-15

Expand Down
9 changes: 5 additions & 4 deletions src/Fantomas.Core.Tests/CodePrinterHelperFunctionsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,8 @@ let ``unindentWithTriviaAwareness splices before multiline block comment`` () =

[<Test>]
let ``unindentWithTriviaAwareness finds trivia past trailing restore events`` () =
// Simulates the try-with pattern where restore/unindent events follow the trivia
// Simulates an atCurrentColumn scope closing after the trivia. The newline has to move past the
// restore events, or the RestoreIndent would undo the unindent before the next line is written.
let f =
indent
+> !-"content"
Expand All @@ -1044,10 +1045,10 @@ let ``unindentWithTriviaAwareness finds trivia past trailing restore events`` ()
Write "content"
WriteLineBecauseOfTrivia
WriteTrivia "// trailing comment"
UnIndentBy 4
WriteLineBecauseOfTrivia
RestoreAtColumn 0
RestoreIndent 0 ] -> Assert.Pass()
RestoreIndent 0
UnIndentBy 4
WriteLineBecauseOfTrivia ] -> Assert.Pass()
| _ -> Assert.Fail $"Unexpected events: %A{events}"

// =============================================================================
Expand Down
147 changes: 147 additions & 0 deletions src/Fantomas.Core.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2626,3 +2626,150 @@ let getBlah _ =
}
|> Cmd.ofAsyncMsg
"""

[<Test>]
let ``comment after match bang as last expression in computation expression, 3481`` () =
formatSourceString
"""
let f () = task {
match! g () with
| Ok _ -> ()
| Error _ -> ()

// comment
}

let x = 1
"""
config
|> prepend newline
|> should
equal
"""
let f () =
task {
match! g () with
| Ok _ -> ()
| Error _ -> ()

// comment
}

let x = 1
"""

[<Test>]
let ``comment after match bang in test module, 3481`` () =
formatSourceString
"""
namespace X
module TopologicalBuilderTests =

[<Fact>]
let ``Builder works with ANY backend (backend-agnostic principle)`` () = task {
let program = topological simulatorBackend {
return ()
}

match! TopologicalBuilder.execute simulatorBackend program with
| Ok _ -> Assert.True(true)
| Error err -> Assert.Fail($"Program failed: {err.Message}")

// Programs are COMPLETELY backend-agnostic!
}

[<Fact>]
let ``Builder with braiding sequence`` () =
task {
// Increased backend capacity to 20 anyons to support 6 logical qubits
let backend = TopologicalUnifiedBackendFactory.createUnified AnyonSpecies.AnyonType.Ising 20

()
}
"""
config
|> prepend newline
|> should
equal
"""
namespace X

module TopologicalBuilderTests =

[<Fact>]
let ``Builder works with ANY backend (backend-agnostic principle)`` () =
task {
let program = topological simulatorBackend { return () }

match! TopologicalBuilder.execute simulatorBackend program with
| Ok _ -> Assert.True(true)
| Error err -> Assert.Fail($"Program failed: {err.Message}")

// Programs are COMPLETELY backend-agnostic!
}

[<Fact>]
let ``Builder with braiding sequence`` () =
task {
// Increased backend capacity to 20 anyons to support 6 logical qubits
let backend =
TopologicalUnifiedBackendFactory.createUnified AnyonSpecies.AnyonType.Ising 20

()
}
"""

[<Test>]
let ``comment after match as last expression in computation expression`` () =
formatSourceString
"""
let f () = task {
match g () with
| Ok _ -> ()
| Error _ -> ()
// comment
}

let x = 1
"""
config
|> prepend newline
|> should
equal
"""
let f () =
task {
match g () with
| Ok _ -> ()
| Error _ -> ()
// comment
}

let x = 1
"""

[<Test>]
let ``comment after match as last expression in let binding`` () =
formatSourceString
"""
let f () =
match g () with
| Ok _ -> ()
| Error _ -> ()
// comment

let x = 1
"""
config
|> prepend newline
|> should
equal
"""
let f () =
match g () with
| Ok _ -> ()
| Error _ -> ()
// comment

let x = 1
"""
29 changes: 23 additions & 6 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ type LongExpressionLayout =
| NewlineOnly

/// Walk backward from a node, skipping events that don't represent user-visible content
/// (restore/unindent/indent events that unwind surrounding contexts).
/// (restore/unindent/indent events that unwind surrounding contexts, and the node markers of debug mode).
/// Returns the node where trailing trivia ends, or null if no trivia is found.
let findTrailingTriviaNewline (events: EventList) : EventNode =
let mutable current = events.Tail
Expand All @@ -793,6 +793,8 @@ let findTrailingTriviaNewline (events: EventList) : EventNode =
| RestoreAtColumn _
| UnIndentBy _
| IndentBy _
| NodeStart _
| NodeEnd _
| WriteLine -> true
| _ -> false
) do
Expand Down Expand Up @@ -856,13 +858,28 @@ let unindentWithTriviaAwareness (ctx: Context) =
writerEvent (UnIndentBy unindentAmount) ctx
else

// Splice the UnIndentBy into the DLL before the trailing trivia newline,
// and update the WriterModel using the same logic as WriterModel.update.
ctx.WriterEvents.InsertBefore(triviaNewline, UnIndentBy unindentAmount)
|> ignore
// The trivia newline was written inside whatever scopes the content opened, and the events after
// it are those scopes unwinding. An `atCurrentColumn` scope clamps the indent to its column and
// restores the old indent on exit, so an UnIndentBy spliced in front of the newline would be
// undone before the next line is written. See https://github.com/fsprojects/fantomas/issues/3481.
// Move the newline past the unwinding events instead, with the UnIndentBy right before it, so the
// line after the trivia starts at the unindented column.
ctx.WriterEvents.Remove(triviaNewline)
ctx.WriterEvents.Append(UnIndentBy unindentAmount) |> ignore
ctx.WriterEvents.Append(WriteLineBecauseOfTrivia) |> ignore

// The model already counted the newline at its old position; only the column it lands on changes.
let m: WriterModel =
WriterModel.update ctx.Config.MaxLineLength (UnIndentBy unindentAmount) ctx.WriterModel

let indentAfterNewline: int = max m.Indent m.AtColumn

{ ctx with
WriterModel = WriterModel.update ctx.Config.MaxLineLength (UnIndentBy unindentAmount) ctx.WriterModel
WriterModel =
{ m with
Indent = indentAfterNewline
Column = indentAfterNewline
}
}

let indentSepNlnUnindent f =
Expand Down
Loading