How to quickly jump to the class declaration line in IntelliJ? - intellij-idea

Suppose a Foo.java file which defines the Foo class.
The cursor is now on some line of Foo.java. The symbol under the cursor is not necessarily Foo).
How to quickly jump to the class declaration line (i.e., the line like public class Foo {) in Intellij?
Any shortcuts for this?
I use the ideavim plugin. Therefore, solutions relying on vim functionalities are OK.

On Mac you can do cmd + up arrow
(on Windows or Linux, I'm assuming it will be ctrl + up arrow)
This will select the current file in the breadcrumb (navigation bar) then hit enter and you'll be at the class declaration
You can look to which key combination this is mapped for you by looking in the keymaps for 'Jump to Navigation Bar'

From 2022.3.1 on Mac, cmd + up may not work for this case. it need you jump to a specific method.
Instead of cmd + up using navigation bar, cmd + F12 which show the file structure popup, then use left arrow to select class name if method is selected default, press enter and you'll be navigated to class declaration line.

Related

In JetBrains Rider, is there keyboard shortcut to add a new class file?

In JetBrains Rider, is there keyboard shortcut to add a new class file? Perhaps with the same popup to name the C# class, then to save in the currently selected/open directory.
Would save many mouse clicks :-)
To generate new class file in Rider:
Method 1
In the Solution Explorer, select a node.
Press Alt+Insert | Ctrl+N | ⌘ N.
Choose Class/Interface in the context menu.
Reference
Method 2
In the Code Editor, press Ctrl+Alt+Insert | ⌃ ⌥ N.
Choose Class/Interface in the context menu.
This will add the new file in the same folder as the current file.
Reference
There is no dedicated shortcut for a new class, but you can select a node in the Solution Explorer, press Alt+Insert, and then pick the desired template from the popup.

Jump to implementation instead of declaration in IntelliJ

In IntelliJ I can click on an symbol while holding the Ctrl key which will let IntelliJ (in case of methods) navigate to the declaration. This is documented here.
If the method was defined in an interface, IntelliJ will open that interface but not the implementation.
Is there a way to tell IntelliJ always jump to the implementation if there is at least one or make it asking me which I want to jump to?
Navigate | Implementation(s), by default mapped to Ctr/Cmd+Alt+left mouse button I believe.

Is there a shortcut to implement interface methods in intellij?

When I type something along the line of this:
class Foo implements Bar{}
intellij idea highlights that line and then I have to hover over it with the mouse pointer until a red lightbulb pops up and click "Implement methods". Is there a keyboard shortcut for this?
Ctrl+I/Cmd+I (Windows/Mac) is the shortcut for "Implement Methods" in the default IntelliJ keymap.
It also depends on which Keymap you are using. For instance, I am using Jetbrains Rider on MacOS and the shortcut for implementing missing members is "Cmd+Shift+I".
To be able to learn your shortcut key, go to Preferences (Cmd + ',') and select Keymap. You can just type 'implementation' to the search bar on the right, or Navigate to the Main menu > Code > Implement Methods. You will see the shortcut defined according to your settings.

Choose between open declaration/implementation on Ctrl + click in IntelliJ IDEA

Details:
IDE: IntelliJ IDEA 14
O.S.:: Windows 7
If I want to navigate to the declaration of a method I can choose one of the following approaches:
press Ctrl + left click
right click > Go To > Declaration
press Ctrl + B
If I want to navigate to the implementation of a method I can choose one of the following approaches:
press Ctrl + Alt + left click
right click > Go To > Implementation(s)
press Ctrl + Alt + B
In Eclipse, if I press Ctrl and the mouse is over a method, a popup appears and I can choose what to do (go to Implementation/Declaration). If I press "left click" ( Ctrl is still pressed ) the first option will be chosen.
How can I achieve the same / a similar behavior in IntelliJ IDEA ?
There is not a built-in option for this. You would need to configure a custom quick list to do such.
Open the Settings dialog and go to "Quick Lists" (It's under "Appearance & Behavior" in IDEA 14)
Click the add button to the right of the middle pane
Give your list a name
Click the add button to the right of the far right pane (where is reads "no actions")
Add the Go to Declaration action (Main Menu > Navigate > Go to Declaration)
Add the Go to Implementation(s) action (Main Menu > Navigate > Go to Implementation(s))
Click Apply
Go to Keymap in the settings
Under the "Quick List" node, find the Quick List you just created and map a short-cut (keyboard or mouse) to it. You can, of course, remap one of the shortcuts used by the Go to Declaration or the Go to Implementation(s) action
Click OK to close out of the settings and give it a try.
Note that in the quick list that opens, you will have numbers next to each action for quick selection.
Edit:
Adding a screenshot of final quick list for additional clarity
I was looking for the same thing, because I was also used to it in Eclipse. Nevertheless, in 99% of the cases, I went to the implementation. If that's your case too, you may consider changing the Ctrl+click shortcut in IntelliJ:
Open File → Settings... → Keymap
Search for "implementation", and identify the item "Main menu → Navigate → Implementation(s)" in the list
Right-click the item and choose "Add Mouse Shortcut"
Add the Ctrl+click shortcut by making a Ctrl+left click on the window
When hitting the OK button, you have two choises :
Remove the existing shortcut (opening the declaration)
Leave the existing shortcut : In that case, when using then Ctrl+click, both the declaration and the implementation will be opened (at least when they are in different files)
I know this answer comes like 2 years too late, but hopefully it may help others?
EDIT : This shortcut also applies when you want to navigate to the declaration of a super class. Therefore, my "99%" first declared above drops a bit.
I've added the ctrl + right-click as a shortcut to go the declaration (which conflicts with no other shortcut - but you need a mouse with at least two buttons...).
If you select the method and hit Ctrl+T it will show the implementations of the methods. You can click on the options to navigate to that implemented method. Hope this helps.
On mac you can open the implementation(s) with command + option + click. Right -click on the method and check "go to" for the other options and shortcuts you might need.
My IntelliJ (2016.2) shows the declaration of a method (in a PHP interface) as one of the implementations in the implementation popup, so maybe you can do this all from the Implementations popup now.

How do I create a new class in IntelliJ without using the mouse?

Is there a way to create a new class in a desired location without using the mouse in IntelliJ?
I understand there is no keyboard binding in the default keymap.
If you are already in the Project View, press Alt+Insert (New) | Class. Project View can be activated via Alt+1.
To create a new class in the same directory as the current one use Ctrl+Alt+Insert (New...).
You can also do it from the Navigation Bar, press Alt+Home, then choose package with arrow keys, then press Alt+Insert.
Another useful shortcut is View | Select In (Alt+F1), Project (1), then Alt+Insert to create a class near the existing one or use arrow keys to navigate through the packages.
And yet another way is to just type the class name in the existing code where you want to use it, IDEA will highlight it in red as it doesn't exist yet, then press Alt+Enter for the Intention Actions pop-up, choose Create Class.
You can also use: ctrl+alt+insert
With Esc and Command + 1 you can navigate between project view and editor area - back and forward, in this way you can select the folder/location you need
With Control +Option + N you can trigger New file menu and select whatever you need, class, interface, file, etc. This works in editor as well in project view and it relates to the current selected location
// please consider that this is working with standard key mapping
For Mac Os, command + 1 , then press control + return
On Mac OS 10.14.5, Idea Intellij 2019.1.3 - Press command + 1 to navigate to project files then press control + n
On Mac you can navigate to the location in Project view where you want to create your class and then use ⌘N followed by Enter.
I do this a lot, and I don't have an insert key on my laptop, so I made my own keybinding for it. You can do this by opening Settings > IDE Settings > Keymap and navigating to Main menu > File > New... (I would recommend typing "new" into the search box - that will narrow it down considerably).
Then you can add a new keyboard shortcut for it by double clicking on that item and selecting Add Keyboard Shortcut.
Alt-Home until you're in Packages view
Down-arrow until package is highlighted
Alt-insert
Enter X 2
Type name
$PROFIT$
If you use Mac, you are in luck. One can change the keymap for Intellij as Mac OS X, then you can use option+C.
In my (linux mint) system I can not get working combination alt+insert so I do the next steps:
alt+1 (navigate to "tree") --> "context button - analog right mouse click" (between right alt and ctrl) -- then with arrows (up or down) desired choice (create new class or package or ...)
Hope it helps some "mint" owners )).
I also searched this answer. Equivalent of command+N on Mac OS for Windows is ctr + alt + insert which #manyways already answered. If you searching this in settings it is in Settings > IDE Settings > Keymap, Other > New ...
If the difficulty is in finding the option that makes .java or .class files (Like me), then simply,
click on the folder you want to create file on. select new, and type file's name along with extension.
For example, instead of, helloWorld type helloworld.java or any file extension you desire.