Skip to content

Commit

Permalink
chore: Fixes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Dec 17, 2024
1 parent 253ff2d commit 65101dc
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 85 deletions.
132 changes: 62 additions & 70 deletions metals-docs/src/main/scala/docs/WorksheetModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,68 @@ class WorksheetModifier extends StringModifier {
reporter: Reporter,
): String = {

val (howYouSeeEvaluations, howToHover) = info match {
case "vscode" =>
"as a inlay hint at the end of the line." ->
"hover on the inlay hint to expand it."
case _ =>
"as a comment as the end of the line." -> "hover on the comment to expand."
}

s"""|## Worksheets
|
|Worksheets are a great way to explore an api, try out an idea, or code
|up an example and quickly see the evaluated expression or result. Behind
|the scenes worksheets are powered by the great work done in
|[mdoc](https://scalameta.org/mdoc/).
|
|### Getting started with Worksheets
|
|To get started with a worksheet you can either use the `metals.new-scala-file`
|command and select *Worksheet* or create a file called `*.worksheet.sc`.
|This format is important since this is what tells Metals that it's meant to be
|treated as a worksheet and not just a Scala script. Where you create the
|script also matters. If you'd like to use classes and values from your
|project, you need to make sure the worksheet is created inside of your sources next to any existing Scala files.
|directory. You can still create a worksheet in other places, but you will
|only have access to the standard library and your dependencies.
|
|### Evaluations
|
|After saving you'll see the result of the expression ${howYouSeeEvaluations}
|You may not see the full result for example if it's too long, so you are also
|able to ${howToHover}
|
|Keep in mind that you don't need to wrap your code in an `object`. In worksheets
|everything can be evaluated at the top level.
|
|### Using dependencies in worksheets
|
|You are able to include an external dependency in your worksheet by including
|it in one of the following two ways.
|
|```scala
|// $$dep.`organisation`::artifact:version` style
|import $$dep.`com.lihaoyi::scalatags:0.7.0`
|
|// $$ivy.`organisation::artifact:version` style
|import $$ivy.`com.lihaoyi::scalatags:0.7.0`
|```
|
|`::` is the same as `%%` in sbt, which will append the current Scala binary version
|to the artifact name.
|
|You can also import `scalac` options in a special `$$scalac` import like below:
|
|```scala
|import $$scalac.`-Ywarn-unused`
|```
|
|### Troubleshooting
|
|Since worksheets are not standard Scala files, you may run into issues with some constructs.
|For example, you may see an error like this:
|
|```
|value classes may not be a member of another class - mdoc
|```
|
|This means that one of the classes defined in the worksheet extends AnyVal, which is
|not currently supported. You can work around this by moving the class to a separate file or removing
|the AnyVal parent.
|""".stripMargin
"""|## Worksheets
|
|Worksheets are a great way to explore an api, try out an idea, or code
|up an example and quickly see the evaluated expression or result. Behind
|the scenes worksheets are powered by the great work done in
|[mdoc](https://scalameta.org/mdoc/).
|
|### Getting started with Worksheets
|
|To get started with a worksheet you can either use the `metals.new-scala-file`
|command and select *Worksheet* or create a file called `*.worksheet.sc`.
|This format is important since this is what tells Metals that it's meant to be
|treated as a worksheet and not just a Scala script. Where you create the
|script also matters. If you'd like to use classes and values from your
|project, you need to make sure the worksheet is created inside of your sources next to any existing Scala files.
|directory. You can still create a worksheet in other places, but you will
|only have access to the standard library and your dependencies.
|
|### Evaluations
|
|After saving you'll see the result of the expression as a inlay hint at the end of the line.
|You may not see the full result for example if it's too long, so you are also
|able to hover on the inlay hint to expand it.
|
|Keep in mind that you don't need to wrap your code in an `object`. In worksheets
|everything can be evaluated at the top level.
|
|### Using dependencies in worksheets
|
|You are able to include an external dependency in your worksheet by including
|it in one of the following two ways.
|
|```scala
|// $$dep.`organisation`::artifact:version` style
|import $$dep.`com.lihaoyi::scalatags:0.7.0`
|
|// $$ivy.`organisation::artifact:version` style
|import $$ivy.`com.lihaoyi::scalatags:0.7.0`
|```
|
|`::` is the same as `%%` in sbt, which will append the current Scala binary version
|to the artifact name.
|
|You can also import `scalac` options in a special `$$scalac` import like below:
|
|```scala
|import $$scalac.`-Ywarn-unused`
|```
|
|### Troubleshooting
|
|Since worksheets are not standard Scala files, you may run into issues with some constructs.
|For example, you may see an error like this:
|
|```
|value classes may not be a member of another class - mdoc
|```
|
|This means that one of the classes defined in the worksheet extends AnyVal, which is
|not currently supported. You can work around this by moving the class to a separate file or removing
|the AnyVal parent.
|""".stripMargin
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class InlayHintResolveProvider(
definitionProvider: DefinitionProvider,
compilers: Compilers,
)(implicit ec: ExecutionContextExecutorService, rc: ReportContext) {

def resolve(
inlayHint: InlayHint,
token: CancelToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,7 @@ abstract class MetalsLspService(
CancelTokens.future { token =>
compilers
.hover(params, token)
.map(_.map(_.toLsp()))
.map(
_.orNull
)
.map(_.map(_.toLsp()).orNull)
}
}

Expand All @@ -999,7 +996,7 @@ abstract class MetalsLspService(
compilers.inlayHints(params, token)
else Future.successful(List.empty[l.InlayHint].asJava)
worksheet <- worksheetProvider.inlayHints(
params.getTextDocument().getUri().toAbsolutePath,
params.getTextDocument().getUri().toAbsolutePathSafe,
token,
)
} yield (hints.asScala ++ worksheet).asJava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ class WorksheetProvider(
)
}

def inlayHints(
path: Option[AbsolutePath],
token: CancelToken,
): Future[List[InlayHint]] = {
path match {
case Some(path) => inlayHints(path, token)
case None => Future.successful(Nil)
}
}

def inlayHints(
path: AbsolutePath,
token: CancelToken,
Expand Down Expand Up @@ -212,11 +222,9 @@ class WorksheetProvider(
.map { stat =>
val statEnd = stat.position().toLsp.getEnd()
// Update positions so that they don't break
distance.toRevised(statEnd) match {
case Left(_) =>
case Right(right) =>
statEnd.setLine(right.startLine)
statEnd.setCharacter(right.startColumn)
distance.toRevised(statEnd).foreach { right =>
statEnd.setLine(right.startLine)
statEnd.setCharacter(right.startColumn)
}
val hint = new InlayHint(
statEnd,
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ abstract class BaseWorksheetLspSuite(
cleanWorkspace()
val cancelled = Promise[Unit]()
client.onWorkDoneProgressStart = { (message, cancelParams) =>
scribe.info(message)
if (message.startsWith("Evaluating worksheet")) {
cancelled.trySuccess(())
server.fullServer.didCancelWorkDoneProgress(cancelParams)
Expand All @@ -227,9 +226,7 @@ abstract class BaseWorksheetLspSuite(
)
_ <- server.didOpen("a/src/main/scala/Main.worksheet.sc")
_ <- cancelled.future.withTimeout(10.seconds)
_ = client.onWorkDoneProgressStart = (message, _) => {
scribe.info(message)
}
_ = client.onWorkDoneProgressStart = (_, _) => {}
_ <- server.didSave("a/src/main/scala/Main.worksheet.sc")(
_.replace("Stream", "// Stream")
)
Expand Down

0 comments on commit 65101dc

Please sign in to comment.