Skip to content

Commit

Permalink
Add activation option for batch
Browse files Browse the repository at this point in the history
  • Loading branch information
dubreuia committed Jan 16, 2019
1 parent e513963 commit e1fa97a
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Thank you to JetBrains that supports our plugin: they provide an open-source lic
### All JetBrains products

- Optimize imports
- Run on file save or shortcut (or both)
- Run on file save, shortcut, batch (or a combination)
- Run on multiple files by choosing a scope
- Reformat code (whole file or only changed text)
- Rearrange code (reorder methods, fields, etc.)
- Include / exclude files with regex support
- Works any file type (Java, Python, XML, etc.)
- Works on any file type (Java, Python, XML, etc.)
- Uses a settings file per project you can commit (see [Files location](#files-location))
- Available keymaps and actions for activation (see [Keymap and actions](#keymap-and-actions))

Expand Down Expand Up @@ -94,6 +94,7 @@ You can quickly toggle the plugin activation by using the "Enable Save Action" a
| --- | ---
| Activate save actions on file save | Enable / disable the plugin on file save. Before saving each file, it will perform the configured actions below
| Activate save actions on shortcut | Enable / disable the plugin on shortcut, by default "CTRL + SHIFT + S" (configured in "File > Keymaps > Main menu > Code > Save Actions")
| Activate save actions on batch | Enable / disable the plugin on batch, by using "Code > Save Actions > Execute on multiple files"
| No action if compile errors | Enable / disable no action if there are compile errors. Applied to each file individually

### Global
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/dubreuia/core/action/BatchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import static com.dubreuia.core.ExecutionMode.batch;
import static com.dubreuia.core.component.Component.COMPONENT_NAME;
import static com.dubreuia.core.component.SaveActionManager.LOGGER;
import static com.dubreuia.model.Action.activate;
import static com.dubreuia.model.Action.activateOnBatch;
import static java.util.Collections.synchronizedSet;

/**
* This action runs the save actions on the given scope of files. The user is asked for the scope using a
* standard IDEA dialog. It delegates to {@link SaveActionManager}. Originally based on
* This action runs the save actions on the given scope of files, only if property
* {@link com.dubreuia.model.Action#activateOnShortcut} is enabled. The user is asked for the scope using a standard
* IDEA dialog. It delegates to {@link SaveActionManager}. Originally based on
* {@link com.intellij.codeInspection.inferNullity.InferNullityAnnotationsAction}.
*
* @author markiewb
Expand All @@ -57,7 +58,7 @@ public void visitFile(PsiFile psiFile) {
psiFiles.add(psiFile);
}
});
SaveActionManager.getInstance().processPsiFilesIfNecessary(project, psiFiles, activate, batch);
SaveActionManager.getInstance().processPsiFilesIfNecessary(project, psiFiles, activateOnBatch, batch);
LOGGER.info("End BatchAction#analyze processed " + psiFiles.size() + " files");
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/dubreuia/model/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public enum Action {
activateOnShortcut("Activate save actions on shortcut (default \"CTRL + SHIFT + S\")",
activation, false),

activateOnBatch("Activate save actions on batch (\"Code > Save Actions > Execute on multiple files\")",
activation, false),

noActionIfCompileErrors("No action if compile errors",
activation, false),

Expand All @@ -39,7 +42,7 @@ public enum Action {

// Build

compile("Compile file",
compile("Compile files (using \"Build > Build Project\")",
build, false),

// Java fixes
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/dubreuia/processors/BuildProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.util.function.BiFunction;
import java.util.stream.Stream;

import static com.dubreuia.core.ExecutionMode.normal;
import static com.dubreuia.core.ExecutionMode.shortcut;
import static com.dubreuia.utils.Helper.toVirtualFiles;

/**
Expand All @@ -24,10 +22,8 @@
public enum BuildProcessor implements Processor {

compile(Action.compile,
EnumSet.of(normal, shortcut),
(project, psiFiles) -> () -> {
if (SaveActionManager.getInstance().isCompilingAvailable()) {
// TODO this doesn't work in webstorm
CompilerManager.getInstance(project).compile(toVirtualFiles(psiFiles), null);
}
}),
Expand All @@ -36,12 +32,10 @@ public enum BuildProcessor implements Processor {

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

BuildProcessor(Action action, Set<ExecutionMode> modes, BiFunction<Project, PsiFile[], Runnable> command) {
BuildProcessor(Action action, BiFunction<Project, PsiFile[], Runnable> command) {
this.action = action;
this.command = command;
this.modes = modes;
}

@Override
Expand All @@ -51,7 +45,7 @@ public Action getAction() {

@Override
public Set<ExecutionMode> getModes() {
return modes;
return EnumSet.allOf(ExecutionMode.class);
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/dubreuia/ui/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;

import static com.dubreuia.model.Action.activate;
import static com.dubreuia.model.Action.activateOnBatch;
import static com.dubreuia.model.Action.activateOnShortcut;
import static com.dubreuia.model.Action.customUnqualifiedStaticMemberAccess;
import static com.dubreuia.model.Action.reformat;
Expand Down Expand Up @@ -227,7 +228,9 @@ private void updateCheckboxEnabled(ActionEvent event) {
private void updateCheckboxEnabledIfActiveSelected() {
for (Map.Entry<Action, JCheckBox> checkbox : checkboxes.entrySet()) {
Action currentCheckBoxKey = checkbox.getKey();
if (!activate.equals(currentCheckBoxKey) && !activateOnShortcut.equals(currentCheckBoxKey)) {
if (!activate.equals(currentCheckBoxKey)
&& !activateOnShortcut.equals(currentCheckBoxKey)
&& !activateOnBatch.equals(currentCheckBoxKey)) {
checkbox.getValue().setEnabled(isActiveSelected());
}
}
Expand Down Expand Up @@ -255,7 +258,8 @@ private void updateCheckboxEnabledGroupStaticExclusive(ActionEvent event) {
private boolean isActiveSelected() {
boolean activateIsSelected = checkboxes.get(activate).isSelected();
boolean activateShortcutIsSelected = checkboxes.get(activateOnShortcut).isSelected();
return activateIsSelected || activateShortcutIsSelected;
boolean activateBatchIsSelected = checkboxes.get(activateOnBatch).isSelected();
return activateIsSelected || activateShortcutIsSelected || activateBatchIsSelected;
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/dubreuia/ui/GeneralPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import java.util.Map;

import static com.dubreuia.model.Action.activate;
import static com.dubreuia.model.Action.activateOnBatch;
import static com.dubreuia.model.Action.activateOnShortcut;
import static com.dubreuia.model.Action.noActionIfCompileErrors;

class GeneralPanel {

private static final String TEXT_TITLE_ACTIONS = "General";

private Map<Action, JCheckBox> checkboxes;
private final Map<Action, JCheckBox> checkboxes;

GeneralPanel(Map<Action, JCheckBox> checkboxes) {
this.checkboxes = checkboxes;
Expand All @@ -27,6 +28,7 @@ JPanel getPanel() {
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(checkboxes.get(activate));
panel.add(checkboxes.get(activateOnShortcut));
panel.add(checkboxes.get(activateOnBatch));
panel.add(checkboxes.get(noActionIfCompileErrors));
panel.add(Box.createHorizontalGlue());
panel.setMinimumSize(new Dimension(Short.MAX_VALUE, 0));
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<ul>
<li>Optimize imports</li>
<li>Run on file save or shortcut (or both)</li>
<li>Run on file save, shortcut, batch (or a combination)</li>
<li>Run on multiple files by choosing a scope</li>
<li>Reformat code (whole file or only changed text)</li>
<li>Rearrange code (reorder methods, fields, etc.)</li>
<li>Include / exclude files with regex support</li>
<li>Works any file type (Java, Python, XML, etc.)</li>
<li>Works on any file type (Java, Python, XML, etc.)</li>
<li>Uses a settings file per project you can commit</li>
<li>Available keymaps and actions for activation</li>
<li>Eclipse configuration file `.epf` support (Java IDE only)</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static com.dubreuia.integration.ActionTestFile.Reformat_OK_Import_OK;
import static com.dubreuia.integration.ActionTestFile.Reformat_OK_Rearrange_OK;
import static com.dubreuia.model.Action.activate;
import static com.dubreuia.model.Action.activateOnBatch;
import static com.dubreuia.model.Action.activateOnShortcut;
import static com.dubreuia.model.Action.organizeImports;
import static com.dubreuia.model.Action.rearrange;
Expand Down Expand Up @@ -46,7 +47,7 @@ void should_reformat_with_shortcut_produces_indented_file_on_shortcut() {

@Test
void should_reformat_as_batch_produces_indented_file() {
storage.setEnabled(activate, true);
storage.setEnabled(activateOnBatch, true);
storage.setEnabled(reformat, true);
assertSaveActionBatch(Reformat_KO_Import_KO, Reformat_OK_Import_KO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static com.dubreuia.integration.ActionTestFile.UseBlocks_OK;
import static com.dubreuia.model.Action.accessCanBeTightened;
import static com.dubreuia.model.Action.activate;
import static com.dubreuia.model.Action.activateOnBatch;
import static com.dubreuia.model.Action.activateOnShortcut;
import static com.dubreuia.model.Action.customUnqualifiedStaticMemberAccess;
import static com.dubreuia.model.Action.explicitTypeCanBeDiamond;
Expand Down Expand Up @@ -77,7 +78,7 @@ void should_fieldCanBeFinal_add_final_to_field_on_shortcut() {

@Test
void should_fieldCanBeFinal_add_final_to_field_on_batch() {
storage.setEnabled(activate, true);
storage.setEnabled(activateOnBatch, true);
storage.setEnabled(fieldCanBeFinal, true);
assertSaveActionBatch(FieldCanBeFinal_KO, FieldCanBeFinal_OK);
}
Expand All @@ -92,9 +93,9 @@ void should_localCanBeFinal_add_final_to_local_variable_and_parameters() {
@Test
@Disabled("do not work")
void should_methodMayBeStatic_add_static_keyword_to_method() {
storage.setEnabled(activateOnShortcut, true);
storage.setEnabled(activate, true);
storage.setEnabled(methodMayBeStatic, true);
assertSaveActionShortcut(MethodMayBeStatic_KO, MethodMayBeStatic_OK);
assertSaveAction(MethodMayBeStatic_KO, MethodMayBeStatic_OK);
}

@Test
Expand Down

0 comments on commit e1fa97a

Please sign in to comment.