Skip to content

Commit

Permalink
SLI-244 Project configuration to exclude files from analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Dec 29, 2017
1 parent e59102e commit 97b2893
Show file tree
Hide file tree
Showing 26 changed files with 379 additions and 200 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=3.2-SNAPSHOT
org.gradle.jvmargs=-XX:MaxPermSize=256M
sonarlintCoreVersion=3.0.0.1140
sonarlintCoreVersion=3.1-SNAPSHOT
29 changes: 23 additions & 6 deletions src/main/java/org/sonarlint/intellij/actions/ExcludeAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.sonarlint.intellij.config.project.ExclusionItem;
import org.sonarlint.intellij.config.project.SonarLintProjectSettings;
import org.sonarlint.intellij.messages.ProjectConfigurationListener;
import org.sonarlint.intellij.util.SonarLintUtils;

public class ExcludeAction extends DumbAwareAction {
Expand All @@ -21,12 +24,14 @@ public void update(AnActionEvent e) {

if (project == null || !project.isInitialized() || project.isDisposed()) {
e.getPresentation().setEnabled(false);
return;
}

VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
if (!ActionPlaces.isPopupPlace(e.getPlace()) || files == null || files.length == 0) {
e.getPresentation().setEnabled(false);
e.getPresentation().setVisible(false);
return;
}

SonarLintProjectSettings settings = SonarLintUtils.get(project, SonarLintProjectSettings.class);
Expand All @@ -45,20 +50,32 @@ public void update(AnActionEvent e) {
Project project = e.getProject();
VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);

if (project == null || files == null || files.length == 0) {
if (project == null || project.isDisposed() || files == null || files.length == 0) {
return;
}

SonarLintProjectSettings settings = SonarLintUtils.get(project, SonarLintProjectSettings.class);
List<String> exclusions = new ArrayList<>(settings.getFileExclusions());

Arrays.stream(files)
.map(vf -> SonarLintUtils.getRelativePath(project, vf))
List<String> newExclusions = Arrays.stream(files)
.map(vf -> toExclusion(project, vf))
.filter(path -> !exclusions.contains(path))
.forEach(exclusions::add);
.collect(Collectors.toList());

settings.setFileExclusions(exclusions);
// TODO trigger update
if (!newExclusions.isEmpty()) {
exclusions.addAll(newExclusions);
settings.setFileExclusions(exclusions);
ProjectConfigurationListener projectListener = project.getMessageBus().syncPublisher(ProjectConfigurationListener.TOPIC);
projectListener.changed(settings);
}
}

private String toExclusion(Project project, VirtualFile virtualFile) {
String filePath = SonarLintUtils.getRelativePath(project, virtualFile);
if (virtualFile.isDirectory()) {
return new ExclusionItem(ExclusionItem.Type.DIRECTORY, filePath).toStringWithType();
} else {
return new ExclusionItem(ExclusionItem.Type.FILE, filePath).toStringWithType();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@

import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ui.UIUtil;
import java.util.Collections;
import java.util.Arrays;
import javax.swing.Icon;
import org.jetbrains.annotations.Nullable;
import org.sonarlint.intellij.analysis.AnalysisCallback;
import org.sonarlint.intellij.analysis.SonarLintStatus;
import org.sonarlint.intellij.trigger.SonarLintSubmitter;
import org.sonarlint.intellij.trigger.TriggerType;
import org.sonarlint.intellij.ui.SonarLintConsole;
import org.sonarlint.intellij.util.SonarLintAppUtils;
import org.sonarlint.intellij.util.SonarLintUtils;

public class SonarAnalyzeEditorFileAction extends AbstractSonarAction {
Expand All @@ -46,31 +46,41 @@ public SonarAnalyzeEditorFileAction(@Nullable String text, @Nullable String desc
}

@Override
protected boolean isEnabled(Project project, SonarLintStatus status) {
protected boolean isEnabled(AnActionEvent e, Project project, SonarLintStatus status) {
if (status.isRunning()) {
return false;
}

return SonarLintUtils.get(SonarLintAppUtils.class).getSelectedFile(project) != null;
VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);

if (files == null || files.length == 0) {
return false;
}

// TODO ideally we would already check if the files are analyzable
return true;
}

@Override
protected boolean isVisible(String place) {
return true;
}

@Override
public void actionPerformed(AnActionEvent e) {
Project project = e.getProject();

if (project == null) {
return;
}

SonarLintSubmitter submitter = SonarLintUtils.get(project, SonarLintSubmitter.class);
VirtualFile selectedFile = SonarLintUtils.get(SonarLintAppUtils.class).getSelectedFile(project);
VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);

if (selectedFile == null) {
if (files == null || files.length == 0) {
SonarLintUtils.get(project, SonarLintConsole.class).error("No files for analysis");
return;
}

submitter.submitFiles(Collections.singleton(selectedFile), TriggerType.ACTION, new ShowIssuesCallable(project), executeBackground(e));
SonarLintSubmitter submitter = SonarLintUtils.get(project, SonarLintSubmitter.class);
submitter.submitFiles(Arrays.asList(files), TriggerType.ACTION, new ShowIssuesCallable(project), executeBackground(e));
}

private class ShowIssuesCallable implements AnalysisCallback {
Expand All @@ -97,9 +107,9 @@ private void showCurrentFileTab() {
/**
* Whether the analysis should be launched in the background.
* Analysis should be run in background in the following cases:
* - Keybinding used (place = MainMenu)
* - Macro used (place = unknown)
* - Action used, ctrl+shift+A (place = GoToAction)
* - Keybinding used (place = MainMenu)
* - Macro used (place = unknown)
* - Action used, ctrl+shift+A (place = GoToAction)
*/
private static boolean executeBackground(AnActionEvent e) {
return ActionPlaces.isMainMenuOrActionSearch(e.getPlace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
Expand All @@ -51,11 +52,17 @@
public class LocalFileExclusions {
private FileExclusions projectExclusions;
private FileExclusions globalExclusions;
private Supplier<Boolean> powerSaveModeCheck;

public LocalFileExclusions(Project project, SonarLintGlobalSettings settings, SonarLintProjectSettings projectSettings) {
this(project, settings, projectSettings, () -> PowerSaveMode.isEnabled());
}

public LocalFileExclusions(Project project, SonarLintGlobalSettings settings, SonarLintProjectSettings projectSettings, Supplier<Boolean> powerSaveModeCheck) {
loadGlobalExclusions(settings);
loadProjectExclusions(projectSettings);
subscribeToSettingsChanges(project);
this.powerSaveModeCheck = powerSaveModeCheck;
}

private Set<String> getExclusionsOfType(Collection<ExclusionItem> exclusions, ExclusionItem.Type type) {
Expand Down Expand Up @@ -93,11 +100,11 @@ private void subscribeToSettingsChanges(Project project) {
}

public Result checkExclusionAutomaticAnalysis(VirtualFile file, @Nullable Module module) {
if (!shouldAnalyze(file, module)) {
if (!canAnalyze(file, module)) {
return Result.excluded(null);
}

if (PowerSaveMode.isEnabled()) {
if (powerSaveModeCheck.get()) {
return Result.excluded("power save mode is enabled");
}

Expand Down Expand Up @@ -156,7 +163,7 @@ private Result checkSourceFolder(SourceFolder sourceFolder) {
return Result.notExcluded();
}

public boolean shouldAnalyze(VirtualFile file, @Nullable Module module) {
public boolean canAnalyze(VirtualFile file, @Nullable Module module) {
if (module == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public AnalysisResults analyzeModule(Module module, Collection<VirtualFile> file
}

// configure files
VirtualFileTestPredicate testPredicate = new VirtualFileTestPredicate(module);
VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
List<ClientInputFile> inputFiles = getInputFiles(testPredicate, filesToAnalyze);

// Analyze
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sonarlint.intellij.analysis;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.ModuleAdapter;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.SourceFolder;
Expand All @@ -9,7 +10,7 @@
import java.util.Collection;
import java.util.function.Predicate;

public class VirtualFileTestPredicate implements Predicate<VirtualFile> {
public class VirtualFileTestPredicate extends ModuleAdapter implements Predicate<VirtualFile> {
private final Collection<String> testFolderPrefixes;
private final ModuleRootManager moduleRootManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public Type type() {
}

public String toStringWithType() {
return StringUtils.capitalize(type.name().toLowerCase()) + ":" + item;
return StringUtils.capitalize(type.name()) + ":" + item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import org.apache.commons.lang.StringUtils;

public class ExclusionTable {
private final Supplier<ExclusionItem> onAdd;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void mouseClicked(MouseEvent evt) {
panel.add(toolbarDecorator.createPanel(), BorderLayout.CENTER);
}

public JComponent getComponent() {
public JPanel getComponent() {
return panel;
}

Expand Down Expand Up @@ -164,7 +165,7 @@ public List<ExclusionItem> items() {
@Override public Object getValueAt(int rowIndex, int columnIndex) {
ExclusionItem item = rows.get(rowIndex);
if (columnIndex == 0) {
return item.type();
return StringUtils.capitalize(item.type().name().toLowerCase());
}
return item.item();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.sonarlint.intellij.config.project.ProjectExclusionsPanel">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="3a4f8" binding="tablePanel" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="19a4c" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="The exclusions will only apply to analysis triggered automatically."/>
</properties>
</component>
</children>
</grid>
</form>
Loading

0 comments on commit 97b2893

Please sign in to comment.