This getting started guide is intended for those who want to start working with the Scala IntelliJ IDEA plugin. Hope it gives you a good kick to start exploring the plugin for yourself, and, of course, reading this space - you can find more useful information about other helpful plugin features, like Java/Scala cross-language interoperability which includes refactorings, coding assistance and much more.
In this guide:
- One of the latest IntelliJ IDEA EAP builds (can be downloaded from IntelliJ IDEA EAP page).
- Scala SDK should be downloaded and installed on your computer. This can be done in one of the following ways.
- Use the IzPack Installer, which is suitable for all platforms. In this case you don't need to download API documentation.
- Download Scala SDK archive for your specific platform, Scala API documentation and sample, unzip archives to the locations of your choice, and configure environment variables
Note that after configuring environment variables, you have to re-login in order for environment variables to take effect.
Setting up the Environment
- Run IntelliJ IDEA. On the Welcome Screen, click Open Plugin Manager.
- Click Available tab Press Browse repositories button and type Scala in the Search box to quickly locate plugin in the list.
Image Removed Image Added
- Right-click plugin name, select Download and Install from the menu, and restart IntelliJ IDEA to apply the changes.
- After IntelliJ IDEA is back, open Plugin Manager again to examine the list of installed plugins. If everything is OK, there should be Scala plugin, listed in black, with a tick mark next to its name. All this means the plugin is installed, enabled and is working OK. If there's no tick mark, or plugin name is listed in red, or you can't find it at all - there may be some problem you need to address, like getting the latest IntelliJ IDEA EAP version.
There is an alternative way to install the Scala plugin.Download the Scala plugin from the Nightly Builds for Leda page and unzip the archive to the directory <user home>/.IntelliJIdea<xx>/config/plugins. After that, run IntelliJ IDEA.
- If you plan to create other projects that involve this library, select Global - thus you will be able to share the library anywhere on the current machine.
- If you select Project level, then all the jar archives will be included in your project. This makes your project portable between the different computers and independent from the Scala SDK.
To significantly speed-up project compilation you may configure the Fast Scala Compiler (FSC).
Exploring Project Structure
Next, pay attention to the Scala facet added to our module:
Image RemovedImage Added
As you see, IntelliJ IDEA has automatically specified the Scala compiler library, and added it to the list of module dependencies. If you want to learn more about project configuration, refer to http://devnet.jetbrains.net/thread/290032.
- Right-click src folder (or press Alt-Ins in default Windows IntelliJ IDEA keymap) and select Package. Type the package name helloworld. Then, right-click the newly created package and choose Scala Class on the context menu:
- In the dialog box that opens, select the type of the Scala entity to be created - in our case, let it be Scala Object, type HelloWorld as the object name, and click OK. Here is our first Scala object.
You can skip first step. Instead of HelloWorld you can type helloworld.HelloWorld then appropriate package will be created automatically.
To choose 'Object' in the combobox you can use up and down arrows on your keyboard, just to do it slightly faster.
- Now, we're going to make it runnable. There're several ways to achieve that. First off, we can extend the default scala.Application trait. With IntelliJ IDEA advanced code completion (in this case, the Scala Class Names) we App trait. Just type App and space, you even don't have to type everything by ourselves. App plus Ctrl+Space gives you neat a and quick choice need to use code completion, which will work automatically (see the picture below).
Image Removed Image Added
Or, we can use a more Java-like approach: create main() method inside of object body. Again, IntelliJ IDEA lends us a hand with Live Templates. All we do is type main and press Tab (or press Ctrl+J in the default Windows IntelliJ IDEA keymap, and select main from the suggestion list). IntelliJ IDEA expands this macro to a complete well-formed main() method body.
- Let's teach the application to do some work. If you're familiar with IntelliJ IDEA, you should probably be aware of that running a simple Java application is just a matter of pressing Ctrl+Shift+F10 (on default Windows keymap). Good news is that it works equally well for Scala applications.
All we have to do is to type the code that displays the trivial Hello World message and then press Ctrl+Shift+F10 - or right-click the editor background and choose Run HelloWorld.main on the context menu. The application output is displayed in the console.
You can also turn your Scala object into a script. Just remove all declarations from the file, leave the executable statement only - println("Hello, World!"), and run the script as described above.