# IntelliJ IDEA

An Integrated Development Environment (IDE) is custom software designed to support all aspects of software development. It typically includes a code editor, debugging, testing and profiling tools, and anything else that might be needed to support your workflow.

While you certainly can use command-line tools to build applications, it's strongly encouraged to use an IDE which provides a large number of advanced features (e.g. debugging, profiling support, auto-completion in code, integration with other systems and so on).

In this course, we’re going to use IntelliJ IDEA, an IDE produces by JetBrains (the company that invented Kotlin), which provides all development functionality, and integrates with all of our tools.

Anything we can do to reduce the friction of common software activities is worth pursuing. This is why we like Integrated Development Environment (IDE)s like IntelliJ -- they support a range of common activities:

  • Producing new code: tools for navigating existing classes, maybe filling in code snippets for us.
  • Reading existing code: browsing classes, maybe diagramming the system to allow us to build a mental model of an existing system.
  • Refactoring: the ability to produce a series of possibly sweeping changes without breaking existing code. e.g. renaming a method, and everywhere it's invoked; extracting an interface from an existing set of classes.
  • Debugging: visualizations and other tools designed to help diagnose.
  • Profiling: tools to help us understand performance and runtime behaviour.

# Installation

IntelliJ IDEA can be downloaded from https://www.jetbrains.com/intellij

There are two versions of IntelliJ IDEA: the Community Edition, which is open source, and the Ultimate Edition, which is more capable. JetBrains offers free educational licenses to students, which includes a license for IntelliJ IDEA Ultimate if you wish. Either one will work for this course.

Make sure to install the version appropriate for your system architecture (e.g. x86 for an Intel processor, or ARM for Apple M1/M2 processors).

IntelliJ will attempt to use the Java JDK that you installed previously. If intelliJ is unable to locate a suitable JDK, it may complain that it cannot locate a Project JDK (i.e. Java JDK). To fix this, click on Setup SDK and enter details for the JDK that you installed (i.e. where it is installed).

# Creating a Project

IntelliJ fully supports Gradle, and we can create a new project directly in the IDE.

From the Splash screen, select Create New Project.

New Project Wizard
New Project Wizard

You will need to supply project parameters.

  • Kotlin as your programming language.,
  • Gradle for your build system.
    • Gradle can use either Groovy or Kotlin as a DSL.
    • Either is fine, though course examples are mostly in Groovy (they were created before Kotlin was widely supported).
  • JDK should point to your installation.

If successful, IntelliJ IDEA will open into the main window to an empty project.

Empty Project Window
Empty Project Window

After the project has loaded, use View > Tools> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

# Navigating Projects

IntelliJ has a number of windows that it will display by default. You can toggle project windows.

  • Project: a list of all files (Cmd+1).
  • Structure: methods and properties of the current open class/source file (Cmd+7).
  • Source: the current source files (no hotkey).
  • Git: Git status and log (Cmd+9) - not shown.
  • Gradle: tasks that are available to run (no hotkey) - not shown.

# Producing New Code

Feature What it does Activate
Live templates Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements. Tab
Implement methods of an interface or abstract class Inserts stubs for required methods, with the correct method signature and return types. Ctrl+I
Override methods of a superclass Creates an override method that contains a call to the method of the superclass. Ctrl+O
Code completion Helps you complete the names of classes, methods, fields, and keywords within the visibility scope. Ctrl+Space

# Running Sample Code

We maintain a public Git repository of the source code shown in lectures. To get a copy, git clone the repository URL. This command, for instance, would create a working copy of the slides and sample code in a directory named cs346-slides.

$ git clone https://git.uwaterloo.ca/cs346/public/1241/slides cs346-slides

Each sub-folder contains a project built using Gradle and IntelliJ, which should be runnable either from the command-line or from the IDE using Gradle.

You can build and execute these projects directly in IntelliJ:

  • File -> Open and navigate to the top-level directory containing the build.gradle.kts file. Do NOT open a specific file, just the directory. Click Ok.
  • After the project has loaded, use View -> Tools -> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

IntelliJ with Gradle window open
IntelliJ with Gradle window open

# Reading Code

IntelliJ can generate UML diagrams from existing code. These diagrams will reflect the structure of actual classes and methods in your application.

The documentation contains a section on source code navigation that is worth reading carefully.

  • To navigate backwards, press Cmd+[. To navigate forward, press Cmd+].
  • To navigate to the last edited location, press Shift+Cmd+Backspace.
  • To move caret between matching code block braces, press Ctrl+M.
  • To move the caret to the next word or the previous word, press Alt+Right or Alt+Left.

There are also built-in dialogues that help you navigate through existing code.

Feature What it does Hotkey
Show recent locations Show the files and sections that have been viewed recently in a dialog, where you can quickly move between them. Shift+Cmd+E
Show type hierarchy Type hierarchies show parent and child classes of a class. Ctrl+H
Show method hierarchy Method hierarchies show subclasses where the method overrides the selected one as well as superclasses or interfaces where the selected method gets overridden. Shift+Cmd+H