-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathUtil.lean
More file actions
59 lines (51 loc) · 1.99 KB
/
Copy pathUtil.lean
File metadata and controls
59 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/-
Copyright (c) 2022 Lars König. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lars König
-/
module
prelude
public import Lean.Server.InfoUtils
public import Lean.Linter.Init
public import Lean.Elab.Term
public section
namespace Lean.Linter
open Lean.Elab
/-- Go upwards through the given `tree` starting from the smallest node that
contains the given `range` and collect all `MacroExpansionInfo`s on the way up.
The result is `some []` if no `MacroExpansionInfo` was found on the way and
`none` if no `InfoTree` node was found that covers the given `range`.
Return the result reversed, s.t. the macro expansion that would be applied to
the original syntax first is the first element of the returned list. -/
def collectMacroExpansions? {m} [Monad m] (range : Lean.Syntax.Range) (tree : Elab.InfoTree) : m <| Option <| List Elab.MacroExpansionInfo := do
if let .some <| .some result ← go then
return some result.reverse
else
return none
where
go : m <| Option <| Option <| List Elab.MacroExpansionInfo := tree.visitM (postNode := fun _ i _ results => do
let results := results |>.filterMap id |>.filterMap id
-- we expect that at most one InfoTree child returns a result
if let results :: _ := results then
if let .ofMacroExpansionInfo i := i then
return some <| i :: results
else
return some results
else if i.contains range.start && i.contains (includeStop := true) range.stop then
if let .ofMacroExpansionInfo i := i then
return some [i]
else
return some []
else
return none)
/-- Get the `parentDecl`s of every elaborated body in the infotree. -/
def getDeclsByBody (t : InfoTree) : List Name :=
t.collectNodesBottomUp fun ctx i _ decls =>
match i with
| .ofCustomInfo i =>
if i.value.typeName == ``Lean.Elab.Term.BodyInfo then
if let some decl := ctx.parentDecl? then
decl :: decls
else decls
else decls
| _ => decls