Skip to content

Commit a4fff91

Browse files
committed
chore: improve trace.Elab.snapshotTree
1 parent 582d6e7 commit a4fff91

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Lean/Language/Basic.lean

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ prelude
1212
import Init.System.Promise
1313
import Lean.Message
1414
import Lean.Parser.Types
15+
import Lean.Elab.InfoTree
1516

1617
set_option linter.missingDocs true
1718

@@ -46,6 +47,8 @@ def Snapshot.Diagnostics.empty : Snapshot.Diagnostics where
4647
The base class of all snapshots: all the generic information the language server needs about a
4748
snapshot. -/
4849
structure Snapshot where
50+
/-- Debug description shown by `trace.Elab.snapshotTree`, defaults to the caller's decl name. -/
51+
desc : String := by exact decl_name%.toString
4952
/--
5053
The messages produced by this step. The union of message logs of all finished snapshots is
5154
reported to the user. -/
@@ -71,7 +74,7 @@ structure SnapshotTask (α : Type) where
7174
range? : Option String.Range
7275
/-- Underlying task producing the snapshot. -/
7376
task : Task α
74-
deriving Nonempty
77+
deriving Nonempty, Inhabited
7578

7679
/-- Creates a snapshot task from a reporting range and a `BaseIO` action. -/
7780
def SnapshotTask.ofIO (range? : Option String.Range) (act : BaseIO α) : BaseIO (SnapshotTask α) := do
@@ -203,15 +206,19 @@ abbrev SnapshotTree.children : SnapshotTree → Array (SnapshotTask SnapshotTree
203206
| mk _ children => children
204207

205208
/-- Produces debug tree format of given snapshot tree, synchronously waiting on all children. -/
206-
partial def SnapshotTree.format : SnapshotTree → Format := go none
207-
where go range? s :=
208-
let range := match range? with
209-
| some range => f!"{range.start}..{range.stop} "
210-
| none => ""
211-
let element := f!"{s.element.diagnostics.msgLog.unreported.size} diagnostics"
212-
let children := Std.Format.prefixJoin .line <|
213-
s.children.toList.map fun c => go c.range? c.get
214-
.nestD f!"• {range}{element}{children}"
209+
partial def SnapshotTree.format [Monad m] [MonadFileMap m] [MonadLiftT IO m] :
210+
SnapshotTree → m Format :=
211+
go none
212+
where go range? s := do
213+
let file ← getFileMap
214+
let mut desc := f!"• {s.element.desc}"
215+
if let some range := range? then
216+
desc := desc ++ f!"{file.toPosition range.start}-{file.toPosition range.stop} "
217+
desc := desc ++ .prefixJoin "\n• " (← s.element.diagnostics.msgLog.toList.mapM (·.toString))
218+
if let some t := s.element.infoTree? then
219+
desc := desc ++ f!"\n{← t.format}"
220+
desc := desc ++ .prefixJoin "\n" (← s.children.toList.mapM fun c => go c.range? c.get)
221+
return .nestD desc
215222

216223
/--
217224
Helper class for projecting a heterogeneous hierarchy of snapshot classes to a homogeneous

0 commit comments

Comments
 (0)