Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Minor aesthetic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Landolfi committed Feb 19, 2018
1 parent d7b0363 commit a34ec1e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
4 changes: 2 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>ch.ricardo.plugins.intellij.mockery</id>
<name>Golang mockery plugin</name>
<version>1.0</version>
<name>Golang mockery</name>
<version>1.0.1</version>
<vendor email="[email protected]" url="https:/github.com/ricardo-ch">Ricardo.ch</vendor>

<description><![CDATA[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.intellij.ide.util.ChooseElementsDialog;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.fileEditor.FileDocumentManager;
Expand All @@ -36,6 +37,7 @@

public class GenMockeryActionHandler extends EditorActionHandler {

// Dialog for allowing the user to choose which mocks he wants to generate
private class InterfaceChooser extends ChooseElementsDialog<String> {

private InterfaceChooser (Project project, List<String> interfaces) {
Expand All @@ -53,31 +55,39 @@ protected Icon getItemIcon(String s) {
}
}

// ProcessListener implementation which triggers project refresh on termination
private class ProjectRefreshingProcessListener implements ProcessListener {

@Override
public void doExecute(Editor editor, Caret caret, DataContext dataContext) {
final Project project;

final StringBuilder out = new StringBuilder();
final StringBuilder err = new StringBuilder();
private ProjectRefreshingProcessListener(Project project) {
this.project = project;
}

ArrayList<String> interfaces = new ArrayList<>();
for (String s : editor.getDocument().getText().split("\\r?\\n")) {
Pattern pattern = Pattern.compile("type (.*?) interface");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
{
interfaces.add(matcher.group(1));
}
@Override public void startNotified(@NotNull ProcessEvent processEvent) {}
@Override public void processWillTerminate(@NotNull ProcessEvent processEvent, boolean b) { }
@Override public void onTextAvailable(@NotNull ProcessEvent processEvent, @NotNull Key key) { }

@Override
public void processTerminated(@NotNull ProcessEvent processEvent) {
VfsUtil.markDirtyAndRefresh(true, true, true, project.getBaseDir());
}

HintManager.getInstance().showErrorHint(editor, "not supported");
}

if (interfaces.isEmpty()) {
// This method is called once the user selects the 'Golang mockery' Action
@Override
public void doExecute(@NotNull Editor editor, Caret caret, DataContext dataContext) {

// Get all interfaces
List<String> interfaces = getInterfacesInDocument(editor.getDocument());

if (interfaces.isEmpty()) {
HintManager.getInstance().showErrorHint(editor, "No interfaces found");
return;
}

// Let the user choose those he wants mocks for
InterfaceChooser chooser = new InterfaceChooser(editor.getProject(), interfaces);
chooser.selectElements(interfaces);
List<String> selectedInterfaces = chooser.showAndGetResult();
Expand All @@ -86,6 +96,7 @@ public void doExecute(Editor editor, Caret caret, DataContext dataContext) {
return;
}

// Do the generation
Path filePath = Paths.get(FileDocumentManager.getInstance().getFile(editor.getDocument()).getCanonicalPath());

GeneralCommandLine commandLine = PtyCommandLine.isEnabled() ? new PtyCommandLine() : new GeneralCommandLine();
Expand All @@ -107,32 +118,29 @@ public void doExecute(Editor editor, Caret caret, DataContext dataContext) {
return;
}

processHandler.addProcessListener(new OutputListener(out, err));
processHandler.addProcessListener(new ProcessListener() {
@Override
public void startNotified(@NotNull ProcessEvent processEvent) {

}

@Override
public void processTerminated(@NotNull ProcessEvent processEvent) {
VfsUtil.markDirtyAndRefresh(true, true, true, editor.getProject().getBaseDir());
}

@Override
public void processWillTerminate(@NotNull ProcessEvent processEvent, boolean b) {

}

@Override
public void onTextAvailable(@NotNull ProcessEvent processEvent, @NotNull Key key) {
final StringBuilder out = new StringBuilder();
final StringBuilder err = new StringBuilder();

}
});
processHandler.addProcessListener(new OutputListener(out, err));
processHandler.addProcessListener(new ProjectRefreshingProcessListener(editor.getProject()));
processHandler.startNotify();
ExecutionHelper.executeExternalProcess(editor.getProject(), processHandler, new ExecutionModes.BackGroundMode("mockery generation"), commandLine);

}

// Looks for Go interfaces in a given document
private List<String> getInterfacesInDocument(Document document) {
List<String> interfaces = new ArrayList<>();
for (String s : document.getText().split("\\r?\\n")) {
Pattern pattern = Pattern.compile("type (.*?) interface");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
{
interfaces.add(matcher.group(1));
}
}
return interfaces;
}
}


0 comments on commit a34ec1e

Please sign in to comment.