IntelliJ IDEA Java JetBrains AI

From Code to Clarity With the Redesigned Structure Tool Window

Developers usually spend more time reading existing code than writing new code. To understand the existing codebase of an application, developers spend a good amount of time looking at how various frameworks and libraries are configured and how different components interact with each other.

While developing JetBrains AI Assistant, we enriched prompts with context extracted from file links and dependencies. This context proved valuable for both AI and human understanding.

In this article, you will learn:

  • How our R&D on AI Assistant led to redesigning the Structure tool window.
  • How to use the new Structure tool window’s Logical view to explore and understand your existing codebase.
  • How you can perform various context-relevant actions from the Structure tool window itself.

Background

AI assistants can produce disappointing results when asked to generate or explain code. This is because they can lack the rich context that developers are aware of. A project is not just code. It’s a complex interplay of components, both explicit like method calls, and implicit, defined by the framework being used. Understanding these intricate connections is crucial for accurate AI assistance.

To get the best results from AI Assistant, we need to describe all this context to it. This same context would be invaluable for developers. In IntelliJ IDEA, the main code navigation tool is the Project tool window. It provides a code-level view of the project, including folders, packages, and files. Though familiar, the Project tool window doesn’t show links between code components. Developers have to figure those out themselves by looking for class usages for explicit links and reading the framework documentation for implicit ones.

Unsurprisingly, IntelliJ IDEA knows a lot about project internals. While implementing support for a language or framework in the IDE, we, as developers, have to understand how a technology works under the hood. For example, if you open a file from the project implemented with the Spring Boot framework, you’ll see components like bean indicators, injection points, and web API endpoints.

We recently introduced an internal API to implement domain context providers – code that can collect extended information about a project structure, used tech stack, etc. The first step was to provide better context for AI Assistant. We conducted R&D by seamlessly enriching prompts with the information from domain providers – this showed good results!

The second step involved using this information to show the code structure from the framework perspective. In IntelliJ IDEA, the Structure tool window presents the structure of a selected file. We decided to enhance this window and add a Logical view, which illustrates the file structure from the framework point of view. This means that you can now see how a selected piece of code, such as a file or class, is connected to other parts of the application. This context is sometimes more valuable than the code itself because it helps you understand how an application works.

Structure tool window redesign

Let’s explore a Spring Boot application to learn how the new Structure tool window provides better insights into your application by providing a logical structure of the components, indicating how they all work together.

The entry point of any Spring Boot application is the main application class annotated with @SpringBootApplication. The class code’s physical structure is simple:

@SpringBootApplication
public class BlogApplication {

	public static void main(String[] args) {
		SpringApplication.run(BlogApplication.class, args);
	}
}

The magic of Spring Boot is hidden in bean configurations – classes that instantiate required services based on various conditions and put them into the Spring context. Now, we can see it!

Domain context providers know which configurations are scanned and which beans are instantiated by bean configuration – it’s all revealed in the structure.

Now, let’s take a look at how the Structure tool window shows the logical view of a Spring Boot REST controller.

The Structure tool window displays the autowired Spring beans in the Autowires node and the API endpoints defined in the controller class. When you click on any API endpoint, you will be taken to that method in the editor. You can also click on the API gutter icon to invoke that API endpoint in the HTTP Client.

The Spring Framework provides event publishing and consuming capabilities using ApplicationEventPublisher. If the selected Spring bean publishes events using ApplicationEventPublisher, you can see these details under the Publishers node.

Similarly, if the Spring bean implements event listeners using @EventListener, the listeners’ details are shown under the Listeners node.

Let’s take another example: JPA entities.

The Structure tool window also provides a Logical view of JPA entities by providing a lot of context, such as the column mappings, relationship with other entities, Spring Data JPA repositories associated with the entity, and related DTOs and projections:

If you select the Entity node, you can see the DDL gutter icon which you can use to generate an SQL script to create the table.

Similarly, IntelliJ IDEA provides a Logical view for other Spring components like MVC controllers, services, repositories, and configuration classes.

Conclusion

An application’s complex structure consists of various parts, including code, dependencies, build processes, and deployment scripts. A solid grasp of the connections between these components is crucial for a comprehensive understanding of the application’s architecture and functionality. 

By introducing domain context providers, we can explain the code structure from another point of view, revealing hidden framework-specific connections between various components. This can be useful for AI Assistant, so its answers could be improved, as well as for developers to help them understand the code better.

The redesigned Structure tool window’s Logical view allows you to see the application structure and navigate through linked components. The context-specific actions associated with the selected component make it easy to perform various tasks right from the Structure tool window itself.

Currently, the IDE supports Jakarta EE and Spring, and we plan to expand this support to other technologies like frameworks, build tools, and Docker configuration files.

You can find the official documentation for Logical structure here .

The redesigned Structure tool window is available in IntelliJ IDEA Ultimate 2024.3, and the context-relevant actions support will be available from version 2024.3.1. You can install the latest version of the IDE from the JetBrains Toolbox App, or you can download and install it from our website.

Download IntelliJ IDEA

image description