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

Feature/plugin actions #25

Merged
merged 11 commits into from
Jan 14, 2025
Prev Previous commit
Next Next commit
feat: Added plugin action. WIP
Signed-off-by: Nahuel Rodriguez <12597182+Nahuel92@users.noreply.github.com>
  • Loading branch information
Nahuel92 committed Nov 18, 2024
commit d37f2a062a4584e311f2cd68fb9e4229ced4b044
77 changes: 45 additions & 32 deletions src/main/java/io/github/nahuel92/pit4u/action/PIT4UAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.ide.projectView.impl.nodes.ClassTreeNode;
import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.PsiQualifiedNamedElement;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScopesCore.DirectoryScope;
Expand Down Expand Up @@ -70,25 +68,28 @@ private static boolean shouldShow(final AnActionEvent e) {
.getPath().contains("test");
}

private static void setDirectoryLabel(final AnActionEvent e, final PsiDirectoryNode psiDirectoryNode) {
final var javaDirectoryService = JavaDirectoryService.getInstance();
final var dirPackage = javaDirectoryService.getPackage(psiDirectoryNode.getValue());
if (dirPackage != null) {
e.getPresentation().setText("Mutate All Classes in " + dirPackage.getQualifiedName());
private static void setProjectLabel(final AnActionEvent e) {
final var project = e.getProject();
if (project != null) {
e.getPresentation().setText("Mutate all in " + project.getName());
}
}

private static void setClassLabel(final AnActionEvent e, final ClassTreeNode classTreeNode) {
if (classTreeNode.getPsiClass() instanceof PsiQualifiedNamedElement psiQualifiedNamedElement) {
e.getPresentation().setText("Mutate Class " + psiQualifiedNamedElement.getQualifiedName());
}
private static void setDirectoryLabel(final AnActionEvent e, final PsiDirectory psiDirectory) {
final var javaDirectoryService = JavaDirectoryService.getInstance();
final var dirPackage = javaDirectoryService.getPackage(psiDirectory);
e.getPresentation().setText("Mutate all in " + getDirectoryName(dirPackage, psiDirectory));
}

private static void setProjectLabel(final AnActionEvent e) {
final var project = e.getProject();
if (project != null) {
e.getPresentation().setText("Mutate Project " + project.getName());
private static String getDirectoryName(final PsiPackage dirPackage, final PsiDirectory psiDirectory) {
if (dirPackage == null || dirPackage.getQualifiedName().isBlank()) {
return psiDirectory.getName();
}
return dirPackage.getQualifiedName();
}

private static void setClassLabel(final AnActionEvent e, final PsiClass psiClass) {
e.getPresentation().setText("Mutate " + psiClass.getQualifiedName());
}

private static RunnerAndConfigurationSettings getRunConfig(final RunManager runManager) {
Expand All @@ -106,31 +107,35 @@ private static RunnerAndConfigurationSettings getRunConfig(final RunManager runM
}

private static PIT4UEditorStatus getPit4UEditorStatus(final AnActionEvent e, final Project project, final String basePath) {
final var selectedFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (selectedFile == null) {
return null;
}
final var status = new PIT4UEditorStatus();
final var path = Path.of(basePath);
status.setReportDir(path.resolve("target").toString());
status.setSourceDir(path.resolve("src").resolve("main").resolve("java").toString());
final var fullyQualifiedPackages = getFullyQualifiedPackages(selectedFile, project);
final var fullyQualifiedPackages = getFullyQualifiedPackages(e, project);
status.setTargetClasses(fullyQualifiedPackages);
status.setTargetTests(fullyQualifiedPackages);
return status;
}

private static String getFullyQualifiedPackages(final VirtualFile virtualFile, final Project project) {
private static String getFullyQualifiedPackages(final AnActionEvent event, final Project project) {
if (event.getData(CommonDataKeys.PSI_ELEMENT) instanceof PsiQualifiedNamedElement psiQualifiedNamedElement) {
return psiQualifiedNamedElement.getQualifiedName();
}

final var selectedFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
if (selectedFile == null) {
return null;
}

final var javaFiles = FileTypeIndex.getFiles(
JavaFileType.INSTANCE,
new DirectoryScope(project, virtualFile, true)
new DirectoryScope(project, selectedFile, true)
);
return javaFiles.stream()
.map(PsiManager.getInstance(project)::findFile)
.filter(e -> e instanceof PsiJavaFile)
.map(e -> ((PsiJavaFile) e).getPackageName())
.distinct()
.collect(Collectors.joining(".*, "));
.map(e -> ((PsiJavaFile) e).getPackageName() + ".*")
.collect(Collectors.joining(","));
}

private static void executeRunConfiguration(final RunnerAndConfigurationSettings runConfig) {
Expand Down Expand Up @@ -161,21 +166,29 @@ public ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}

private static boolean isProjectRoot(final Project project, final PsiDirectory psiDirectory) {
if (project == null || project.getBasePath() == null) {
return false;
}
return project.getBasePath().equals(psiDirectory.getVirtualFile().getPath());
}

@Override
public void update(@NotNull final AnActionEvent e) {
if (!shouldShow(e)) {
e.getPresentation().setEnabledAndVisible(false);
return;
}
e.getPresentation().setEnabledAndVisible(true);
final var navigatables = e.getData(CommonDataKeys.NAVIGATABLE_ARRAY);
if (navigatables == null || navigatables[0] == null) {
final var navigatable = e.getData(CommonDataKeys.NAVIGATABLE);
if (navigatable == null) {
return;
}
switch (navigatables[0]) {
case PsiDirectoryNode psiDirectoryNode -> setDirectoryLabel(e, psiDirectoryNode);
case ClassTreeNode classTreeNode -> setClassLabel(e, classTreeNode);
default -> setProjectLabel(e);
switch (navigatable) {
case PsiDirectory psiDirectory when isProjectRoot(e.getProject(), psiDirectory) -> setProjectLabel(e);
case PsiDirectory psiDirectory -> setDirectoryLabel(e, psiDirectory);
case PsiClass psiClass -> setClassLabel(e, psiClass);
default -> throw new IllegalStateException("Unexpected value: " + navigatable);
}
}

Expand Down