Lecture: Working With IntelliJ IDEA

For programmers who would like to use IntelliJ IDEA with this course.

You need to watch the first lecture of this course, and to install the software described in that lecture before attempting to follow these instructions. You need the IDEA 12 Ultimate Edition to work with the projects provided with this course. JetBrains offers a 30 day trial for IDEA Ultimate. This lecture was produced with IDEA 12.1.3.

A word about the Scala compiler - scalac has been packaged many ways, and IDEA currently provides most of them. Regardless of how scalac is packaged, it needs a lot more memory than the Java compiler. In particular, large projects, or projects that use implicits heavily require more memory. The instructions below are intended to guide you through the most appropriate way of configuring the Scala compiler options. Some of the options currently available will be removed in the next major version of IDEA - this is good because the options that are being removed are not as effective as the newer options that have recently been added. The following instructions only discuss how to work with the Scala compiler configuration options that are expected to remain going forward. This lecture will be updated whenever configuration settings for Scala change with new releases of IDEA.

Configuring IDEA

Under Mac and Linux, and Windows with Cygwin, you can launch IDEA like the following. Be sure that JAVA_HOME is set. Of course, the directory you installed into will likely be different:

/opt/idea-IU-129.161/bin/idea.sh > /dev/null 2>&1 &

When IDEA starts for the first time, it will run the Initial Configuration Wizard, and ask for the plugins that you want to enable. I have not noticed issues with having lots of unnecessary plugins enabled, and you can disable plugins easily at any time, so you should press the Skip button and move on.

  1. When IDEA finally presents the Welcome panel, you will see an entry labeled Configure. Click on that button.
  2. On the Configure panel click on Plugins. This is where you can download and install the Scala and sbt plugins.
  3. If you are not using Play Framework 1.0, disable the Playframework support plugin.
  4. Click on the Browse Repositories... button and scroll down until you see Scala and SBT. You could also click on Install JetBrains plugin..., which would not display the 3rd party SBT plugin.
  5. Highlight both plugins and right-click, so you see Download and Install. If you are using Play 2.0, also install the Play 2.0 Support plugin (from the JetBrains repository) and the RemoteCall plugin (from the third party repository) and wait for the plugins to download. I do not recommend that you run plugin downloads in the background.
  6. Close the Browse Repositories window.
  7. Click OK.
  8. Allow IntelliJ IDEA to restart.
  9. Verify that the plugins were installed.
  10. If you are running Ubuntu Linux, click on Configure / Create Desktop Entry.
  11. You need to define a default project JDK before you can configure Scala. To do that:
    1. Click on Project Defaults / Project Structure / Project
    2. Click the New... button, then select JDK. Browse to your JDK. For Ubuntu Linux, this will be under /usr/lib/jvm.
    3. Click OK and then click on the back arrow.
  12. Project defaults / Project Structure is where you specify the default JDK and Java language level.
    1. I selected JDK 1.7 and language level 7.0 Diamonds, ARM, multi-catch, etc.. No need to specify anything else, just click OK.
  13. Back up to the Configure menu, and click on Settings.
  14. Template Project Settings - these settings can also be applied to an opened project by selecting File / Project Structure from an open project.
    1. Compiler
      1. Ensure that Use external build is set, which is the default.
      2. It is safest (but a bit slower) to enable Clear output directory on rebuild.
      3. You should enable Compile independent modules in parallel.
      4. The default Compiler process heap size (Mbytes) of 700MB is for non-Scala compilers. This memory is allocated on first use, so you can ignore this setting.
      5. Scala Compiler - nothing to configure in external build mode.
    2. Inspections
      1. I turn these warnings off:
        1. HTML / File reference problems (so Play templates do not have a lot of complaints over missing files)

        2. Scala / Method signature / Method with Unit result type defined like function

        3. Scala / Method signature / Method with Unit result type defined with equals sign

      2. I turned these warnings on:

        1. Scala / General / Relative import

    3. Scala
      1. Imports
        1. I set Class count to use import with '_': to 9999 because I like explicit imports.
        2. Set Add full qualified imports.
        3. Unset Import the shortest path for ambiguous references because that setting can get you into trouble when you optimize imports.
      2. Worksheet:
        1. Evaluation results length before line break: I set this to 120.
        2. Output cutoff limit: I set this to 100
    4. SBT:
      1. VM parameters can be whatever you want. Setting Xmx and XX:MaxPermSize will help compile large projects faster. My settings are:
        -Xmx2536M -XX:MaxPermSize=512M
    5. ​Version Control: Unset Notify about VCS root errors
  15. IDE Settings
    1. Appearance:

      1. Enable Show line numbers

      2. Change the Theme to Darcula. IDEA will restart.

      3. If you are used to the Eclipse IDE's shortcut keys, you can use them with IDEA by setting them from File / Settings / Keymap. Select the predefined Eclipse keymap.

    2. Editor - uncheck Allow placement of caret after end of line.

      1. Appearance - enable Show method separators so horizontal lines are drawn between methods
      2. ​Colors & Fonts select Scheme name Darcula
      3. If your monitor has a 16:9 or 16:10 aspect ratio, you may want to maximize vertical dimension for your editor. In that case, set Editor Tab Appearance / Placement to Left or Right. You can also reclaim some vertical space by disabling the toolbar by right-clicking on it and deselecting Show Toolbar. You can re-enable the toolbar with the View / Toolbar menu item.
    3. Scala
      1. Enable Run compile server (in external build mode), which keeps the compiler 'warm' between compilations, thereby reducing the compile time.
      2. Make sure the JVM SDK points to a valid JDK.
      3. JVM maximum heap size, MB: This memory is used to convert the IDEA project model to something compatible with the Zinc server, and to parse error messages from the compiler. The lifespan of this memory allocation is only during a compilation, and should not need to be adjusted.
  16. Click OK

  17. Back arrrow, you now see the Configure menu

Tuning Memory Allocation

You can use jvisualvm, provided with the Java Development Kit, to measure memory usage of the IDE, so you can optimize the memory settings. The VisualVM site has many articles that discuss how to use the tool. I suggest that you do not use the VisualVM IDE plugin to measure the IDE that it runs from, and instead run the tool standalone!

In the IDEA installation directory, edit bin/idea.vmoptions. My development machine has 32 GB RAM, and I use that memory to run several virtual machines simultaneously. Each VM has 7 GB RAM. Here are my settings:

-Xms1128m
-Xmx2512m
-XX:MaxPermSize=500m
-XX:ReservedCodeCacheSize=164m
-XX:+UseCodeCacheFlushing
-ea
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true

Converting SBT Projects Into IntelliJ Projects

At the command line, navigate to the oocomp_code directory. It contains two directories, called courseNotes and assignment.

To export the courseNotes sbt project to IDEA, change to the courseNotes directory and run this command:

sbt gen-idea

If this is the first time you run sbt with this project, you will have to wait several minutes while many dependencies are downloaded. Eventually you will see something like:

[info] Loading global plugins from C:\Users\Mike Slinn\.sbt\plugins
[info] Loading project definition from C:\scalaJavaInterop\ooCompat\courseNotes\project
[info] Set current project to scalaJavaOOCompatCourse (in build 
file:/C:/scalaJavaInterop/ooCompat/courseNotes/)
[info] Trying to create an Idea module scalaJavaOOCompatCourse
[info] Excluding folder target
[info] Created C:\scalaJavaInterop\ooCompat\courseNotes/.idea/IdeaProject.iml
[info] Created C:\scalaJavaInterop\ooCompat\courseNotes\.idea
[info] Excluding folder C:\scalaJavaInterop\ooCompat\courseNotes\target
[info] Created C:\scalaJavaInterop\ooCompat\courseNotes\.idea_modules/scalaJavaOOCompatCourse.iml
[info] Created 
C:\scalaJavaInterop\ooCompat\courseNotes\.idea_modules/scalaJavaOOCompatCourse-build.iml

Now change to the the assignment directory and run the same command:

sbt gen-idea

Again, output should look something like:

[info] Loading global plugins from C:\Users\Mike Slinn\.sbt\plugins
[info] Loading project definition from C:\scalaJavaInterop\ooCompat\assignment\project
[info] Set current project to scalaJavaOOCompatAssignment (in build 
file:/C:/scalaJavaInterop/ooCompat/assignment/)
[info] Trying to create an Idea module scalaJavaOOCompatAssignment
[info] Excluding folder target
[info] Created C:\scalaJavaInterop\ooCompat\assignment/.idea/IdeaProject.iml
[info] Created C:\scalaJavaInterop\ooCompat\assignment\.idea
[info] Excluding folder C:\scalaJavaInterop\ooCompat\assignment\target
[info] Created 
C:\scalaJavaInterop\ooCompat\assignment\.idea_modules/scalaJavaOOCompatAssignment.iml
[info] Created 
C:\scalaJavaInterop\ooCompat\assignment\.idea_modules/scalaJavaOOCompatAssignment-build.iml

The gen-idea command creates two directories, named .idea and .idea_modules. Both of these directories should be mentioned in the .gitignore file. You can examine the .gitignore files provided with the projects for this course.

When finished, go back to IDEA and File / Open the directory. The Scala project should load into IDEA. We can now finish setting up IDEA. You will notice a small rotating icon at the bottom of the IDEA window, and the word Indexing... next to it. The first time IDEA encounters a new jar, such as found with a new JDK, it will index the jar. This can take a while, and the process might slow down your computer noticeably.

Configuring IDEA Scala Projects

There is a vertical button at the upper left of the IDEA window, labeled Project. Click on that button and you will see the files and directories that comprise your project. The top two directories are the .idea* directories; normally you should not need to look inside. The project directory contains the sbt project files and directories. The src directory contains your source code, including Java and Scala code, and can also include resources. The External libraries directory is presented to you by IDEA so you can browse the project dependencies. These files actually reside in ~/.ivy2. Note that there are two versions of the Scala compiler: 2.9.x and 2.10.x. The build system still uses 2.9.x but your code is compiled with Scala 2.10.1.

We need to configure the project structure before we can compile. File / Project structure opens a new window, and the Project SDK is probably invalid. You need to tell IDEA where your Java compiler is, so click the New... button, select JDK and navigate to the directory that you installed the JDK. Click the Apply button at the lower right of the window. We are done with Project Settings.

Building and Running with IDEA

The menu item Build / Rebuild Project does not do a proper clean. If you really want to do a clean build, go to the command line, and type sbt clean, or type play clean for a Play Framework web application. While IDEA builds, you will see the status line at the bottom of the screen shows all the actions performed. The lower left of the screen displays the information that the FSC build server is being used, with v2.10.1 of the Scala compiler. Now let's run the program.

The easy way to run the program is to right-click on a Java file that contains a main() method or a Scala file that defines an object that extends App. For example, let's run the Scala console application in courseNotes/src/main/scala/com/micronautics/scalaJava/ScalaJava.scala. To do that, right-click on the file You will see three green icons, for running or debugging the program. When you use one of these menu items, a run/debug configuration is automatically created that you can edit. The Run or Debug console appears when you run the program, and output appears there.

The configuration file can be written to disk by clicking share, then OK. This creates a file in .idea/runConfigurations that you can save to git if you like:

git add -fA .idea/runConfigurations

Helpful Hints

You may find that the editor does not parse complex Scala code or Play templates properly. If this happens you will see lots of good code highlighted as errors on the screen, and cutting and pasting may work improperly. You can temporarily toggle the Scala for the editor parser off and on by clicking in the small [T] symbol at the bottom right of the screen, in the status bar area. When Scala parsing is disabled, the symbol displays as [_].

Useful IntelliJ Hot Keys

If you have set up Eclipse keyboard shortcuts as described, the following extra keyboard shortcuts are specific to IntelliJ IDEA. The Key Promoter and Shortcut Keys List plugins are helpful for learning IntelliJ keyboard hot keys.

Alt + = Shows the inferred type of the highlighted variable or expression.

Ctrl + hover Shows summary of all kinds of useful information about artifact under cursor.

Ctrl + Space Code completion.

Ctrl + B Rebuild project.

Ctrl + Shift + A Find Action (learn key bindings, or do unbound actions)

Ctrl + Alt + V Create a variable from an expression.

Ctrl + Shift + B Toggle breakpoint on current line.

Alt + Shift + K Show default IntelliJ IDEA key bindings, requires Shortcut Keys List plugin.

Ctrl + Shift + J Join lines.

Ctrl + Shift + V Past from 5 most recent copies.

Hot Keys From Eclipse Key Bindings

The following are some of the keys defined when you enable Eclipse key bindings.

Ctrl + D Delete line.

Ctrl + E and Ctrl + Shift + E Show list of recent files.

Ctrl + F Highlight all occurrences of selected text, and optionally search in current directory.

Ctrl + H Find in project, or if a directory is highlighted in the project pane, restrict search to that subdirectory tree.

Ctrl + Alt + B Pop up list of overrides or implementations, or go to implementation if there is only one.

Ctrl + / Comment / uncomment current line (toggle).

Ctrl + Shift + / Comment / uncomment current selection (toggle).

F2 or Ctrl + Shift + Space Show type or method signature of method under cursor.

Ctrl + Shift + F Reformat file or selected code.

Alt + Shift + R Rename artifact under cursor.

Ctrl + Shift + R List matching files anywhere in project, optionally open one. Can also specify a filter

Price Free
Signed up TODO