feat: add helper functions for completion

This commit is contained in:
Leonardo de Moura 2021-04-01 21:06:39 -07:00
parent 63b96f62e7
commit 8d00a06d97
2 changed files with 36 additions and 1 deletions

View file

@ -133,8 +133,11 @@ where fmtPos pos info :=
| SourceInfo.original .. => pos
| _ => f!"{pos}†"
def TermInfo.runMetaM (info : TermInfo) (ctx : ContextInfo) (x : MetaM α) : IO α :=
ctx.runMetaM info.lctx x
def TermInfo.format (cinfo : ContextInfo) (info : TermInfo) : IO Format := do
cinfo.runMetaM info.lctx do
info.runMetaM cinfo do
return f!"{← Meta.ppExpr info.expr} : {← Meta.ppExpr (← Meta.inferType info.expr)} @ {formatStxRange cinfo info.stx}"
def CommandInfo.format (cinfo : ContextInfo) (info : CommandInfo) : IO Format := do

View file

@ -29,6 +29,21 @@ where go ctx?
| _ => []
| _ => []
partial def InfoTree.foldInfo (f : ContextInfo → Info → αα) (init : α) : InfoTree → α :=
go none init
where go ctx? a
| context ctx t => go ctx a t
| node i ts =>
let a := match ctx? with
| none => a
| some ctx => f ctx i a
ts.foldl (init := a) (go ctx?)
| _ => a
def Info.isTerm : Info → Bool
| ofTermInfo _ => true
| _ => false
def Info.stx : Info → Syntax
| ofTacticInfo i => i.stx
| ofTermInfo i => i.stx
@ -42,6 +57,23 @@ def Info.pos? (i : Info) : Option String.Pos :=
def Info.tailPos? (i : Info) : Option String.Pos :=
i.stx.getTailPos? (originalOnly := true)
def Info.size? (i : Info) : Option Nat := OptionM.run do
let pos ← i.pos?
let tailPos ← i.tailPos?
return tailPos - pos
-- `Info` without position information are considered to have "infinite" size
def Info.isSmaller (i₁ i₂ : Info) : Bool :=
match i₁.size?, i₂.pos? with
| some sz₁, some sz₂ => sz₁ < sz₂
| some _, none => true
| _, _ => false
def Info.occursBefore? (i : Info) (hoverPos : String.Pos) : Option Nat := OptionM.run do
let tailPos ← i.tailPos?
guard (tailPos ≤ hoverPos)
return hoverPos - tailPos
def InfoTree.smallestInfo? (p : Info → Bool) (t : InfoTree) : Option (ContextInfo × Info) :=
let ts := t.deepestNodes fun ctx i => if p i then some (ctx, i) else none