Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
* Add remove marker menu command
  • Loading branch information
arafsheikh committed May 24, 2016
1 parent 9be81c9 commit 513e6be
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 178 deletions.
8 changes: 5 additions & 3 deletions META-INF/MANIFEST.MF
Expand Up @@ -3,13 +3,15 @@ Bundle-ManifestVersion: 2
Bundle-Name: Plugin
Bundle-SymbolicName: com.coala.plugin;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-ClassPath: .,
lib/json-20151123.jar
Bundle-Vendor: COALA
Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources;bundle-version="3.10.1",
org.eclipse.jdt.core;bundle-version="3.11.2",
org.eclipse.ui.ide;bundle-version="3.11.0",
org.eclipse.core.expressions;bundle-version="3.5.0",
org.eclipse.core.runtime;bundle-version="3.11.1"
org.eclipse.core.runtime;bundle-version="3.11.1",
org.eclipse.e4.core.di
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ClassPath: .,
lib/json-20151123.jar
Import-Package: javax.inject;version="1.0.0"
Binary file added bin/com/coala/plugin/handlers/Plugin.class
Binary file not shown.
Binary file added bin/com/coala/plugin/handlers/RemoveMarkers.class
Binary file not shown.
Binary file removed bin/com/coala/plugin/handlers/SampleHandler.class
Binary file not shown.
34 changes: 26 additions & 8 deletions plugin.xml
Expand Up @@ -5,22 +5,32 @@
<extension
point="org.eclipse.ui.commands">
<category
name="Sample Category"
name="Main Category"
id="com.coala.plugin.commands.category">
</category>
<command
categoryId="com.coala.plugin.commands.category"
id="com.coala.plugin.commands.sampleCommand"
name="Sample Command">
id="com.coala.plugin.commands.runcoalaCommand"
name="Run coala Command">
</command>
</extension>
<command
categoryId="com.coala.plugin.commands.category"
id="com.coala.plugin.commands.removeMarkersCommand"
name="Remove Markers Command">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="com.coala.plugin.commands.sampleCommand"
class="com.coala.plugin.handlers.SampleHandler">
commandId="com.coala.plugin.commands.runcoalaCommand"
class="com.coala.plugin.handlers.Plugin">
</handler>
<handler
commandId="com.coala.plugin.commands.removeMarkersCommand"
class="com.coala.plugin.handlers.RemoveMarkers">
</handler>
</extension>
<!--
<extension
point="org.eclipse.ui.bindings">
<key
Expand All @@ -30,6 +40,7 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>
-->
<extension
point="org.eclipse.ui.menus">
<menuContribution
Expand All @@ -41,12 +52,19 @@
mnemonic="M">
<command
label = "Run on Current file"
commandId="com.coala.plugin.commands.sampleCommand"
commandId="com.coala.plugin.commands.runcoalaCommand"
mnemonic="S"
id="com.coala.plugin.menus.sampleCommand">
</command>
<command
label = "Remove markers from current file"
commandId="com.coala.plugin.commands.removeMarkersCommand"
mnemonic="C"
id="com.coala.plugin.menus.removeMarkersCommand">
</command>
</menu>
</menuContribution>
<!--
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
<toolbar
Expand All @@ -59,6 +77,7 @@
</command>
</toolbar>
</menuContribution>
-->
</extension>

<extension id="coolmarker" point="org.eclipse.core.resources.markers"/>
Expand All @@ -69,5 +88,4 @@
<persistent value="true"/>
</extension>


</plugin>
106 changes: 106 additions & 0 deletions src/com/coala/plugin/handlers/Plugin.java
@@ -0,0 +1,106 @@
package com.coala.plugin.handlers;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import org.json.JSONArray;
import org.json.JSONObject;

/**
* The PluginHandler extends AbstractHandler, an IHandler base class.
*
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public class Plugin extends AbstractHandler {

/**
* The constructor.
*
* @throws IOException
*/
public Plugin() {
}

public Object execute(ExecutionEvent event) throws ExecutionException {
IFile file = (IFile) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart()
.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
try {
new RemoveMarkers().execute(event);
String json = getcoalaJSON(file, "CheckstyleBear");
processJSON(json);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public void processJSON(String json) throws IOException {
JSONObject obj = new JSONObject(json);
JSONArray arr = obj.getJSONObject("results").getJSONArray("default");
for (int i = 0; i < arr.length(); i++) {
String message = arr.getJSONObject(i).getString("message");
String origin = arr.getJSONObject(i).getString("origin");
int severity = arr.getJSONObject(i).getInt("severity");
JSONArray arr2 = arr.getJSONObject(i).getJSONArray("affected_code");
for (int j = 0; j < arr2.length(); j++) {
int end_line = arr2.getJSONObject(j).getJSONObject("end").getInt("line");
createCoolMarker(end_line, 3 - severity, "( " + origin + " ): " + message);
}
}
}

public IMarker createCoolMarker(int line_num, int flag, String message) {
// :: param : flag = 1 for error
// flag = 2 for warning
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
IFileEditorInput input = (IFileEditorInput) editor.getEditorInput();
IFile file = input.getFile();
IResource resource = (IResource) file;
try {

IMarker marker = resource.createMarker("com.coala.plugin.coolproblem");
// marker.setAttribute("coolFactor", "ULTRA");
marker.setAttribute(IMarker.LINE_NUMBER, line_num);
if (flag == 1)
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
else if (flag == 2)
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.TEXT, "Awesome Marker");

return marker;
} catch (CoreException e) {
return null;
}
}

public static String getcoalaJSON(IFile file, String bear) throws IOException {
String path = file.getRawLocation().toOSString();
String cmd = "coala-json -f " + path + " -b " + bear;
System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
InputStream is = proc.getInputStream();
Scanner s = new Scanner(is);
s.useDelimiter("\\A");
String val = "";
if (s.hasNext()) {
val = s.next();
} else {
val = "";
}
s.close();
return val;
}
}
39 changes: 39 additions & 0 deletions src/com/coala/plugin/handlers/RemoveMarkers.java
@@ -0,0 +1,39 @@

package com.coala.plugin.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;

public class RemoveMarkers extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IFile file = ((IFileEditorInput) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor().getEditorInput()).getFile();
IMarker[] problems = null;
IResource resource = (IResource) file;
int depth = IResource.DEPTH_INFINITE;
try {
problems = resource.findMarkers(IMarker.PROBLEM, true, depth);

} catch (CoreException e) {
e.printStackTrace();
}
for (IMarker m : problems) {
try {
if (m.getType().equals("com.coala.plugin.coolproblem"))
m.delete();
} catch (CoreException e) {
e.printStackTrace();
}
}
return null;
}
}

0 comments on commit 513e6be

Please sign in to comment.