Skip to content

Commit 5be2577

Browse files
committed
Allow to treat detekt findings as errors - Closes #2
1 parent 5ecc3d0 commit 5be2577

File tree

4 files changed

+97
-112
lines changed

4 files changed

+97
-112
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="io.gitlab.arturbosch.detekt.config.DetektConfigurationForm">
3-
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="500" height="400"/>
6+
<xy x="20" y="20" width="680" height="400"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
1010
<children>
11-
<grid id="d8a4f" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
12-
<margin top="0" left="0" bottom="0" right="0"/>
13-
<constraints>
14-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
15-
</constraints>
16-
<properties/>
17-
<border type="none"/>
18-
<children>
19-
<component id="4b496" class="javax.swing.JCheckBox" binding="enableDetekt">
20-
<constraints>
21-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
22-
</constraints>
23-
<properties>
24-
<selected value="true"/>
25-
<text value="Enable Detekt"/>
26-
<toolTipText value="Enable Detekt"/>
27-
</properties>
28-
</component>
29-
<component id="557e8" class="javax.swing.JCheckBox" binding="checkTestSources">
30-
<constraints>
31-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
32-
</constraints>
33-
<properties>
34-
<text value="Analyze Test Code"/>
35-
<toolTipText value="Perfom Analisis of Test Sources"/>
36-
</properties>
37-
</component>
38-
<component id="92365" class="javax.swing.JComboBox" binding="detektVersion">
39-
<constraints>
40-
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
41-
</constraints>
42-
<properties>
43-
<editable value="false"/>
44-
<enabled value="false"/>
45-
<model>
46-
<item value="1.0.0.RC6-3"/>
47-
</model>
48-
<toolTipText value="Select Detekt Version"/>
49-
</properties>
50-
</component>
51-
</children>
52-
</grid>
53-
<vspacer id="1b35">
54-
<constraints>
55-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
56-
</constraints>
57-
</vspacer>
5811
<grid id="6b450" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5912
<margin top="0" left="0" bottom="0" right="0"/>
6013
<constraints>
61-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
14+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
6215
</constraints>
6316
<properties/>
6417
<border type="none"/>
@@ -82,6 +35,38 @@
8235
</component>
8336
</children>
8437
</grid>
38+
<component id="4b496" class="javax.swing.JCheckBox" binding="enableDetekt">
39+
<constraints>
40+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
41+
</constraints>
42+
<properties>
43+
<selected value="true"/>
44+
<text value="Enable Detekt"/>
45+
<toolTipText value="Enable Detekt"/>
46+
</properties>
47+
</component>
48+
<component id="557e8" class="javax.swing.JCheckBox" binding="checkTestSources">
49+
<constraints>
50+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
51+
</constraints>
52+
<properties>
53+
<text value="Analyze Test Code"/>
54+
<toolTipText value="Perfom Analisis of Test Sources"/>
55+
</properties>
56+
</component>
57+
<component id="b76c7" class="javax.swing.JCheckBox" binding="treatAsErrors">
58+
<constraints>
59+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
60+
</constraints>
61+
<properties>
62+
<text value="Treat detekt findings as errors"/>
63+
</properties>
64+
</component>
65+
<vspacer id="f22cd">
66+
<constraints>
67+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
68+
</constraints>
69+
</vspacer>
8570
</children>
8671
</grid>
8772
</form>
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,69 @@
11
package io.gitlab.arturbosch.detekt.config;
22

3-
import com.intellij.openapi.fileChooser.*;
4-
import com.intellij.openapi.ui.*;
5-
import com.intellij.openapi.util.*;
6-
import com.intellij.ui.*;
3+
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
4+
import com.intellij.openapi.ui.TextComponentAccessor;
5+
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
6+
import com.intellij.openapi.util.Comparing;
7+
import com.intellij.ui.IdeBorderFactory;
8+
import org.jetbrains.annotations.NotNull;
9+
710
import javax.swing.*;
8-
import org.jetbrains.annotations.*;
911

1012
/**
1113
* @author Dmytro Primshyts
1214
*/
1315
public class DetektConfigurationForm {
14-
private JCheckBox enableDetekt;
15-
private JCheckBox checkTestSources;
16-
private JComboBox detektVersion;
17-
private TextFieldWithBrowseButton configurationFilePath;
18-
private JPanel myMainPanel;
19-
20-
private DetektConfigStorage detektConfigStorage;
21-
22-
@NotNull
23-
public JComponent createPanel(@NotNull DetektConfigStorage detektConfigStorage) {
24-
this.detektConfigStorage = detektConfigStorage;
25-
26-
myMainPanel.setBorder(IdeBorderFactory.createTitledBorder("Detekt settings"));
16+
private JCheckBox enableDetekt;
17+
private JCheckBox checkTestSources;
18+
private TextFieldWithBrowseButton configurationFilePath;
19+
private JPanel myMainPanel;
20+
private JCheckBox treatAsErrors;
2721

28-
FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(
29-
true,
30-
false,
31-
false,
32-
false,
33-
false,
34-
false);
22+
private DetektConfigStorage detektConfigStorage;
3523

36-
configurationFilePath.addBrowseFolderListener(
37-
"",
38-
"Detekt rules file",
39-
null,
40-
fileChooserDescriptor,
41-
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
42-
);
24+
@NotNull
25+
public JComponent createPanel(@NotNull DetektConfigStorage detektConfigStorage) {
26+
this.detektConfigStorage = detektConfigStorage;
4327

44-
return myMainPanel;
45-
}
28+
myMainPanel.setBorder(IdeBorderFactory.createTitledBorder("Detekt settings"));
4629

47-
public void apply() {
48-
detektConfigStorage.setEnableDetekt(enableDetekt.isSelected());
49-
detektConfigStorage.setCheckTestFiles(checkTestSources.isSelected());
30+
FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(
31+
true,
32+
false,
33+
false,
34+
false,
35+
false,
36+
false);
5037

51-
Object selectedDetektVersion = detektVersion.getSelectedItem();
38+
configurationFilePath.addBrowseFolderListener(
39+
"",
40+
"Detekt rules file",
41+
null,
42+
fileChooserDescriptor,
43+
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
44+
);
5245

53-
if (selectedDetektVersion != null) {
54-
detektConfigStorage.setDetektVersion(selectedDetektVersion.toString());
55-
} else {
56-
detektConfigStorage.setDetektVersion("");
57-
}
46+
return myMainPanel;
47+
}
5848

59-
detektConfigStorage.setRulesPath(configurationFilePath.getText());
60-
}
49+
public void apply() {
50+
detektConfigStorage.setEnableDetekt(enableDetekt.isSelected());
51+
detektConfigStorage.setCheckTestFiles(checkTestSources.isSelected());
52+
detektConfigStorage.setTreatAsError(treatAsErrors.isSelected());
53+
detektConfigStorage.setRulesPath(configurationFilePath.getText());
54+
}
6155

62-
public void reset() {
63-
enableDetekt.setSelected(detektConfigStorage.getEnableDetekt());
64-
checkTestSources.setSelected(detektConfigStorage.getCheckTestFiles());
65-
configurationFilePath.setText(detektConfigStorage.getRulesPath());
66-
}
56+
public void reset() {
57+
enableDetekt.setSelected(detektConfigStorage.getEnableDetekt());
58+
checkTestSources.setSelected(detektConfigStorage.getCheckTestFiles());
59+
treatAsErrors.setSelected(detektConfigStorage.getTreatAsError());
60+
configurationFilePath.setText(detektConfigStorage.getRulesPath());
61+
}
6762

68-
public boolean isModified() {
69-
return !Comparing.equal(detektConfigStorage.getEnableDetekt(), enableDetekt.isSelected())
70-
|| !Comparing.equal(detektConfigStorage.getCheckTestFiles(), checkTestSources.isSelected())
71-
|| !Comparing.equal(detektConfigStorage.getDetektVersion(), detektVersion.getSelectedItem())
72-
|| !Comparing.equal(detektConfigStorage.getRulesPath(), configurationFilePath.getText());
73-
}
63+
public boolean isModified() {
64+
return !Comparing.equal(detektConfigStorage.getEnableDetekt(), enableDetekt.isSelected())
65+
|| !Comparing.equal(detektConfigStorage.getCheckTestFiles(), checkTestSources.isSelected())
66+
|| !Comparing.equal(detektConfigStorage.getTreatAsError(), treatAsErrors.isSelected())
67+
|| !Comparing.equal(detektConfigStorage.getRulesPath(), configurationFilePath.getText());
68+
}
7469
}

Diff for: src/main/kotlin/io/gitlab/arturbosch/detekt/DetektAnnotator.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ class DetektAnnotator : ExternalAnnotator<PsiFile, List<Finding>>() {
4747
override fun apply(file: PsiFile,
4848
annotationResult: List<Finding>,
4949
holder: AnnotationHolder) {
50+
51+
val configuration = DetektConfigStorage.instance(file.project)
5052
annotationResult.forEach {
51-
holder.createWarningAnnotation(
52-
it.charPosition.toTextRange(),
53-
it.id + ": " + it.messageOrDescription()
54-
)
53+
val textRange = it.charPosition.toTextRange()
54+
val message = it.id + ": " + it.messageOrDescription()
55+
if (configuration.treatAsError) {
56+
holder.createErrorAnnotation(textRange, message)
57+
} else {
58+
holder.createWarningAnnotation(textRange, message)
59+
}
5560
}
5661
}
5762

Diff for: src/main/kotlin/io/gitlab/arturbosch/detekt/config/DetektConfigStorage.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DetektConfigStorage : PersistentStateComponent<DetektConfigStorage> {
2424
var checkTestFiles: Boolean = false
2525

2626
@Tag
27-
var detektVersion: String = ""
27+
var treatAsError: Boolean = false
2828

2929
@Tag
3030
var rulesPath: String = ""
@@ -34,8 +34,8 @@ class DetektConfigStorage : PersistentStateComponent<DetektConfigStorage> {
3434
override fun loadState(state: DetektConfigStorage) {
3535
this.enableDetekt = state.enableDetekt
3636
this.checkTestFiles = state.checkTestFiles
37-
this.detektVersion = state.detektVersion
3837
this.rulesPath = state.rulesPath
38+
this.treatAsError = state.treatAsError
3939
}
4040

4141
companion object {

0 commit comments

Comments
 (0)