Zulip Chat Archive
Stream: lean4
Topic: (bug?) handleDocumentSymbol does not work for lemmas
Lua Viana Reis (Aug 30 2025 at 19:57):
I noticed that the LSP textDocument/documentSymbol request is not returning mathlib's lemmas among the other declarations. For instance, you can check that in VSCode by clicking the three dots in the header line:
I'm curious whether this bug is in the mathlib side or Lean's side. It seems mathlib is properly expanding lemma to theorem and marking the lemma as a declaration, but somehow it's still not visible to the server?
Lua Viana Reis (Aug 30 2025 at 20:47):
It seems the problem is in the Lean side, because foo below also does not show:
macro "stupid" dec:command : command => pure dec
stupid theorem foo : 1 = 1 := rfl
Robin Arnez (Aug 30 2025 at 20:49):
Well it only cares about docs#Lean.Parser.Command.declarations and lemma isn't a declaration, nor is your "stupid" macro
Lua Viana Reis (Aug 30 2025 at 20:50):
@Robin Arnez I don't think this is (exactly) the issue. If you look at the source of lemma in Mathlib, it does change the syntax kind. But perhaps the syntax after macro expansion is not considered
Robin Arnez (Aug 30 2025 at 20:51):
Yeah that's what I mean. It looks at the original syntax and docs#lemma != docs#Lean.Parser.Command.declaration
Lua Viana Reis (Aug 30 2025 at 20:53):
wouldn't it be desirable to apply macro expansion too? I found the fact lemmas are not reported a bit annoying, especially in Emacs where I like to navigate the file with imenu (which is like a TOC)
Lua Viana Reis (Aug 30 2025 at 21:58):
it also does not work with open [identifier] in [command] syntax. Leading to the slightly ironic fact that handleDocumentSymbol is not listing itself
Kyle Miller (Aug 30 2025 at 22:08):
Looks like a bug to me. (I think Robin is just explaining the error, not justifying it.)
Want to report it on Github if it's not already there? https://github.com/leanprover/lean4/issues
Lua Viana Reis (Aug 31 2025 at 03:36):
I'm trying to learn more about Lean's internals, so I attempted an implementation myself
but I'll try to report the issue there, I'm not sure if my way is the "right" way (via Snapshot.infoTree)
Lua Viana Reis (Aug 31 2025 at 03:42):
if I import it in another file, everything shows up in the list
code
edit: I made an issue lean4#10190
Lua Viana Reis (Sep 12 2025 at 18:39):
@Kyle Miller do you think I could turn this implementation into a PR?
Kyle Miller (Sep 12 2025 at 18:48):
Let's ping @Marc Huisinga about this issue (lean4#10190), since he's responsible for LSP.
Robin Arnez (Sep 12 2025 at 18:49):
I don't think the elaborator is a good thing to look at since the syntax may be completely different before macro expansion. I'd simply try macro expanding the syntax (or reading macro expansion info?) but I'm not sure how exactly you'd be able to do that in RequestM
Lua Viana Reis (Sep 12 2025 at 18:57):
Do you have an example in mind of how different it could be? From my usage so far, only autogenerated identifiers sometimes look odd in the listing
Lua Viana Reis (Sep 12 2025 at 18:58):
and I think think this would be the biggest possible problem, for only the source position and identifier of the declaration are taken into account for the document symbols
Robin Arnez (Sep 12 2025 at 19:00):
I believe the stupid macro would already break the elaborator approach since you'd be looking at the wrong syntax, so it'd look at theorem foo : 1 = 1 := rfl instead of foo
Robin Arnez (Sep 12 2025 at 19:00):
and then you just get <unknown>
Lua Viana Reis (Sep 12 2025 at 19:02):
but in what sense it would "break" it? this is how it is reported with the patch:
Lua Viana Reis (Sep 12 2025 at 19:03):
personally, I like that the declarations with autogenerated names are listed as well
Robin Arnez (Sep 12 2025 at 19:03):
Hmm, does it work with
macro "slightly" "stupid" dec:command : command => pure dec
slightly stupid theorem abc : 1 = 1 := rfl
too?
Robin Arnez (Sep 12 2025 at 19:04):
Oh wait are you looking at the info tree? I already wondered how you do that lol
Lua Viana Reis (Sep 12 2025 at 19:04):
Robin Arnez (Sep 12 2025 at 19:05):
I guess maybe this approach does work then? I'm not too sure though, let's wait on what @Marc Huisinga thinks about this
Marc Huisinga (Sep 15 2025 at 11:46):
I'd be happy to review a PR that fixes this issue. Having some tests for this feature would also be great.
Note that since the document symbols request is used for all kinds of UI features, not just the symbol search, it should attempt to not report any redundant entries.
In the future, we may also still need to rewrite this feature entirely so that it doesn't have to wait for the full elaboration of the entire file anymore.
Lua Viana Reis (Sep 15 2025 at 15:49):
Thanks @Marc Huisinga :-) I have two questions:
- What is the criterion for redundancy? In the example above, the
macroexpands to two named declarations. Should it report only one since they belong to the same position in the file? And if so, which one? Personally, I prefer both declarations to be listed. - When you say it could be rewritten not to wait for the exaboration of the entire file, what is the idea here? To only apply macro expansion to the syntax, or to use some sort of cache?
Lua Viana Reis (Sep 15 2025 at 15:57):
Perhaps another question that should be considered first:
- What should count as an entry in the symbols list? Currently, it's a declaration, because they usually have names. That said, there is a special case for unnamed
instances, which show up asinstance : type annotationin the list. Perhaps, we could add special cases formacros andsyntaxs which often are not explicitely named as well.
Lua Viana Reis (Sep 15 2025 at 16:03):
Another idea would be to explicitely have a class IsDocumentSymbol Name for some syntax nodes, which could be instantiated by providing the way to get the name and position from the syntax.
Lua Viana Reis (Nov 18 2025 at 07:44):
@Marc Huisinga I had this code sitting here for a while (I'm using it as a server plugin), so I made a PR lean4#11228. I think the questions above are still valid, so feel free to modify or close it, but I'm still making a case for it as in Emacs having the symbols is very very convenient for jumping around the file fast.
Lua Viana Reis (Nov 18 2025 at 15:25):
Marc Huisinga said:
In the future, we may also still need to rewrite this feature entirely so that it doesn't have to wait for the full elaboration of the entire file anymore.
Do you mean, report a partial list of symbols during elaboration?
Last updated: Dec 20 2025 at 21:32 UTC