devoxx / DevoxxGenieIDEAPlugin

DevoxxGenie is a plugin for IntelliJ IDEA that uses local LLM's (Ollama, LMStudio, GPT4All, Llama.cpp and Exo) and Cloud based LLMs to help review, test, explain your project code.
https://devoxx.com
MIT License
156 stars 24 forks source link

Add multiple selected files using right-click #221

Closed stephanj closed 1 month ago

stephanj commented 1 month ago

When more than one file is selected and user opens the popup menu, the "Add file to window context" should add all selected files not just one.

image

It should also say : "Add File(s) to Context Window"

image

stephanj commented 1 month ago

According to Claude this is what needs happening:

To add the feature of selecting multiple files for the "Add file to window context" menu, we need to modify a few components of the plugin. Let's go through the changes step by step:

  1. First, we need to modify the AddFileAction class to handle multiple file selections. Here's how we can update it:
public class AddFileAction extends DumbAwareAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            return;
        }

        ensureToolWindowVisible(project);

        FileListManager fileListManager = FileListManager.getInstance();
        VirtualFile[] selectedFiles = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);

        if (selectedFiles != null && selectedFiles.length > 0) {
            List<VirtualFile> filesToAdd = new ArrayList<>();
            for (VirtualFile file : selectedFiles) {
                if (!file.isDirectory() && !fileListManager.contains(file)) {
                    filesToAdd.add(file);
                }
            }

            if (!filesToAdd.isEmpty()) {
                fileListManager.addFiles(filesToAdd);
                NotificationUtil.sendNotification(project, "Added " + filesToAdd.size() + " file(s) to prompt context");
            } else {
                NotificationUtil.sendNotification(project, "No new files to add or only directories selected");
            }
        } else {
            NotificationUtil.sendNotification(project, "No files selected");
        }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
        e.getPresentation().setEnabledAndVisible(files != null && files.length > 0);
    }
}
  1. Next, we need to update the FileListManager class to handle adding multiple files at once:
public class FileListManager {
    // ... existing code ...

    public void addFiles(@NotNull List<VirtualFile> newFiles) {
        List<VirtualFile> actuallyAddedFiles = new ArrayList<>();
        for (VirtualFile file : newFiles) {
            if (!files.contains(file)) {
                files.add(file);
                actuallyAddedFiles.add(file);
            }
        }
        if (!actuallyAddedFiles.isEmpty()) {
            notifyObserversOfBatchAdd(actuallyAddedFiles);
        }
    }

    private void notifyObserversOfBatchAdd(List<VirtualFile> addedFiles) {
        for (FileListObserver observer : observers) {
            observer.filesAdded(addedFiles);
        }
    }

    // ... existing code ...
}
  1. We also need to update the FileListObserver interface to include a method for batch file additions:
public interface FileListObserver {
    void fileAdded(VirtualFile file);
    void filesAdded(List<VirtualFile> files);
    void allFilesRemoved();
}
  1. Now, we need to update any classes that implement FileListObserver to handle the new filesAdded method. For example, the PromptContextFileListPanel class:
public class PromptContextFileListPanel extends JPanel implements FileListObserver {
    // ... existing code ...

    @Override
    public void filesAdded(List<VirtualFile> files) {
        for (VirtualFile file : files) {
            FileEntryComponent fileLabel = new FileEntryComponent(project, file, this);
            filesPanel.add(fileLabel);
        }
        updateFilesPanelVisibility();
        updateUIState();
    }

    // ... existing code ...
}
  1. Finally, we should update the plugin.xml file to ensure the action is available for multiple selections:
<action id="AddFileToContextWindow" 
        class="com.devoxx.genie.action.AddFileAction" 
        text="Add File(s) to Window Context" 
        description="Add selected file(s) to the prompt context">
    <add-to-group group-id="ProjectViewPopupMenu" anchor="after" relative-to-action="AddToFavorites"/>
</action>

These changes will allow users to select multiple files and add them all to the window context at once. The action will be enabled as long as at least one file is selected, and it will handle both single and multiple file selections appropriately.

Remember to update any relevant tests to cover the new functionality, especially for the AddFileAction and FileListManager classes.

This implementation maintains the existing functionality while extending it to handle multiple files. It also provides feedback to the user about how many files were added or if no new files could be added (e.g., if all selected files were already in the context or if only directories were selected).

stephanj commented 1 month ago

image