IntelliJ IDEA 2019.2 Help

Work with Scala worksheet and Ammonite

You can quickly evaluate and run Scala code using either a Scala worksheet or Ammonite.

Since in IntelliJ IDEA Scala worksheet and Ammonite files have the same file extension .sc, you need to let IntelliJ IDEA know how to treat such files in your project.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), go to Languages & Frameworks | Scala.

  2. On the Worksheet tab, in the Treat .sc files as field the Always Worksheet option is selected by default.

    Select Always Ammonite for the Ammonite support.

    You can also select the Ammonite in test sources, otherwise Worksheet option to treat only test files as Ammonite.

    Scala settings
  3. Save your changes.

While in the settings, you can also set the output cutoff limit, specify in what mode you want to run the worksheet or Ammonite script, or configure how you want IntelliJ IDEA to treat .sc and scratch files.

Create an .sc file

  1. Right-click your project and select New|Scala Worksheet. We recommend that you create an .sc file in the src directory to avoid problems with code highlighting.

  2. In the New Scala Worksheet window, type the name of the file and click OK.

    As a result, a file with .sc extension opens. Based on the file configuration, it is treated either as a Scala worksheet or as Ammonite.

Run Scala code using Scala worksheet

  1. Create a Scala worksheet file and open it in the editor.

    Scala worksheet

  2. Add your code.

    Besides standard actions such as code completion and string formatting, IntelliJ IDEA supports the following actions:

    • Evaluating Scala objects

    • Folding the output without affecting your code on the left side and expanding only that block of the output that matches a specific statement.

      sc wsheet unfold

  3. Click the Run button to see the results.

Work with the current worksheet settings

  1. Open a Scala worksheet file in the editor. Click the Settings icon on the toolbar of the worksheet editor to open the Worksheet settings dialog.

  2. In the Worksheet settings dialog, configure the current worksheet settings or the default worksheet settings for the whole project that will affect other worksheets.

    sc ws settings

  3. Use the following options for your configuration:
    • Interactive Mode: select this checkbox to have the results shown automatically.

    • Make project: clear this check checkbox to disable automatic checking of project's changes and thus improve evaluation performance.

    • Run type: from this list, select the appropriate evaluation mode. For example, select the REPL type to quickly evaluate Scala expressions. When you add new expressions they are executed incrementally.

    • Use class path of module: this field is disabled since the worksheet is a file that you can share between other projects and should stay where you created it. You can change its location in the default settings, but it will be applied across the whole project and will affect other worksheets.

      If you need to change a class path, we recommend that you create and use a scratch file instead.

      sc scratch file
    • Compiler profile: in this field select a compiler profile for your worksheet. Select one from the list or click the Settings icon to access the Scala compiler settings for more options.

Configure the Ammonite support in IntelliJ IDEA

  • Depending on your project, do the following:
    • If you have an sbt project, add the following code to the build.sbt file:

      libraryDependencies += { val version = scalaBinaryVersion.value match { case "2.10" => "1.0.3" case _ ⇒ "1.6.5" } "com.lihaoyi" % "ammonite" % version % "test" cross CrossVersion.full } sourceGenerators in Test += Def.task { val file = (sourceManaged in Test).value / "amm.scala" IO.write(file, """object amm extends App { ammonite.Main.main(args) }""") Seq(file) }.taskValue
      Refresh the project to import the changes.

    • If you have a regular Scala project, download Ammonite and specify the location of its executable. For example, you can modify the $path variable or specify the path in the default Ammonite run/debug configuration, in the Amm executable field.

Run Scala scripts using Ammonite

  1. Create an Ammonite file and open it in the editor.

  2. Add your code.

    Besides standard code completion and importing actions, IntelliJ IDEA supports the following Ammonite-specific actions:

    • Code completion for Ammonite-specific commands

    • References to code from other Ammonite files and code completion for them

  3. In the left gutter, click the Run icon and select Run script to view the results in the Run tool window.

    Alternatively, you can create and run an Ammonite run/debug configuration.

Last modified: 17 October 2019