aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/src/main/java
diff options
context:
space:
mode:
authorAmaury Levé2018-06-21 11:33:42 +0200
committerElena Vilchik2018-06-21 11:33:42 +0200
commit913028fc4c913fec3bbc1800c00e526413040e01 (patch)
tree0d1b7cadc4d45d82a1f0e3c91e96670d4b813551 /sonar-css-plugin/src/main/java
parent6472431ad488158bfcf863a7b4a5655e1ecc55e8 (diff)
downloadsonar-css-913028fc4c913fec3bbc1800c00e526413040e01.tar.bz2
Add comment lines and lines of code metrics (#49)
Diffstat (limited to 'sonar-css-plugin/src/main/java')
-rw-r--r--sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java142
1 files changed, 90 insertions, 52 deletions
diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java
index 6257b74..1c635c7 100644
--- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java
+++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java
@@ -20,7 +20,10 @@
package org.sonar.css.plugin;
import java.io.IOException;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
@@ -28,6 +31,9 @@ import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
@@ -35,10 +41,16 @@ public class MetricSensor implements Sensor {
private static final Logger LOG = Loggers.get(MetricSensor.class);
+ private final FileLinesContextFactory fileLinesContextFactory;
+
+ public MetricSensor(FileLinesContextFactory fileLinesContextFactory) {
+ this.fileLinesContextFactory = fileLinesContextFactory;
+ }
+
@Override
public void describe(SensorDescriptor descriptor) {
descriptor
- .onlyOnLanguage(CssLanguage.KEY);
+ .onlyOnLanguage(CssLanguage.KEY);
}
@Override
@@ -48,72 +60,98 @@ public class MetricSensor implements Sensor {
Tokenizer tokenizer = new Tokenizer();
- for (InputFile input : inputFiles) {
- saveHighlights(context, input, tokenizer);
+ for (InputFile file : inputFiles) {
+ try {
+ List<CssToken> tokenList = tokenizer.tokenize(file.contents());
+
+ saveHighlights(context, file, tokenList);
+ saveLineTypes(context, file, tokenList);
+
+ } catch (IOException e) {
+ LOG.error(String.format("Failed to read file '%s'", file.toString()), e);
+ }
}
}
- private static void saveHighlights(SensorContext sensorContext, InputFile input, Tokenizer tokenizer) {
- try {
- NewHighlighting highlighting = sensorContext.newHighlighting().onFile(input);
- List<CssToken> tokenList = tokenizer.tokenize(input.contents());
+ private static void saveHighlights(SensorContext context, InputFile file, List<CssToken> tokenList) {
+ NewHighlighting highlighting = context.newHighlighting().onFile(file);
- for (int i = 0; i < tokenList.size(); i++) {
- CssToken currentToken = tokenList.get(i);
- CssToken nextToken = i + 1 < tokenList.size() ? tokenList.get(i + 1) : null;
+ for (int i = 0; i < tokenList.size(); i++) {
+ CssToken currentToken = tokenList.get(i);
+ CssToken nextToken = i + 1 < tokenList.size() ? tokenList.get(i + 1) : null;
- TypeOfText highlightingType = null;
- switch (currentToken.type) {
- case COMMENT:
- highlightingType = TypeOfText.COMMENT;
- break;
+ TypeOfText highlightingType = null;
+ switch (currentToken.type) {
+ case COMMENT:
+ highlightingType = TypeOfText.COMMENT;
+ break;
- case STRING:
- highlightingType = TypeOfText.STRING;
- break;
+ case STRING:
+ highlightingType = TypeOfText.STRING;
+ break;
- case NUMBER:
- highlightingType = TypeOfText.CONSTANT;
- break;
+ case NUMBER:
+ highlightingType = TypeOfText.CONSTANT;
+ break;
- case AT_IDENTIFIER:
- highlightingType = TypeOfText.ANNOTATION;
- break;
+ case AT_IDENTIFIER:
+ highlightingType = TypeOfText.ANNOTATION;
+ break;
- case DOLLAR_IDENTIFIER:
+ case DOLLAR_IDENTIFIER:
+ highlightingType = TypeOfText.KEYWORD;
+ break;
+
+ case HASH_IDENTIFIER:
+ if (currentToken.text.matches("^#[0-9a-fA-F]+$")) {
+ highlightingType = TypeOfText.CONSTANT;
+ } else {
highlightingType = TypeOfText.KEYWORD;
- break;
-
- case HASH_IDENTIFIER:
- if (currentToken.text.matches("^#[0-9a-fA-F]+$")) {
- highlightingType = TypeOfText.CONSTANT;
- } else {
- highlightingType = TypeOfText.KEYWORD;
- }
- break;
-
- case IDENTIFIER:
- // We want to highlight the property key of a css/scss/less file and as the tokenizer is putting the ':' into another token
- // we need to look for identifier followed by a PUNCTUATOR token with text ':'.
- if (nextToken != null && nextToken.text.equals(":")) {
- highlightingType = TypeOfText.KEYWORD_LIGHT;
- }
- break;
-
- default:
- highlightingType = null;
- }
+ }
+ break;
+
+ case IDENTIFIER:
+ // We want to highlight the property key of a css/scss/less file and as the tokenizer is putting the ':' into another token
+ // we need to look for identifier followed by a PUNCTUATOR token with text ':'.
+ if (nextToken != null && nextToken.text.equals(":")) {
+ highlightingType = TypeOfText.KEYWORD_LIGHT;
+ }
+ break;
+
+ default:
+ highlightingType = null;
+ }
- if (highlightingType != null) {
- highlighting.highlight(currentToken.startLine, currentToken.startColumn, currentToken.endLine, currentToken.endColumn, highlightingType);
- }
+ if (highlightingType != null) {
+ highlighting.highlight(currentToken.startLine, currentToken.startColumn, currentToken.endLine, currentToken.endColumn, highlightingType);
}
+ }
- highlighting.save();
+ highlighting.save();
+ }
- } catch (IOException e) {
- LOG.error(String.format("Failed to read file '%s'", input.toString()), e);
+ private void saveLineTypes(SensorContext context, InputFile file, List<CssToken> tokenList) {
+ // collect line types
+ Set<Integer> linesOfCode = new HashSet<>();
+ Set<Integer> linesOfComment = new HashSet<>();
+
+ for (CssToken token : tokenList) {
+ for (int line = token.startLine; line <= token.endLine; line++) {
+ if (token.type.equals(CssTokenType.COMMENT)) {
+ linesOfComment.add(line);
+ } else {
+ linesOfCode.add(line);
+ }
+ }
}
+
+ context.<Integer>newMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(linesOfCode.size()).save();
+ context.<Integer>newMeasure().on(file).forMetric(CoreMetrics.COMMENT_LINES).withValue(linesOfComment.size()).save();
+
+ FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(file);
+ linesOfCode.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1));
+ linesOfComment.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, 1));
+ fileLinesContext.save();
}
}