Skip to content

Commit 4eb2aa2

Browse files
committed
Add NodeStart/NodeEnd debug events to WriterEvents
1 parent a2f94ca commit 4eb2aa2

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

src/Fantomas.Core.Tests/CodeFormatterTests.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,28 @@ let b = 0
127127
match oak.ModulesOrNamespaces.[0].Declarations.[0] with
128128
| ModuleDecl.TopLevelBinding node -> Assert.That(node.HasContentBefore, Is.True)
129129
| _ -> Assert.Fail()
130+
131+
[<Test>]
132+
let ``GetWriterEventsAsync emits NodeStart and NodeEnd events`` () =
133+
let events =
134+
CodeFormatter.GetWriterEventsAsync(false, "let x = 1") |> Async.RunSynchronously
135+
136+
let nodeStarts =
137+
events
138+
|> Array.choose (function
139+
| WriterEvent.NodeStart(nodeType, _) -> Some nodeType
140+
| _ -> None)
141+
142+
let nodeEnds =
143+
events
144+
|> Array.choose (function
145+
| WriterEvent.NodeEnd(nodeType, _) -> Some nodeType
146+
| _ -> None)
147+
148+
Assert.That(nodeStarts, Is.Not.Empty)
149+
Assert.That(nodeEnds, Is.Not.Empty)
150+
// Every NodeStart should have a matching NodeEnd
151+
Assert.That(nodeStarts.Length, Is.EqualTo nodeEnds.Length)
152+
// Oak is the root node
153+
Assert.That(nodeStarts.[0], Is.EqualTo "Oak")
154+
Assert.That(nodeEnds |> Array.last, Is.EqualTo "Oak")

src/Fantomas.Core/CodeFormatter.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ type CodeFormatter =
110110
let ast, _ = asts.[0]
111111
let oak = ASTTransformer.mkOak (Some sourceText) ast
112112
let oak = Trivia.enrichTree config sourceText ast oak
113-
let context = Context.Context.Create config
113+
114+
let context =
115+
{ Context.Context.Create config with
116+
DebugMode = true }
117+
114118
let ctx = context |> CodePrinter.genFile oak
115119
return Context.dumpEvents ctx
116120
}

src/Fantomas.Core/CodePrinter.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ let leaveNode<'n when 'n :> Node> (n: 'n) =
137137
col sepNone n.ContentAfter (genTrivia n)
138138

139139
let genNode<'n when 'n :> Node> (n: 'n) (f: Context -> Context) =
140-
enterNode n +> recordCursorNode f n +> leaveNode n
140+
onlyIfCtx (fun ctx -> ctx.DebugMode) (writerEvent (NodeStart(n.GetType().Name, sprintf "%O" n.Range)))
141+
+> enterNode n
142+
+> recordCursorNode f n
143+
+> leaveNode n
144+
+> onlyIfCtx (fun ctx -> ctx.DebugMode) (writerEvent (NodeEnd(n.GetType().Name, sprintf "%O" n.Range)))
141145

142146
let genSingleTextNode (node: SingleTextNode) = !-node.Text |> genNode node
143147

src/Fantomas.Core/Context.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ module WriterModel =
116116
| RestoreAtColumn c -> { m with AtColumn = c }
117117
| SetIndent c -> { m with Indent = c }
118118
| RestoreIndent c -> { m with Indent = c }
119+
| NodeStart _
120+
| NodeEnd _ -> m
119121

120122
match m.Mode with
121123
| Dummy
@@ -171,17 +173,23 @@ module WriterEvents =
171173

172174
[<System.Diagnostics.DebuggerDisplay("\"{Dump()}\""); NoComparison>]
173175
type Context =
174-
{ Config: FormatConfig
175-
WriterModel: WriterModel
176-
WriterEvents: Queue<WriterEvent>
177-
FormattedCursor: pos option }
176+
{
177+
Config: FormatConfig
178+
WriterModel: WriterModel
179+
WriterEvents: Queue<WriterEvent>
180+
FormattedCursor: pos option
181+
/// When enabled, genNode emits NodeStart/NodeEnd WriterEvents around each Oak node.
182+
/// Only used by CodeFormatter.GetWriterEventsAsync for diagnostic output.
183+
DebugMode: bool
184+
}
178185

179186
/// Initialize with a string writer and use space as delimiter
180187
static member Default =
181188
{ Config = FormatConfig.Default
182189
WriterModel = WriterModel.init
183190
WriterEvents = Queue.empty
184-
FormattedCursor = None }
191+
FormattedCursor = None
192+
DebugMode = false }
185193

186194
static member Create config : Context =
187195
{ Context.Default with Config = config }

src/Fantomas.Core/Context.fsi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ type WriterModel =
3939

4040
[<System.Diagnostics.DebuggerDisplay("\"{Dump()}\""); NoComparison>]
4141
type Context =
42-
{ Config: FormatConfig
43-
WriterModel: WriterModel
44-
WriterEvents: Queue<WriterEvent>
45-
FormattedCursor: pos option }
42+
{
43+
Config: FormatConfig
44+
WriterModel: WriterModel
45+
WriterEvents: Queue<WriterEvent>
46+
FormattedCursor: pos option
47+
/// When enabled, genNode emits NodeStart/NodeEnd WriterEvents around each Oak node.
48+
/// Only used by CodeFormatter.GetWriterEventsAsync for diagnostic output.
49+
DebugMode: bool
50+
}
4651

4752
/// Initialize with a string writer and use space as delimiter
4853
static member Default: Context

src/Fantomas.Core/WriterEvent.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ type WriterEvent =
1616
| RestoreIndent of restoreIndent: int
1717
| SetAtColumn of setAtColumn: int
1818
| RestoreAtColumn of restoreAtColumn: int
19+
| NodeStart of nodeType: string * range: string
20+
| NodeEnd of endNodeType: string * endRange: string

0 commit comments

Comments
 (0)