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.
0_logo
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.

1_pref

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.
2_addAmm
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.
3_addIvy
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.
4_notations

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:
5_run
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!

This entry was posted in New Features. Bookmark the permalink.

4 Responses to Ammonite Support

  1. Lachlan O’Dea says:

    I’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.

  2. gcsocial says:

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

  3. Jeff says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *