How to search for constructor / method invocation of given signature in IntelliJ? - intellij-idea

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"

Related

Intellij shortcut directly go to any variable Class or method

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:

Kotlin - specify type explicitly in IntelliJ after extracting a variable shortcut

When coding in Java (and Kotlin!) in Intellij, I usually extract variables/fields from the expression that they are being assigned. In other words, if I want to write the following code in Kotlin:
val name = getMyString()
Then I would write getMyString, highlight it and extract the variable using a keyboard shortcut. Nice and simple.
But let's say I want the value type to be shown explicitly, meaning:
val name: String = getMyString()
If I use variable extraction, IntelliJ will offer me a checkbox, which I have to use my mouse to click, that will show the type. Another annoying option would be to set the variable name, then use Alt + Enter show the type explicitly.
Unlike previous questions (or JetBrains documentation), I do not want to have the type displayed by default. I just want to know if there is a keyboard shortcut to check the aforementioned checkbox, without disrupting the flow of writing the code.
After using the extract variable shortcut (Ctrl+Alt
+V for Windows), you can use
Alt+T to toggle Specify type explicitly
Alt+V to toggle Declare with var

Resharper naming convention rule for void methods

Does anyone know how (or if it's possible) to create a Resharper naming convention rule that doesn't allow void methods to start with "Get"?
So this would be fine:
public string GetFoo();
But the rule would complain about this:
public void GetFoo();
You can't really add this as a naming convention, but you could create a search pattern that will look for void methods starting with Get, and flag them with a Warning or Error.
For this, go to ReSharper's Options, then Code Inspection → Custom Patterns:
Click Add Pattern
In the new dialog, select Find
Type in the following pattern: void $method$($args$){ $stmt$ }, and press Add Placeholder → Extract from pattern. This should create 3 placeholders in the list.
Double click the method placehoder, and add the following RegEx: ^Get.*
In the Pattern Severity combobox select Show as Warning or Show as Error, depending on your preference.
In the description, write something like Void methods should not begin with Get.
Click Add, then Save (or Save To → Team Shared, to have this pattern stored in the team-shared settings, available to all your teammates).
ReSharper will now flag all void methods that start with Get:

How to generate a void method in IntelliJ IDEA?

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

where can I read the source code?

I am using squeak4.1 for development, when I am looking up add method in method chain below: Kernel-Numbers -> Integer -> arithmetic -> + ,the method for adding is +, in + method I find
sample code like this :
ifTrue: [^ (self digitAdd: t1) normalize].
Can I know how I can trace into digitAdd and look the implementation of add method in smalltalk? thanks first!
As the message is send to self, you can query the Integer class itself for its definition. For this, right click Integer in the System Browser, select "find method" and enter `digitAdd' in the search window that appears. Click the "Accept" button. This will show you the message definition.
You can also use the search facility in Squeak. (the search box on the main menu bar).
Select string 'digitAdd:' in text editor, and press Alt-m shortcut or right-click and in opened menu find 'implementors of it'.
This will open a window with all methods in all classes in system which implement given message.