Skip to content

Fix standard library symbols completion (solves #9760) #9805

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

Merged

Conversation

prolativ
Copy link
Contributor

@prolativ prolativ commented Sep 16, 2020

Symbols which are available in scope by default like println or Runnable were not suggested by LSP completion feature.
Some symbols like Seq or BigDecimal were suggested neither by LSP nor in REPL.
This PR solves both these issues.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

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.

Looks good! It would be good to merge it also to 0.27.0. It's broken in 0.27.0-RC1, what is the rule with fixing release candidates?

@smarter
Copy link
Member

smarter commented Sep 18, 2020

I haven't checked but I assume this regressed in 6990aab, /cc @TheElectronWill

@@ -1284,7 +1284,9 @@ class Definitions {

private val ScalaImportFns: List[RootRef] =
JavaImportFns :+
RootRef(() => ScalaPackageVal.termRef)
RootRef(() => ScalaPackageVal.termRef) :+
RootRef(() => ScalaPackageObject.termRef)
Copy link
Contributor

Choose a reason for hiding this comment

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

This just makes compilation slower. An import from ScalaPackageVal will automatically import members of scala.package. On the other hand, now we have one more import to search for every root reference.

What breaks if we do not do this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Without this change the PR doesn't fix anything. But I agree that we shouldn't need it.

Copy link
Contributor Author

@prolativ prolativ Sep 21, 2020

Choose a reason for hiding this comment

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

As stated in the PR description, this PR fixes 2 issues (caused by 2 separate things in the code base but influencing users in a similar way):

  • Completion of symbols from scala package object, like Seq or BigDecimal
  • Completion of other predefined symbols (from Predef and top level definitions from scala package), like println

The latter issue is indeed a regression introduced in the PR that @smarter mentioned and it occurs only in LSP. The former issue must be older, even though nobody else seems to have spotted it before, it affects both LSP and REPL and is fixed specifically by this line (although this itself wouldn't fix LSP without the other fix).
I'm not really sure how the problem could be solved without adding this line. Interestingly this only changes the behaviour of symbols completion. Symbols like Seq or BigDecimal cause no problems in compilation and symbols completion should probably have exactly the same view of symbols in scope as the compiler.

Copy link
Contributor

@TheElectronWill TheElectronWill Sep 21, 2020

Choose a reason for hiding this comment

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

Ah right, I forgot the other issue. Adding root imports to the context like you did is indeed the right solution for this second issue.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

If we can make it work without the change in ScalaImportFns the rest LGTM

Copy link
Contributor

@TheElectronWill TheElectronWill left a comment

Choose a reason for hiding this comment

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

Actually, just adding RootRef(() => ScalaPackageObject.termRef) to ScalaImportFns solves the issue (for the REPL, at least). And adding scala root imports to the rootContext (to end up in a state similar to before #9394) doesn't. So the real issue seems to be somewhere else 🤔

@prolativ
Copy link
Contributor Author

@odersky @smarter as after the discussion it looks like adding that import function is the proper solution, can we merge this PR? Or is the compilation slowdown so significant that we need to find some hack for that as a workaround?

Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

I think the addition of ScalaPackageObject in the root import is a workaround for an underlying but that needs to be fixed: when importing a package, we should also get completions for members of the package object. For example:

import scala.collection._
val foo: TraversableOnc<TAB>

This should find TraversableOnce since it's defined in https://github.com/scala/scala/blob/d003bf5159acaca883f90e1be822bef9316639de/src/library/scala/collection/package.scala#L23, but it doesn't find anything. If I explicitly import the package object it works:

import scala.collection.`package`.
val foo: TraversableOnc<TAB>

So the completion logic needs to be fixed to handle package objects correctly.

@prolativ prolativ force-pushed the fix-standard-library-symbols-completion branch 2 times, most recently from 2d7881d to f1cb964 Compare September 24, 2020 12:10
@@ -264,7 +264,12 @@ object Completion {
private def accessibleMembers(site: Type)(using Context): Seq[Symbol] = site match {
case site: NamedType if site.symbol.is(Package) =>
// Don't look inside package members -- it's too expensive.
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we were intentionally not taking package object members into account, @odersky do you remember the context of this comment? (added in 518e9fd)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to get this PR finally merged but I'm not sure what I should do to push it forward. If the comment is still valid, I can revert some of the changes and leave only the fix for the regression but from user's perspective completion not working for symbols like List or Iterator might be quite confusing and irritating.

Copy link
Contributor

Choose a reason for hiding this comment

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

We might do it on the Metals side, but it will require a bit more work still.

Copy link
Member

Choose a reason for hiding this comment

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

Just asked Martin IRL about this and he thinks it's fine to make this change. So LGTM from me, but please remove that comment since it's no longer accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@smarter smarter added this to the 3.0.0-M1 milestone Sep 30, 2020
@prolativ prolativ force-pushed the fix-standard-library-symbols-completion branch from f1cb964 to 138c742 Compare October 1, 2020 09:49
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

Thank you!

@smarter smarter merged commit 19cf871 into scala:master Oct 1, 2020
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.

6 participants