Skip to content

Commit

Permalink
Fix #131 Actions are now DumbAwareAction
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanj committed Jul 1, 2024
1 parent 284a281 commit d2e9a4e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/devoxx/genie/action/AddDirectoryAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.devoxx.genie.service.FileListManager;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
Expand All @@ -14,7 +15,7 @@

import static com.devoxx.genie.ui.util.WindowPluginUtil.ensureToolWindowVisible;

public class AddDirectoryAction extends AnAction {
public class AddDirectoryAction extends DumbAwareAction {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Expand Down Expand Up @@ -57,4 +58,14 @@ public void update(@NotNull AnActionEvent e) {
VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
e.getPresentation().setEnabledAndVisible(file != null && file.isDirectory());
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

@Override
public boolean isDumbAware() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devoxx.genie.action;

import com.devoxx.genie.service.LLMProviderService;
import com.devoxx.genie.service.ProjectContentService;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/devoxx/genie/action/AddFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

import com.devoxx.genie.service.FileListManager;
import com.devoxx.genie.ui.util.NotificationUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import static com.devoxx.genie.ui.util.WindowPluginUtil.ensureToolWindowVisible;

public class AddFileAction extends AnAction {
public class AddFileAction extends DumbAwareAction {

/**
* Add file to the window context.
*
* @param e the action event
*/
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();
Expand All @@ -29,10 +25,28 @@ public void actionPerformed(@NotNull AnActionEvent e) {

FileListManager fileListManager = FileListManager.getInstance();
VirtualFile selectedFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (selectedFile != null && !fileListManager.contains(selectedFile)) {
if (selectedFile != null && !selectedFile.isDirectory() && !fileListManager.contains(selectedFile)) {
fileListManager.addFile(selectedFile);
} else if (selectedFile != null && selectedFile.isDirectory()) {
NotificationUtil.sendNotification(project, "Cannot add directories. Please select a file.");
} else {
NotificationUtil.sendNotification(project, "File already added");
NotificationUtil.sendNotification(project, "File already added or no file selected");
}
}

@Override
public void update(@NotNull AnActionEvent e) {
VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
e.getPresentation().setEnabledAndVisible(file != null && !file.isDirectory());
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

@Override
public boolean isDumbAware() {
return true;
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/devoxx/genie/action/AddSnippetAction.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.devoxx.genie.action;

import com.devoxx.genie.service.FileListManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightVirtualFile;
import org.jetbrains.annotations.NotNull;

import static com.devoxx.genie.ui.util.WindowPluginUtil.ensureToolWindowVisible;

public class AddSnippetAction extends AnAction {
public class AddSnippetAction extends DumbAwareAction {

public static final String CODE_SNIPPET = "codeSnippet";
public static final Key<VirtualFile> ORIGINAL_FILE_KEY = Key.create("ORIGINAL_FILE");
Expand Down Expand Up @@ -72,8 +73,8 @@ private static void addSelectedFile(VirtualFile selectedFile) {
* @param selectionModel the selection model
* @param selectedText the selected text
*/
private void createAndAddVirtualFile(VirtualFile originalFile,
SelectionModel selectionModel,
private void createAndAddVirtualFile(@NotNull VirtualFile originalFile,
@NotNull SelectionModel selectionModel,
String selectedText) {
LightVirtualFile virtualFile = new LightVirtualFile(originalFile.getName(), selectedText);
virtualFile.setFileType(fileType);
Expand All @@ -83,4 +84,15 @@ private void createAndAddVirtualFile(VirtualFile originalFile,
virtualFile.putUserData(SELECTION_END_KEY, selectionModel.getSelectionEnd());
FileListManager.getInstance().addFile(virtualFile);
}


@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

@Override
public boolean isDumbAware() {
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Mon Jul 01 20:49:31 CEST 2024
#Mon Jul 01 21:30:51 CEST 2024
version=0.2.0

0 comments on commit d2e9a4e

Please sign in to comment.