Permalink
Browse files

Add MarkerResolution and QuickFix

  • Loading branch information...
arafsheikh committed Jun 25, 2016
1 parent 7d83c96 commit 794c21a63bde0ceab60b6db8dbafc1038637c69a
@@ -6,7 +6,8 @@ Bundle-Version: 1.0.0.qualifier
Bundle-ClassPath: .,
lib/json-20151123.jar,
lib/commons-exec-1.3.jar,
lib/commons-logging-1.2.jar
lib/commons-logging-1.2.jar,
lib/commons-io-2.5.jar
Bundle-Vendor: COALA
Export-Package: com.coala.core.handlers;x-friends:="com.coala.core.tests",
com.coala.core.utils,
@@ -18,6 +19,7 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.expressions;bundle-version="3.5.0",
org.eclipse.core.runtime;bundle-version="3.11.1",
org.eclipse.e4.core.di,
org.junit
org.junit,
org.eclipse.jface
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: javax.inject;version="1.0.0"
@@ -6,4 +6,5 @@ bin.includes = plugin.xml,\
icons/,\
lib/json-20151123.jar,\
lib/commons-exec-1.3.jar,\
lib/commons-logging-1.2.jar
lib/commons-logging-1.2.jar,\
lib/commons-io-2.5.jar
Binary file not shown.
@@ -109,5 +109,12 @@
<attribute name="coolFactor"/>
<persistent value="true"/>
</extension>
<extension point="org.eclipse.ui.ide.markerResolution">
<markerResolutionGenerator
markerType="com.coala.core.problem"
class="com.coala.core.quickfix.QuickFixer"/>
</extension>
</plugin>
@@ -0,0 +1,60 @@
package com.coala.core.quickfix;
import com.coala.core.utils.DiffUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator;
/**
* This class is responsible for providing marker resolution.
*/
public class QuickFixer implements IMarkerResolutionGenerator {
@Override
public IMarkerResolution[] getResolutions(IMarker mk) {
try {
Object problem = mk.getAttribute(IMarker.MESSAGE);
String diff = mk.getAttribute("diff", null);
if (diff != null) {
diff = DiffUtils.cleanDiff(diff);
return new IMarkerResolution[] { new QuickFix("Apply patch for " + problem, diff) };
}
return new IMarkerResolution[] {};
} catch (CoreException ex) {
return new IMarkerResolution[0];
}
}
private class QuickFix implements IMarkerResolution {
String label;
String diff;
QuickFix(String label, String diff) {
this.label = label;
this.diff = diff;
}
public String getLabel() {
return label;
}
public void run(IMarker marker) {
try {
String location = (String) marker.getAttribute("file");
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile file = (IFile) root.findMember(location.substring(1));
if (DiffUtils.applyDiffToFile(file, diff)) {
marker.delete();
}
} catch (CoreException ex) {
System.out.println("Marker resolution failed.");
ex.printStackTrace();
}
}
}
}
@@ -0,0 +1,45 @@
package com.coala.core.utils;
import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
public class DiffUtils {
/**
* Cleans the given `diff` to contain only the changes. This is the required format
* for the `diff_match_patch` library.
*/
public static String cleanDiff(String diff) {
return diff.substring(diff.indexOf("@@"));
}
/**
* Apply the given `diff` to the `IFile`.
* Returns true if the `diff` was applied successfully, false otherwise.
*/
public static boolean applyDiffToFile(IFile file, String diff) {
diff_match_patch dmp = new diff_match_patch();
try {
InputStream in = file.getContents();
String contents = IOUtils.toString(in, "UTF-8");
LinkedList<diff_match_patch.Patch> patch = (LinkedList<diff_match_patch.Patch>) dmp
.patch_fromText(diff);
Object[] res = dmp.patch_apply(patch, contents);
in = IOUtils.toInputStream((String) res[0], "UTF-8");
file.setContents(in, IFile.KEEP_HISTORY, null);
return true;
} catch (CoreException ex) {
System.out.println("Opening InputStream failed.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("InputStream to String conversion failed.");
ex.printStackTrace();
}
return false;
}
}
@@ -143,13 +143,21 @@ public static void processJsonAndMark(String json, IFile file) throws IOExceptio
JSONArray result = results.getJSONArray(key);
if (result instanceof JSONArray) {
for (int i = 0; i < result.length(); i++) {
String diff = null;
String message = result.getJSONObject(i).getString("message");
String origin = result.getJSONObject(i).getString("origin");
String filePath = result.getJSONObject(i).getJSONArray("affected_code").getJSONObject(0)
.getString("file");
if (result.getJSONObject(i).get("diffs") instanceof JSONObject) {
diff = result.getJSONObject(i).getJSONObject("diffs").getString(filePath);
} else {
diff = null;
}
int severity = result.getJSONObject(i).getInt("severity");
JSONArray affectedCodeArray = result.getJSONObject(i).getJSONArray("affected_code");
for (int j = 0; j < affectedCodeArray.length(); j++) {
int endLine = affectedCodeArray.getJSONObject(j).getJSONObject("end").getInt("line");
createCoolMarker(file, endLine, 3 - severity, message);
createCoolMarker(file, endLine, 3 - severity, message, diff);
}
}
}
@@ -177,18 +185,24 @@ public static void processJsonAndMark(String json, IProject project) throws IOEx
JSONArray result = results.getJSONArray(key);
if (result instanceof JSONArray) {
for (int i = 0; i < result.length(); i++) {
String diff = null;
String projectPath = project.getLocation().toOSString();
String filePath = result.getJSONObject(i).getJSONArray("affected_code").getJSONObject(0)
.getString("file");
IPath path = new Path(filePath.substring(projectPath.length()));
IFile file = project.getFile(path);
String message = result.getJSONObject(i).getString("message");
String origin = result.getJSONObject(i).getString("origin");
if (result.getJSONObject(i).get("diffs") instanceof JSONObject) {
diff = result.getJSONObject(i).getJSONObject("diffs").getString(filePath);
} else {
diff = null;
}
int severity = result.getJSONObject(i).getInt("severity");
JSONArray affectedCodeArray = result.getJSONObject(i).getJSONArray("affected_code");
for (int j = 0; j < affectedCodeArray.length(); j++) {
int endLine = affectedCodeArray.getJSONObject(j).getJSONObject("end").getInt("line");
createCoolMarker(file, endLine, 3 - severity, message);
createCoolMarker(file, endLine, 3 - severity, message, diff);
}
}
}
@@ -206,8 +220,11 @@ public static void processJsonAndMark(String json, IProject project) throws IOEx
* Severity 1 for error, 2 for warning.
* @param message
* Problem message on marker.
* @param diff
* The diff to fix the issue.
*/
public static void createCoolMarker(IFile file, int lineNum, int flag, String message) {
public static void createCoolMarker(IFile file, int lineNum, int flag, String message,
String diff) {
IResource resource = (IResource) file;
try {
IMarker marker = resource.createMarker("com.coala.core.problem");
@@ -218,6 +235,8 @@ public static void createCoolMarker(IFile file, int lineNum, int flag, String me
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
}
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute("file", file.toString());
marker.setAttribute("diff", diff);
} catch (CoreException ex) {
ex.printStackTrace();
}
Oops, something went wrong.

0 comments on commit 794c21a

Please sign in to comment.