Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple selected files using right-click #221

Closed
stephanj opened this issue Aug 19, 2024 · 2 comments
Closed

Add multiple selected files using right-click #221

stephanj opened this issue Aug 19, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@stephanj
Copy link
Collaborator

stephanj commented Aug 19, 2024

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 stephanj added the enhancement New feature or request label Aug 19, 2024
@stephanj stephanj self-assigned this Aug 19, 2024
@stephanj
Copy link
Collaborator Author

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 added a commit that referenced this issue Aug 19, 2024
stephanj added a commit that referenced this issue Aug 19, 2024
Feat #221
@stephanj
Copy link
Collaborator Author

image

stephanj added a commit that referenced this issue Dec 9, 2024
stephanj added a commit that referenced this issue Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant