Child pages
  • Getting Started with IntelliJ IDEA Scala Plugin

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

For now Scala plugin for IntelliJ IDEA is in a stage of active development. Its last versions are available for IntelliJ IDEA EAP. So, to start, first let's download last EAP

Wiki Markup
{redirect:https://www.jetbrains.com/idea/help/creating-and-running-your-scala-application.html|delay=0}

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:

Table of Contents

Before You Begin

Currently, IntelliJ IDEA Scala plugin is in beta stage and is being actively developed. Here is the list of what you will need to try it:

...

In plugin manager window we choose Scala plugin. One could use search line to simplify this process. Clicking with right mouse button on it, we install it and restart IDEA to apply changes (we have to start it manually after shutdown).
Image Removed
 
After stratup we can notice that Scala plugin appeared in the list of installed plugins. So, now we are ready to create our first Scala project in IntelliJ IDEA.  Click Create New Project on Welcome page. Choose Create project from scratch. Let first module will be Java Module.
 
Image Removed
 
In the list of desired technologies mark Scala checkbox and click to button with points to adjust our Scala SDK for compilation and using Scala libraries. Here we should point to installation directory of Scala programming language as follows.

 
Image Removed
 
Click finish and IDEA asks, what type of library we want to create - Application-level or Project level one? Main difference is that project library settings will be saved in project file, which may be useful to share same settings between several developers. Application-level library will be visible to attach for all projects on current machine.

Note: In near-term future we're going to make configuration process more flexible to not gather all scala-related jars in all library. It's crucial moment for projects like liftweb applications, which being inported from Maven repository may contain Scala jars separated by various libraries.
So, we just adjusted all necessary details to start writing our HelloWorld application. To create it right-click (or press Alt-Ins in defaul Windows IDEA keymap) on src folder and choose Scala Object.
 
Image Removed
 
 Type name of our object, namely "HelloWorld" in a name dialog, and here is our first Scala object. There are several ways to make it runnable. The simplest one is to inherit it form defauls scala.Application trait and put all appropriate logic to its body. We don't have to type all "Application" name. Just type several first letters and press Ctrl-Space. Class name completion will do all remaining work for us.
 
Image Removed
 
Another, more  java like way is to create well-known main() method inside of object body. To simplify this and several others copmmon operations Scala plugin for IntelliJ IDEA provides useful live templates. For instance, to create new well-formed main() maethod just type main and press Tab.
 
Image Removed
 
Now we can put somethind to our main method body and run application. Users already familiar with IDEA know, that to run simple application it's enough just press Ctrl-Shift-F10 (in Windows keymap). We're able to act the same way in Scala.  Notice, that now our HelloWorld object is marked with green triangle in Project view, which means, that it can be run immediately as plain Java application. Another way to run it is to create Run configuration manually and choose Application type.
 
Image Removed
 
Our first  acquaintance with Scala plugin for IntelliJ IDEA draw to a close, but don't forget to read about and try to use other features, such as Java/Scala cross-language interoperation, refactorings, smart editor actions and many others!

 

  • Scala SDK should be downloaded and installed on your computer.

Setting up the Environment

After you have downloaded, installed and configured all the prerequisites, follow this simple procedure to get the Scala plugin up and running.

  1. Run IntelliJ IDEA. On the Welcome Screen, click Open Plugin Manager.
  2. Press Browse repositories button and type Scala in the Search box to quickly locate plugin in the list.
    Image Added
  3. Right-click plugin name, select Download and Install from the menu, and restart IntelliJ IDEA to apply the changes.
  4. 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.
    Tip

    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.

Creating a Project

If everything is fine, you're ready to create the Scala project with IntelliJ IDEA.

  1. Back to the Welcome Screen, click Create New Project. The good old IntellIJ IDEA New Project wizard apperas. Here you choose to create a new project from scratch:
    Image Added
  2. All you need is a Java Module with a name that makes sense, located somewhere you can find it later without spending a whole day digging through your entire hard drive, and the src directory that will store your source code:
    Image Added   Image Added
  3. On top of that, select Scala on the Select Desired Technologies. By default all the necessary jars will be downloaded automatically, but you also may pick them from an already existing IDEA library or a secret place on your hard drive.
    Image Added
  4. It's possible to adjust the level of a library you want to create. Select Global or Project from the Level drop-down list.
  • 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.
Tip

To significantly speed-up project compilation you may configure the Fast Scala Compiler (FSC).

Exploring Project Structure

Now, when the project is ready, it is advisable to explore its settings. To do that, on the main toolbar, click Image Added. In the Project Structure dialog, click Modules, and then select the Dependencies tab:
Image Added

Next, pay attention to the Scala facet added to our module:

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

Creating Source Code

Now everything is ready for the first HelloWorld application.

  1. 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:
    Image Added
  2. 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.
    Image Added
    Tip

    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.

  3. Now, we're going to make it runnable. There're several ways to achieve that. First off, we can extend the default scala.App trait. Just type App and space, you even don't need to use code completion, which will work automatically (see the picture below).
    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.
    Image Added
  4. 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.
     
    Image Added
    Tip

    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.