Skip to content

Commit

Permalink
feat: capture and forward diagnosticCode
Browse files Browse the repository at this point in the history
This relates to the grand plan of
scala/scala3#14904 and recently forwarding
the `diagnosticCode` has been merged in
scala/scala3#15565 and also backported so it
should show up in the 3.2.x series. While this pr isn't super exciting,
it's just making sure we capture the code and forward it, this should
unlock _much_ better ways to determine what code actions are available
for a given diagnostic. Meaning we don't have to do lovely things like
regex on the diagnostic message for Scala 3 diagnostics.

NOTE: that this does need some more changes in the build servers before
this is usable. So we can wait for those to be merged in if you'd like.

- [ ] sbt - sbt/sbt#6998
- [ ] Bloop - scalacenter/bloop#1750
- [ ] Mill - com-lihaoyi/mill#1912

Now if you look at the trace file for a diagnostic you'll see the
addition of the code:

```
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 9,
          "character": 15
        },
        "end": {
          "line": 9,
          "character": 19
        }
      },
      "severity": 1,
      "code": "7",
      "source": "sbt",
      "message": "Found:    (\u001b[32m\"hi\"\u001b[0m : String)\nRequired: Int\n\nThe following import might make progress towards fixing the problem:\n\n  import sourcecode.Text.generate\n\n"
    }
  ],
```

Refs: scala/scala3#14904
  • Loading branch information
ckipp01 committed Oct 24, 2022
1 parent adadc11 commit 980d444
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ final class Diagnostics(
d.getSeverity,
d.getSource,
)
if (d.getCode() != null) ld.setCode(d.getCode())
ld.setData(d.getData)
ld
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,10 @@ object MetalsEnrichments
fansi.Str(diag.getMessage, ErrorMode.Strip).plainText,
diag.getSeverity.toLsp,
if (diag.getSource == null) "scalac" else diag.getSource,
// We omit diag.getCode since Bloop's BSP implementation uses 'code' with different semantics
// than LSP. See https://github.com/scalacenter/bloop/issues/1134 for details
)
if (diag.getCode() != null) {
ld.setCode(diag.getCode())
}
ld.setData(diag.getData)
ld
}
Expand Down

0 comments on commit 980d444

Please sign in to comment.