jetbrains-plugin

Module: jetbrains-plugin Cohesion: 0.80 Members: 0

jetbrains-plugin

This document provides a technical overview of the jetbrains-plugin module, detailing its architecture, key components, and operational flow within the JetBrains IDE environment.

Code Buddy JetBrains Plugin

The jetbrains-plugin module implements the client-side integration for the Code Buddy AI assistant within JetBrains IDEs. Its primary purpose is to provide a seamless interface for developers to interact with an external Code Buddy server, leveraging AI capabilities for tasks like code explanation, review, test generation, and error fixing directly within their development workflow.

Architecture Overview

The plugin follows a standard JetBrains plugin architecture, extending various platform extension points to integrate its functionality. It consists of several core components:

  1. Actions: User-triggered commands (e.g., "Explain Code", "Fix Error") that gather context from the IDE.
  2. UI Components: A dedicated tool window (ChatPanel) for AI interaction and a status bar widget for connection status.
  3. API Client: A singleton (CodeBuddyClient) responsible for all communication with the external Code Buddy server.
  4. Settings: Persistent storage and a UI for configuring the server connection and AI model.

The general flow for an AI interaction initiated by a user action is:

  1. User triggers an AnAction.
  2. The action gathers relevant code context (selected text, file content, cursor position).
  3. A health check is performed against the Code Buddy server via CodeBuddyClient.
  4. If connected, a prompt message is constructed and sent to the ChatPanel.
  5. The ChatPanel then uses CodeBuddyClient to send the message to the server.
  6. The server's AI response is received and displayed in the ChatPanel.
graph TD
    A[User Interaction] --> B(AnAction Triggered)
    B --> C{Gather IDE Context}
    B --> D(CodeBuddyClient.healthCheck)
    D -- Connected --> E(Construct AI Prompt)
    E --> F(ChatPanel.sendMessageFromAction)
    F --> G(CodeBuddyClient.chat)
    G -- HTTP Request --> H[Code Buddy Server]
    H -- HTTP Response --> G
    G --> F
    F --> I(Display Response in ChatPanel)

    subgraph JetBrains Plugin
        B
        C
        D
        E
        F
        G
        I
        J[CodeBuddySettings]
        K[CodeBuddyConfigurable]
        L[CodeBuddyStatusBarWidget]
    end

    J -- Configures --> G
    K -- Modifies --> J
    L -- Periodically calls --> D

Key Components

1. Plugin Entry Point (jetbrains-plugin/src/main/resources/META-INF/plugin.xml)

This XML file is the manifest for the plugin, declaring its capabilities and how it integrates with the JetBrains platform.

2. User Actions (com.codebuddy.plugin.actions.*)

These classes extend com.intellij.openapi.actionSystem.AnAction and provide the entry points for user-initiated AI interactions.

Common Action Flow: All actions share a similar actionPerformed logic:

  1. Retrieve Project and Editor (if applicable) from AnActionEvent.
  2. Perform a CodeBuddyClient.healthCheck() on a pooled thread to ensure server connectivity.
  3. If the server is disconnected, display a warning using Messages.showWarningDialog.
  4. If connected, construct a detailed prompt message based on the action's purpose and gathered context.
  5. Open the "Code Buddy" tool window (ToolWindowManager.getInstance(project).getToolWindow("Code Buddy")?.show) and send the constructed message to the active ChatPanel instance via ChatPanel.getActiveInstance()?.sendMessageFromAction(message).
  6. The update method controls when an action is enabled and visible based on project context, editor selection, etc.

3. Core Communication (com.codebuddy.plugin.api.CodeBuddyClient)

This class is the sole interface for interacting with the external Code Buddy server.

4. User Interface (com.codebuddy.plugin.ui.*)

These classes manage the visual components of the plugin.

5. Configuration and Persistence (com.codebuddy.plugin.settings.*)

These classes handle the plugin's settings, allowing users to configure the Code Buddy server details.

Development Guidelines

Adding New Actions

  1. Create a new Kotlin class extending AnAction in com.codebuddy.plugin.actions.
  2. Implement actionPerformed(e: AnActionEvent) to gather context, perform a healthCheck(), construct a prompt, and call ChatPanel.getActiveInstance()?.sendMessageFromAction(message).
  3. Implement update(e: AnActionEvent) to control when the action is enabled/visible.
  4. Register the action in plugin.xml within an appropriate (e.g., CodeBuddy.Menu or CodeBuddy.EditorPopup).

UI Styling

The ChatPanel uses HTMLEditorKit and a StyleSheet to render messages. To modify the appearance of chat messages:

Error Handling

Threading Model

JetBrains IDEs are sensitive to UI blocking. Adhere to the following: