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

Commit

Permalink
Closes #64: Add UI for configuring CFLint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
denuno committed May 17, 2016
1 parent 54fbc60 commit 2bed5f6
Show file tree
Hide file tree
Showing 14 changed files with 1,918 additions and 161 deletions.
2 changes: 1 addition & 1 deletion org.cfeclipse.cfml/plugin.xml
Expand Up @@ -1878,7 +1878,7 @@
point="org.eclipse.ui.propertyPages">
<page
class="org.cfeclipse.cfml.properties.ProjectPropertyPage"
id="org.cfeclipse.cfml.properties.samplePropertyPage"
id="org.cfeclipse.cfml.properties.ProjectPropertyPage"
name="%page.name.10"
nameFilter="*">
<filter name="projectNature" value="org.cfeclipse.cfml.CFENature" />
Expand Down
45 changes: 26 additions & 19 deletions org.cfeclipse.cfml/src/org/cfeclipse/cfml/builders/CFEBuilder.java
@@ -1,16 +1,11 @@
package org.cfeclipse.cfml.builders;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.cfeclipse.cfml.CFMLPlugin;
import org.cfeclipse.cfml.parser.CFLintPlugin;
import org.cfeclipse.cfml.preferences.CFLintPreferenceConstants;
import org.cfeclipse.cfml.preferences.ParserPreferenceConstants;
import org.cfeclipse.cfml.cflint.CFLintConfigUI;
import org.cfeclipse.cfml.properties.CFMLPropertyManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand All @@ -19,15 +14,11 @@
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.preference.IPreferenceStore;

import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.CFLint;
import com.cflint.config.CFLintConfig;
import com.cflint.plugins.CFLintScanner;
import com.cflint.tools.CFLintFilter;

/**
Expand All @@ -38,21 +29,29 @@
*/
public class CFEBuilder extends IncrementalProjectBuilder {

CFLintConfig config = null;
CFLint cflint;
CFMLPropertyManager propertyManager = new CFMLPropertyManager();
private CFLint cflint;
private CFMLPropertyManager propertyManager = new CFMLPropertyManager();
private HashMap<String, CFLintConfig> projectCFLintConfigs = new HashMap<String, CFLintConfig>();

@Override
protected void clean(IProgressMonitor monitor) throws CoreException {
super.clean(monitor);
projectCFLintConfigs = new HashMap<String, CFLintConfig>();
}

protected IProject[] build(int kind, Map args, IProgressMonitor monitor) {
if (!propertyManager.getCFLintEnabledProject(getProject())) {
try {
IResourceDelta delta = getDelta(getProject());
delta.getResource().deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_ONE);
if(delta != null) {
delta.getResource().deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_ONE);
}
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}

if (kind == IncrementalProjectBuilder.FULL_BUILD) {
fullBuild(monitor);
} else {
Expand Down Expand Up @@ -98,7 +97,15 @@ private void getResourceBuildMarkers(IResource res) {
try {
res.deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_ONE);
// res.deleteMarkers(null, true, IResource.DEPTH_ONE);
if (cflint == null) {
String projectName = getProject().getName();
CFLintConfig config = projectCFLintConfigs.get(projectName);
boolean reload = false;
if(config == null) {
config = CFLintConfigUI.getProjectCFLintConfig(getProject());
projectCFLintConfigs.put(projectName, config);
reload = true;
}
if (cflint == null || reload) {
cflint = new CFLint(config);
cflint.setVerbose(true);
cflint.setLogError(true);
Expand Down Expand Up @@ -139,8 +146,8 @@ private void addMarker(IResource res, BugInfo bug) {
try {
IMarker marker = res.createMarker(MARKER_TYPE);
int lineNumber = bug.getLine();
marker.setAttribute(IMarker.MESSAGE, bug.getSeverity() + ": " + bug.getMessage());
if (bug.getSeverity().equals("WARNING")) {
marker.setAttribute(IMarker.MESSAGE, bug.getSeverity() + ": " + bug.getMessage() + " (" + bug.getMessageCode() + ")");
if (bug.getSeverity().startsWith("WARN")) {
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
}
else if (bug.getSeverity().equals("INFO")) {
Expand Down
119 changes: 119 additions & 0 deletions org.cfeclipse.cfml/src/org/cfeclipse/cfml/cflint/CFLintConfigUI.java
@@ -0,0 +1,119 @@
package org.cfeclipse.cfml.cflint;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.cfeclipse.cfml.CFMLPlugin;
import org.cfeclipse.cfml.properties.CFMLPropertyManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import com.cflint.config.CFLintConfig;
import com.cflint.config.CFLintPluginInfo;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;
import com.cflint.config.ConfigUtils;

public class CFLintConfigUI {

private ArrayList<RuleEditor> ruleEditors;
private static CFMLPropertyManager propertyManager = new CFMLPropertyManager();

public void buildGUI(Composite composite, IProject iProject) {
CFLintPluginInfo info = ConfigUtils.loadDefaultPluginInfo();
List<PluginInfoRule> enabledRules = getProjectCFLintConfig(iProject).getRules();
List<PluginMessage> excludeMessages = getProjectCFLintConfig(iProject).getExcludes();
Button enabledCheckbox = new Button(composite, SWT.CHECK);
enabledCheckbox.setText("Enable/Disable All Rules");
enabledCheckbox.setSelection(false);
enabledCheckbox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
boolean enableAll = btn.getSelection();
for (RuleEditor ruleEditor : ruleEditors) {
ruleEditor.setRuleEnabled(enableAll);
}
}
});
ruleEditors = new ArrayList<RuleEditor>();
HashMap<String, String> descriptions = ConfigUtils.loadDescriptions();
for (PluginInfoRule rule : info.getRules()) {
RuleEditor ruleEdit;
if(enabledRules.contains(rule)){
rule = enabledRules.get(enabledRules.indexOf(rule));
ruleEdit = new RuleEditor(composite, SWT.NONE, rule, true, descriptions, excludeMessages);
} else {
ruleEdit = new RuleEditor(composite, SWT.NONE, rule, true, descriptions, excludeMessages);
}
ruleEdit.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
ruleEdit.setLayout(new GridLayout(1, false));
ruleEditors.add(ruleEdit);
}
}

public static CFLintConfig getProjectCFLintConfig(IProject iProject) {
CFLintConfig currentConfig = null;
File configFile = getConfigFile(iProject);
if (configFile.exists()) {
try {
currentConfig = ConfigUtils.unmarshalJson(new FileInputStream(configFile),
CFLintConfig.class);
} catch (Exception e) {
e.printStackTrace();
}
}
return currentConfig;
}

public static File getConfigFile(IProject iProject) {
File configFile;
if(!propertyManager.getCFLintStoreConfigInProject(iProject)) {
configFile = iProject.getWorkingLocation(CFMLPlugin.PLUGIN_ID).append("cflint.definition.json").toFile();
} else {
configFile = iProject.getProject().getLocation().append("cflint.definition.json").toFile();
}
return configFile;
}

public void setProjectRules(IProject iProject) {
File configFile = getConfigFile(iProject);
ArrayList<PluginInfoRule> rules = new ArrayList<PluginInfoRule>();
ArrayList<PluginMessage> excludes = new ArrayList<PluginMessage>();
for (RuleEditor ruleEditor : ruleEditors) {
rules.add(ruleEditor.getRule());
excludes.addAll(ruleEditor.getExcludes());
}
try {
String config = ConfigUtils.marshalJson(rules);
String excludesStr = ConfigUtils.marshalJson(excludes);
PrintWriter writer;
writer = new PrintWriter(new BufferedWriter(new FileWriter(configFile)));
writer.write("{\"rule\":" + config + ", \"excludes\":"+excludesStr+"}");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
iProject.build(IncrementalProjectBuilder.CLEAN_BUILD,null);
iProject.build(IncrementalProjectBuilder.FULL_BUILD,null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
@@ -0,0 +1,86 @@
package org.cfeclipse.cfml.cflint;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;

public class MessageEditor extends Group {
private Text messageText;
private PluginMessage message;
private Combo severityCombo;
private Button enabledCheckbox;
private boolean messageEnabled;
/**
* Create the composite.
* @param parent
* @param style
* @param message
* @param descriptions
* @param b
*/
public MessageEditor(Composite parent, int style, PluginMessage message, HashMap<String, String> descriptions, boolean enabled) {
super(parent, style);
this.message = message;
messageEnabled = enabled;
this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.setLayout(new GridLayout(2, false));

enabledCheckbox = new Button(this, SWT.CHECK);
GridData gdata = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gdata.horizontalSpan = 2;
enabledCheckbox.setLayoutData(gdata);
enabledCheckbox.setText("Code: " + message.getCode() + " -- Description: " + descriptions.get(message.getCode()));
enabledCheckbox.setSelection(enabled);
enabledCheckbox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
setMessageEnabled(btn.getSelection());
}
});

severityCombo = new Combo(this, SWT.NONE);
severityCombo.setItems(new String[] {"INFO", "WARN", "ERROR"});
severityCombo.setText(message.getSeverity());

messageText = new Text(this, SWT.BORDER);
messageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
messageText.setText(message.getMessageText() == null ? "" : message.getMessageText());

setMessageEnabled(enabled);

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}

public boolean getMessageEnabled() {
return messageEnabled;
}
public void setMessageEnabled(boolean enabled) {
messageEnabled = enabled;
enabledCheckbox.setSelection(enabled);
severityCombo.setEnabled(enabled);
messageText.setEnabled(enabled);
}

public PluginMessage getMessage() {
message.setMessageText(messageText.getText());
message.setSeverity(severityCombo.getText());
return message;
}

}
@@ -0,0 +1,53 @@
package org.cfeclipse.cfml.cflint;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;

import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginParameter;

public class ParameterEditor extends Group {
private Text parameterText;
private PluginParameter parameter;

/**
* Create the composite.
* @param parent
* @param style
* @param parameter
*/
public ParameterEditor(Composite parent, int style, PluginParameter parameter) {
super(parent, style);
this.parameter = parameter;
this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.setLayout(new GridLayout(3, false));

Label parameterLabel = new Label(this, SWT.NONE);
parameterLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
parameterLabel.setText("Parameter");

Label parameterNameLabel = new Label(this, SWT.NONE);
parameterNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
parameterNameLabel.setText(parameter.getName());

parameterText = new Text(this, SWT.BORDER);
parameterText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
parameterText.setText(parameter.getValue());

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}

public PluginParameter getParameter() {
parameter.setValue(parameterText.getText());
return parameter;
}

}

0 comments on commit 2bed5f6

Please sign in to comment.