Skip to content

Commit 51fafef

Browse files
david-christiansenarthur-adjedj
authored andcommitted
feat: suggest qualified names while editing Verso docstrings (leanprover#10584)
This PR causes Verso docstrings to search for a name in the environment that is at least as long as the current name, providing it as a suggestion.
1 parent d9472ab commit 51fafef

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Lean/Elab/DocString/Builtin.lean

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Lean.Elab.Tactic.Doc
2222
import Lean.Data.EditDistance
2323
public import Lean.Elab.DocString.Builtin.Keywords
2424
import Lean.Server.InfoUtils
25+
import Lean.Meta.Hint
2526

2627

2728
namespace Lean.Doc
@@ -132,6 +133,15 @@ meta def checkNameExists : PostponedCheckHandler := fun _ info => do
132133
| throwError "Expected `{.ofConstName ``PostponedName}`, got `{.ofConstName info.typeName}`"
133134
discard <| realizeGlobalConstNoOverload (mkIdent name)
134135

136+
private def getQualified (x : Name) : DocM (Array Name) := do
137+
let names := (← getEnv).constants.toList
138+
let names := names.filterMap fun (y, _) => if !isPrivateName y && x.isSuffixOf y then some y else none
139+
names.toArray.mapM fun y => do
140+
let y ← unresolveNameGlobalAvoidingLocals y
141+
pure y
142+
143+
open Lean.Doc
144+
135145
/--
136146
Displays a name, without attempting to elaborate implicit arguments.
137147
-/
@@ -168,7 +178,20 @@ def name (full : Option Ident := none) (scope : DocScope := .local)
168178
addConstInfo n x
169179
pure x
170180
else
171-
realizeGlobalConstNoOverloadWithInfo n
181+
try
182+
realizeGlobalConstNoOverloadWithInfo n
183+
catch
184+
| err => do
185+
let ref ← getRef
186+
if let `(inline|role{$_x $_args*}%$tok[$_*]) := ref then
187+
let ss ← getQualified n.raw.getId
188+
let h ←
189+
if ss.isEmpty then pure m!""
190+
else m!"Insert a fully-qualified name:".hint (ref? := some tok) <|
191+
ss.map fun x => { suggestion := s!" (full := {x})" ++ "}", previewSpan? := ref}
192+
logErrorAt s m!"{err.toMessageData}{h}"
193+
else logErrorAt s m!"{err.toMessageData}"
194+
return .code s!"{n.raw.getId}"
172195
return .other (.mk ``Data.Const (.mk (Data.Const.mk x))) #[.code s.getString]
173196
| .import xs =>
174197
let name :=

tests/lean/run/versoDocs.lean

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ def a := "a"
199199
def b := "b"
200200
end A
201201

202-
/-- error: Unknown constant `a` -/
202+
/--
203+
error: Unknown constant `a`
204+
205+
Hint: Insert a fully-qualified name:
206+
{name ̲(̲f̲u̲l̲l̲ ̲:̲=̲ ̲A̲.̲a̲)̲}`a`
207+
-/
203208
#guard_msgs in
204209
/--
205210
role {name}`a` here
@@ -223,7 +228,13 @@ def testDef' := 15
223228
-/
224229
def testDef'' := 15
225230

226-
/-- error: Unknown constant `b` -/
231+
/--
232+
error: Unknown constant `b`
233+
234+
Hint: Insert a fully-qualified name:
235+
• {name ̲(̲f̲u̲l̲l̲ ̲:̲=̲ ̲A̲.̲b̲)̲}`b`
236+
• {name ̲(̲f̲u̲l̲l̲ ̲:̲=̲ ̲M̲e̲t̲a̲.̲G̲r̲i̲n̲d̲.̲A̲r̲i̲t̲h̲.̲C̲u̲t̲s̲a̲t̲.̲D̲v̲d̲S̲o̲l̲u̲t̲i̲o̲n̲.̲b̲)̲}`b`
237+
-/
227238
#guard_msgs in
228239
/--
229240
{open A only:=a}
@@ -312,6 +323,18 @@ Suggestions are as well.
312323
-/
313324
def otherAttr := ()
314325

326+
/--
327+
error: Unknown constant `Constraint.add`
328+
329+
Hint: Insert a fully-qualified name:
330+
{name ̲(̲f̲u̲l̲l̲ ̲:̲=̲ ̲O̲m̲e̲g̲a̲.̲C̲o̲n̲s̲t̲r̲a̲i̲n̲t̲.̲a̲d̲d̲)̲}`Constraint.add`
331+
-/
332+
#guard_msgs in
333+
/--
334+
{name}`Constraint.add`
335+
-/
336+
def nameErrSuggestions := ()
337+
315338
/--
316339
Options control Lean.
317340
Examples:

0 commit comments

Comments
 (0)