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

feature: Replace synthetic decorations with inlay hints #5983

Merged
merged 6 commits into from Feb 19, 2024

Conversation

jkciesluk
Copy link
Member

No description provided.

@jkciesluk jkciesluk force-pushed the inlay-hints branch 2 times, most recently from 3ec6048 to df32716 Compare January 3, 2024 08:51
@jkciesluk jkciesluk marked this pull request as ready for review January 3, 2024 09:07
@jkciesluk jkciesluk mentioned this pull request Jan 3, 2024
4 tasks
Copy link
Contributor

@kasiaMarek kasiaMarek left a comment

Choose a reason for hiding this comment

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

Sometimes hover on inlay hints feels broken.
this is line 107 in Scala 3 PcCollector
https://github.com/scalameta/metals/assets/26606662/9e7af9a2-e38d-4fef-9763-9678d2e3112c
Also it doesn't update hover if you move from one inlay hint to another but both of those things may be a VSCode issue.

But in other cases hover info seems to be missing, e.g. on List in Scala 3.

): Future[InlayHint] = {
scala.util.Try {
val data = inlayHint.getData().asInstanceOf[JsonArray]
getLabelParts(inlayHint).zip(parseData(data))
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be a bit cleaner to use create a class for what you're parsing and use CommonMtagsEnrichments#decodeJson?

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried many ways for parsing this, but didn't manage to make anything cleaner work, I had issues with trying to decode optional values or Either.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought that org.eclipse.lsp4j.jsonrpc.messages.Either would solve this.

metals-bench/src/main/scala/bench/InlayHintsBench.scala Outdated Show resolved Hide resolved
|""".stripMargin,
_ <- server.assertInlayHints(
"MyTests.sc",
s"""|#!/usr/bin/env -S scala-cli shebang --java-opt -Xms256m --java-opt -XX:MaxRAMPercentage=80
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to pass the whole file input, won't that be available in buffers in testing server?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think most tested methods do it this way.

I've changed the signature so the tests should be more readable.

@jkciesluk jkciesluk force-pushed the inlay-hints branch 2 times, most recently from 9ea107d to a7de426 Compare January 11, 2024 16:02
@@ -167,7 +168,7 @@ public abstract CompletableFuture<List<TextEdit>> convertToNamedArguments(Offset
/**
* Returns decorations for missing type adnotations, inferred type parameters, implicit parameters and conversions.
*/
public CompletableFuture<List<SyntheticDecoration>> syntheticDecorations(SyntheticDecorationsParams params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will break mima, if we have an older mtags that is no longer release this will throw an exception.

We could do two things:

  • have a way to translate older syntheticDecorations to inlay hints (even if worse or not complete) <- this would be the best option
  • just deprecate it and have inlay hints return nothing as a default like now (this will make the decorations stop working when using older Scala versions)

Anyway I would not remove the old method

Copy link
Member Author

Choose a reason for hiding this comment

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

have a way to translate older syntheticDecorations to inlay hints (even if worse or not complete) <- this would be the best option

How should we approach this? In Compilers.inlayHints, if pc.inlayHints returns empty results, try to run pc.syntheticDecorations and transform the result to inlayHints?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, we've added syntheticDecorations to the Presentation Compiler in the last release, so the only scala versions for which decorations would stop working are these for which we drop support in the next release (assuming in would contain inlayHints)?

Copy link
Contributor

Choose a reason for hiding this comment

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

We also have to take into account the compiler itself, which uses the same interface currently. So we should at least not remove the method. But we might not bother with translating decorations to inlay hints then.

@@ -1,12 +0,0 @@
package scala.meta.pc;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not remove it, mima should, but I think I haven't updated it previously.

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.

Great work! I left some comments about the interfaces that should be handled properly before merging.

object InlayHints {
def empty: InlayHints = InlayHints(Nil, Set.empty)

def makeLabelParts(
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this method adds the additional [, ] parts? Could add a comment and might be better to name it properly.

| implicit val ttt: Set[Int] = Set(1)
|}
|""".stripMargin,
// TODO: We should show info about renames (`val ttt: S[Int]` instead of `val ttt: Set[Int]`)
Copy link
Contributor

Choose a reason for hiding this comment

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

That should be shown properly with the recent changes by @kasiaMarek right?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not possible to make it work now, because we the tooltip we show, comes from hover on a ttt definition. On ttt definition there is no information about a rename from Set to S.

But I think thats not a big issue

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok sure, we can leave it for now, not super important. I guess we would need to handle the resolve request in the compiler to make that work. And it's maybe not worth it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Especially that the user will see the rename + what it dealiases to, so maybe that is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and if there are different renames in hint position and symbol definition position, the logic could get quite messy

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this could be handled if we were passing two positions to hover, one of the symbol we want to hover on, and the other one where the hover info will actually be shown, so the context for the second one could be used for printing. But it may be too optimistic to assume it will just work, and definitely not worth to consider in this PR. I still think we should create an issue considering this, we can give low-priority and leave it be, but it will be there for sake of documentation.

@jkciesluk jkciesluk force-pushed the inlay-hints branch 4 times, most recently from 3a35fbc to c9b3015 Compare January 15, 2024 14:20
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.

Let's rebase and merge @jkciesluk ! I think we should be good to go

jkciesluk added a commit to jkciesluk/dotty that referenced this pull request Feb 21, 2024
jkciesluk added a commit to scala/scala3 that referenced this pull request Feb 22, 2024
@igor-ramazanov
Copy link

Just wanted to say thank you for this PR, appreciate.

This will greatly help people working in less mainstream editors like Kakoune, Helix or Zed.

Thanks.

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.

None yet

4 participants