Skip to content

Make go-to-definition on a typeclass projection application go to the instance(s) - #1767

Merged
Kha merged 25 commits into
leanprover:masterfrom
rish987:go-to-instance
Jan 19, 2023
Merged

Kha merged 25 commits into
leanprover:masterfrom
rish987:go-to-instance

Conversation

@rish987

@rish987 rish987 commented Oct 22, 2022 •

Copy link
Copy Markdown
Contributor

Closes #1722, pending #1903.

@rish987
rish987 force-pushed the go-to-instance branch 2 times, most recently from ba85f34 to e06e665 Compare October 22, 2022 03:32
Comment thread tests/lean/interactive/jumpMutual.lean Resolved
@rish987

rish987 commented Oct 23, 2022

Copy link
Copy Markdown
Contributor Author

Hmm the CI test failures are puzzling me (it looks like they're still not getting the instance locations for some reason), because everything passes locally when I do:

$ ./test_single.sh {goTo,goto2}.lean

@Kha

Kha commented Oct 23, 2022

Copy link
Copy Markdown
Member

The interactive tests are not designed for referencing core files in their output. Either you'll need to change the existing tests not to reference core instances, or we implement something in the vein of #1722 (comment) that would prevent these tests from showing instances.

@leodemoura

Copy link
Copy Markdown
Member

@rish987 Today we were discussing this PR in the dev meeting. Could you please clarify the semantics you are implementing?

@rish987

rish987 commented Oct 24, 2022 •

Copy link
Copy Markdown
Contributor Author

Sure, I was trying to follow what Sebastian described here, basically, on go-to-definition or go-to-type if we get an Info node that represents an application of a typeclass projection, rather than going to the syntax or elaborator, we extract all of the relevant instances and present that in a list. I've also included the elaborator in this list because you were originally only able to get to that through go-to-definition (the syntax is still accessible as before through go-to-declaration).

I think I may need some help with the tests though (I blindly copied over expected outputs that I just noticed include URIs on my local filesystem 😰 so of course it won't work). Perhaps we need some additional filtering to preserve existing behavior (and some new tests as well to specifically test the new functionality), I was discussing this with @Kha yesterday and it sounded like he had an idea regarding that?

@Kha

Kha commented Oct 25, 2022 •

Copy link
Copy Markdown
Member

Right, the fundamental issue is that when looking at the typeclass method call Expr, we don't know if this info node was responsible for the TC synthesis, and thus whether original/canonical syntax was responsible for it. What I proposed to Rish is to simply whitelist syntax that we know does this, e.g. i.stx.isOfKind identKind || i.stx.isOfKind ``Parser.Term.app and probably others as well. A different, probably more general heuristic would be to check if no nested info node contains the same Expr (making use of the tree structure of the info tree, heh).

@rish987
rish987 marked this pull request as draft October 29, 2022 01:18
@Kha Kha mentioned this pull request Dec 1, 2022
@rish987
rish987 marked this pull request as ready for review December 13, 2022 03:06
@rish987

rish987 commented Dec 13, 2022 •

Copy link
Copy Markdown
Contributor Author

Okay this should finally be ready! I've merged in #1903 so it depends on that PR being merged first.

@rish987 rish987 closed this Dec 13, 2022
@rish987

rish987 commented Dec 13, 2022

Copy link
Copy Markdown
Contributor Author

Whoops, didn't mean to close it!

@rish987 rish987 reopened this Dec 13, 2022

@Kha Kha left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great all in all!

Comment thread src/Lean/Server/InfoUtils.lean Resolved
Comment thread src/Lean/Server/InfoUtils.lean Resolved
Comment thread src/Lean/Widget/Basic.lean Outdated
Comment on lines 18 to 22
structure InfoWithCtx where
ctx : Elab.ContextInfo
info : Elab.Info
children : PersistentArray InfoTree
deriving Inhabited, TypeName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to InfoUtils and use it in hoverableInfoAt? and locationLinksOfInfo etc. The deriving TypeName should probably be left here though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, let me know what you think of the refactor.

@Vtec234 Vtec234 Jan 1, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry to only comment on this now but InfoWithCtx is only really intended for use as a way of bundling data together in the RPC protocol so that interactive expressions and go-to-def in the infoview work. As Gabriel put it, it's a kind of "type popup + go-to-def thunk". Unless you need to do something with RPC here, it would be better not to move it and to add another type with whatever semantics you need instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicating it wouldn't be hard, but since the type is opaque in the RPC protocol I'm not really seeing the motivation. It's a sensible bundle of data to have in the server in general, and the name fits as well. Even if we don't necessarily need children in the widgets.

@Vtec234 Vtec234 Jan 1, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type seems like a better fit for Elab.InfoTree, if anything, because it was already hacky as far as the RPC interface is concerned — the outputs of delaboration are Elab.Infos but they should really be a much more restricted Delab.Info without variants like .ofCompletion. But okay, since InfoWithCtx is opaque we can always keep it around for uses such as in this PR but replace it with something else in the RPC interface. Btw @rish987 you probably want to also make the output of hoverableInfoAt? and termGoalAt? be InfoWithCtx, document what the children field is, and change the docstring to note that it's used for more than just RPC.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks for the suggestions.

@Kha Kha changed the title feat: make go-to-definition on a typeclass projection application go to the instance(s) Make go-to-definition on a typeclass projection application go to the instance(s) Dec 13, 2022
@Kha Kha added the changelog label Dec 13, 2022
@rish987
rish987 force-pushed the go-to-instance branch 2 times, most recently from 6527820 to acf4acb Compare January 3, 2023 04:09
@rish987
rish987 force-pushed the go-to-instance branch 3 times, most recently from aca714d to 9ebb657 Compare January 3, 2023 04:18
@rish987
rish987 marked this pull request as draft January 16, 2023 23:41
Comment thread tests/lean/interactive/goTo.lean Resolved
@rish987
rish987 marked this pull request as ready for review January 19, 2023 02:21
@rish987
rish987 requested a review from Kha January 19, 2023 02:21
@Kha
Kha enabled auto-merge (squash) January 19, 2023 08:05
@Kha
Kha merged commit 561e404 into leanprover:master Jan 19, 2023
github-merge-queue Bot pushed a commit that referenced this pull request Jul 21, 2025
This PR improves the 'Go to Definition' UX, specifically:
- Using 'Go to Definition' on a type class projection will now extract
the specific instances that were involved and provide them as locations
to jump to. For example, using 'Go to Definition' on the `toString` of
`toString 0` will yield results for `ToString.toString` and `ToString
Nat`.
- Using 'Go to Definition' on a macro that produces syntax with type
class projections will now also extract the specific instances that were
involved and provide them as locations to jump to. For example, using
'Go to Definition' on the `+` of `1 + 1` will yield results for
`HAdd.hAdd`, `HAdd α α α` and `Add Nat`.
- Using 'Go to Declaration' will now provide all the results of 'Go to
Definition' in addition to the elaborator and the parser that were
involved. For example, using 'Go to Declaration' on the `+` of `1 + 1`
will yield results for `HAdd.hAdd`, `HAdd α α α`, `Add Nat`,
``macro_rules | `($x + $y) => ...`` and `infixl:65 " + " => HAdd.hAdd`.
- Using 'Go to Type Definition' on a value with a type that contains
multiple constants will now provide 'Go to Definition' results for each
constant. For example, using 'Go to Type Definition' on `x` for `x :
Array Nat` will yield results for `Array` and `Nat`.

### Details
'Go to Definition' for type class projections was first implemented by
#1767, but there were still a couple of shortcomings with the
implementation. E.g. in order to jump to the instance in `toString 0`,
one had to add another space within the application and then use 'Go to
Definition' on that, or macros would block instances from being
displayed. Then, when the .ilean format was added, most 'Go to
Definition' requests were already handled using the .ileans in the
watchdog process, and so the file worker never received them to handle
them with the semantic information that it has available.

This PR resolves most of the issues with the previous implementation and
refactors the 'Go to Definition' control flow so that 'Go to Definition'
requests are always handled by the file worker, with the watchdog merely
using its .ilean position information to update the positions in the
response to a more up-to-date state. This is necessary because the file
worker obtains its position information from the .oleans, which need to
be rebuilt in order to be up-to-date, while the watchdog always receives
.ilean update notifications from each active file worker with the
current position information in the editor.

Finally, all of the 'Go to Definition' code is refactored to be easier
to maintain.

### Breaking changes
`InfoTree.hoverableInfoAt?` has been generalized to
`InfoTree.hoverableInfoAtM?` and now takes a general `filter` argument
instead of several boolean flags, as was the case before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go-to-definition on typeclass method should go to instance

4 participants