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

Apply a quicklist with description ending on ONLY only when the filtype matches #357

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ intellij {
// From branch 2018.3
// version '2018.3'
// From branch 2019.3
// version '193.3519.25-EAP-SNAPSHOT'
version 'LATEST-EAP-SNAPSHOT'
version '202.8194.7'
plugins = ['java', 'coverage']
pluginName 'Save Actions'
// Do not touch the plugin.xml file
Expand Down
31 changes: 22 additions & 9 deletions src/main/java/com/dubreuia/processors/BuildProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@
import com.intellij.openapi.actionSystem.ex.QuickList;
import com.intellij.openapi.actionSystem.ex.QuickListsManager;
import com.intellij.openapi.compiler.CompilerManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Stream;

Expand Down Expand Up @@ -100,14 +96,19 @@ public enum BuildProcessor implements Processor {
.collect(toList());
for (QuickList quickList : quickLists) {
String[] actionIds = quickList.getActionIds();
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
Editor selectedTextEditor = fileEditorManager.getSelectedTextEditor();
if (!quickListIsApplicable(quickList, selectedTextEditor)) {
continue;
}
for (String actionId : actionIds) {
AnAction action = ActionManager.getInstance().getAction(actionId);
if (action == null) {
continue;
}
Map<String, Object> data = new HashMap<>();
data.put(PROJECT.getName(), project);
data.put(EDITOR.getName(), FileEditorManager.getInstance(project).getSelectedTextEditor());
data.put(EDITOR.getName(), selectedTextEditor);
DataContext dataContext = getSimpleContext(data, null);
AnActionEvent event = AnActionEvent.createFromAnAction(action, null, UNKNOWN, dataContext);
action.actionPerformed(event);
Expand All @@ -122,6 +123,18 @@ public SaveCommand getSaveCommand(Project project, Set<PsiFile> psiFiles) {

;

private static boolean quickListIsApplicable(QuickList quickList, Editor editor) {
String description = quickList.getDescription();
if (description == null || !description.endsWith("ONLY")) {
return true;
}
if (editor instanceof EditorImpl) {
@NotNull String extension = ((EditorImpl) editor).getVirtualFile().getFileType().getDefaultExtension();
return description.endsWith(extension + " ONLY");
}
return false;
}

private final Action action;
private final BiFunction<Project, PsiFile[], Runnable> command;

Expand Down