Skip to content

Commit dc83870

Browse files
committed
Fix indentation of module declaration with hash directives
When hash directives surrounded the access modifier in a module declaration, the access modifier and module name lost their indentation. Wrapping the post-keyword content with indent/unindent preserves correct indentation when directive trivia forces newlines. Fixes fsprojects#3188
1 parent ebc4f8e commit dc83870

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Long delegate type with generic args no longer breaks around arrow. [#2468](https://github.com/fsprojects/fantomas/issues/2468)
1616
- Idempotency problem when formatting NUnit Assert.That with lambda argument. [#1740](https://github.com/fsprojects/fantomas/issues/1740)
1717
- Comment between lines of no-break infix expression no longer loses indentation. [#2944](https://github.com/fsprojects/fantomas/issues/2944)
18+
- Hash directives around access modifier in module declaration no longer lose indentation. [#3188](https://github.com/fsprojects/fantomas/issues/3188)
1819

1920
## [8.0.0-alpha-002] - 2025-12-15
2021

src/Fantomas.Core.Tests/ModuleTests.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,27 @@ namespace ``G-Research``.``FSharp X``.``Analyzers Y``
10881088
module StringAnalyzers =
10891089
()
10901090
"""
1091+
1092+
[<Test>]
1093+
let ``hash directives around access modifier in module, 3188`` () =
1094+
formatSourceString
1095+
"""
1096+
[<RequireQualifiedAccess>]
1097+
module
1098+
#if !MCP
1099+
internal
1100+
#endif
1101+
Fantomas.Core.CodeFormatterImpl
1102+
"""
1103+
config
1104+
|> prepend newline
1105+
|> should
1106+
equal
1107+
"""
1108+
[<RequireQualifiedAccess>]
1109+
module
1110+
#if !MCP
1111+
internal
1112+
#endif
1113+
Fantomas.Core.CodeFormatterImpl
1114+
"""

src/Fantomas.Core/CodePrinter.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,10 +4050,12 @@ let genModule (m: ModuleOrNamespaceNode) =
40504050
(genXml header.XmlDoc
40514051
+> genAttributes header.Attributes
40524052
+> genMultipleTextsNode header.LeadingKeyword
4053+
+> indent
40534054
+> sepSpace
40544055
+> genAccessOpt header.Accessibility
40554056
+> onlyIf header.IsRecursive (sepSpace +> !-"rec" +> sepSpace)
40564057
+> optSingle genIdentListNode header.Name
4058+
+> unindent
40574059
|> genNode header)
40584060
+> newline)
40594061
m.Header

0 commit comments

Comments
 (0)