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

Allow import <ident> to show completions #14069

Merged
merged 2 commits into from Dec 13, 2021
Merged

Conversation

tgodzik
Copy link
Contributor

@tgodzik tgodzik commented Dec 7, 2021

Previously, there would be no completions after simple import annot. Now, we make sure that the completions mode is set correctly for that case.

Additionally, whenever both java.lang and scala imports conflict we choose scala one.

Connected to #13623

But it will not work with package objects yet. We should most likely open up a separate issue for that?

Previously, there would be no completions after simple `import annot`. Now, we make sure that the completions mode is set correctly for that case.

Additionally, whenever both `java.lang` and `scala` imports conflict we choose scala one.
@tgodzik tgodzik requested a review from prolativ December 7, 2021 17:41
// import scala.annotation
def isJavaLangAndScala = denotss match
case List(first, second) =>
isScalaPackage(first) && isJavaLangPackage(second) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't assume that in case of a conflict there would be only 2 colliding items.
Consider this code (which should be added as a test case)

import java.lang.annotation; import annot${m1}

Here the completion should be ("annotation", Module, "java.lang.annotation") but currently no completion is returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ach damn, so maybe:
denotss.forall(d => isScalaPackage(d) || isJavaLangPackage(d))
would work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be good now!

@mtk
Copy link
Contributor

mtk commented Dec 8, 2021

i'm confused by this. if i recreate that example, "annotation" is the completion after the java import. i have no idea what module the repl thinks it is but does it matter?

@prolativ
Copy link
Contributor

prolativ commented Dec 9, 2021

@mtk Do you mean you are actually getting any completion suggestion for the example I gave? Which version of the compiler are you using?
Anyway even though the REPL currently provides completions without showing their exact types this is something that would be actually quite easy to change, simply nobody has time to work on the improvement.
Secondly, if Metals rely on completions logic implemented here, you would also get confusing results in your IDE if this is done wrong.

@mtk
Copy link
Contributor

mtk commented Dec 9, 2021 via email

@prolativ
Copy link
Contributor

That's really strange. Indeed this seems to work in the REPL but when I created a test case in CompletionTest.scala

  @Test def importAnnotationAfterImport : Unit =
    code"""import java.lang.annotation; import annot${m1}"""
          .withSource
          .completion(m1,
            Set(
              ("annotation", Module, "scala.annotation")
            )
          )

it fails with no completions

@mtk
Copy link
Contributor

mtk commented Dec 11, 2021

i don't know enough about the testing framework to be sure (even though i worked with tomasz on this) but i'm guessing that the test is too specific. it doesn't want to just verify that the completion of 'anno' is 'tation'. it wants to verify that the completion came from a specific namespace. and in the case of annotation, it is provided by both 'scala' and 'java.lang' but the test is demanding that it comes from only 'scala'. does this make sense to the two of you?

@prolativ
Copy link
Contributor

@mtk I would say when something gets completed automatically for you it's usually better to know what this actually is exactly than just have to guess, especially if you don't have to check it manually but the tooling does this for you. Different definitions with the same name can have completely different semantics.
And as mentioned before, this additional information is not currently available in the REPL but it could be exposed there (if you'd like to work on that I can give you some tips) and it's already used by Metals

@odersky odersky merged commit 0367d6b into scala:master Dec 13, 2021
@tgodzik tgodzik deleted the fix-i13623 branch December 13, 2021 12:22
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