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.
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.
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!