🇬🇧 English
🇬🇧 English
Appearance
🇬🇧 English
🇬🇧 English
Appearance
This page is written for version:
1.21
This page is written for version:
1.21
This page gives useful bits of information, to speed up and ease the workflow of developers. Incorporate them into yours, to your liking. It may take some time to learn and get used to the shortcuts and other options. You can use this page as a reference for that.
WARNING
Key binds in the text are listed as Windows shortcuts and refer to the default keymap of IntelliJ IDEA, if not stated otherwise. Refer to the File > Settings > Keymap
Settings or search for the functionality elsewhere if you are using a different Keyboard layout.
IntelliJ has many different ways of traversing projects. If you have generated sources using the ./gradlew genSources
commands in the terminal or used the Tasks > fabric > genSources
Gradle Tasks in the Gradle Window, you can manually go through the source files of Minecraft in the Project Window's External Libraries.
The Minecraft Source code can be found if you look for net.minecraft
in the Project Window's External Libraries. If your project uses split sources from the online Template mod generator, there will be two sources, as indicated by the name (client/common). Additionally other sources of projects, libraries and dependencies, which are imported via the build.gradle
file will also be available. This method is often used when browsing for assets, tags and other files.
Pressing Shift twice opens up a Search window. In there you can search for your project's files and classes. When Activating the checkbox include non-project items
or by pressing Shift two times again, the search will look not only in your own project, but also in other's, such as the External Libraries.
Another useful tool in IntelliJ is the Recent
window. You can open it with the Shortcut CTRL+E. In there you can jump to the files, which you have already visited and open tool windows, such as the Structure or Bookmarks window.
If you need to check out either the definition or the usage of variables, methods, classes, and other things, you can press CTRL+Left Click or use Middle Mouse Button (pressing mouse wheel) on their name. This way you can avoid long scrolling sessions or a manual search for a definition which is located in another file.
You can bookmark lines of code, files or even opened Editor tabs. Especially when researching source codes, it can help out to mark spots which you want to find again quickly in the future.
Either right click a file in the Project
window, an editor tab or the line number in a file. Creating Mnemonic Bookmarks
enables you to quickly switch back to those bookmarks using their hotkeys, CTRL and the digit, which you have chosen for it.
It is possible to create multiple Bookmark lists at the same time if you need to separate or order them, in the Bookmarks
window. Breakpoints will also be displayed in there.
By opening the Structure
window (Alt+7) you will get an overview of your currently active class. You can see which Classes and Enums are located in that file, which methods have been implemented and which fields and variables are declared.
Sometimes it can be helpful, to activate the Inherited
option at the top in the View options as well, when looking for potential methods to override.
By placing the cursor on a class name and pressing CTRL+H you can open a new Type Hierarchy window, which shows all parent and child classes.
Code completion should be activated by default. You will automatically get the recommendations while writing your code. If you closed it by accident or just moved your cursor to a new place, you can use CTRL+Space to open them up again.
For example, when using Lambdas, you can write them quickly using this method.
The Generate menu can be quickly accessed with ⌘/CTRLN. In a Java file, you will be able to generate constructors, getters, setters, and override or implement methods, and much more. You can also generate accessors and invokers if you have the Minecraft Development plugin installed.
In addition, you can quickly override methods with ⌘/CTRLO and implement methods with ⌘/CTRLI.
In a Java test file, you will be given options to generate related testing methods, as follows:
Displaying parameters should be activated by default. You will automatically get the types and names of the parameters while writing your code. If you closed them by accident or just moved your cursor to a new place, you can use CTRL+P to open them up again.
Methods and classes can have multiple implementations with different parameters, also known as Overloading. This way you can decide on which implementation you want to use, while writing the method call.
Refactoring is the process of restructuring code without changing its runtime functionality. Renaming and Deleting parts of the code safely is a part of that, but things like extracting parts of the code into separate methods and introducing new variables for repeated code statements are also called "refactoring".
Many IDEs have an extensive tool kit to aid in this process. In IntelliJ simply right click files or parts of the code to get access to the available refactoring tools.
It is especially useful to get used to the Rename
refactoring tool's key bind, Shift+F6, since you will rename many things in the future. Using this feature, every code occurrence of the renamed code will be renamed and will stay functionally the same.
Sometimes simpler tools are needed to edit code occurrences.
Key bind | Function |
---|---|
CTRL+F | Find in current file |
CTRL+R | Replace in current file |
CTRL+Shift+F | Find in a bigger scope (can set specific file type mask) |
CTRL+Shift+R | Replace in a bigger scope (can set specific file type mask) |
If enabled, all of those tools allow for a more specific pattern matching using "Regex".
Good code should be easily readable and self-documenting. Picking expressive names for variables, classes and methods can help a lot, but sometimes comments are necessary to leave notes or temporarily disable code for testing.
To comment out code faster, open IntelliJ's Settings, look for the Comment with Line Comment
and the Comment with Block Comment
entries, and set their Key binds to your preferences.
Now you can highlight the necessary code and use the shortcuts, to comment the section out.
// private static final int PROTECTION_BOOTS = 2;
private static final int PROTECTION_LEGGINGS = 5;
// private static final int PROTECTION_CHESTPLATE = 6;
private static final int PROTECTION_HELMET = 1;
/*
ModItems.initialize();
ModSounds.initializeSounds();
ModParticles.initialize();
*/
private static int secondsToTicks(float seconds) {
return (int) (seconds * 20 /*+ 69*/);
}
In IntelliJ, right next to the line numbers, you can have small [+] and [-] icons. Those can be used to temporarily collapse methods, if-statements, classes and many other things if you are not actively working on them. To create a custom block which can be collapsed, use the region
and endregion
comments.
// region collapse block name
ModBlocks.initialize();
ModBlockEntities.registerBlockEntityTypes();
ModItems.initialize();
ModSounds.initializeSounds();
ModParticles.initialize();
// endregion
WARNING
If you notice that you are using too many of them, consider refactoring your code to make it more readable!
When working on code, it can come in handy to leave notes, on what still needs to be taken care of. Sometimes you may also spot a potential issue in the code, but you don't want to stop focusing on the current problem. In this case, use the TODO
or FIXME
comments.
IntelliJ will keep track of them in the TODO
window and may notify you, if you commit code, which uses those type of comments.
A great way of documenting your code is the usage of JavaDoc. JavaDocs not only provide useful information for implementation of methods and classes, but are also deeply integrated into IntelliJ.
When hovering over method or class names, which have JavaDoc comments added to them, they will display this information in their information window.
To get started, simply write /**
above the method or class definition and press enter. IntelliJ will automatically generate lines for the return value and the parameters but you can change them however you want. There are many custom functionalities available and you can also use HTML if needed.
Minecraft's ScreenHandler
class has some examples. To toggle the render view, use the pen button near the line numbers.
There are many more shortcuts and handy little tricks, which would go above the scope of this page. Jetbrains has many good talks, videos and documentation pages about how to further customize your workspace.
Use PostFix Completion to alter code after writing it quickly. Often used examples contain .not
, .if
, .var
, .null
, .nn
, .for
, .fori
, .return
and .new
. Besides the existing ones, you can also create your own in IntelliJ's Settings.
Use Live Templates to generate your custom boilerplate code faster.
Anton Arhipov from Jetbrains also had an in depth talk about Regex Matching, Code Completion, Debugging and many other topics in IntelliJ.
For even more information, check out Jetbrains' Tips & Tricks site. Most of their posts are also applicable to Fabric's ecosystem.