SomeClass {
AbstractType/Interface instanceVariable = new SpecificType();
...
SomeFn(){
instanceVariable.instanceMethod();
| <- go to SpecificType.instanceMethod() directly
instanceVariable.instanceMethod();
| < go to SpecificType of instanceVariable directly
}
Looking for an Intellij shortcut to go to variable's Specific Class. To go to instanceVariable in a large code and want to go to its SpecificType class. It needs three commands, go to instanceVariable definition using Go to Declaration (F3), see its definition, move cursor to actual type new SpecificType();, go into it and come back where you were using (Command + [). We can also hover over "instanceVariable" definition using F2 to see what its type is, but, there is no direct shortcut to go to actual Class "SpecificType" defining this instance variable.
Same with instanceMethod(), there is "Go to Implementation" on this method of an instance (Shift+Command+I) and choose specific type whereas instance is already defined right in the class as "SpecificType" here. See method type using F2 to see which class, in this case, it will show "SpecificType" the method type belongs to, then choose the specific implementation and choose SpecificType from several drop downs.
It would be nice if there was a shortcut to directly to to exact SpecificType Class or its method instead of multiple commands.
Why Main Menu | Navigate | Go to Implementation(s) action (Alt+Cmd+B default shortcut) doesn't work for you? Invoke it on the method call and from drop-down choose the implementation you are interested in:
Related
I've been using the Intellij Idea constructor generator as per https://www.jetbrains.com/help/idea/generating-constructors.html and it's been working fine except for the location of generated constructors.
"The generated constructors are inserted at the points defined in the Order of Members section of the Code Style settings. By default, the code generator places constructors after the fields."
The Code Style settings doesn't have anything about Order of Members that I can find, and the code generator seems to be just putting constructors wherever the cursor happens to be at the time.
How do you get the code generator to put them in the right place?
If the cursor is located in a position where IntelliJ can validly insert a constructor then IntelliJ will insert the generated constructor in that position.
If the cursor is not located in a position where IntelliJ can validly insert a constructor then IntelliJ will insert the generated constructor according to the "Order of Members".
To verify this ...
Place your cursor on the class name and then invoke the constructor generator (Code > Generate ... > Constructor) and the generated constructor will be inserted according to "Order of Members"; typically after member declarations and any other constructors already present in the class.
Place your cursor on an empty line within the class and then invoke the constructor generator (Code > Generate ... > Constructor) and the generated constructor will be inserted at your cursor location.
Have you looked under Settings -> Editor -> Code Style -> Java? There, you'll find an "Arrangement" tab that lets you define the order of fields, methods and constructors:
Is it possible to search for constructor / method invocation of given signature in IntelliJ?
For example, suppose I have two methods
void myMethod(int x, int y);
void myMethod(double x, double y);
How to search second one invocation?
Simplest way is the following:
Just invoke Find Usages (Alt+F7) on the second method. If the search results contain both methods, click on the cog and spanner Settings icon in the Find tool window. Deselect the Include overloaded methods checkbox and click on the Find button.
This settings dialog can also be reached directly via the menu Edit | Search | Find Usages Settings...
For reference I'm giving another option - structural search (Edit > Find > Search > Search Structurally).
The template you need is something like this:
$Instance$.$MethodCall$($Parameter$)
In "Edit Variables" dialog you need to adjust "MethodCall" to be "myMethod", "Parameter" count to be 2 and its "Expression type" to be "double"
Inside a method of some class, I typed a field name that doesn't exist yet. I invoked the Generate > Field command. IDEA generated the field but placed it somewhere far up the class - probably at a position determined by Code Style > Arrangement.
Is there a way to configure IDEA to place newly generated code as close to the cursor as possible? E.g., in the example above I wanted the new field to be placed right above my method.
In Eclipse, when I type main ctr+space, it will generate a static void main method for me. And when I type methodName ctr+space, smart code completion will suggest generating the method named methodName.
How can I auto-generate a void method in IntelliJ?
To create a new method from usage in the code like:
...
someMethodName()
...
AltEnter on the red code:
It's also possible to type void methodName() and use Complete Statement (CtrlShiftEnter), it will become:
void methodName() {
|
}
You could create your own Live Template as #Makoto answered, but programming by intention seems to be more natural. When you don't have a method, you write code that will use it, then create the method from the intention action - this way IDEA will generate the method signature automatically according to the parameters and return type in the not yet existing method usage, like String result = someMethod(stringParam);.
Finally, it is worth nothing that in IntelliJ IDEA main() method can be generated using psvmTab.
IntelliJ IDEA 15
Generate a main method
Default:
Type psvm (public static void main) > press Tab
Use the template from Eclipse (main instead of psvm)
File > Settings or press Ctrl + Alt + S
Editor > Live Templates
From the right side, click on the "+" sign > Live Template
Add the following details:
Abbreviation: main
Description: main() method declaration
Template text:
public static void main(String[] args){
$END$
}
You will see the new template added in Others.
Click on Define
Select Java > Press on OK
Type main in your Java code > press Tab
Generate a void method
Type your method name followed by parentheses (+ the arguments, if you use them) - E.g.: m() or m(1,2) > Press Alt + Enter > Click on "Create method ..." (or press Enter if it is already selected)
Type the abbreviation of the main() method template:
Press the template invocation key. By default, it is Tab. The abbreviation expands to the main() method.
IntelliJ makes use of Live Templates to do its code completion. It's then a matter of deciding what shorthand name you wish to use to name your void method.
Here's an example. Create a live template in Settings > Live Templates, then select the "Other" box. Hit the + on the right, then give your template a shorthand keystroke name.
Here's the one that I typed up. With the two different variables $NAME$ and $ARGS$, I can tab between them when I need to fill them in. $END$ is where the cursor ends when I'm done tabbing through the other two variables.
void $NAME$ ($ARGS$) {
$END$
}
The shorthand name I used is pmeth. So, every time I type pmeth into IntelliJ in a Java file, then hit Tab, this method is filled in, and my cursor automatically starts at $NAME$.
type psvm on the Java class and then Cntrl+Period key
Just type main and a suggestion will pop up. Press enter.
Simply Type the abbreviation of the main() method template:
psvm then Enter
If my class has a non-empty constructor, is it possible to auto-complete parameters in the new expression?
With Eclipse, if you press ctrl+space when the cursor is between the parenthesis:
MyClass myObject = new MyClass();
it will find the appropriate parameters.
--> MyClass myObject = new MyClass(name, value);
When I use ctrl+shift+spacebar after the new, Intellij shows me the constructors, but I can't choose one for auto-completion. Am I missing an option?
I usually start with CtrlP (Parameter Info action) to see what arguments are accepted (auto guess complete is way to error prone in my opinion). And if as in your case you want to fill in name type n a dropdown menu appears with all available variables/fields (etc) starting with n Arrow Up/Down and Tab to select name, or CtrlSpace to select a method (or even CtrlAltSpace to be killed by suggestions;-), followed by , and v Tab for value.
Well I used the eclipse key map where Parameter Info is unassigned.
Here is how to change that:
Well there's the Ctrl+Shift+Space combination, which tries to come up with a set of possible arguments. And if you press the Ctrl+Shift+Space a second time, Idea tries find arguments which fit across multiple calls & conversions.
So in your example Ctrl+Shift+Space would almost certainly bring up the 'name' as suggestion. And the next Ctrl+Shift+Space would bring up 'value' as suggestion.
In Intellij Idea 2016.3 you can use option + return. It will ask you if you want to introduce the named argument for the argument you are on and all the followers.
There's no such possibility yet. As IDEA doesn't fill the arguments automatically, distinguishing the constructors in the lookup makes no sense. There's a request for that (http://youtrack.jetbrains.net/issue/IDEABKL-5496) although I sincerely believe such a behavior is too dangerous and error-prone.