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

Add convertToNamedParameters support for Scala 3 #4131

Merged
merged 7 commits into from
Jul 18, 2022

Conversation

jkciesluk
Copy link
Member

@jkciesluk jkciesluk commented Jul 11, 2022

Closes #4059
Add Scala 3 support for #3971

Copy link
Member

@tanishiking tanishiking 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 for working on this @jkciesluk !

This implementation converts all Apply to named arguments instead of selected Apply, we have to convert only the selected one (in the following case, we should convert only the arguments of foo method).

named-convert

We should add unit test that has multiple Apply


Also, we would need to copy some test suite from https://github.com/scalameta/metals/blob/ec89eac187c5c9e1358a8229ebac85b64a71a8a9/tests/unit/src/test/scala/tests/codeactions/ConvertToNamedArgumentsLspSuite.scala to https://github.com/scalameta/metals/blob/ec89eac187c5c9e1358a8229ebac85b64a71a8a9/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala (I'm wondering we can cross run those LSP test 🤔 but not this time)

.map(ScalaVersions.isScala3Version)
.getOrElse(false)
if (isScala3) Future.successful(Nil)
else {
Copy link
Member

Choose a reason for hiding this comment

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

👍

.foreach(result.addOne(_))
case t =>
traverseChildren(t)
traverser.traverse(tree)(using newctx)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of traversing the whole tree, it would be useful to check the enclosing tree (of the given position (via param)) using Interactive.pathTo or Interactive.enclosingTree

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi, thank you for help!
Using pathTo works. I've also copied tests to https://github.com/scalameta/metals/blob/ec89eac187c5c9e1358a8229ebac85b64a71a8a9/tests/slow/src/test/scala/tests/feature/Scala3CodeActionLspSuite.scala and extended them by testing multiple Apply.

Copy link
Member

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

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

LGTM!

@tanishiking
Copy link
Member

You may have to rebase on main and run scalafmt for #4050

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! Just very minor remarks about the tests

@@ -328,6 +338,264 @@ class Scala3CodeActionLspSuite
expectNoDiagnostics = false,
)

check(
Copy link
Contributor

Choose a reason for hiding this comment

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

We can really just add one test (first one looks good), since we only want to test the branch when we have Scala 3 dialect. All the other cases are already handled in the ConvertToNamedArgumentsLspSuite.

Anything specific to Scala 3 we can test in cross suite.

The reason is for this comment is that these tests each can last up to a couple of seconds and it's harsh on out CI.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right.

All the other cases are already handled in the ConvertToNamedArgumentsLspSuite.
ConvertToNamedArgumentsLspSuite test against Scala 2.13, it doesn't verify anything against Scala 3.

Actually, those tests that verify the behavior (rather than the first one) should be in PC tests instead of xxxLSPSuite.
Sorry, we should did this in #3971

For example, Implementing all abstract member code action
have two (three?) test suite

  • cross test in pc (presentation compiler)
    • class AutoImplementAbstractMembersSuite extends BaseCodeActionSuite {
    • These tests verify the behavior of code action, but it doesn't check the code action is available or not. Those test anyway invoke the code action even if the user won't see it's available.
    • These tests run against all Scala versions
    • You can run those tests with the specific version by ++3.1.3 cross/testOnly tests.pc.AutoImplementAbstractMemberSuite.
  • ImplementAbstractMemberLspSuite
  • Scala3CodeActionLspSuite
    • check(
      "given-object-creation",
      """|package a
      |
      |trait Foo:
      | def foo(x: Int): Int
      | def bar(x: String): String
      |
      |given <<Foo>> with {}
      |""".stripMargin,
      s"""|${ImplementAbstractMembers.title}
      |""".stripMargin,
      """|package a
      |
      |trait Foo:
      | def foo(x: Int): Int
      | def bar(x: String): String
      |
      |given Foo with {
      |
      | override def foo(x: Int): Int = ???
      |
      | override def bar(x: String): String = ???
      |
      |}
      |""".stripMargin,
      )
    • This test check the code action is available in Scala3. We test this because if the Scala3 compiler has different error message than Scala2, we might need to change the regex to capture the error message for missing implementation error.
    • This test runs against only Scala3.

So, we should migrate most of ConvertToNamedArgumentsLspSuite into pc suite (that we haven't yet implemented). but it can be done in another PR :)

Copy link
Member

@kpodsiad kpodsiad Jul 15, 2022

Choose a reason for hiding this comment

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

So what is the difference between:

  • cross test in pc (presentation compiler) which have ignoreScala2 tag
  • slow/Scala3CodeActionLspSuite

I think I still don't get it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Scala3CodeActionLspSuite will run the entire server, while cross tests will only invoke the compiler, which should be much faster.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably fine to test if the workflow works for Scala 3, which confirms one new path for testing, while we can test anything specific to Scala 3 compiler in the cross tests.

import tests.codeactions.BaseCodeActionLspSuite

class Scala3CodeActionLspSuite
extends BaseCodeActionLspSuite("cross-code-actions") {

override protected val scalaVersion: String = BuildInfo.scala3
val filterAction: CodeAction => Boolean = { act: CodeAction =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? I see that we anyway ConvertToNamedArguments action showing up below?

@jkciesluk jkciesluk requested a review from tgodzik July 14, 2022 16:36
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!

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!

@tgodzik tgodzik merged commit d97fcd2 into scalameta:main Jul 18, 2022
@jkciesluk jkciesluk deleted the namedparams branch August 9, 2022 06:11
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.

Append parameter names in method for simple types for Scala3
4 participants