= Gerrit Code Review - IntelliJ Setup

== Prerequisites
You need an installation of IntelliJ version 2016.2 or later. The latest version
might not yet be in-sync with the Bazel plugin for IntelliJ. It usually becomes
so quite quickly after new IDEA versions get released, though. It should then be
possible to use the fairly latest IntelliJ release with an updated Bazel plugin.

In addition, Java 8 must be specified on your path or via `JAVA_HOME` so that
building with Bazel via the Bazel plugin is possible.

TIP: If the synchronization of the project with the BUILD files using the Bazel
plugin fails and IntelliJ reports the error **Could not get Bazel roots**, this
indicates that the Bazel plugin couldn't find Java 8.

Bazel must be installed as described by
<<dev-bazel#installation,Building with Bazel - Installation>>.

== Installation of the Bazel plugin

. Go to *File -> Settings -> Plugins*.
. Click on *Browse Repositories*.
. Search for the plugin `IntelliJ with Bazel`.
. Install it.
. Restart IntelliJ.

== Creation of IntelliJ project

. Go to *File -> Import Bazel Project*.
. For *Use existing bazel workspace -> Workspace*, select the directory
containing the Gerrit source code.
. Choose *Import from workspace* and select the `.bazelproject` file which is
located in the top directory of the Gerrit source code.
. Adjust the path of the project data directory and the name of the project if
desired.

TIP: The project data directory can be separate from the source code. One
advantage of this is that project files don't need to be excluded from version
control.

Unfortunately, the created project seems to have a broken output path. To fix
it, please complete the following steps:

. Go to *File -> Project Structure -> Project Settings -> Modules*.
. Switch to the tab *Paths*.
. Click on *Inherit project compile output path*.
. Click on *Use module compile output path*.

== Recommended settings

=== Code style

==== google-java-format plugin
Install the `google-java-format` plugin by following these steps:

. Go to *File -> Settings -> Plugins*.
. Click on *Browse Repositories*.
. Search for the plugin `google-java-format`.
. Install it.
. Restart IntelliJ.

Every time you start IntelliJ, make sure to use *Code -> Reformat with
google-java-format* on an arbitrary line of code. This replaces the default
CodeStyleManager with a custom one. Thus, uses of *Reformat Code* either via
*Code -> Reformat Code*, keyboard shortcuts, or the commit dialog will use the
custom style defined by the `google-java-format` plugin.

==== Code style settings
The `google-java-format` plugin is the preferred way to format the code. As it
only kicks in on demand, it's also recommended to have code style settings
which help to create properly formatted code as-you-go. Those settings can't
completely mimic the format enforced by the `google-java-format` plugin but try
to be as close as possible. So before submitting code, please make sure to run
*Reformat Code*.

. Download
https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml[
intellij-java-google-style.xml].
. Go to *File -> Settings -> Editor -> Code Style*.
. Click on *Manage*.
. Click on *Import*.
. Choose `IntelliJ IDEA Code Style XML`.
. Select the previously downloaded file `intellij-java-google-style.xml`.
. Make sure that `Google Style` is chosen as *Scheme*.

In addition, the EditorConfig settings (which ensure a consistent style between
Eclipse, IntelliJ, and other editors) should be applied on top of that. Those
settings are in the file `.editorconfig` of the Gerrit source code. IntelliJ
will automatically pick up those settings if the EditorConfig plugin is enabled
and configured correctly as can be verified by:

. Go to *File -> Settings -> Plugins*.
. Ensure that the EditorConfig plugin is enabled.
. Go to *File -> Settings -> Editor -> Code Style*.
. Ensure that *Enable EditorConfig support* is checked.

NOTE: If IntelliJ notifies you later on that the EditorConfig settings override
the code style settings, simply confirm that.

=== Copyright
Copy the folder `$(gerrit_source_code)/tools/intellij/copyright` (not just the
contents) to `$(project_data_directory)/.idea`. If it already exists, replace
it.

=== File header
By default, IntelliJ adds a file header containing the name of the author and
the current date to new files. To disable that, follow these steps:

. Go to *File -> Settings -> Editor -> File and Code Templates*.
. Select the tab *Includes*.
. Select *File Header*.
. Remove the template code in the right editor.

=== Commit message
To simplify the creation of commit messages which are compliant with the
<<dev-contributing#commit-message,Commit Message>> format, do the following:

. Go to *File -> Settings -> Version Control*.
. Check *Commit message right margin (columns)*.
. Make sure that 72 is specified as value.
. Check *Wrap when typing reaches right margin*.

In addition, you should follow the instructions of
<<dev-contributing#git_commit_settings,this section>> (if you haven't
done so already):

* Install the Git hook for the `Change-Id` line.
* Set up the HTTP access.

Setting up the HTTP access will allow you to commit changes via IntelliJ without
specifying your credentials. The Git hook won't be noticeable during a commit
as it's executed after the commit dialog of IntelliJ was closed.

== Run configurations
Run configurations can be accessed on the toolbar. To edit them or add new ones,
choose *Edit Configurations* on the drop-down list of the run configurations
or go to *Run -> Edit Configurations*.

=== Pre-configured run configurations

In order to be able to use the pre-configured run configurations, the following
steps are necessary:

. Make sure that the folder `runConfigurations` exists within
`$(project_data_directory)/.idea`. If it doesn't exist, create it.
. Specify the IntelliJ path variable `GERRIT_TESTSITE`. (This configuration is
shared among all IntelliJ projects.)
.. Go to *Settings -> Appearance & Behavior -> Path Variables*.
.. Click on the *+* to add a new path variable.
.. Specify `GERRIT_TESTSITE` as name and the path to your local test site as
value.

The copied run configurations will be added automatically to the available run
configurations of the IntelliJ project.

==== Gerrit Daemon
WARNING: At the moment running this configuration results in a
`java.io.FileNotFoundException`. To debug a local Gerrit server with IntelliJ,
use the instructions of <<dev-readme#run_daemon,Running the Daemon>> in
combination with <<remote-debug,Debugging a remote Gerrit server>>.

Copy `$(gerrit_source_code)/tools/intellij/gerrit_daemon.xml` to
`$(project_data_directory)/.idea/runConfigurations/`.

This run configuration starts the Gerrit daemon similarly as
<<dev-readme#run_daemon,Running the Daemon>>.

NOTE: The <<dev-readme#init,Site Initialization>> has to be completed
before this run configuration works properly.

=== Unit tests
To create run configurations for unit tests, run or debug them via a right-click
on a method, class, file, or package. The created run configuration is a
temporary one and can be saved to make it permanent.

Normally, this approach generates JUnit run configurations. When the Bazel
plugin manages a project, it intercepts the creation and creates a Bazel test
run configuration instead, which can be used just like the standard ones.

TIP: If you would like to execute a test in NoteDb mode, add
`--test_env=GERRIT_NOTEDB=READ_WRITE` to the *Bazel flags* of your run
configuration.

[[remote-debug]]
=== Debugging a remote Gerrit server
If a remote Gerrit server is running and has opened a debug port, you can attach
IntelliJ via a `Remote debug configuration`.

. Go to *Run -> Edit Configurations*.
. Click on the *+* to add a new configuration.
. Choose *Remote*.
. Adjust *Configuration -> Settings -> Host* and *Port*.
. Start this configuration in `Debug` mode.

GERRIT
------
Part of link:index.html[Gerrit Code Review]

SEARCHBOX
---------
