Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type annotation on code selection #3060

Merged
merged 14 commits into from
Sep 24, 2021
Merged

Conversation

KacperFKorban
Copy link
Collaborator

@KacperFKorban KacperFKorban commented Aug 21, 2021

closes #3059
Peek 2021-08-21 21-36

We are limited by frontend capabilities, so to display the information properly we have to use the type annotation as hover.

Things that may be controversial:

  • Hover for literals (Only for ranges now)
  • Expression type for every hover (Only for ranges now)
  • key binding here

vscode plugin changes: scalameta/metals-vscode#652

@KacperFKorban KacperFKorban force-pushed the range-hover branch 2 times, most recently from 3f72351 to c0d2f7c Compare August 22, 2021 00:34
Copy link
Member

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

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

So this is really rad @KacperFKorban, I will use this a ton!

Using nvim-metals, this seems to just work as expected out of the box.

Screen.Recording.2021-08-22.at.15.56.00.mov

With that in mind and looking at the code fully recognising I may have missed something, it seems that the biggest thing we are still looking for is the offset coming off of the end of the range. In my video above, that's all that is being sent in, the position at the end of the visual selection. Do we actually need the full range? Won't the type at the end point always be what we are after? If that's possible, then we don't actually need to break away from the LSP spec for hover by using our own class instead of the TextDocumentPositionParams.

@KacperFKorban
Copy link
Collaborator Author

So this is really rad @KacperFKorban, I will use this a ton!

Using nvim-metals, this seems to just work as expected out of the box.

Screen.Recording.2021-08-22.at.15.56.00.mov
With that in mind and looking at the code fully recognising I may have missed something, it seems that the biggest thing we are still looking for is the offset coming off of the end of the range. In my video above, that's all that is being sent in, the position at the end of the visual selection. Do we actually need the full range? Won't the type at the end point always be what we are after? If that's possible, then we don't actually need to break away from the LSP spec for hover by using our own class instead of the TextDocumentPositionParams.

Hmmm, can it be the case that nvim sends different position?

In vscode triggering the hover on the end of an expression doesn't work. Range on a term name seems to include the position before, but not the position after. Like here:
Peek 2021-08-22 20-56

Technically endOffset - 1 can work for ranges, but it can bring problems with position requests.

@ckipp01
Copy link
Member

ckipp01 commented Aug 22, 2021

Hmmm, can it be the case that nvim sends different position?

in vscode triggering the hover on the end of an expression doesn't work. Range on a term name seems to include the position before, but not the position after. Like here:

Gotcha. I think it's partly because when nvim sends in the position from the selection range, it's still just a single point that often isn't actually after the symbol, since unlike VS Code, you can't really extend past the text like in your image.

Either way, great work on this 👍🏼 I'll for sure play around with this a bit more to see if anything will even need to be added for this to work the same way in nvim-metals as it does in VS Code.

@KacperFKorban
Copy link
Collaborator Author

@ckipp01 I thought of an example that won't work with just the end offset- method calls in infix notation.
e.g. for:

List("1", "2", "3").filter(<<_ contains "2">>)

the result will be Boolean.
and for:

List("1", "2", "3").filter(_ contains "2@@")

the result will be String.

@ayoub-benali
Copy link
Contributor

Really cool feature ! What needs to be done for none VS code clients, say Sublime Text ? :)

@tgodzik
Copy link
Contributor

tgodzik commented Aug 27, 2021

Really cool feature ! What needs to be done for none VS code clients, say Sublime Text ? :)

You would need to send a range instead of position, similar to scalameta/metals-vscode#652 - this was actually discussed in the LSP github repo, but not sure if anything will change on that front. This is already being done for Rust and I think Haskell.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Thanks for the great work! I have a few comments, overall it would be awesome to limit the changes to only the code selection if possible.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM! Just one small typo, I will push the change and rebase on master

tests/mtest/src/main/scala/tests/TestHovers.scala Outdated Show resolved Hide resolved
@tgodzik tgodzik merged commit 42ea48a into scalameta:main Sep 24, 2021
ckipp01 added a commit to ckipp01/metals that referenced this pull request Oct 3, 2021
This is a follow-up to the work done in scalameta#3060, but since
positions is now optional, we need to ensure to use `getPosition`
instead of position like we did here. This was causing the following
exception to be throw:

```
Caused by: java.lang.IllegalArgumentException: Property must not be null: position
        at org.eclipse.lsp4j.util.Preconditions.checkNotNull(Preconditions.java:29)
        at org.eclipse.lsp4j.TextDocumentPositionParams.<init>(TextDocumentPositionParams.java:49)
        at scala.meta.internal.metals.Compilers.withPCAndAdjustLsp(Compilers.scala:594)
        at scala.meta.internal.metals.Compilers.hover(Compilers.scala:365)
        at scala.meta.internal.metals.MetalsLanguageServer.$anonfun$hover$1(MetalsLanguageServer.scala:1359)
        at scala.meta.internal.metals.CancelTokens$.future(CancelTokens.scala:38)
        at scala.meta.internal.metals.MetalsLanguageServer.hover(MetalsLanguageServer.scala:1357)
        ... 17 more
```

I'm surprised the test didn't catch this, because this shouldn't work at
all without this change, but by quickly looking at the tests, it's
wasn't clear to me right away why that was the case. I'll need to look
further to understand the tests, but figured I'd send up this quick fix
right away because now it won't work at all without it.
@ayoub-benali
Copy link
Contributor

Really cool feature ! What needs to be done for none VS code clients, say Sublime Text ? :)

You would need to send a range instead of position, similar to scalameta/metals-vscode#652 - this was actually discussed in the LSP github repo, but not sure if anything will change on that front. This is already being done for Rust and I think Haskell.

@tgodzik Do you mind posting the link so I get the context ?

@tgodzik
Copy link
Contributor

tgodzik commented Nov 8, 2021

The discussion is happening here: microsoft/language-server-protocol#377

@KacperFKorban KacperFKorban deleted the range-hover branch November 8, 2021 12:42
ayoub-benali added a commit to scalameta/metals-sublime that referenced this pull request Nov 14, 2021
This is needed to handle scalameta/metals#3060
It should be handle by LSP package once microsoft/language-server-protocol#377
is resolved
@bloznelis
Copy link

Sorry for necrobumping, but a question for @ckipp01, did you manage to make it work properly in nvim-metals?

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.

Type annotation on code selection
6 participants