File tree Expand file tree Collapse file tree 6 files changed +59
-11
lines changed
Expand file tree Collapse file tree 6 files changed +59
-11
lines changed Original file line number Diff line number Diff 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" )
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -137,7 +137,11 @@ let leaveNode<'n when 'n :> Node> (n: 'n) =
137137 col sepNone n.ContentAfter ( genTrivia n)
138138
139139let 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
142146let genSingleTextNode ( node : SingleTextNode ) = !- node.Text |> genNode node
143147
Original file line number Diff line number Diff 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>]
173175type 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 }
Original file line number Diff line number Diff line change @@ -39,10 +39,15 @@ type WriterModel =
3939
4040[<System.Diagnostics.DebuggerDisplay( " \" {Dump()}\" " ); NoComparison>]
4141type 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments