IntelliJ is one of the best supported IDE for editing TruffleRuby.
It provides a real polyglot editor experience by having great support for Java, Ruby and Python all in one IDE. For instance, you can jump from primitives in Ruby code to the corresponding Java class in IntelliJ.
Note that the global IntelliJ settings are accessed differently based on the platform.
- On macOS, it's under
IntelliJ
->Preferences
- On Linux, it's under
File
->Settings
.
We refer to these global settings as Preferences
in the rest of the document.
First, make sure the project is already built from the command line:
jt build
Installing the Eclipse Code Formatter plugin is recommended to have the Java source code always formatted to match the project's guidelines.
-
Install the
Eclipse Code Formatter
plugin withPreferences
->Plugins
. -
The plugin will be fully configured in next step where the project files are generated with
jt idea
.
To format the source code use the Reformat Code
IntelliJ shortcut.
That's ⌥⌘L
on macOS and Ctrl+Alt+L
on Linux.
If imports are not being optimized as well, use IntelliJ action Reformat File...
(⌥⇧⌘L
on macOS and Ctrl+Alt+Shift+L
on Linux)
and make sure the checkbox Optimize imports
is checked.
You can also format the whole codebase on the command line with jt format
.
These settings should be set to avoid unwanted formatting changes to the codebase.
In Preferences
-> Editor
-> Code Style
-> Java
:
- In
Imports
set the count to use import * to a large value to disable import *, e.g.999
- In
Wrapping and Braces
->Field Annotations
select the optionDo not wrap after a single annotation
.
To be able to see and edit properly all the files in the TruffleRuby repository
you need to install the plugins for Ruby and Python in Preferences
-> Plugins
.
Make sure you have configured Ruby and Python SDKs. When the project files are
generated it will try to use existing Python 2.7 SDK and a Ruby SDK which has
truffleruby-jvm
in its name. If rbenv
is used the truffleruby-jvm
build
should be linked automatically and the rbenv: truffleruby-jvm
Ruby SDK
should be created automatically when the IDE starts.
jt idea
If you want to include other GraalVM projects besides TruffleRuby and its dependencies, you can do so
by dynamically importing (--dy
) other suites. For example, to have Native Image and Tools in the IDE:
jt idea --dy /substratevm,/tools
From the IntelliJ launcher, select Open
and navigate to this repository root.
There should now be the following in your workspace:
com
,org
– roots of Java packages TruffleRuby depend onmx
– the mx tooltruffleruby
– Root directory which contains all TruffleRuby files including the Java and Ruby source files. Syntax highlighting and project navigation should work for both Java and Ruby.
Go to File
-> Project Structure
-> Platform Settings
-> SDKs
and add the JVMCI OpenJDK shown by $ jt install jvmci
with the +
button -> Add JDK...
.
Checkstyle style checks for Java code can be imported into IntelliJ via the CheckStyle-IDEA plugin.
This is optional and you can choose to just use jt checkstyle
and
tool/cleanup-imports.rb
instead.
- Select
Preferences
, thenPlugins
. - Select
Browse Repositories
and search forCheckstyle
- Select
Checkstyle-IDEA
, thenInstall
.
- Select
Preferences
,Other Settings
, thenCheckstyle
. - Select
+
to add a local Checkstyle Configuration. - Enter a description, e.g.
TruffleRuby Checkstyle
- Enter the location of the configuration file, e.g.
/Users/myuser/Documents/truffleruby/src/main/.checkstyle_checks.xml
- Complete the import, then check the
Active
checkbox next to the new configuration, then selectOK
.
- Select
View
->Tool Windows
->Checkstyle
- The Checkstyle tools window has options to check the current file, project, module, changelist, or only modified files.
- The style issues will be highlighted in the tool window, on the right side of the editor or inline in the source.
Using the Code
-> Reformat Code
tool using IntelliJ's default settings may resolve the majority of Checkstyle
errors. Optionally, you can import Checkstyle styles into your code formatting as follows:
- Select
Preferences
->Editor
->Code Style
. - Using the gear icon, next to the
Scheme
, selectImport Scheme
->Checkstyle Configuration
. - Select the
src/main/.checkstyle_checks.xml
file to import Checkstyle into the scheme. - Use
Reformat Code
tool as usual with the new Checkstyle settings imported.
The Wrap to Column
plugin is useful to conveniently wrap to a given max number
of characters per line, for instance in Markdown files. Install it from
Preferences
-> Plugins
. You then need to configure the width to 80
in
Preferences
-> Tools
-> Wrap to Column
-> Right margin override
.