Skip to content

Commit

Permalink
wip #310 support rider using build actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dubreuia committed Jun 1, 2020
1 parent a347c5d commit 49fc7a7
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 15 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/dubreuia/core/component/SaveActionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.dubreuia.core.ExecutionMode;
import com.dubreuia.model.Action;
import com.dubreuia.model.IDE;
import com.dubreuia.model.Storage;
import com.dubreuia.model.StorageFactory;
import com.dubreuia.processors.Processor;
import com.dubreuia.processors.Processor.OrderComparator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
Expand Down Expand Up @@ -63,13 +65,15 @@ public static SaveActionManager getInstance() {
private boolean javaAvailable;
private final boolean compilingAvailable;
private StorageFactory storageFactory;
private final IDE ide;

private SaveActionManager() {
processors = new ArrayList<>();
running = false;
javaAvailable = false;
compilingAvailable = initCompilingAvailable();
storageFactory = DEFAULT;
ide = initIde();
}

private boolean initCompilingAvailable() {
Expand All @@ -80,6 +84,11 @@ private boolean initCompilingAvailable() {
}
}

private IDE initIde() {
String productName = ApplicationNamesInfo.getInstance().getProductName();
return IDE.valueOfOrDefault(productName);
}

void addProcessors(Stream<Processor> processors) {
processors.forEach(this.processors::add);
this.processors.sort(new OrderComparator());
Expand All @@ -105,6 +114,10 @@ public Storage getStorage(Project project) {
return storageFactory.getStorage(project);
}

public IDE getIde() {
return ide;
}

@Override
public void beforeAllDocumentsSaving() {
LOGGER.info("[+] Start SaveActionManager#beforeAllDocumentsSaving");
Expand All @@ -115,7 +128,6 @@ public void beforeAllDocumentsSaving() {

private void beforeDocumentsSaving(List<Document> documents) {
LOGGER.info("Locating psi files for " + documents.size() + " documents: " + documents);

Map<Project, Set<PsiFile>> projectPsiFiles = new HashMap<>();
documents.forEach(document -> stream(ProjectManager.getInstance().getOpenProjects())
.forEach(project -> ofNullable(PsiDocumentManager.getInstance(project).getPsiFile(document))
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/dubreuia/model/IDE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dubreuia.model;

import java.util.Arrays;

public enum IDE {

Rider("Rider"),

Other("Other");

private final String name;

IDE() {
this(null);
}

IDE(String name) {
this.name = name;
}

public static IDE valueOfOrDefault(String name) {
return Arrays.stream(values())
.filter(ide -> ide.name.equals(name))
.findFirst()
.orElse(IDE.Other);
}

}
12 changes: 12 additions & 0 deletions src/main/java/com/dubreuia/processors/BuildProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
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.Document;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;

import java.util.Arrays;
Expand All @@ -32,6 +35,7 @@
import static com.intellij.openapi.actionSystem.ActionPlaces.UNKNOWN;
import static com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR;
import static com.intellij.openapi.actionSystem.CommonDataKeys.PROJECT;
import static com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE_ARRAY;
import static com.intellij.openapi.actionSystem.impl.SimpleDataContext.getSimpleContext;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -80,9 +84,17 @@ public enum BuildProcessor implements Processor {
if (action == null) {
continue;
}
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
Document[] uncommittedDocuments = psiDocumentManager.getUncommittedDocuments();
VirtualFile[] uncommitedVirtualFiles = Arrays.stream(uncommittedDocuments)
.map(psiDocumentManager::getPsiFile)
.filter(Objects::nonNull)
.map(PsiFile::getVirtualFile)
.toArray(VirtualFile[]::new);
Map<String, Object> data = new HashMap<>();
data.put(PROJECT.getName(), project);
data.put(EDITOR.getName(), FileEditorManager.getInstance(project).getSelectedTextEditor());
data.put(VIRTUAL_FILE_ARRAY.getName(), uncommitedVirtualFiles);
DataContext dataContext = getSimpleContext(data, null);
AnActionEvent event = AnActionEvent.createFromAnAction(action, null, UNKNOWN, dataContext);
action.actionPerformed(event);
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/com/dubreuia/ui/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dubreuia.ui;

import com.dubreuia.core.component.SaveActionManager;
import com.dubreuia.model.Action;
import com.dubreuia.model.Storage;
import com.dubreuia.model.java.EpfStorage;
Expand All @@ -26,6 +27,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static com.dubreuia.model.Action.activate;
Expand All @@ -37,6 +39,7 @@
import static com.dubreuia.model.Action.reformatChangedCode;
import static com.dubreuia.model.Action.reload;
import static com.dubreuia.model.Action.unqualifiedStaticMemberAccess;
import static com.dubreuia.model.IDE.Rider;

public class Configuration implements Configurable {

Expand Down Expand Up @@ -82,14 +85,14 @@ private void initFirstLaunch() {
}

private void initActionListeners() {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
for (Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
checkbox.getValue().addActionListener(checkboxActionListener);
}
}

@Override
public boolean isModified() {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
for (Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
if (storage.isEnabled(checkbox.getKey()) != checkbox.getValue().isSelected()) {
return true;
}
Expand All @@ -105,7 +108,7 @@ public boolean isModified() {

@Override
public void apply() {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
for (Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
storage.setEnabled(checkbox.getKey(), checkbox.getValue().isSelected());
}
storage.setExclusions(new HashSet<>(exclusions));
Expand All @@ -128,7 +131,7 @@ public void reset() {
}

private void updateSelectedStateOfCheckboxes(Set<Action> selectedActions) {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
for (Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
boolean isSelected = selectedActions.contains(checkbox.getKey());
checkbox.getValue().setSelected(isSelected);
}
Expand Down Expand Up @@ -163,7 +166,7 @@ public String getHelpTopic() {

private JPanel initComponent() {
for (Action action : Action.values()) {
checkboxes.put(action, new JCheckBox(action.getText()));
checkboxes.put(action, initCheckBox(action));
}
generalPanel = new GeneralPanel(checkboxes);
formattingPanel = new FormattingPanel(checkboxes);
Expand Down Expand Up @@ -224,6 +227,17 @@ private JPanel initRootPanel(JPanel general, JPanel actions, JPanel build, JPane
return panel;
}

private JCheckBox initCheckBox(Action action) {
String text;
if ((SaveActionManager.getInstance().getIde().equals(Rider)) &&
action.equals(reformat) || action.equals(reformatChangedCode)) {
text = action.getText() + " (disabled on Rider, use 'Build Actions' below)";
} else {
text = action.getText();
}
return new JCheckBox(text);
}

private void updateInclusions() {
inclusions.clear();
inclusions.addAll(storage.getInclusions());
Expand All @@ -247,10 +261,11 @@ private void updateCheckboxEnabled(ActionEvent event) {
updateCheckboxGroupExclusive(event, reformat, reformatChangedCode);
updateCheckboxGroupExclusive(event, compile, reload);
updateCheckboxGroupExclusive(event, unqualifiedStaticMemberAccess, customUnqualifiedStaticMemberAccess);
updateCheckboxEnabledRider();
}

private void updateCheckboxEnabledIfActiveSelected() {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
for (Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
Action currentCheckBoxKey = checkbox.getKey();
if (!activate.equals(currentCheckBoxKey)
&& !activateOnShortcut.equals(currentCheckBoxKey)
Expand All @@ -274,6 +289,15 @@ private void updateCheckboxGroupExclusive(ActionEvent event, Action checkbox1, A
}
}

private void updateCheckboxEnabledRider() {
if (SaveActionManager.getInstance().getIde().equals(Rider)) {
checkboxes.get(reformat).setSelected(false);
checkboxes.get(reformat).setEnabled(false);
checkboxes.get(reformatChangedCode).setSelected(false);
checkboxes.get(reformatChangedCode).setEnabled(false);
}
}

private boolean isActiveSelected() {
boolean activateIsSelected = checkboxes.get(activate).isSelected();
boolean activateShortcutIsSelected = checkboxes.get(activateOnShortcut).isSelected();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/dubreuia/ui/FormattingPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.dubreuia.model.Action;
import com.intellij.ui.IdeBorderFactory;

import javax.swing.*;
import java.awt.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.util.Map;

import static com.dubreuia.model.Action.organizeImports;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/dubreuia/ui/GeneralPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.dubreuia.model.Action;
import com.intellij.ui.IdeBorderFactory;

import javax.swing.*;
import java.awt.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.util.Map;

import static com.dubreuia.model.Action.activate;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/dubreuia/ui/java/IdeSupportPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.intellij.ui.components.JBLabel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;

import static com.dubreuia.ui.Configuration.BOX_LAYOUT_MAX_HEIGHT;
import static com.dubreuia.ui.Configuration.BOX_LAYOUT_MAX_WIDTH;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/dubreuia/ui/java/InspectionPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.dubreuia.model.Action;
import com.intellij.ui.IdeBorderFactory;

import javax.swing.*;
import java.awt.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.util.Map;

import static com.dubreuia.model.ActionType.java;
Expand Down

0 comments on commit 49fc7a7

Please sign in to comment.