Skip to content

Commit 3700c83

Browse files
committed
Add position info to declTypes
1 parent 212fe72 commit 3700c83

File tree

7 files changed

+75
-17
lines changed

7 files changed

+75
-17
lines changed

REPL/JSON.lean

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def Tactic.of (goals tactic : String) (pos endPos : Lean.Position) (proofState :
117117
proofState,
118118
usedConstants }
119119

120+
structure DeclType where
121+
pos: Pos
122+
endPos : Pos
123+
type: String
124+
pp: String
125+
deriving ToJson, FromJson
126+
127+
/-- Construct the JSON representation of a Declaration type. -/
128+
def DeclType.of (type pp : String) (pos endPos : Lean.Position) : DeclType :=
129+
{ pos := ⟨pos.line, pos.column⟩,
130+
endPos := ⟨endPos.line, endPos.column⟩,
131+
type,
132+
pp }
133+
120134
/--
121135
A response to a Lean command.
122136
`env` can be used in later calls, to build on the stored environment.
@@ -126,7 +140,7 @@ structure CommandResponse where
126140
messages : List Message := []
127141
sorries : List Sorry := []
128142
tactics : List Tactic := []
129-
decls: List String := []
143+
decls: List DeclType := []
130144
infotree : Option Json := none
131145
deriving FromJson
132146

REPL/Lean/InfoTree.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ def tactics (t : InfoTree) : List (ContextInfo × Syntax × List MVarId × List
272272
range.snd,
273273
i.getUsedConstantsAsSet.toArray )
274274

275+
def declType (t : InfoTree) : Option (ContextInfo × Expr × Syntax × LocalContext × Position × Position) :=
276+
let terms: List (TermInfo × ContextInfo) := t.findTermNodes
277+
match terms.getLast? with
278+
| some ⟨i, ctx⟩ =>
279+
let range := stxRange ctx.fileMap i.stx
280+
(ctx, i.expr, i.stx, i.lctx, range.fst, range.snd)
281+
| _ => none
282+
275283
def rootGoals (t : InfoTree) : List (ContextInfo × List MVarId × Position) :=
276284
t.findRootGoals.map fun ⟨i, ctx, rootGoals⟩ =>
277285
let range := stxRange ctx.fileMap i.stx

REPL/Main.lean

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,17 @@ def tactics (trees : List InfoTree) (env? : Option Environment) : M m (List Tact
131131
return Tactic.of goals tactic pos endPos proofStateId ns
132132

133133

134-
def declTypes (trees: List InfoTree) : M m (List String) := do
135-
let terms := trees.map InfoTree.findTermNodes
136-
let innermost := (fun (t : TermInfo) => do pure (← Meta.ppExpr (← Lean.Meta.inferType t.expr)).pretty')
137-
let inner := (fun t : (TermInfo × ContextInfo) => t.snd.runMetaM t.fst.lctx (innermost t.fst))
138-
let optional_decls: List String ← terms.mapM fun treeterms => do
139-
match treeterms.getLast? with
140-
| none => pure ""
141-
| some a => inner a
142-
pure (optional_decls.filter (fun s => !s.isEmpty))
134+
def declTypes (trees: List InfoTree) : M m (List DeclType) := do
135+
let exprType: Expr → MetaM String := fun (expr: Expr) => do pure (← Meta.ppExpr (← Lean.Meta.inferType expr)).pretty'
136+
let treeDecl := (fun t => do
137+
let dt := InfoTree.declType t
138+
match dt with
139+
| some ⟨ctx, expr, stx, lctx, pos, endPos⟩ =>
140+
let type := (← ctx.runMetaM lctx (exprType expr))
141+
let pp := Format.pretty stx.prettyPrint
142+
pure [DeclType.of type pp pos endPos]
143+
| _ => pure [])
144+
trees.flatMapM treeDecl
143145

144146
def collectRootGoalsAsSorries (trees : List InfoTree) (env? : Option Environment) : M m (List Sorry) := do
145147
trees.flatMap InfoTree.rootGoals |>.mapM

test/Mathlib/test/decltypenamespace.expected.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"endPos": {"line": 1, "column": 75},
77
"data": "Goals accomplished!"}],
88
"env": 1,
9-
"decls": ["∑ k ∈ Nat.properDivisors 198, k = 270"]}
9+
"decls":
10+
[{"type": "∑ k ∈ Nat.properDivisors 198, k = 270",
11+
"pp": "with_namespace",
12+
"pos": {"line": 1, "column": 8},
13+
"endPos": {"line": 1, "column": 22}}]}
1014

test/Mathlib/test/decltypeopen.expected.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"endPos": {"line": 1, "column": 75},
77
"data": "Goals accomplished!"}],
88
"env": 1,
9-
"decls": ["∑ k ∈ properDivisors 198, k = 270"]}
9+
"decls":
10+
[{"type": "∑ k ∈ properDivisors 198, k = 270",
11+
"pp": "with_namespace",
12+
"pos": {"line": 1, "column": 8},
13+
"endPos": {"line": 1, "column": 22}}]}
1014

test/gettype.expected.out

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@
44
"endPos": {"line": 1, "column": 51},
55
"data": "Goals accomplished!"}],
66
"env": 0,
7-
"decls": ["∀ (p : Prop), p → p"]}
7+
"decls":
8+
[{"type": "∀ (p : Prop), p → p",
9+
"pp": "show_p",
10+
"pos": {"line": 1, "column": 8},
11+
"endPos": {"line": 1, "column": 14}}]}
812

9-
{"env": 1, "decls": ["∀ (p : Prop), p → p"]}
13+
{"env": 1,
14+
"decls":
15+
[{"type": "∀ (p : Prop), p → p",
16+
"pp": "show_q",
17+
"pos": {"line": 1, "column": 4},
18+
"endPos": {"line": 1, "column": 10}}]}
1019

11-
{"env": 2, "decls": ["∀ (p : Prop), p → p", "∀ (q : Prop), q → q"]}
20+
{"env": 2,
21+
"decls":
22+
[{"type": "∀ (p : Prop), p → p",
23+
"pp": "show_p",
24+
"pos": {"line": 1, "column": 4},
25+
"endPos": {"line": 1, "column": 10}},
26+
{"type": "∀ (q : Prop), q → q",
27+
"pp": "show_q",
28+
"pos": {"line": 3, "column": 4},
29+
"endPos": {"line": 3, "column": 10}}]}
1230

1331
{"messages":
1432
[{"severity": "warning",
@@ -17,5 +35,13 @@
1735
"data":
1836
"unused variable `p`\nnote: this linter can be disabled with `set_option linter.unusedVariables false`"}],
1937
"env": 3,
20-
"decls": ["Prop", "∀ (q : Prop), Type → q → q"]}
38+
"decls":
39+
[{"type": "Prop",
40+
"pp": "q",
41+
"pos": {"line": 1, "column": 10},
42+
"endPos": {"line": 1, "column": 11}},
43+
{"type": "∀ (q : Prop), Type → q → q",
44+
"pp": "show_q",
45+
"pos": {"line": 2, "column": 4},
46+
"endPos": {"line": 2, "column": 10}}]}
2147

test/gettype.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
{"cmd": "def show_q (p : Prop) (h : p) : p := h", "declTypes": true}
44

5-
{"cmd": "def show_q (p : Prop) (h : p) : p := h\n\ndef show_p (q : Prop) (h : q) : q := h", "declTypes": true}
5+
{"cmd": "def show_p (p : Prop) (h : p) : p := h\n\ndef show_q (q : Prop) (h : q) : q := h", "declTypes": true}
66

77
{"cmd": "variable (q : Prop)\ndef show_q (p : Type) (h : q) : q := h", "declTypes": true}

0 commit comments

Comments
 (0)