|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Worksheet mode with Dotty IDE" |
| 4 | +--- |
| 5 | + |
| 6 | +A worksheet is a Scala file that is evaluated on save, and the result of each |
| 7 | +expression is shown in a column to the right of your program. Worksheets are |
| 8 | +like a REPL session on steroids, and enjoy 1st class editor support: completion, |
| 9 | +hyperlinking, interactive errors-as-you-type, etc. Worksheet use the extension |
| 10 | +`.sc`. |
| 11 | + |
| 12 | +How to use the worksheets |
| 13 | +========================= |
| 14 | +The only supported client for the Worksheet mode is [Visual Studio |
| 15 | +Code](https://code.visualstudio.com/). |
| 16 | + |
| 17 | +To use the worksheets, start Dotty IDE by [following the |
| 18 | +instruction]("./ide-support.html") and create a new file `MyWorksheet.sc` and |
| 19 | +write some code: |
| 20 | + |
| 21 | +```scala |
| 22 | +val xyz = 123 |
| 23 | +println("Hello, worksheets!") |
| 24 | +456 + xyz |
| 25 | +``` |
| 26 | + |
| 27 | +On top of the buffer, the message `Run this worksheet` appears. Click it to |
| 28 | +evaluate the code of the worksheet. Each line of output is printed on the right |
| 29 | +of the expression that produced it. The worksheets run with the classes of your |
| 30 | +project and its dependencies on their classpath. |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Note that the worksheet are fully integrated with the rest of Dotty IDE: While |
| 35 | +typing, errors are shown, completions are suggested, and you can use all the |
| 36 | +other features of Dotty IDE such as go to definition, find all references, etc. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +Implementation details |
| 41 | +====================== |
| 42 | + |
| 43 | +In overview, the worksheets extend the Language Server Protocol and rely on the |
| 44 | +Dotty REPL to evaluate code. |
| 45 | + |
| 46 | +## Evaluation |
| 47 | +Each of the individual expressions and statements of the worksheet is extracted |
| 48 | +and passed to the Dotty REPL. After the REPL has finished evaluating one unit of |
| 49 | +input, it emits a special delimiter that indicates the end of the output for |
| 50 | +this input. (See `dotty.tools.languageserver.worksheet.InputStreamConsumer`) |
| 51 | + |
| 52 | +This process continues until all input has been evaluated. |
| 53 | + |
| 54 | +The Dotty REPL is run in a separate JVM. The `Evaluator` (see |
| 55 | +`dotty.tools.languageserver.worksheet.Evaluator`) will re-use a JVM if the |
| 56 | +configuration of the project hasn't changed. |
| 57 | + |
| 58 | +## Communication with the client |
| 59 | +The worksheets extend the Language Server Protocol and add one request and one |
| 60 | +notification. |
| 61 | + |
| 62 | +### Run worksheet request |
| 63 | +The worksheet run request is sent from the client to the server to request that |
| 64 | +the server runs a given worksheet and streams the result. |
| 65 | + |
| 66 | +*Request:* |
| 67 | + |
| 68 | + - method: `worksheet/run` |
| 69 | + - params: `WorksheetRunParams` defined as follows: |
| 70 | + ```typescript |
| 71 | + interface WorksheetRunParams { |
| 72 | + /** |
| 73 | + * The worksheet to evaluate. |
| 74 | + */ |
| 75 | + textDocument: VersionedTextDocumentIdentifier; |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | +*Response:* |
| 80 | + |
| 81 | + - result: `WorksheetRunResult` defined as follows: |
| 82 | + ```typescript |
| 83 | + interface WorksheetRunResult { |
| 84 | + /** |
| 85 | + * Indicates whether evaluation was successful. |
| 86 | + */ |
| 87 | + success: boolean; |
| 88 | + } |
| 89 | + ``` |
| 90 | + |
| 91 | +### Worksheet output notification |
| 92 | +The worksheet output notification is sent from the server to the client to |
| 93 | +indicate that worksheet execution has produced some output. |
| 94 | + |
| 95 | +*Notification:* |
| 96 | + |
| 97 | + - method: `worksheet/publishOutput` |
| 98 | + - params: `WorksheetRunOutput` defined as follows: |
| 99 | + ```typescript |
| 100 | + interface WorksheetRunOutput { |
| 101 | + /** |
| 102 | + * The worksheet that produced this output. |
| 103 | + */ |
| 104 | + textDocument: VersionedTextDocumentIdentifier; |
| 105 | + |
| 106 | + /** |
| 107 | + * The line number of the expression that produced this output. |
| 108 | + */ |
| 109 | + line: int; |
| 110 | + |
| 111 | + /** |
| 112 | + * The output that has been produced. |
| 113 | + */ |
| 114 | + content: string; |
| 115 | + } |
| 116 | + ``` |
0 commit comments