diff options
Diffstat (limited to 'sonar-batch')
10 files changed, 49 insertions, 36 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java index 786ad0cca68..1399f66a438 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/DefaultHighlightingBuilder.java @@ -40,7 +40,7 @@ public class DefaultHighlightingBuilder implements HighlightingBuilder { @Override public HighlightingBuilder highlight(int startOffset, int endOffset, TypeOfText typeOfText) { Preconditions.checkState(!done, "done() already called"); - builder.registerHighlightingRule(startOffset, endOffset, typeOfText.cssClass()); + builder.registerHighlightingRule(startOffset, endOffset, typeOfText); return this; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingData.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingData.java index 8e4f5314ec6..2a539efe189 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingData.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingData.java @@ -47,7 +47,7 @@ public class SyntaxHighlightingData implements Data { .append(FIELD_SEPARATOR) .append(highlightingRule.getEndPosition()) .append(FIELD_SEPARATOR) - .append(highlightingRule.getTextType()) + .append(highlightingRule.getTextType().cssClass()) .append(RULE_SEPARATOR); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java index 792cfb2eb3a..aba6bd66029 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilder.java @@ -22,6 +22,7 @@ package org.sonar.batch.highlighting; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Ordering; import org.elasticsearch.common.collect.Sets; +import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import javax.annotation.Nullable; @@ -51,7 +52,7 @@ public class SyntaxHighlightingDataBuilder { return syntaxHighlightingRuleSet; } - public SyntaxHighlightingDataBuilder registerHighlightingRule(int startOffset, int endOffset, String typeOfText) { + public SyntaxHighlightingDataBuilder registerHighlightingRule(int startOffset, int endOffset, HighlightingBuilder.TypeOfText typeOfText) { SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(startOffset, endOffset, typeOfText); this.syntaxHighlightingRuleSet.add(syntaxHighlightingRule); diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java index e6faa7651b4..9ccf5758557 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRule.java @@ -19,21 +19,23 @@ */ package org.sonar.batch.highlighting; +import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; + import java.io.Serializable; public class SyntaxHighlightingRule implements Serializable { private final int startPosition; private final int endPosition; - private final String textType; + private final HighlightingBuilder.TypeOfText textType; - private SyntaxHighlightingRule(int startPosition, int endPosition, String textType) { + private SyntaxHighlightingRule(int startPosition, int endPosition, HighlightingBuilder.TypeOfText textType) { this.startPosition = startPosition; this.endPosition = endPosition; this.textType = textType; } - public static SyntaxHighlightingRule create(int startPosition, int endPosition, String textType) { + public static SyntaxHighlightingRule create(int startPosition, int endPosition, HighlightingBuilder.TypeOfText textType) { return new SyntaxHighlightingRule(startPosition, endPosition, textType); } @@ -45,7 +47,7 @@ public class SyntaxHighlightingRule implements Serializable { return endPosition; } - public String getTextType() { + public HighlightingBuilder.TypeOfText getTextType() { return textType; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java index e56ee8f0d13..4cc9ea53ab8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java @@ -22,6 +22,7 @@ package org.sonar.batch.highlighting; import com.persistit.Value; import com.persistit.encoding.CoderContext; import com.persistit.encoding.ValueCoder; +import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; class SyntaxHighlightingRuleValueCoder implements ValueCoder { @@ -30,14 +31,14 @@ class SyntaxHighlightingRuleValueCoder implements ValueCoder { SyntaxHighlightingRule rule = (SyntaxHighlightingRule) object; value.put(rule.getStartPosition()); value.put(rule.getEndPosition()); - value.put(rule.getTextType()); + value.put(rule.getTextType().ordinal()); } @Override public Object get(Value value, Class clazz, CoderContext context) { int startPosition = value.getInt(); int endPosition = value.getInt(); - String type = value.getString(); + HighlightingBuilder.TypeOfText type = HighlightingBuilder.TypeOfText.values()[value.getInt()]; return SyntaxHighlightingRule.create(startPosition, endPosition, type); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index 92cb3a3ffbb..2905cc6b0d7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -287,21 +287,22 @@ public class BatchMediumTester { } /** - * Get highlighting type at a given position in an inputfile + * Get highlighting types at a given position in an inputfile * @param charIndex 0-based offset in file */ @CheckForNull - public HighlightingBuilder.TypeOfText highlightingTypeFor(InputFile file, int charIndex) { + public List<HighlightingBuilder.TypeOfText> highlightingTypeFor(InputFile file, int charIndex) { SyntaxHighlightingData syntaxHighlightingData = highlightingPerFile.get(file); if (syntaxHighlightingData == null) { return null; } + List<HighlightingBuilder.TypeOfText> result = new ArrayList<HighlightingBuilder.TypeOfText>(); for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.syntaxHighlightingRuleSet()) { if (sortedRule.getStartPosition() <= charIndex && sortedRule.getEndPosition() > charIndex) { - return HighlightingBuilder.TypeOfText.forCssClass(sortedRule.getTextType()); + result.add(sortedRule.getTextType()); } } - return null; + return result; } /** diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java index 5b15a3abcd8..b290c9a0dc9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.source; +import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText; import org.sonar.api.component.Component; import org.sonar.api.source.Highlightable; import org.sonar.batch.highlighting.SyntaxHighlightingDataBuilder; @@ -70,7 +71,8 @@ public class DefaultHighlightable implements Highlightable { @Override public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { - builder.registerHighlightingRule(startOffset, endOffset, typeOfText); + TypeOfText type = org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.forCssClass(typeOfText); + builder.registerHighlightingRule(startOffset, endOffset, type); return this; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java index e511bc388de..4b192a47731 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataBuilderTest.java @@ -27,6 +27,9 @@ import org.junit.rules.ExpectedException; import java.util.Collection; import static org.fest.assertions.Assertions.assertThat; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CLASSIC_COMMENT; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CPP_DOC; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.KEYWORD; public class SyntaxHighlightingDataBuilderTest { @@ -39,12 +42,12 @@ public class SyntaxHighlightingDataBuilderTest { public void setUpSampleRules() { SyntaxHighlightingDataBuilder highlightingDataBuilder = new SyntaxHighlightingDataBuilder(); - highlightingDataBuilder.registerHighlightingRule(0, 10, "cd"); - highlightingDataBuilder.registerHighlightingRule(10, 12, "k"); - highlightingDataBuilder.registerHighlightingRule(24, 38, "k"); - highlightingDataBuilder.registerHighlightingRule(42, 50, "k"); - highlightingDataBuilder.registerHighlightingRule(24, 65, "cppd"); - highlightingDataBuilder.registerHighlightingRule(12, 20, "cd"); + highlightingDataBuilder.registerHighlightingRule(0, 10, CLASSIC_COMMENT); + highlightingDataBuilder.registerHighlightingRule(10, 12, KEYWORD); + highlightingDataBuilder.registerHighlightingRule(24, 38, KEYWORD); + highlightingDataBuilder.registerHighlightingRule(42, 50, KEYWORD); + highlightingDataBuilder.registerHighlightingRule(24, 65, CPP_DOC); + highlightingDataBuilder.registerHighlightingRule(12, 20, CLASSIC_COMMENT); highlightingRules = highlightingDataBuilder.getSyntaxHighlightingRuleSet(); } @@ -58,14 +61,14 @@ public class SyntaxHighlightingDataBuilderTest { public void should_order_by_start_then_end_offset() throws Exception { assertThat(highlightingRules).onProperty("startPosition").containsOnly(0, 10, 12, 24, 24, 42); assertThat(highlightingRules).onProperty("endPosition").containsOnly(10, 12, 20, 38, 65, 50); - assertThat(highlightingRules).onProperty("textType").containsOnly("cd", "k", "cd", "k", "cppd", "k"); + assertThat(highlightingRules).onProperty("textType").containsOnly(CLASSIC_COMMENT, KEYWORD, CLASSIC_COMMENT, KEYWORD, CPP_DOC, KEYWORD); } @Test public void should_suport_overlapping() throws Exception { SyntaxHighlightingDataBuilder builder = new SyntaxHighlightingDataBuilder(); - builder.registerHighlightingRule(0, 15, "k"); - builder.registerHighlightingRule(8, 12, "cppd"); + builder.registerHighlightingRule(0, 15, KEYWORD); + builder.registerHighlightingRule(8, 12, CPP_DOC); builder.build(); } @@ -75,8 +78,8 @@ public class SyntaxHighlightingDataBuilderTest { throwable.expectMessage("Cannot register highlighting rule for characters from 8 to 15 as it overlaps at least one existing rule"); SyntaxHighlightingDataBuilder builder = new SyntaxHighlightingDataBuilder(); - builder.registerHighlightingRule(0, 10, "k"); - builder.registerHighlightingRule(8, 15, "k"); + builder.registerHighlightingRule(0, 10, KEYWORD); + builder.registerHighlightingRule(8, 15, KEYWORD); builder.build(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java index 59de88ca848..75a78d1becd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/highlighting/SyntaxHighlightingDataTest.java @@ -25,6 +25,9 @@ import org.junit.Test; import java.util.List; import static org.fest.assertions.Assertions.assertThat; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CLASSIC_COMMENT; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.CPP_DOC; +import static org.sonar.api.batch.sensor.highlighting.HighlightingBuilder.TypeOfText.KEYWORD; public class SyntaxHighlightingDataTest { @@ -32,12 +35,12 @@ public class SyntaxHighlightingDataTest { public void should_serialize_rules_to_string() throws Exception { List<SyntaxHighlightingRule> orderedHighlightingRules = Lists.newArrayList( - SyntaxHighlightingRule.create(0, 10, "cd"), - SyntaxHighlightingRule.create(10, 12, "k"), - SyntaxHighlightingRule.create(12, 20, "cd"), - SyntaxHighlightingRule.create(24, 38, "k"), - SyntaxHighlightingRule.create(24, 65, "cppd"), - SyntaxHighlightingRule.create(42, 50, "k") + SyntaxHighlightingRule.create(0, 10, CLASSIC_COMMENT), + SyntaxHighlightingRule.create(10, 12, KEYWORD), + SyntaxHighlightingRule.create(12, 20, CLASSIC_COMMENT), + SyntaxHighlightingRule.create(24, 38, KEYWORD), + SyntaxHighlightingRule.create(24, 65, CPP_DOC), + SyntaxHighlightingRule.create(42, 50, KEYWORD) ); String serializedRules = new SyntaxHighlightingData(orderedHighlightingRules).writeString(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java index 7e2c71db869..b55a2125cbe 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java @@ -87,10 +87,10 @@ public class HighlightingMediumTest { .start(); InputFile file = result.inputFiles().get(0); - assertThat(result.highlightingTypeFor(file, 0)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); - assertThat(result.highlightingTypeFor(file, 9)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); - assertThat(result.highlightingTypeFor(file, 10)).isNull(); - assertThat(result.highlightingTypeFor(file, 11)).isEqualTo(HighlightingBuilder.TypeOfText.KEYWORD); + assertThat(result.highlightingTypeFor(file, 0)).containsExactly(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 9)).containsExactly(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 10)).isEmpty(); + assertThat(result.highlightingTypeFor(file, 11)).containsExactly(HighlightingBuilder.TypeOfText.KEYWORD); } @@ -126,7 +126,7 @@ public class HighlightingMediumTest { System.out.println("Duration: " + (System.currentTimeMillis() - start)); InputFile file = result.inputFiles().get(0); - assertThat(result.highlightingTypeFor(file, 0)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 0)).containsExactly(HighlightingBuilder.TypeOfText.STRING); } |