Class Projects

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class Projects extends AnahataToolkit
A toolkit for interacting with the IntelliJ IDEA Project APIs.

This toolkit acts as a bridge between the model-agnostic Anahata core framework and the IntelliJ Platform, providing real-time visibility into open projects, directory structures, and project lifecycle management operations.

Author:
anahata
  • Constructor Details

    • Projects

      public Projects()
      Default constructor for the IntelliJ Projects toolkit.
  • Method Details

    • getChildrenProviders

      public List<ContextProvider> getChildrenProviders()
      Gets the list of immediate child context providers.

      Returns the list of active project child providers, implementing a dynamic synchronization pattern that captures newly opened or closed projects in real-time.

      Specified by:
      getChildrenProviders in interface ContextProvider
      Overrides:
      getChildrenProviders in class AnahataToolkit
      Returns:
      A list of context providers.
    • syncProjects

      private void syncProjects()
      Synchronizes project context providers with currently open IDE projects.
    • getProjectProvider

      public Optional<ProjectContextProvider> getProjectProvider(String projectPath)
      Returns a project context provider by path.
      Parameters:
      projectPath - The absolute path of the project.
      Returns:
      An Optional containing the provider.
    • getSystemInstructions

      public List<String> getSystemInstructions() throws Exception
      Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.

      Returns the master instruction guide on how to resolve files, folders, and structures within the active IntelliJ IDE workspace.

      Returns:
      A list of system instruction strings.
      Throws:
      Exception - if an error occurs during instruction generation.
    • populateMessage

      public void populateMessage(RagMessage ragMessage)
      Populates the given RagMessage with dynamic, just-in-time context parts. These parts are appended to the end of the user's prompt (RAG).

      Scans all open projects in the IntelliJ Platform, extracts their base paths, and appends their physical file-system trees to the RAG message to provide full workspace observability.

      Parameters:
      ragMessage - The message to be augmented with context.
    • appendDirectoryStructure

      private void appendDirectoryStructure(Path dir, StringBuilder sb, String indent) throws IOException
      Recursively appends a clean, machine-readable directory structure to the builder.

      Implements smart filtering to skip heavy target, out, and hidden folders, and utilizes a depth control threshold to prevent context-window token blowup.

      Parameters:
      dir - The active directory path.
      sb - The string builder workspace.
      indent - The prefix indent spacer.
      Throws:
      IOException - if directory reading fails.
    • getOpenProjects

      public List<String> getOpenProjects()
      Returns a list of absolute paths of all currently open IntelliJ projects.
      Returns:
      List of project base paths.
    • closeProjects

      public void closeProjects(List<String> projectPaths)
      Closes specific open projects in the IDE.
      Parameters:
      projectPaths - A list of absolute paths of the projects to close.
    • openProject

      public String openProject(String projectPath)
      Opens a project directory programmatically in the IntelliJ IDE.

      Performs a thread-safe operation on the Event Dispatch Thread (EDT) and uses progressive API resolution with reflection to guarantee compatibility across different IntelliJ platform versions.

      Parameters:
      projectPath - The absolute path of the project directory to open.
      Returns:
      A status message describing the outcome of the open operation.
    • setProjectProviderEnabled

      public void setProjectProviderEnabled(String projectPath, boolean enabled)
      Toggles the context provider state for a specific project.

      Locates the appropriate provider by its canonical path and updates its activation state.

      Parameters:
      projectPath - The absolute path of the project.
      enabled - Whether to enable the context provider.
    • buildProject

      public String buildProject(String projectPath, boolean rebuild) throws AgiToolException
      Compiles a project and returns a synchronous build result (error/warning counts).

      Mirrors the NetBeans Projects.invokeAction build/rebuild capability using IntelliJ's CompilerManager. Compilation is triggered on the EDT and this method blocks on a latch off the EDT until the asynchronous build completes, so the model receives the actual outcome rather than a fire-and-forget acknowledgement.

      Parameters:
      projectPath - the absolute path of the open project to build.
      rebuild - true to force a full rebuild; false for an incremental make.
      Returns:
      a human-readable build summary.
      Throws:
      AgiToolException - if the project is not open or the build is interrupted.
    • saveAllDocuments

      public String saveAllDocuments()
      Persists all unsaved editor documents across the IDE to disk.

      Useful before a build or an external tool run so on-disk state matches the editor.

      Returns:
      a confirmation message.
    • findProjectByPath

      private com.intellij.openapi.project.Project findProjectByPath(String projectPath)
      Resolves an open Project by its canonical base path.
      Parameters:
      projectPath - the absolute base path.
      Returns:
      the matching open project, or null if none is open at that path.
    • getProjectSdk

      public String getProjectSdk(String projectPath) throws AgiToolException
      Returns information about the currently configured Project SDK for an open project.
      Parameters:
      projectPath - The absolute path of the open project.
      Returns:
      A formatted summary of the configured SDK, or an unconfigured warning.
      Throws:
      AgiToolException - if the project is not open.
    • listAvailableSdks

      public String listAvailableSdks()
      Lists all JDKs registered in IntelliJ's ProjectJdkTable as well as suggested system JDK paths.
      Returns:
      A formatted list of available SDKs and detected system JDK paths.
    • setProjectSdk

      public String setProjectSdk(String projectPath, String sdkNameOrHomePath) throws AgiToolException
      Configures the project SDK for an open project.

      If an existing registered SDK name is supplied (e.g. "21" or "corretto-25"), it is attached directly to the project. If a directory path to a JDK home is supplied, the JDK is first registered in ProjectJdkTable and then attached to the project.

      Parameters:
      projectPath - The absolute path of the open project.
      sdkNameOrHomePath - The registered SDK name or absolute path to a JDK home directory.
      Returns:
      A confirmation message describing the configured SDK.
      Throws:
      AgiToolException - if the project is not open or the SDK could not be configured.
    • autoConfigureProjectSdk

      public String autoConfigureProjectSdk(String projectPath) throws AgiToolException
      Automatically discovers the best available JDK on the system and assigns it as the Project SDK.
      Parameters:
      projectPath - The absolute path of the open project.
      Returns:
      A status message describing the configured SDK.
      Throws:
      AgiToolException - if the project is not open or no JDK can be found.
    • ensureProjectSdkConfigured

      private com.intellij.openapi.projectRoots.Sdk ensureProjectSdkConfigured(com.intellij.openapi.project.Project project)
      Ensures that the specified project has a valid Project SDK attached, auto-detecting and registering one if needed.
      Parameters:
      project - The open project to inspect.
      Returns:
      The active or newly configured Sdk, or null if no JDK could be found.