The great advantage in using IntelliJ is the possibility to make your job faster; however, it will only be possible if you know there are some tricks you can use.
IntelliJ provides a good way to show you what can be done in the IDE to improve your performance. In the Help menu, you will find an option called Productivity Guide; as you may notice, this is a guide that will help you improve your productivity.
The guide analyzes the usage of IntelliJ and identifies the features you use a lot and the ones you practically don't use, so it can create statistics and configure the Tip of the Day window, showing the most relevant tips for when the developer is performing time-consuming operations.
In the productivity guide dialog, as shown in the following screenshot, you will see the quantity of time for which you used a feature and a description of the feature. The description is the same as that used in the Tip of the Day dialog and is very useful when you search for features you never used. Even knowing you can identify unused features in this guide is, for a new user, important to keep the Show Tips on Startup option (in the Tips of the Day window) checked; this is the simplest way to discover the abilities of IntelliJ.
The faster you navigate through the source code of your project, the sooner you can get things done. I have already presented, in Chapter 1, Getting Started with IntelliJ IDEA 12, two ways to navigate the source code: using the Project tool window and the Navigation bar; however, there are better ways to navigate directly to what you want.
A faster way to execute some actions in every application is using shortcuts. IntelliJ has a lot of them to make the most different things and some of them are used to navigate:
The small list of shortcuts above is intended to show you some features directed to navigation; a complete list of shortcuts for your preferred operational system can be downloaded from http://www.jetbrains.com/idea/documentation/index.jsp.
In conjunction with shortcuts, IntelliJ provides visual elements that can be used to navigate through and understand the real state of the code. Some of these visual elements can be observed in the gutter area confirming that you are changing your source code. There will be different icons according to the type of code you are editing; these icons are important for two reasons: you can use them to navigate and they will show you if you have correctly done what you wanted. For example, if the Spring icon appears next to a setter, you know the bean property declaration in the XML file worked correctly. As with the gutter area, the marker bar can be used to navigate the source code; however, the marks here just navigate the content of the current file. The following image shows a list of the implementations of a method of an interface when the developer clicks on the Implementation icon:
Sometimes, you will need to see the current file open in the Project tool tab and perhaps you will see the source tree collapsed. Depending on the quantity of hierarchies and files, it can take some time. To minimize the time spent on this task, you can use the scroll from source button available in the title bar in the Project window; this way IntelliJ can expand the source tree and select the current file in use, as you can see in the following screenshot:
IntelliJ provides good code facilities; however, sometimes you could under-utilize some of them, probably because you don't know they exist. Generally, the code completion feature is activated when the developer is typing; however, sometimes, the developer should actively force the code completion feature. The following list presents some useful shortcuts when working directly with the code:
You will notice that you can select a suggestion IntelliJ made using the enter key; however, the suggestion will expand and won't override the sentence already present in the line (if this is the case). You may lose some time deleting the characters after the expanded sentence. To avoid this, you can select the suggestion using the tab key and this way the previous sentence will be swapped and the expanded sentence will occupy its space.
To speed up your development even more, IntelliJ provides live templates. They are predefined code fragments to help you insert frequently-used or custom code in your source. Live templates can be of three types, listed as follows:
psvm
abbreviation in the body of a class will create the default main method.fori
abbreviation will create a for
sentence and position the cursor in places where it needs a variable or parameter.try
/catch
block.Live templates can be activated using the Ctrl + J shortcut and selecting the desired option from the list or typing the abbreviation and clicking on the template invocation key (by default, it is Tab). If you look carefully, the abbreviations follow a pattern; for example, iteration templates start with it, followed by the abbreviation of the group that will be iterated.
To use the Surround template, you first need to select a sentence in the source file; after this, you should use the Ctrl + Alt + T shortcut. When you do this, a dialog appears asking which type of surrounding you want; just select one and press enter as shown in the following screenshot:
IntelliJ comes with a default list of live templates; this list covers a lot of possibilities but, if you need one specific template, you can create your own in the Settings dialog. By default, the list of live templates will be shown only when you use the Ctrl + J shortcut. This way, if you type for in the code editor, the fori
live template won't be suggested. However, we can change this behavior.
Go to Help | Find Action (or simply press Ctrl + Shift + A) and type Registry
in the search field; make sure that the option Include non-menu actions is checked. When you select the registry result, the Registry dialog will appear. Find the optionshow.live.templates.in.completion and check it. In the following figure, you will see live templates in the completion list even if you didn't ask for them:
As you already know, refactoring is a sensitive task that should be performed with extreme care; a simple mistake in a refactoring could generate lots of bugs. IntelliJ provides a large range of facilities to help you in the refactoring process. Even with IntelliJ being so smart, it could make some mistakes and it is your responsibility to avoid this. In the Refactor menu, you will find all possible refactor options, some of which will be disabled depending on the position of the cursor. The shortcuts have an important role here as in other parts of the IDE. However, you will notice that the Refactor menu has a bunch of submenus that don't have any shortcut; because of this, there is a shortcut that will help you a lot when you don't want to lift your hands off the keyboard—Ctrl + Alt + Shift + T. This shortcut will open a pop-up that will show all the refactoring options available for the piece of code that the cursor is hovering over.
Even with that shortcut helping a lot, there are other shortcuts that help the developer and Shift + F6 is one of them. This shortcut permits us to rename the element that the cursor is hovering over and changes all usages of this element. I should alert you about the usage of the Rename refactor in JSF files—this approach runs a search in other JSF files and changes sentences in places that you didn't want to modify. For example, you are using the var
element in the datatable of an XHTML file and, coincidently, another XHTML datatable has the same value in the var
element. If you proceed with the refactoring, all usages of that name in other XHTML files will change too. There is a worse side-effect; if you use primefaces datatables, you will notice that some usages of the var
element in the same datatable won't change (for example, the rowKey
option). But don't worry, this is the only casein which I had problems using the Rename refactor and, in this case, I used the replace action (Ctrl + R).
Even though it is bad practice, sometimes we need to copy a class to use some code that has already been implemented to solve a similar problem. The approach of "copy a class and paste a class" was taking up too much of my time because I needed to use the mouse to select the class file in the project tree. After some time, I discovered that IntelliJ provides a simple utility to do this job; basically, you just need to press F5 and enter the name of the new class in the dialog that will appear. If you prefer, in this dialog, you can specify where the new file will be stored.
Another important facility is the move refactor; using F6 you can move a file to another place, even as an inner class. To change the signature of a method, just use Ctrl + F6 and the options available to add and remove parameters in the change Signature dialog that will appear. If you need to migrate a type of method, variable, or parameter, just position the cursor over the element and press Ctrl + Shift + F6 and a new dialog will be shown to help you change the type of the element.
Delegation is the simple concept of handing a task over to another part of the program. IntelliJ provides an easy way of doing this. First of all, call the delegate option available using Alt + Insert, then by clicking on an option under Generate or the option Code | Generate... in the main menu. In the pop up that appears, select the delegate methods, then the object to be delegated, and click on OK in the next dialog. Finally, a new dialog will be displayed to permit you to select the methods you want to delegate.
If you need to transform an element to static, the Make Static refactor will do it in a simple way. The option convert to an instance method does the reverse job when a class is passed as parameter. There are other refactor options available that you can use in specific cases, such as Invert Boolean and Remove Middleman. Invert Boolean will simply invert the return of a method that returns Boolean. Remove Middleman is helpful when you have a class that forwards many of its method calls to objects of other classes; using this option, you can simplify your design.
If you are an attentive user, you will notice that, in the majority of dialogs used to manipulate your code, there exists a preview button. It is good practice to analyze all the changes IntelliJ will automatically apply in your code before they take effect, so use the preview button all the time, unless you really trust what IntelliJ will do. After you click on the Preview button, the find tool will appear (sometimes it won't, but the results will be there) with all usages it found of the element that will be changed. For example, if you want to rename a method, IntelliJ will find every usage of this method and show them in a list so you can review the changes before you proceed. With this tool, you can include or exclude a usage from participating in the refactoring process and decide whether you will proceed with the refactoring or it will be canceled as shown in the following screenshot:
Perhaps you made a modification that didn't please you; for such cases, you have the Local History option. Local history is a kind of version control system that is independent of other VCSs, so you don't need any VCS configured to use it and it is active all the time. Different from general VCSs, local history is for personal use only, so you can't share access with other users. It facilitates the development once you can revert your modifications any time you want using the difference window.
To call history to a file, just right-click anywhere in editor (or use the Alt + ~ shortcut) and you will see the Local History option; in that option, you will see three suboptions: Show History, Show History for Selection, and Put Label. The first two options are similar as they show a dialog where the user can see the differences between the versions; however, the Show History for Selection option will show only the selected content in the difference dialog. The option Put Label is used to define a label to a revision; this way, you will have a more organized history.
In some cases, the local history can be cleared, such as when you install a new version of IntelliJ and when the cache is invalidated. The invalidation of the cache can occur when the cache is overloaded or manually by the user using the option invalidate cache in the file menu. Local history stores a good range of modifications that happen in your code; however, it isn't a full-blown VCS, so if you need real version control, it is better to use a distributed VCS like Git or Subversion.
3.147.84.157