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

Auto-import missing implicits #141

Open
kubukoz opened this issue Jul 16, 2020 · 5 comments
Open

Auto-import missing implicits #141

kubukoz opened this issue Jul 16, 2020 · 5 comments
Assignees

Comments

@kubukoz
Copy link

kubukoz commented Jul 16, 2020

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
Copy link
Contributor

tgodzik commented Jul 16, 2020

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
Copy link
Member

gabro commented Aug 23, 2021

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
Copy link
Member

tanishiking commented Jul 8, 2022

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:

  • Index extension methods in ScalaToplevelMtags so we can find a symbol of the missing extension method by MetalsSymbolSearch.
    • Only when includeInnerClasses = true (maybe we should rename this flag, or add another flag)
  • Make ImportMissingSymbol code action available for the "value xxx is not a member of yyy, ..."

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
Copy link
Member

tanishiking commented Jul 21, 2022

  • auto import extension method
  • auto import missing given instances
  • auto import implicit class (for Scala2)
  • auto import implicit conversions (it's virtually impossible)
    • in order to make it possible we have to index all the symbols (not only implicit ones but literally everything because with implicit conversions we can call anything from any object (via implicit conversions))
    • and collect all of the possible implicit conversions, and search methods by name, then filter by types and track down which the needed implicit conversions.
    • However, it requires tons of memory and computations, I don't think it's reasonable to make it (actually, that's why Scala3 requires us to import scala.language.implicitConvesron and make it explicit to create an implicit conversion).

@kubukoz
Copy link
Author

kubukoz commented Jul 21, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants