Skip to content

Commit

Permalink
BearMenu: Use coala-json to get list of bears
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
arafsheikh committed Jul 12, 2016
1 parent 9cf253d commit f4283ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 77 deletions.
94 changes: 17 additions & 77 deletions com.coala.core/src/com/coala/core/menu/BearMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.coala.core.utils.ExternalUtils;

import org.apache.commons.exec.ExecuteException;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
Expand All @@ -10,8 +11,10 @@
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.PlatformUI;
import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;

public class BearMenu extends ContributionItem {

Expand All @@ -24,7 +27,13 @@ public BearMenu(String id) {

@Override
public void fill(Menu menu, int index) {
String[] bears = getBears();
ArrayList<String> bears = new ArrayList<>();
try {
bears = getBears();
} catch (IOException ex) {
System.out.println("Bear list could not be fetched");
ex.printStackTrace();
}

for (final String bear : bears) {
MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
Expand All @@ -44,81 +53,12 @@ public void widgetSelected(SelectionEvent event) {
}
}

private String[] getBears() {
// TODO: Use coala-json to fetch list of bears
String[] bears = {"AlexBear",
"AnnotationBear",
"BootLintBear",
"CMakeLintBear",
"CPDBear",
"CPPCheckBear",
"CPPCleanBear",
"CPPLintBear",
"CSSAutoPrefixBear",
"CSSLintBear",
"CSharpLintBear",
"CheckstyleBear",
"ClangASTPrintBear",
"ClangBear",
"ClangCloneDetectionBear",
"ClangComplexityBear",
"ClangFunctionDifferenceBear",
"CoffeeLintBear",
"DartLintBear",
"DockerfileLintBear",
"ESLintBear",
"FormatRBear",
"GNUIndentBear",
"GitCommitBear",
"GoImportsBear",
"GoLintBear",
"GoReturnsBear",
"GoTypeBear",
"GoVetBear",
"GofmtBear",
"HTMLLintBear",
"HaskellLintBear",
"InferBear",
"InvalidLinkBear",
"JSComplexityBear",
"JSHintBear",
"JSONFormatBear",
"JavaPMDBear",
"JuliaLintBear",
"KeywordBear",
"LanguageToolBear",
"LatexLintBear",
"LineCountBear",
"LineLengthBear",
"LuaLintBear",
"MarkdownBear",
"MatlabIndentationBear",
"PEP8Bear",
"PHPLintBear",
"PerlCriticBear",
"ProseLintBear",
"PyCommentedCodeBear",
"PyDocStyleBear",
"PyImportSortBear",
"PyLintBear",
"PyUnusedCodeBear",
"RLintBear",
"RadonBear",
"RuboCopBear",
"RubySyntaxBear",
"SCSSLintBear",
"SQLintBear",
"ScalaLintBear",
"ShellCheckBear",
"SpaceConsistencyBear",
"TSLintBear",
"VHDLLintBear",
"VerilogLintBear",
"VintBear",
"XMLBear",
"YAMLLintBear",
"reSTLintBear"};
return bears;
private ArrayList<String> getBears() throws ExecuteException, IOException {
JSONArray bearList = ExternalUtils.getAvailableBears();
ArrayList<String> bearNames = new ArrayList<>();
for (int i = 0; i < bearList.length(); i++) {
bearNames.add(bearList.getJSONObject(i).getString("name"));
}
return bearNames;
}
}

17 changes: 17 additions & 0 deletions com.coala.core/src/com/coala/core/utils/ExternalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ public void onProcessFailed(ExecuteException executeException) {
executor.execute(cmdLine, resultHandler);
}

/**
* Get details of all bears that are available on the user's system.
*
* @return JSONArray containing details of bears.
*/
public static JSONArray getAvailableBears() throws ExecuteException, IOException {
CommandLine cmdLine = new CommandLine("coala-json");
cmdLine.addArgument("-B");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.execute(cmdLine);
JSONArray bearList = new JSONObject(stdout.toString()).getJSONArray("bears");
return bearList;
}

/**
* Process the JSON output of coala and add marker for each problem.
*
Expand Down

0 comments on commit f4283ec

Please sign in to comment.