@@ -368,38 +368,37 @@ def currentFile : Selector := fun _ cfg => do
368368 | _ => continue
369369 return suggestions
370370
371- builtin_initialize librarySuggestionsExt : SimplePersistentEnvExtension Syntax (Option Syntax ) ←
371+ builtin_initialize librarySuggestionsExt : SimplePersistentEnvExtension Name (Option Name ) ←
372372 registerSimplePersistentEnvExtension {
373- addEntryFn := fun _ stx => some stx -- Last entry wins
373+ addEntryFn := fun _ name => some name -- Last entry wins
374374 addImportedFn := fun entries =>
375- -- Take the last selector syntax from all imported modules
375+ -- Take the last selector name from all imported modules
376376 entries.foldl (init := none) fun acc moduleEntries =>
377- moduleEntries.foldl (init := acc) fun _ stx => some stx
377+ moduleEntries.foldl (init := acc) fun _ name => some name
378378 }
379379
380- /--
381- Helper function to elaborate and evaluate selector syntax.
382- This is shared by both validation (`elabSetLibrarySuggestions`) and retrieval (`getSelector`).
383- -/
384- def elabAndEvalSelector (stx : Syntax) : MetaM Selector :=
385- Elab.Term.TermElabM.run' do
386- let selectorTerm ← Elab.Term.elabTermEnsuringType stx (some (Expr.const ``Selector []))
387- unsafe Meta.evalExpr Selector (Expr.const ``Selector []) selectorTerm
380+ /-- Attribute for registering library suggestions selectors. -/
381+ builtin_initialize librarySuggestionsAttr : TagAttribute ←
382+ registerTagAttribute `library_suggestions "library suggestions selector" fun declName => do
383+ let decl ← getConstInfo declName
384+ unless decl.type == mkConst ``Selector do
385+ throwError "declaration '{declName}' must have type `Selector`"
386+ modifyEnv fun env => librarySuggestionsExt.addEntry env declName
388387
389388/--
390- Get the currently registered library suggestions selector by evaluating the stored syntax .
389+ Get the currently registered library suggestions selector by looking up the stored declaration name .
391390Returns `none` if no selector is registered or if evaluation fails.
392-
393- Uses `Term.elabTermEnsuringType` to elaborate arbitrary syntax (not just identifiers).
394391-/
395- def getSelector : MetaM (Option Selector) := do
396- let some stx := librarySuggestionsExt.getState (← getEnv) | return none
392+ unsafe def getSelectorImpl : MetaM (Option Selector) := do
393+ let some declName := librarySuggestionsExt.getState (← getEnv) | return none
397394 try
398- let selector ← elabAndEvalSelector stx
399- return some selector
395+ evalConstCheck Selector ``Selector declName
400396 catch _ =>
401397 return none
402398
399+ @[implemented_by getSelectorImpl]
400+ opaque getSelector : MetaM (Option Selector)
401+
403402/-- Generate library suggestions for the given metavariable, using the currently registered library suggestions engine. -/
404403def select (m : MVarId) (c : Config := {}) : MetaM (Array Suggestion) := do
405404 let some selector ← getSelector |
@@ -425,14 +424,12 @@ def elabSetLibrarySuggestions : CommandElab
425424 | `(command| set_library_suggestions $selector) => do
426425 if `Lean.LibrarySuggestions.Basic ∉ (← getEnv).header.moduleNames then
427426 logWarning "Add `import Lean.LibrarySuggestions.Basic` before using the `set_library_suggestions` command."
428- -- Validate that the syntax can be elaborated (to catch errors early)
429- liftTermElabM do
430- try
431- discard <| elabAndEvalSelector selector
432- catch _ =>
433- throwError "Failed to elaborate {selector} as a `MVarId → Config → MetaM (Array Suggestion)`."
434- -- Store the syntax (not the evaluated Selector) for persistence
435- modifyEnv fun env => librarySuggestionsExt.addEntry env selector
427+ -- Generate a fresh name for the selector definition
428+ let name ← liftMacroM <| Macro.addMacroScope `_librarySuggestions
429+ -- Elaborate the definition with the library_suggestions attribute
430+ -- Note: @[ expose ] public, to ensure visibility across module boundaries
431+ -- Use fully qualified `Lean.LibrarySuggestions.Selector` for module compatibility
432+ elabCommand (← `(@[expose, library_suggestions] public def $(mkIdent name) : Lean.LibrarySuggestions.Selector := $selector))
436433 | _ => throwUnsupportedSyntax
437434
438435open Lean.Elab.Tactic in
0 commit comments