Ammonite Support
Ammonite, developed by Li Haoyi, is a well-known toolset that lets us use Scala language for scripting purposes. It contains a modernized REPL, a replacement for the Bash system shell, a Filesystem Library, and specific notations for more productive scripting.
Even though many situations in which you would use Ammonite are intended for the command-line mode, quite a few use cases are also relevant for an IDE. After receiving some feedback from our users, we’ve decided to provide advanced support for this technology in our Scala plugin. The set of enhancements includes: support for Ammonite Notations, Run Configuration, a gutter icon for running scripts more easily, and pop-up actions for automatically importing libraries. Read on for more details and screenshots.
To demonstrate our new features, we’ll use an example of Li Haoyi’s project, which forms a blog from a set of text documents and pictures. As Ammonite is an external tool, there are some prerequisites for its usage:
- Ammonite must be installed on your OS.
- IntelliJ IDEA needs to knows where the Ammonite executable is located. You can:
- add a symlink to already accessible places like usr/bin;
- modify the $path variable; or
- specify the path to the Ammonite executable in Default Ammonite Run Configuration, by using the “Amm executable” field.
- Finally, .sc files have to be treated as Ammonite files (by default, IntelliJ IDEA treats them as Worksheets). You can define this in File | Settings | Languages & Frameworks | Scala | Worksheet.
Importing Libraries
After you’ve completed the above, IntelliJ IDEA suggests adding Ammonite standard dependencies to your project when you open an .sc file for the first time. This is required for code processing in the Editor and for navigation.
IntelliJ IDEA will download the latest version of Ammonite. Besides, if an opened file contains $ivy.
imports, IntelliJ IDEA will suggest adding them to the project as well. Note that declared libraries have to be present in local caches. If they’re missing, just run the code with these imports once and Ammonite will automatically add them to the cache.
After these imports are done, you are all set and can successfully use IDE tools to work with Ammonite code. If you open a new project, IntelliJ IDEA will suggest performing these actions one by one, as new imports appear.
Ammonite Notations, Predefs and Builtins
The editor correctly handles all Ammonite-specific code and provides appropriate highlighting and navigation.
Launching Scripts
You can run your scripts right from the Editor by using the gutter icon to the left of the code that looks like a ‘play’ button. The Scala plugin will automatically create a Run Configuration and show the output of the script in the Run Tool Window:
We would love to learn more about your experiences with Ammonite and IntelliJ IDEA. Please share with us! Feature requests, pull requests and improvement suggestions are very welcome, too! Your feedback can influence the roadmap for Ammonite support a lot. Just add a comment here or create an issue in YouTrack. Thanks!
Happy developing!
Lachlan O’Dea says:
May 7, 2018I’ve tried it and it’s very cool.
The big issue I hit is when I add the $ivy imports to the project, it only adds the libraries explicitly imported, not their dependencies. In practice I had to add the libraries manually via the “add from Maven” feature so IntelliJ knew about all the dependencies.
gcsocial says:
May 11, 2018specify the path to the Ammonite executable in Default Ammonite Run Configuration, by using the “Amm executable” field. Is this in IntelliJ? I don’t see this field in IntelliJ.
Anton Yalyshev says:
May 12, 2018Here it is: https://ibb.co/eEDATy
Scala plugin build #2018.1.9
Jeff says:
May 15, 2018I am running #2018.1.9, but it never prompts me for ammonite imports, just ivy imports, which work fine, by either clicking the link, or doing alt->return and choosing create library from package (or similar).
Would love to get the ammonite packages included. Intellij knows where my binary is (as its in the base path), worksheets->Always Ammonite is configured correctly
Thanks if anyone has the special sauce to work around this, I’d love to hear it. Thanks for the plugin!!
Dyno Fu says:
September 6, 2018if I want my script to install a package from a private maven repository, how to add the repository to Intellij so that it can find the package?
Alex Berg says:
October 5, 2018I have added some trouble-shooting info here on stack overflow (arberg):
https://stackoverflow.com/questions/48002919/how-to-get-intellij-to-recognize-imports-in-scala-script
The IntelliJ ‘add’ buttons fails silently in different situations such as
* Scala/JDK isn’t yet loaded in project
* No Ammonite release exists for the given scala version. At the time of writing the current stable Ammonite release 1.2.1 is not released for Scala 2.12.7
Alex Berg says:
October 5, 2018Here’s the ammonite bug-report regarding this: https://github.com/lihaoyi/Ammonite/issues/879
Stian Lågstad says:
October 23, 2018Is there a way to debug .sc scripts when recognized as ammonite scripts? I’d like to use the IntelliJ debugger as I normally do, with breakpoints and evaluations.
Anton Yalyshev says:
October 23, 2018Unfortunately there is no straight way to debug Ammonite script by IntelliJ tools.
You may create and promote a feature request at https://youtrack.jetbrains.com/issues/scl or vote for https://youtrack.jetbrains.com/issue/SCL-12530
Also, Ammonite 1.0.0 provides Script Debug REPL (http://ammonite.io/#ScriptDebugREPL). Maybe it will be helpful for your case.
david perez says:
November 6, 2018How to force IDEA to reprocess the $ivy imports that my script contains
david perez says:
November 6, 2018How to force IDEA to reprocess the $ivy imports that my script contains?
Janbo says:
March 16, 2019Is there any way to run this on Windows 10
Leonid Ilyevsky says:
October 2, 2019One minor but annoying issue: when I use the build dependencies import in the mill build script build.sc, the imported package definition between the back quotes is highlighted red.
For example:
$ivy.`com.typesafe:config:1.3.4`
The error says “Cannot resolve symbol `com.typesafe:config:1.3.4`”.