For programmers who would like to use IntelliJ IDEA with this course.
This video is hosted at ScalaCourses.com. If you are viewing this video from another site you probably want to read the transcript for this video because it has HTML links and images. To do that, visit http://scalacourses.com, click on the Introduction to Scala course, then the Sections and Lectures tab, then scroll down until you see the Working With IntelliJ IDEA lecture, and click on the Transcript tab.
You need to install the software mentioned in the course overview page; at a minimum, install SBT, Scala and IntelliJ IDEA. IDEA 12 or IDEA 13 can be used to work with the projects provided with this course. JetBrains offers a 30 day trial for IDEA Ultimate, but the free version should also work for Scala projects. You need the Ultimate edition in order to work with Play. This lecture was originally produced with IDEA 12.1.3 and was updated to IDEA 13.1.3. If you have an older version of IDEA then some portions of this lecture will not correspond to what you see on your screen.
Under 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-135.909/bin/idea.sh &> /dev/null &
/Applications/IntelliJ\ IDEA\ 13.app/Contents/MacOS/idea &
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.
If you import a settings file (from the Welcome to IntelliJ IDEA screen: Configure / Import settings), by default called
settings.jar, then all of the following settings could be set.
Now go back to the Configure menu and select Project Defaults, which defines defaults for new projects.
/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home. For Ubuntu Linux, this will be under
/usr/lib/jvm. For Windows you must use a 32 bit Java compiler, which defaults to a directory like
C:\Program Files (x86)\Java\jdk1.7.0_60.
Change the Theme to Darcula. IDEA will restart.
Go back to Configure / Preferences / IDE Settings
Editor - uncheck Allow placement of caret after end of line.
HTML / File reference problems (so Play templates do not have a lot of complaints over missing files)
Scala: Method signature / Method with Unit result type defined like function
I turned these warnings on:
Scala: General / Relative import
If you ever have a strange problem with IDEA when working with a project, check that the JDK settings are correct in all three places. These can get out of whack when Java automatically updates.
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
Recommendation - do not follow this process to create a scala project from IDEA, instead create an sbt project (copy
sbtTemplate to make your life easier) then convert it to IDEA using
gen-idea, described next.
1) Start IntelliJ IDEA, and click on Create New Project.
2) Give your new project a name.
|3) The project name will be used as the IntelliJ module name by default.|
|4) Click Finish and you will be presented with this query. Click OK.|
|5) After a moment the Project panel will open up. Click on the module name to open it. The top one or two directories are the
We select Scala Class even if we want to make a Scala object or a Scala case class.
|7) Give your new Scala file a name. The Scala file will have a
|8) IDEA creates a default Scala class with the name of the file.|
|9) We need a main method for our console application. This means we need to extend an
|10) Let's run the console app by right-clicking on it in the Project panel and selecting Debug Weather.main().|
|11) You are notified that an external Scala compiler is being used to compile the program. This is good. While IDEA builds, you will see the status line at the bottom of the screen shows all the actions performed.|
|12) Output appears in the Debug panel which opens up at the bottom of the IDEA window. You can click on the Debug tab to show or hide the Debug panel. You can rerun the app by clicking on the little green bug on the left side of the Debug panel.|
|13) You can edit the debug configuration by clicking on the pull-down menu at the top center of the IDEA window.|
|14) You can add arguments for the JVM and your program, as well as tweak other settings.|
|15) If you enable Share, the debug configuration is written to a file in the .idea directory. You can check it into git if you want:
$ git add -fA .idea/runConfigurations/* $ git commit -m "New run configuration" $ git push
|16) A completed program.|
IntelliJ 13 for Scala has some new options which are non-obvious. You can now open a
build.sbt file instead of running
sbt gen-idea, discussed in the next section. When you do so, you will see the following dialog box. You should always enable both options.
"Use auto-import" option turns on subsequent monitoring of relevant project configuration files (
*.sbt, ./project/**) and project refreshing on changes in those files.
Create directories ... " checkbox can be used to automatically create source and test directories (like
main/src/java) that are reported by SBT but not yet present in a project (so that Project Structure / [Module] / Sources contained no red entries.
The JetBrains Scala team indicated to me that they will soon add the means to explicitly update projects. They also told me that the checkboxes will probably be removed from the project import dialog in a future release. To modify these settings afterwards, you may access them via Project Settings / SBT.
This section discusses how to work with IntelliJ IDEA on Scala projects in a more traditional manner, instead of opening SBT projects directly. This might be required if you are using IDEA 12, or if you have a complex SBT setup.
At the command line, navigate to the
course_scala_intro_code directory. It contains two directories, called
To export the
courseNotes sbt project to IDEA, change to the
courseNotes directory and run this command:
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:\scalaCore\scalaIntro\courseNotes\project [info] Set current project to scalaJavaOOCompatCourse (in build file:/C:/scalaCore/scalaIntro/courseNotes/) [info] Trying to create an Idea module scalaJavaOOCompatCourse [info] Excluding folder target [info] Created C:\scalaCore\scalaIntro\courseNotes/.idea/IdeaProject.iml [info] Created C:\scalaCore\scalaIntro\courseNotes\.idea [info] Excluding folder C:\scalaCore\scalaIntro\courseNotes\target [info] Created C:\scalaCore\scalaIntro\courseNotes\.idea_modules/scalaJavaOOCompatCourse.iml [info] Created C:\scalaCore\scalaIntro\courseNotes\.idea_modules/scalaJavaOOCompatCourse-build.iml
Now change to the the
assignment directory and run the same command:
Again, output should look something like:
[info] Loading global plugins from C:\Users\Mike Slinn\.sbt\plugins [info] Loading project definition from C:\scalaCore\scalaIntro\assignment\project [info] Set current project to scalaIntroAssignment (in build file:/C:/scalaCore/scalaIntro/assignment/) [info] Trying to create an Idea module scalaJavaOOCompatAssignment [info] Excluding folder target [info] Created C:\scalaCore\scalaIntro\assignment/.idea/IdeaProject.iml [info] Created C:\scalaCore\scalaIntro\assignment\.idea [info] Excluding folder C:\scalaCore\scalaIntro\assignment\target [info] Created C:\scalaCore\scalaIntro\assignment\.idea_modules/scalaJavaOOCompatAssignment.iml [info] Created C:\scalaCore\scalaIntro\assignment\.idea_modules/scalaJavaOOCompatAssignment-build.iml
gen-idea command creates two directories, named
.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.
courseNotesand click Choose.
project directory contains the sbt project files and directories. 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, one for the SBT build system and one to compile your code.
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 JDK 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.
WordSearch, and right-click.
The problem is that the Library definition for the scala compiler is missing some or all of its contents. IDEA 13 is supposed to make this problem go away. Until then, here is what you need to do:
scala-library(this is the Scala run-time library, and it consists of the executable jar, source code jar and Javadoc jar)
scala2.10.2 (this is the collection of jars for running the Scala compiler)
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
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.
Shift Shift (twice in a row) - search everywhere, also shows recently edited files
Alt + 1 Toggle project view
Ctrl + E Open recently edited files
Ctrl + Shift + E Open recently changed files
Alt + Home Open navigation bar
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 + Shift + Enter Correct syntax of code
Ctrl + B Rebuild project.
Ctrl + Shift + A Find Action (learn key bindings, or perform unbound actions)
Ctrl + Alt + V Create a variable from an expression.
Ctrl + Shift + B Toggle breakpoint on current line.
Ctrl + Shift + J Join lines.
Ctrl + Shift + V Paste from 5 most recent copies.
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).
Ctrl + Shift + F Reformat file or selected code.
Alt + Shift + R Rename artifact under cursor.
Ctrl + Shift + R List matching files anywhere in project and optionally open one. Can also specify a filter.