BEST SITE FOR WEB DEVELOPERS
Java programming language. Lessons for beginners from hyperskill

Ua

Navigation through code


Theory

It is pretty hard to remember all the shortcuts, and all the actions and productivity boosters IntelliJ IDEA has. However, don't worry about that. All you need to remember is the one action that rules them all, Help | Find Action command (⌘ ⇧ A / Ctrl + Shift + A). Just start typing the action you need, and you will get the list:

Help | Find Action command

§1. Code search

IntelliJ IDEA lets you find and replace text strings in an active editor.

Search through the current file

  • From the main menu, choose Edit | Find | Find, or press ⌘ F / Ctrl + F. The search panel appears at the top of the active editor.
  • If necessary, specify the search options.
  • In the search field, start typing the search string:
Code search

As you type, the first occurrence of the search string after the current cursor position is selected; the other occurrences are highlighted in the editor. In addition, the matching occurrences are marked in the right gutter with stripes.

  • To search for a multi-line fragment, click in the search box.
  • Explore the search results.

Replace in the current file

  • From the main menu, choose Edit | Find | Replace, or press ⌘ R / Ctrl + R. The search and replace pane appears on top of the active editor.
  • If necessary, specify the search and replace options.
  • In the search field, start typing the search string. As you type, the matching occurrences are highlighted in the editor, and a Replace pop-up dialog box opens at the first occurrence, suggesting to replace the current occurrence, or all of them, with an empty string.
  • Start typing the replacing string.
  • Explore the search results, and, using the buttons of the replace dialog box, replace occurrences as required.

See Manage your search page for the detailed description of search and replace options.


§2. Navigating to Class, File, or Symbol by Name

Navigate commands enable you to quickly jump to the desired classes, files, or symbols specified by names. IntelliJ IDEA suggests a look-up list of matching names, from which you can select the desired one, and open it in the editor. This navigation honors CamelCase and snake_case capitalization. Refer to the tips for the detailed list of available techniques.

Declaration and list of usages

Programming is much more about understanding and reading existing code than about writing some new code. While learning, it is also quite important to see your code as a whole and to understand how it works.

When you use any variable, function, or method and need a tip on how it works, let the code speak for itself. Take a look at the definition with ⌘ Y / Ctrl + Shift + I:

Declaration and list of usages

There's one more option: press ⌘ B / Ctrl + B to navigate to the declaration of an entity; use it one more time on the declaration to see the list of usages:

To navigate to the declaration of an entity

§3. Navigating by name

On the main menu, point to Navigate, and then choose Class, File, or Symbol respectively, or use the following shortcuts:

  • Class: ⌘ O / Ctrl + N
  • File (directory): ⌘ ⇧ O / Ctrl + Shift + N
  • Symbol: ⌘ ⌥ O / Ctrl + Shift + Alt + N
  • In the pop-up window, start typing the desired name. So, you can enter characters located anywhere inside the desired name. As you type, the suggestion list shrinks, displaying the matching names only.

     - Class:

Class

You can also navigate directly to a specified anonymous class. It is enough to specify the class name and the anonymous class number, delimited with $ character:

Class

     - File:

File

     - Directory: use the same shortcut as for file navigation, and type the name of the directory you are looking for, the pattern name ending with / or \:

Directory

     - Symbol:

Symbol
  • Click the desired entry in the suggestion list, or select it using the arrow keys, and press Enter.

§4. Conclusion

You can easily find and replace text strings as well as navigate through your code using these simple commands and hotkeys. Don’t forget this feature while using IntelliJ IDEA to boost your efficiency and save time.


You can also view Lesson on hyperskill.org.


Practical tasks and answers

Tasks and answer options are given. The correct option is highlighted in blue color.

№1. The goal

Question: What can you find with Navigate commands?

Select one option from the list:

  • Any button on the IDEA control panel
  • Any line of code in any file
  • Classes, files, or symbols specified by names ✔
  • Public classes

Explanation. The correct answer is Classes, files, or symbols specified by names.

Navigate commands in IntelliJ IDEA allow you to quickly find classes, files, or symbols specified by names. You can use these commands to navigate to the declaration of a symbol, the usages of a symbol, or the definition of a class.

You can also use navigate commands to find any line of code in any file. However, this is not as efficient as using the search feature. The IDEA control panel does not have any buttons that can be navigated to using navigate commands. Public classes are a subset of classes. You can use navigate commands to find public classes, but you can also find other types of classes.

Here are some of the most commonly used navigate commands in IntelliJ IDEA:

  • Navigate to declaration (Ctrl+B or ⌘B): This command navigates to the declaration of the symbol under the caret.
  • Navigate to usages (Alt+F7 or ⌥F7): This command navigates to all the places where the symbol under the caret is used.
  • Navigate to definition (Ctrl+Alt+B or ⇧⌘B): This command navigates to the definition of the class or method under the caret.
  • Find in a path (Ctrl+Shift+F or ⇧⌘F): This command allows you to search for a symbol in the current project or in a specific path.
  • Recent files (Ctrl+E or ⌘E): This command opens a list of the most recently opened files.

№2. Looking for action

Question: What action can you find using the "Find Action" command?

Select one option from the list:

  • All the actions in IntelliJ IDEA ✔
  • All the actions on your computer
  • Only actions you define manually
  • Only actions you have programmed

Explanation. The correct answer is All the actions in IntelliJ IDEA.

The "Find Action" command in IntelliJ IDEA allows you to search for any action, regardless of whether it is a built-in action or a custom action. This includes actions that are available in the menus, actions that are available in the toolbars, and actions that are available through keyboard shortcuts.

You can also use the "Find Action" command to find actions that are not currently visible. For example, if you have a custom action that is hidden, you can use the "Find Action" command to find it and make it visible. The "Find Action" command is a powerful tool that can help you learn about all the actions that are available in IntelliJ IDEA. It can also help you find the action that you need to perform a specific task.

Here are the steps on how to find an action using the "Find Action" command:

  • Press Ctrl+Shift+A (or ⇧⌘A on macOS).
  • In the search field, type the name of the action that you are looking for.
  • IntelliJ IDEA will display a list of actions that match your search criteria.
  • Select the action that you want to use.

№3. Find and replace

Question: What can you find and replace in an active editor?

Select one option from the list:

  • Commented lines
  • Text strings ✔
  • Anything, but you cannot replace it in the active editor
  • Reserved language literals

Explanation. The correct answer is Text strings.

You can find and replace text strings in an active editor in IntelliJ IDEA. You can also find and replace commented lines, but you cannot replace reserved language literals.

To find and replace text strings, you can use the following steps:

  1. Click on the Find icon in the toolbar.
  2. In the Find dialog, enter the text string that you want to find.
  3. Click on the Replace tab.
  4. Enter the text string that you want to replace the found text string with.
  5. Click on the Replace button.
  6. IntelliJ IDEA will find all the occurrences of the text string that you are looking for and replace them with the new text string.

You can also use regular expressions to find and replace text strings. Regular expressions are a powerful tool that can be used to find and replace text strings that match a specific pattern.

To learn more about regular expressions, you can refer to the IntelliJ IDEA documentation:

Regular Expressions: https://www.jetbrains.com/help/idea/regular-expressions.html


№4. Start the navigation

Question: Open "Navigate - Class" and type in "itera". What is the first result (from the top) that appears in the window?

Select one option from the list:

  • Iterable ✔
  • IterableNodeType
  • Iteration
  • Iterator

Explanation. The answer is Iterable.

Iterable is an interface that defines a set of methods that must be implemented by any container that can be iterated over. IterableNodeType is an abstract class that is used to represent the type of element in an iterable container. Iteration is a generic term for the process of traversing the elements of a container.


№5. IntelliJ IDEA search

Question: Let's summarize the commands with which we can navigate through code in IntelliJ IDEA. Match the commands' names with the search they perform.

Match the items from left and right columns:

  • Edit | Find | Replace - Replaces something in the current file
  • Edit | Find | Find - Searches through the current file
  • Help | Find Action - Searches for actions
  • Navigate commands - Search for classes, files, and symbols

Explanation. The Edit | Find | Replace command allows you to find and replace a specific text pattern in the current file. The Help | Find Action command allows you to search for actions in IntelliJ IDEA, such as refactorings, inspections, and code templates. The Navigate commands are a set of keyboard shortcuts that allow you to quickly navigate to different parts of your code, such as the declaration of a symbol, the usages of a symbol, or the definition of a class.

Here are some other navigation commands in IntelliJ IDEA:

  • Ctrl+B (or ⌘B on macOS): Go to the declaration of the symbol under the cursor.
  • Ctrl+Alt+Left (or ⌘[ on macOS): Go to the previous location in the editor.
  • Ctrl+Alt+Right (or ⌘] on macOS): Go to the next location in the editor.
  • Ctrl+E (or ⌘E on macOS): Open the recent files list.
  • Ctrl+Shift+E (or ⌘⇧E on macOS): Open the recent locations list.

№6. Navigate commands

Question: Using navigate commands, one can quickly jump to.

Select one option from the list:

  • any project file specified by name or numbers
  • the desired classes, files, or folders specified by ids
  • any project documentation specified by name
  • the desired classes, files, or symbols specified by names ✔

Explanation. The answer is the desired classes, files, or symbols specified by names.

The navigate commands in IntelliJ IDEA allow you to quickly jump to the desired classes, files, or symbols specified by names. For example, you can use the Ctrl+B (or ⌘B on macOS) shortcut to go to the declaration of the symbol under the cursor. You can also use the Navigate | Symbol menu to search for a symbol by name.

You cannot use the navigate commands to jump to project files specified by name or numbers, or to project documentation specified by name.


№7. Replace the target

Question: Look at the Manage your search page and find out which shortcut you should use to quickly replace the target of your search in the file?

Select one option from the list:

  • Ctrl+Alt+Shift+J ✔
  • Ctrl+Alt+Shift
  • Ctrl+Alt
  • Ctrl+Alt+Shift+G

Explanation. The answer is Ctrl+Alt+Shift+J.

According to the IntelliJ IDEA documentation, the shortcut for quickly replacing the target of your search in the file is Ctrl+Alt+Shift+J. This shortcut will open the Replace in File dialog, where you can specify the search string and the replacement string. You can then click the Replace all button to replace all occurrences of the search string in the file.

The other options are not shortcuts for replacing text in IntelliJ IDEA.


What is navigation through code?

Navigation through code is the process of moving around a codebase to find the information you need. This can be done using a variety of techniques, such as:

  • Using the IDE's navigation features: Most IDEs (Integrated Development Environments) have a variety of features that can help you navigate through code, such as:
    • Go to definition: This will take you to the declaration of the symbol under the cursor.
    • Find all references: This will show you all the places where the symbol under the cursor is used.
    • Navigate back and forth: This will allow you to move back and forth between previously visited locations.
    • Show usages: This will show you all the places where a symbol is used.
  • Using text search: You can also use text search to find specific lines of code or patterns of text.
  • Using a debugger: A debugger can be used to step through code line by line, which can be helpful for understanding how a particular piece of code works.

The best navigation technique to use will depend on the specific task you are trying to accomplish. For example, if you are trying to find the definition of a symbol, you would use the Go to definition feature. If you are trying to find all the places where a symbol is used, you would use the Find all references feature.

Navigation through code is an essential skill for any programmer. By mastering the techniques of navigation, you can save time and frustration when working with code.

Here are some additional tips for navigating through code:

  • Use descriptive variable and function names. This will make it easier to find the code you are looking for.
  • Use comments to document your code. This will also make it easier to understand what the code is doing.
  • Use a consistent coding style. This will make it easier to scan through code quickly.
  • Use a good IDE. A good IDE will have a variety of features that can help you navigate through code.