diff options
Diffstat (limited to 'sonar-css-plugin')
| -rw-r--r-- | sonar-css-plugin/src/main/java/org/sonar/css/plugin/MetricSensor.java | 62 | ||||
| -rw-r--r-- | sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java | 95 |
2 files changed, 115 insertions, 42 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 fb9c522..abbbd50 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 @@ -19,6 +19,9 @@ */ package org.sonar.css.plugin; +import java.io.IOException; +import java.util.List; +import javax.script.ScriptException; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.Sensor; @@ -29,11 +32,6 @@ import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import javax.script.ScriptException; - -import java.io.IOException; -import java.util.Optional; - public class MetricSensor implements Sensor { private static final Logger LOG = Loggers.get(MetricSensor.class); @@ -59,9 +57,45 @@ public class MetricSensor implements Sensor { private static void saveHighlights(SensorContext sensorContext, InputFile input, Tokenizer tokenizer) { try { NewHighlighting highlighting = sensorContext.newHighlighting().onFile(input); - tokenizer.tokenize(input.contents()) - .forEach(token -> getHighlightingType(token).ifPresent(type -> - highlighting.highlight(token.startLine, token.startColumn, token.endLine, token.endColumn, type))); + List<Token> tokenList = tokenizer.tokenize(input.contents()); + + for (int i = 0; i < tokenList.size(); i++) { + Token currentToken = tokenList.get(i); + Token nextToken = i + 1 == tokenList.size() ? null : tokenList.get(i + 1); + + TypeOfText highlightingType = null; + switch (currentToken.type) { + case COMMENT: + highlightingType = TypeOfText.COMMENT; + break; + + case STRING: + highlightingType = TypeOfText.STRING; + break; + + case WORD: + if (Character.isDigit(currentToken.text.charAt(0)) || currentToken.text.matches("^#[0-9a-fA-F]+$")) { + highlightingType = TypeOfText.CONSTANT; + } else if (nextToken != null && nextToken.text.equals(":")) { + highlightingType = TypeOfText.KEYWORD_LIGHT; + } else if (currentToken.text.startsWith(".") || (nextToken != null && nextToken.text.startsWith("{"))) { + highlightingType = TypeOfText.KEYWORD; + } + break; + + case AT_WORD: + highlightingType = TypeOfText.ANNOTATION; + break; + + default: + highlightingType = null; + } + + if (highlightingType != null) { + highlighting.highlight(currentToken.startLine, currentToken.startColumn - 1, currentToken.endLine, currentToken.endColumn, highlightingType); + } + } + highlighting.save(); } catch (ScriptException e) { @@ -71,16 +105,4 @@ public class MetricSensor implements Sensor { } } - private static Optional<TypeOfText> getHighlightingType(Token token) { - switch (token.type) { - case COMMENT: - return Optional.of(TypeOfText.COMMENT); - - case STRING: - return Optional.of(TypeOfText.STRING); - - default: - return Optional.empty(); - } - } } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java index c93c16a..6af504b 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java @@ -22,7 +22,6 @@ package org.sonar.css.plugin; import java.io.File; import java.io.IOException; import java.util.List; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -42,11 +41,6 @@ public class MetricSensorTest { @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); - @Before - public void setUp() { - sensorContext = SensorContextTester.create(tempFolder.getRoot()); - } - @Test public void should_describe() { DefaultSensorDescriptor desc = new DefaultSensorDescriptor(); @@ -58,33 +52,93 @@ public class MetricSensorTest { @Test public void empty_input() throws Exception { highlight("foo"); - assertThat(sensorContext.highlightingTypeAt(inputFile.key(), 1, 0)).isEmpty(); assertThat(sensorContext.highlightingTypeAt(inputFile.key(), 1, 1)).isEmpty(); } @Test - public void singleline_multiline_comment() throws IOException { + public void comment() throws IOException { highlight("/* some comment */"); + assertHighlighting(1, 0, 18, TypeOfText.COMMENT); - assertHighlighting(1, 1, 17, TypeOfText.COMMENT); + highlight("/* some comment\nmultiline */"); + assertHighlighting(1, 0, 15, TypeOfText.COMMENT); + assertHighlighting(2, 0, 12, TypeOfText.COMMENT); } @Test - public void multiline_comment() throws IOException { - String content = "/* some comment\nmultiline */"; - highlight(content); + public void string() throws IOException { + highlight("\"foo\""); + assertHighlighting(1, 0, 5, TypeOfText.STRING); - assertHighlighting(1, 1, 15, TypeOfText.COMMENT); - assertHighlighting(2, 1, 11, TypeOfText.COMMENT); + highlight("\"foo\nbar\""); + assertHighlighting(1, 0, 4, TypeOfText.STRING); + assertHighlighting(2, 0, 4, TypeOfText.STRING); } @Test - public void string() throws IOException { - String content = "\"foo\""; - highlight(content); + public void constant() throws IOException { + highlight("1"); + assertHighlighting(1, 0, 1, TypeOfText.CONSTANT); + + highlight("1.0"); + assertHighlighting(1, 0, 3, TypeOfText.CONSTANT); + + highlight("0px"); + assertHighlighting(1, 0, 3, TypeOfText.CONSTANT); + + highlight("1em"); + assertHighlighting(1, 0, 3, TypeOfText.CONSTANT); + + highlight("#ddd"); + assertHighlighting(1, 0, 4, TypeOfText.CONSTANT); + } + + @Test + public void annotation() throws IOException { + highlight("@bar { }"); + assertHighlighting(1, 0, 4, TypeOfText.ANNOTATION); + + highlight("@my-selector: banner;"); + assertHighlighting(1, 0, 12, TypeOfText.ANNOTATION); + + highlight("@import \"src/themes\""); + assertHighlighting(1, 0, 7, TypeOfText.ANNOTATION); - assertHighlighting(1, 1, content.length() - 1, TypeOfText.STRING); + highlight(".element { color: @@color }"); + assertHighlighting(1, 18, 7, TypeOfText.ANNOTATION); + } + + @Test + public void keyword() throws IOException { + highlight("foo { }"); + assertHighlighting(1, 0, 3, TypeOfText.KEYWORD); + + highlight(".foo { }"); + assertHighlighting(1, 0, 4, TypeOfText.KEYWORD); + + highlight(".foo bar { }"); + assertHighlighting(1, 0, 4, TypeOfText.KEYWORD); + assertHighlighting(1, 5, 3, TypeOfText.KEYWORD); + + highlight(".border-radius(@radius) { }"); + assertHighlighting(1, 0, 14, TypeOfText.KEYWORD); + + highlight("#header { .border-radius(4px); }"); + assertHighlighting(1, 0, 7, TypeOfText.KEYWORD); + assertHighlighting(1, 10, 14, TypeOfText.KEYWORD); + } + + @Test + public void keyword_light() throws IOException { + highlight("bar: foo { }"); + assertHighlighting(1, 0, 3, TypeOfText.KEYWORD_LIGHT); + + highlight("bar { foo: 1px }"); + assertHighlighting(1, 6, 3, TypeOfText.KEYWORD_LIGHT); + + highlight("bar { foo-bar: 1px }"); + assertHighlighting(1, 6, 7, TypeOfText.KEYWORD_LIGHT); } private void highlight(String content) throws IOException { @@ -94,16 +148,13 @@ public class MetricSensorTest { .setContents(content) .build(); + sensorContext = SensorContextTester.create(tempFolder.getRoot()); sensorContext.fileSystem().add(inputFile); new MetricSensor().execute(sensorContext); } private void assertHighlighting(int line, int column, int length, TypeOfText type) { - if (column < 1) { - throw new IllegalStateException("Column should be greater than or equal to 1"); - } - for (int i = column; i < column + length; i++) { List<TypeOfText> typeOfTexts = sensorContext.highlightingTypeAt(inputFile.key(), line, i); assertThat(typeOfTexts).containsOnly(type); |
