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

Scala cli actionable diagnostic #4297

Merged
merged 1 commit into from
Oct 19, 2022

Conversation

lwronski
Copy link
Contributor

This PR adds support for actionable diagnostic from scala-cli. These are diagnostics that contain information that allows users to fix some configs/directives by Quick Fix actions in metals.

For now, in scala-cli there is only implemented actionable diagnostics for updating lib in using directives, and it works in this way with metals:

Screen.Recording.2022-08-24.at.19.57.06.mov

I added didSave flag to check method, because when actionable diagnostics were applied in unit tests and saved content to the file, the scala-cli reload bsp server and at the same time metals expect to get Compile Result from server but fail, because bsp server is down. I attach error logs:

2022.08.23 14:48:03 INFO  Disconnecting from scala-cli session...
2022.08.23 14:48:02 INFO  Shut down connection with build server.
2022.08.23 14:48:03 INFO  Attempting to connect to the build server...
2022.08.23 14:48:03 INFO  Running BSP server [/Users/lwronski/projects/scala-cli/out/cli/standaloneLauncher.dest/launcher, bsp, ., --actions, -S, 2.13.8]
2022.08.23 14:48:02 INFO  tracing is disabled for protocol BSP, to enable tracing of incoming and outgoing JSON messages create an empty file at /Users/lwronski/projects/metals/tests/unit/target/e2e/actionableDiagnostic/empty-string-multiline/.metals/bsp.trace.json or /Users/lwronski/Library/Caches/org.scalameta.metals/bsp.trace.json
tests.codeactions.ActionableDiagnosticsSuite:
==> X tests.codeactions.ActionableDiagnosticsSuite.empty-string-multiline  11.177s scala.meta.internal.metals.MetalsBspException: BSP connection failed in the attempt to get: CompileResult
Caused by: java.util.concurrent.ExecutionException: Boxed Exception
Caused by: java.lang.InterruptedException

To fix it, I just skip saving updating files to disk, and in this way, I avoiding to reload of bsp. I don't find any better solution to fix it in my test ActionableDiagnosticsSuite.

@kpodsiad kpodsiad requested a review from tgodzik August 25, 2022 15:05
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.

Thanks for the great work! I left a bunch of comments, but mostly minor things.

@@ -54,19 +56,31 @@ abstract class BaseCodeActionLspSuite(
changeFile: String => String = identity,
expectError: Boolean = false,
filterAction: CodeAction => Boolean = _ => true,
scalaCli: Boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you maybe add layout as a parameter? Boolean scalaCLi is not clear on what it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In BaseCodeActionLspSuite layout is created base on few parameters such as: scalacOptionsJson, scalafixConf, path, input. I'm not sure what you expect, do you want that users how to pass explicitly layout: String?

For now, I changed name from scalaCli to scalaCliLayout.

@@ -54,19 +56,31 @@ abstract class BaseCodeActionLspSuite(
changeFile: String => String = identity,
expectError: Boolean = false,
filterAction: CodeAction => Boolean = _ => true,
scalaCli: Boolean = false,
skipDiff: Boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
skipDiff: Boolean = false,
assertion: Option[(Boolean, String)],

Maybe something like this? Have separate assertion to make sure skipDiff is not used by mistake?

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 don't understand why you used Boolean there, so I changed the assertion type a bit to:

assertion:  Option[Unit => Unit]

which allows to override default assert in test

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking:

assertion.map{ case (condition, msg) => assert(condition, msg)}

This way we would steer with the types what needs to be done.

@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch 2 times, most recently from 199fed2 to df9a7cb Compare August 25, 2022 18:00
@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch 2 times, most recently from cb8bd0f to d6c2ee7 Compare October 15, 2022 10:10
@lwronski lwronski requested review from tgodzik, ckipp01 and tanishiking and removed request for tgodzik, ckipp01 and tanishiking October 15, 2022 10:34

check(
"actionable-diagnostic-update",
s"""|//> <<>>using lib "com.lihaoyi::os-lib:${oldOsLibVersion.repr}"
Copy link
Member

Choose a reason for hiding this comment

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

What's the actual diagnostic tied to this look like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[Trace - 02:46:40 PM] Received notification 'build/publishDiagnostics'
Params: {
  "textDocument": {
    "uri": "file:///private/tmp/scala-demo/Hello.scala"
  },
  "buildTarget": {
    "uri": "file:/private/tmp/scala-demo/.scala-build/?id\u003dproject_bd2c96d2de"
  },
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 0,
          "character": 15
        },
        "end": {
          "line": 0,
          "character": 40
        }
      },
      "severity": 4,
      "source": "scala-cli",
      "message": "com.lihaoyi::os-lib:0.7.8 is outdated, update to 0.8.1\n     com.lihaoyi::os-lib:0.7.8 -\u003e com.lihaoyi::os-lib:0.8.1",
      "data": {
        "range": {
          "start": {
            "line": 0,
            "character": 15
          },
          "end": {
            "line": 0,
            "character": 40
          }
        },
        "newText": "com.lihaoyi::os-lib:0.8.1"
      }
    }
  ],
  "reset": false
}

}

object ActionableDiagnostic {
def title = "Apply suggestion"
Copy link
Member

Choose a reason for hiding this comment

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

I commented on it in the tests, but the diagnostic message mention what the applied suggestion will be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now no, so I updated it and use AnnotatedTextEdit to send some more additional message about the applied suggestion. I will update this PR after release scala-cli to 0.1.17.

For dependency actionable diagnostic - in field AnnotatedTextEdit. annotationId I set value to: update dependency to 0.8.1.

Screenshot 2022-10-17 at 11 55 41

Copy link
Contributor

Choose a reason for hiding this comment

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

Annotated text edit seems a bit more complex and might not be supported by all the clients, what about doing:

Fix: <diagnostic message> truncated to a sensible length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, so I dropped the idea of AnnotatedTextEdit. So now, I use the first line from diagnostic message to set title of codeAction, here is the result:
Screenshot 2022-10-18 at 14 38 06

}

object ActionableDiagnostic {
def title = "Apply suggestion"
Copy link
Contributor

Choose a reason for hiding this comment

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

Annotated text edit seems a bit more complex and might not be supported by all the clients, what about doing:

Fix: <diagnostic message> truncated to a sensible length?

@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch 2 times, most recently from 7d2fdac to ba4118d Compare October 18, 2022 12:36
@lwronski lwronski requested a review from tgodzik October 18, 2022 12:53
@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch from ba4118d to b1686e2 Compare October 18, 2022 13:54
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!

@lwronski lwronski requested review from kpodsiad and removed request for kpodsiad October 18, 2022 14:08
@lwronski lwronski requested a review from ckipp01 October 18, 2022 14:08
Copy link
Member

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

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

Great work @lwronski. Excited to see more things like this.

@lwronski lwronski requested a review from kpodsiad October 18, 2022 17:55
@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch 2 times, most recently from 0467db0 to 0791d85 Compare October 18, 2022 19:59
@lwronski lwronski requested a review from kpodsiad October 18, 2022 20:09
@lwronski lwronski force-pushed the scala-cli-actionable-diagnostic branch from 0791d85 to a2c1300 Compare October 19, 2022 12:41
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.

5 participants