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

feat: OverrideCompletion can handle symbolPrefix #4037

Merged
merged 5 commits into from
Jun 23, 2022

Conversation

tanishiking
Copy link
Member

@tanishiking tanishiking commented Jun 16, 2022

#3928

This PR enables OverrideCompletions and implement-all abstract members' code action for Scala3 to import using preset renames like java.util => ju.

abstract class JUtil {
  def foo: java.util.List[Int]
}
class Main extends JUtil {
  def foo@@
}

completes

import java.{util => ju}
abstract class JUtil {
  def foo: java.util.List[Int]
}
class Main extends JUtil {
  def foo: ju.List[Int] = ${0:???}
}

@tanishiking

This comment was marked as resolved.

@tanishiking

This comment was marked as resolved.

@dos65

This comment was marked as resolved.

@tanishiking

This comment was marked as resolved.

@tanishiking

This comment was marked as resolved.

@tanishiking tanishiking force-pushed the rename-override-completion branch 2 times, most recently from 916a709 to fd31e37 Compare June 17, 2022 07:51
@tanishiking tanishiking marked this pull request as ready for review June 17, 2022 07:55
config.symbolPrefixes.asScala.flatMap { (from, to) =>
val fullName = from.stripSuffix("/").replace("/", ".")
// val pkg = requiredPackage(fullName)
val pkg = SemanticdbSymbols.inverseSemanticdbSymbol(from)
Copy link
Member Author

Choose a reason for hiding this comment

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

Using inverseSemanticdbSymbol as I found requiredPackage doesn't create proper symbols.

/**
* @param symbol A missing symbol to auto-import
*/
def editsForSymbol(symbol: Symbol): Option[AutoImportEdits] =
inferAutoImport(symbol).map { ai =>
Copy link
Member Author

Choose a reason for hiding this comment

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

Just separate the logic into toEdits method.

tp match
case tp: AppliedType =>
tp.tycon match
case p: PrettyType =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Overriding it only for PrettyType wouldn't work?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, if we have AppliedType(PrettyType("Foo"), Seq(Int, String)), DocPrinter will handle AppliedType using RefinedPrinter's toText and it never call this method for PrettyType recursively, we will end up having Int <none> String instead of Foo[Int, String].

Copy link
Member

@dos65 dos65 left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -33,6 +34,12 @@ object AutoImports:
*/
case Renamed(sym: Symbol, ownerRename: String)

/**
* Rename symbol itself, import only.
* `Map -> ("", import java.{util => ju})`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* `Map -> ("", import java.{util => ju})`
* `Map -> ("ju.Map", no-import)`

Copy link
Member Author

Choose a reason for hiding this comment

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

No, SelfRenamed is for import only, and it won't complete anything (like ju.Map)

Comment on lines +213 to +227
case AutoImport.SelfRenamed(sym, rename) =>
s"${importName(sym.owner)}.{${sym.nameBackticked} => $rename}"
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be unreachable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

oh. now I see that SelfRenamed is a bit different from what I expected))
It's kind of auto import for import 🤔

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.

LGTM!

|import org.eclipse.lsp4j.WorkDoneProgressCreateParams
|import java.{util => ju}
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, should we make it work the same for Scala 2 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed 6889236
I realized Scala2 renames only if the direct owner symbol (of the symbol we're completing) is the java.util.

Previously in Scala3, if we have a rename config
`<java.util> -> "ju"`, Metals always rename `<java.util>` to "ju"

For, the following code

```scala
import org.eclipse.lsp4j.services.LanguageClient

trait Client extends LanguageClient{
  over@@
}
```

we get
// ...
import java.{util => ju}

trait Client extends LanguageClient{
  override def createProgress(x$0: WorkDoneProgressCreateParams): ju.concurrent.CompletableFuture[Void] = ${0:???}
}
```

Now, Metals rename the import only if `java.util` is the direct
owner of the symbol we're completing (here, `CompletableFuture`'s direct
owner is `java.util.concurrent`, so Metals doesn't rename it).

If we complete `java.util.Map`, Metals will complete `ju.Map` and import
`import java.{util => ju}`.
@tanishiking tanishiking requested a review from tgodzik June 23, 2022 02:56
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.

Ship it! 🚢

@tanishiking tanishiking merged commit eab3df8 into scalameta:main Jun 23, 2022
@tanishiking tanishiking deleted the rename-override-completion branch June 23, 2022 08:34
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.

3 participants