scalameta / metals-feature-requests

Issue tracker for Metals feature requests
37 stars 4 forks source link

Auto-import missing implicits #141

Open kubukoz opened 4 years ago

kubukoz commented 4 years ago

Is your feature request related to a problem? Please describe. It's currently not possible to auto-import implicit conversions/classes/parameteres.

Describe the solution you'd like A quick fix that shows up when there are no implicits of the requested type found, but some are known to be available in the workspace. Also, when a selection is not available on a symbol but it would've been if there was an implicit conversion in scope, that could also be suggested.

Describe alternatives you've considered Learning all implicits in the workspace by hand ;)

Additional context IntelliJ IDEA just got an update that allows auto-importing implicits: https://blog.jetbrains.com/scala/2020/07/16/intellij-scala-plugin-2020-2-auto-import-for-implicits/

Search terms: implicit, conversion, autoimport, import

tgodzik commented 4 years ago

Thanks for reporting! I think this is a great idea, I know @olafurpg was thinking about implementing it a while ago, but never actually went with it anywhere.

gabro commented 2 years ago

Specifically for Scala 3, I made a naive attempt at implementing this some time ago.

I think it's useful to re-surface this comment from smarter: https://github.com/scalameta/metals/pull/2536/files#r583848840

Contributing upstream to the reporting api in the compiler may be the way to go

tanishiking commented 2 years ago

I started to do a bit of experimental implementation on this feature. (Importing the extension method in Scala3 is the first milestone).

import-extension

What we need to do for extension methods (in workspace) is:

I guess we can make given instance auto importable easily by indexing those instances. Implicit classes will be a bit complicated (parsing might be a bit complicated, and we have to import the class, instead of methods) but we can handle it I guess.

However, the implicit conversion will be out of scope, in the following scenario, it's hard to tell which we need to import to use xxx on Int. (Also, we end up indexing almost every method inside classes).

class RichInt(n: Int) {
  def xxx = ???
}
implicit def toRichInt(x: Int) = new RichInt(x)

// another file
1.xxx
tanishiking commented 2 years ago
kubukoz commented 2 years ago

For implicit classes, isn't the problem the same as with implicit conversions? Since you can potentially call a.x on any a if there's a matching implicit class