Skip to content

Commit

Permalink
Fix partial #118: add execute editor action
Browse files Browse the repository at this point in the history
  • Loading branch information
dubreuia committed Dec 12, 2019
1 parent 8242089 commit 2b3c68c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Gradle wrapper needs the environment variable JAVA_HOME to work, or the -D flag,
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

# Or use the -D flag
gradle build -D org.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64
gradle build -D org.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64
```

During the Intellij setup, you will need to manually set the Java version in "Gradle Settings", since it doesn't honor
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Supports configurable, Eclipse like, save actions, including "optimize imports",

Using the save actions plugin makes your code cleaner and more uniform across your code base by enforcing your code style and code rules every time you save. The settings file (see [files location](#files-location)) can be shared in your development team so that every developer has the same configuration.

The code style applied by the save actions plugin is the one configured your settings at "File > Settings > Editor > Code Style". For some languages, custom formatter may also be triggered by the save actions plugin. For example for Dart developers, enable "Use the dartfmt tool when formatting the whole file" option in "File > Settings > Editor > Code Style > Dart > Dartfmt".
The code style applied by the save actions plugin is the one configured your settings at "File > Settings > Editor > Code Style". For some languages, custom formatter (Dartfmt, Prettier, etc.) may also be triggered by the save actions plugin. See the [Editor Actions](#editor-actions) configuration for more information.

Thank you to JetBrains that supports our plugin: they provide an open-source license to us, necessary to build, test and deploy this plugin. Check out their products at [https://www.jetbrains.com](https://www.jetbrains.com/?from=intellij-save-actions-plugin).

Expand Down Expand Up @@ -46,6 +46,7 @@ Thank you to JetBrains that supports our plugin: they provide an open-source lic
- Rearrange code (reorder methods, fields, etc.)
- Include / exclude files with regex support
- Works on any file type (Java, Python, XML, etc.)
- Launch any editor action using "quick lists"
- 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 @@ -116,7 +117,20 @@ You can quickly toggle the plugin activation by using the "Enable Save Action" a

| Name | Description
| --- | ---
| Compile file | Enable / disable compiling of the modified file. The compiler might compile other files as well. **Warning: this option triggers one build action for each file, use sparingly (see [#128](https://github.com/dubreuia/intellij-plugin-save-actions/issues/128))**
| *\[experimental\]* Compile file | Enable / disable compiling of the modified file. The compiler might compile other files as well. **Warning: this feature is experimental, please post feedback in the github issues**
| *\[experimental\]* Reload file | Enable / disable reloading of the files in the running debugger, meaning the files will get compiled first. The compiler might compile other files as well. **Warning: this feature is experimental, please post feedback in the github issues**
| *\[experimental\]* Execute action | Enable / disable executing of an action using quick lists (using quick lists at "File > Settings > Appearance & Behavior > Quick Lists"). See [Editor Actions](#editor-actions) for more information **Warning: this feature is experimental, please post feedback in the github issues**

#### Editor Actions

Some languages requires specific actions, such as Dartfmt or Prettier:

- For Dart developers, enable "Use the dartfmt tool when formatting the whole file" option in "File > Settings > Editor > Code Style > Dart > Dartfmt".
- For Prettier (https://prettier.io/) users, read below.

Using the "Execute action" configuration, the plugin can launch arbitrary editor actions. While not all actions will work, it can be used to launch external tools, specific runs, etc. This feature is experimental, you can post your feedback on issue [#118](https://github.com/dubreuia/intellij-plugin-save-actions/issues/118).

The actions are implemented in the form of "quick lists", an IDE function that is used to define a list of actions that can be then executed. Quick lists can be configured at "File > Settings > Appearance & Behavior > Quick Lists", and once configured, one can be selected and used in the plugin, using the "Execution action" configuration drop down list.

### File

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/dubreuia/model/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public enum Action {

// Build

compile("Compile files (using \"Build > Build Project\")",
compile("[experimental] Compile files (using \"Build > Build Project\")",
build, false),

reload("Reload files in running debugger (using \"Run > Reload Changed Classes\")",
reload("[experimental] Reload files in running debugger (using \"Run > Reload Changed Classes\")",
build, false),

executeAction("Execute an action (using quick lists at \"File > Settings > Appearance & Behavior > Quick Lists\"",
executeAction("[experimental] Execute an action (using quick lists at " +
"\"File > Settings > Appearance & Behavior > Quick Lists\")",
build, false),

// Java fixes
Expand Down
65 changes: 43 additions & 22 deletions src/main/java/com/dubreuia/ui/BuildPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@
import com.dubreuia.model.Action;
import com.intellij.openapi.actionSystem.ex.QuickList;
import com.intellij.openapi.actionSystem.ex.QuickListsManager;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.IdeBorderFactory;
import org.jdesktop.swingx.combobox.ListComboBoxModel;

import javax.swing.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.dubreuia.model.Action.compile;
import static com.dubreuia.model.Action.executeAction;
import static com.dubreuia.model.Action.reload;
import static com.dubreuia.ui.Configuration.BOX_LAYOUT_MAX_HEIGHT;
import static com.dubreuia.ui.Configuration.BOX_LAYOUT_MAX_WIDTH;
import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.WEST;
import static java.lang.Short.MAX_VALUE;
import static java.text.MessageFormat.format;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

class BuildPanel {

private static final String TEXT_TITLE_ACTIONS = "Build actions";
private static final int QUICK_LIST_MAX_DESCRIPTION_LENGTH = 100;

private final List<String> quickLists;
private final List<QuickListWrapper> quickListElements;
private final ListComboBoxModel<QuickListWrapper> quickListModel;
private final JPanel panel;

BuildPanel(Map<Action, JCheckBox> checkboxes, List<String> quickLists) {
this.quickLists = quickLists;
quickListElements = new ArrayList<>();
quickListModel = new ListComboBoxModel<>(quickListElements);
quickListModel.addListDataListener(getListDataListener(quickLists));
Expand All @@ -44,21 +52,22 @@ class BuildPanel {
panel.add(wrap(checkboxes.get(compile), null));
panel.add(wrap(checkboxes.get(reload), null));
}
// @SuppressWarnings("unchecked")
// JComboBox<QuickListWrapper> comboBox = new ComboBox<QuickListWrapper>(quickListModel);
// panel.add(wrap(checkboxes.get(executeAction), comboBox));
@SuppressWarnings("unchecked")
JComboBox<QuickListWrapper> comboBox = new ComboBox<QuickListWrapper>(quickListModel);
panel.add(wrap(checkboxes.get(executeAction), comboBox));
panel.add(Box.createHorizontalGlue());
panel.setMinimumSize(new Dimension(MAX_VALUE, 0));
panel.setMinimumSize(new Dimension(Short.MAX_VALUE, 0));
}

JPanel getPanel() {
return panel;
}

void update(List<String> quickLists) {
void update() {
List<QuickListWrapper> quickListWrappers = Arrays
.stream(QuickListsManager.getInstance().getAllQuickLists())
.map(QuickListWrapper::new)
.sorted(comparing(QuickListWrapper::toString))
.collect(toList());
quickListElements.clear();
quickListElements.addAll(quickListWrappers);
Expand All @@ -71,16 +80,19 @@ void update(List<String> quickLists) {
}
}

private JComponent wrap(JCheckBox checkBox, JComboBox comboBox) {
private JComponent wrap(JCheckBox checkBox, @SuppressWarnings("rawtypes") JComboBox comboBox) {
JPanel wrapper = new JPanel();
wrapper.setLayout(new BorderLayout());
wrapper.add(checkBox, WEST);
wrapper.add(checkBox, BorderLayout.WEST);
if (comboBox != null) {
checkBox.addChangeListener(e -> comboBox.setEnabled(checkBox.isSelected()));
checkBox.addChangeListener(e -> {
comboBox.setEnabled(checkBox.isSelected());
update();
});
comboBox.setEnabled(checkBox.isSelected());
wrapper.add(comboBox, CENTER);
wrapper.add(comboBox, BorderLayout.SOUTH);
}
wrapper.setMaximumSize(new Dimension(BOX_LAYOUT_MAX_WIDTH, BOX_LAYOUT_MAX_HEIGHT));
wrapper.setMaximumSize(new Dimension(3000, 100));
return wrapper;
}

Expand All @@ -102,8 +114,16 @@ private String getId() {

@Override
public String toString() {
return quickList.getName()
+ (quickList.getDescription() == null ? "" : " (" + quickList.getDescription() + ")");
String name = quickList.getName();
String description = quickList.getDescription();
if (description == null) {
return name;
}
if (description.length() > QUICK_LIST_MAX_DESCRIPTION_LENGTH) {
description = description.substring(0, QUICK_LIST_MAX_DESCRIPTION_LENGTH);
description = description + "...";
}
return format("{0} ({1})", name, description);
}

}
Expand All @@ -121,8 +141,9 @@ public void intervalRemoved(ListDataEvent e) {

@Override
public void contentsChanged(ListDataEvent e) {
QuickListWrapper selectedItem = (QuickListWrapper) ((ListComboBoxModel) e.getSource())
.getSelectedItem();
@SuppressWarnings("rawtypes")
ListComboBoxModel source = (ListComboBoxModel) e.getSource();
QuickListWrapper selectedItem = (QuickListWrapper) source.getSelectedItem();
if (selectedItem == null) {
return;
}
Expand All @@ -133,4 +154,4 @@ public void contentsChanged(ListDataEvent e) {
};
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/dubreuia/ui/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private void updateExclusions() {
private void updateQuickLists() {
quickLists.clear();
quickLists.addAll(storage.getQuickLists());
buildPanel.update(quickLists);
buildPanel.update();
}

private void updateCheckboxEnabled(ActionEvent event) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
<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>
<li>Automatically fix Java inspections (Java IDEA only)</li>
<li>Launch any editor action using "quick lists"</li>
<li>[Java IDE] Eclipse configuration file `.epf` support</li>
<li>[Java IDE] Automatically fix Java inspections</li>
<li>[Java IDE] Compile project after save</li>
<li>[Java IDE] Reload debugger after save</li>
</ul>
<p><strong>Compatibility</strong></p>
Expand Down

0 comments on commit 2b3c68c

Please sign in to comment.